VirtualBox

Changeset 102297 in vbox for trunk/src/VBox/Runtime/common


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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] =
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