VirtualBox

Changeset 102297 in vbox


Ignore:
Timestamp:
Nov 24, 2023 4:32:03 PM (10 months ago)
Author:
vboxsync
Message:

IPRT/crypto/shacrypt: Better string length checks for RTCrShaCryptXXXToString(). bugref:10551

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/crypto/shacrypt.h

    r102296 r102297  
    8181 * @param   cRounds             Number of rounds used for generating \a pabHash.
    8282 * @param   pszString           Where to store the printable string on success.
    83  * @param   cbString            Size (in bytes) of \a pszString.
     83 * @param   cchString           Size of \a pszString.
     84 *                              Should be at least RTSHA256_DIGEST_LEN + 1 bytes.
    8485 *
    8586 * @note    This implements step 22 of SHA-crypt.txt Version: 0.6 2016-8-31.
    8687 */
    87 RTR3DECL(int) RTCrShaCrypt256ToString(uint8_t abHash[RTSHA256_HASH_SIZE], const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString);
     88RTR3DECL(int) RTCrShaCrypt256ToString(uint8_t abHash[RTSHA256_HASH_SIZE], const char *pszSalt, uint32_t cRounds, char *pszString, size_t cchString);
    8889
    8990
     
    111112 * @param   cRounds             Number of rounds used for generating \a pabHash.
    112113 * @param   pszString           Where to store the printable string on success.
    113  * @param   cbString            Size (in bytes) of \a pszString.
     114 * @param   cchString           Size of \a pszString.
     115 *                              Should be at least RTSHA512_DIGEST_LEN + 1 bytes.
    114116 *
    115117 * @note    This implements step 22 of SHA-crypt.txt Version: 0.6 2016-8-31.
    116118 */
    117 RTR3DECL(int) RTCrShaCrypt512ToString(uint8_t abHash[RTSHA512_HASH_SIZE], const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString);
     119RTR3DECL(int) RTCrShaCrypt512ToString(uint8_t abHash[RTSHA512_HASH_SIZE], const char *pszSalt, uint32_t cRounds, char *pszString, size_t cchString);
    118120
    119121/** @} */
  • trunk/src/VBox/Runtime/common/crypto/shacrypt.cpp

    r102296 r102297  
    173173
    174174RTR3DECL(int) RTCrShaCrypt256ToString(uint8_t abHash[RTSHA256_HASH_SIZE], const char *pszSalt, uint32_t cRounds,
    175                                       char *pszString, size_t cbString)
     175                                      char *pszString, size_t cchString)
    176176{
    177177    AssertPtrReturn(pszSalt,   VERR_INVALID_POINTER);
    178178    AssertReturn   (cRounds,   VERR_INVALID_PARAMETER);
    179     AssertReturn   (cbString, VERR_INVALID_PARAMETER);
     179    AssertReturn   (cchString >= RTSHA256_DIGEST_LEN + 1, VERR_INVALID_PARAMETER);
    180180    AssertPtrReturn(pszString, VERR_INVALID_POINTER);
    181181
    182182    char  *psz = pszString;
    183     size_t cch = cbString;
     183    size_t cch = cchString;
    184184
    185185    *psz = '\0';
     186
     187    size_t cchPrefix;
    186188    if (cRounds == RT_SHACRYPT_DEFAULT_ROUNDS)
    187         psz += RTStrPrintf2(psz, cch, "$5$%s$", pszSalt);
     189        cchPrefix = RTStrPrintf2(psz, cchString, "$5$%s$", pszSalt);
    188190    else
    189         psz += RTStrPrintf2(psz, cch, "$5$rounds=%RU32$%s$", cRounds, pszSalt);
     191        cchPrefix = RTStrPrintf2(psz, cchString, "$5$rounds=%RU32$%s$", cRounds, pszSalt);
     192    AssertReturn(cchPrefix > 0, VERR_BUFFER_OVERFLOW);
     193    AssertReturn(cch >= cchPrefix, VERR_BUFFER_OVERFLOW);
     194    cch -= cchPrefix;
     195    psz += cchPrefix;
     196
     197    /* Make sure that there is enough room to store the base64-encoded hash. */
     198    AssertReturn(cch >= ((RTSHA256_HASH_SIZE / 3) * 4) + 1, VERR_BUFFER_OVERFLOW);
    190199
    191200    static const char acBase64[64 + 1] =
     
    347356
    348357RTR3DECL(int) RTCrShaCrypt512ToString(uint8_t abHash[RTSHA512_HASH_SIZE], const char *pszSalt, uint32_t cRounds,
    349                                       char *pszString, size_t cbString)
     358                                      char *pszString, size_t cchString)
    350359{
    351360    AssertPtrReturn(pszSalt,   VERR_INVALID_POINTER);
    352361    AssertReturn   (cRounds,   VERR_INVALID_PARAMETER);
    353     AssertReturn   (cbString, VERR_INVALID_PARAMETER);
     362    AssertReturn   (cchString >= RTSHA512_DIGEST_LEN + 1, VERR_INVALID_PARAMETER);
    354363    AssertPtrReturn(pszString, VERR_INVALID_POINTER);
    355364
    356365    char  *psz = pszString;
    357     size_t cch = cbString;
    358 
    359     *psz = '\0';
     366    size_t cch = cchString;
     367
     368    size_t cchPrefix;
    360369    if (cRounds == RT_SHACRYPT_DEFAULT_ROUNDS)
    361         psz += RTStrPrintf2(psz, cch, "$6$%s$", pszSalt);
     370        cchPrefix = RTStrPrintf2(psz, cchString, "$6$%s$", pszSalt);
    362371    else
    363         psz += RTStrPrintf2(psz, cch, "$6$rounds=%RU32$%s$", cRounds, pszSalt);
     372        cchPrefix = RTStrPrintf2(psz, cchString, "$6$rounds=%RU32$%s$", cRounds, pszSalt);
     373    AssertReturn(cchPrefix > 0, VERR_BUFFER_OVERFLOW);
     374    AssertReturn(cch >= cchPrefix, VERR_BUFFER_OVERFLOW);
     375    cch -= cchPrefix;
     376    psz += cchPrefix;
     377
     378    /* Make sure that there is enough room to store the base64-encoded hash. */
     379    AssertReturn(cch >= ((RTSHA512_HASH_SIZE / 3) * 4) + 1, VERR_BUFFER_OVERFLOW);
    364380
    365381    static const char acBase64[64 + 1] =
  • trunk/src/VBox/Runtime/testcase/tstRTShaCrypt.cpp

    r102296 r102297  
    227227            && g_aTests[i].pszResultStr)
    228228        {
    229             char szResult[RTSHA512_DIGEST_LEN];
     229            char szResult[RTSHA512_DIGEST_LEN + 1];
    230230
    231231            switch (enmType)
  • trunk/src/VBox/Runtime/tools/RTMkPasswd.cpp

    r102296 r102297  
    177177
    178178    uint8_t abDigest[RTSHA512_HASH_SIZE];
    179     char    szResult[RTSHA512_DIGEST_LEN];
     179    char    szResult[RTSHA512_DIGEST_LEN + 1];
    180180
    181181    switch (enmMethod)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette