VirtualBox

Changeset 74672 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Oct 8, 2018 12:08:51 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/asn1: Hacked code into handling the necessary indefinite length stuff from apple. bugref:9232

Location:
trunk/src/VBox/Runtime
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/VBox/VBoxRTImp.def

    r74654 r74672  
    117117    RTAsn1Core_SetTagAndFlags
    118118    RTAsn1CursorCheckEnd
     119    RTAsn1CursorCheckSeqEnd
     120    RTAsn1CursorCheckSetEnd
    119121    RTAsn1CursorGetBitString
    120122    RTAsn1CursorGetBitStringEx
  • trunk/src/VBox/Runtime/common/asn1/asn1-cursor.cpp

    r74657 r74672  
    6868    pPrimaryCursor->Cursor.fFlags           = (uint8_t)fFlags; Assert(fFlags <= UINT8_MAX);
    6969    pPrimaryCursor->Cursor.cDepth           = 0;
    70     pPrimaryCursor->Cursor.cIndefinedRecs   = 0;
    7170    pPrimaryCursor->Cursor.abReserved[0]    = 0;
     71    pPrimaryCursor->Cursor.abReserved[1]    = 0;
    7272    pPrimaryCursor->Cursor.pPrimary         = pPrimaryCursor;
    7373    pPrimaryCursor->Cursor.pUp              = NULL;
     
    8989    pChild->cDepth          = pParent->cDepth + 1;
    9090    AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
    91     pChild->cIndefinedRecs  = 0;
    9291    pChild->abReserved[0]   = 0;
     92    pChild->abReserved[1]   = 0;
    9393    pChild->pPrimary        = pParent->pPrimary;
    9494    pChild->pUp             = pParent;
     
    114114    pChild->cDepth          = pParent->cDepth + 1;
    115115    AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
    116     pChild->cIndefinedRecs  = 0;
    117116    pChild->abReserved[0]   = 0;
     117    pChild->abReserved[1]   = 0;
    118118    pChild->pPrimary        = pParent->pPrimary;
    119119    pChild->pUp             = pParent;
     
    194194    if (!(pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
    195195        return false;
    196     /* This isn't quite right. */
    197     if (pCursor->cbLeft > pCursor->cIndefinedRecs * (uint32_t)2)
    198         return false;
    199     return ASMMemIsZero(pCursor->pbCur, pCursor->cbLeft);
     196    return pCursor->cbLeft >= 2
     197        && pCursor->pbCur[0] == 0
     198        && pCursor->pbCur[1] == 0;
    200199}
    201200
     
    205204    if (pCursor->cbLeft == 0)
    206205        return VINF_SUCCESS;
    207     if (   (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
    208         && pCursor->cbLeft == pCursor->cIndefinedRecs * (uint32_t)2
    209         && ASMMemIsZero(pCursor->pbCur, pCursor->cbLeft))
    210         return VINF_SUCCESS;
     206
     207    if (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
     208    {
     209        /*
     210         * If we've got two zeros here we're good.  This helps us handle apple code
     211         * signatures, where most of the big structures are of indefinite length.
     212         * The problem here is when rtCrPkcs7ContentInfo_DecodeExtra works the
     213         * octet string, it appears as if there extra padding at the end.
     214         *
     215         * It is of course possible that ASN.1 assumes we will parse the content of
     216         * that octet string as if it were an ASN.1 substructure, looking for the
     217         * end-of-content sequence and propage that up.  However, this works for now.
     218         */
     219        if (pCursor->cbLeft >= 2)
     220        {
     221            if (   pCursor->pbCur[0] == 0
     222                && pCursor->pbCur[1] == 0)
     223                return VINF_SUCCESS;
     224            return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
     225                                       "%u (%#x) bytes left over [indef: %.*Rhxs]",
     226                                       pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
     227        }
     228        return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
     229                                   "%u (%#x) bytes left over [indef len]", pCursor->cbLeft, pCursor->cbLeft);
     230    }
    211231    return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
    212232                               "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
     233}
     234
     235
     236/**
     237 * Worker for RTAsn1CursorCheckSeqEnd and RTAsn1CursorCheckSetEnd.
     238 */
     239static int rtAsn1CursorCheckSeqOrSetEnd(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
     240{
     241    if (pCursor->cbLeft == 0)
     242        return VINF_SUCCESS;
     243
     244    if (pAsn1Core->fFlags & RTASN1CORE_F_INDEFINITE_LENGTH)
     245    {
     246        if (pCursor->cbLeft >= 2)
     247        {
     248            if (   pCursor->pbCur[0] == 0
     249                && pCursor->pbCur[1] == 0)
     250            {
     251                pAsn1Core->cb = (uint32_t)(pCursor->pbCur - pAsn1Core->uData.pu8);
     252                pCursor->cbLeft -= 2;
     253                pCursor->pbCur  += 2;
     254
     255                PRTASN1CURSOR pParentCursor = pCursor->pUp;
     256                if (   pParentCursor
     257                    && (pParentCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
     258                {
     259                    pParentCursor->pbCur  -= pCursor->cbLeft;
     260                    pParentCursor->cbLeft += pCursor->cbLeft;
     261                    return VINF_SUCCESS;
     262                }
     263
     264                if (pCursor->cbLeft == 0)
     265                    return VINF_SUCCESS;
     266
     267                return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
     268                                           "%u (%#x) bytes left over (parent not indefinite length)", pCursor->cbLeft, pCursor->cbLeft);
     269            }
     270            return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END, "%u (%#x) bytes left over [indef: %.*Rhxs]",
     271                                       pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
     272        }
     273        return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
     274                                   "1 byte left over, expected two for indefinite length end-of-content sequence");
     275    }
     276
     277    return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
     278                               "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
     279
     280}
     281
     282
     283RTDECL(int) RTAsn1CursorCheckSeqEnd(PRTASN1CURSOR pCursor, PRTASN1SEQUENCECORE pSeqCore)
     284{
     285    return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSeqCore->Asn1Core);
     286}
     287
     288
     289RTDECL(int) RTAsn1CursorCheckSetEnd(PRTASN1CURSOR pCursor, PRTASN1SETCORE pSetCore)
     290{
     291    return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSetCore->Asn1Core);
    213292}
    214293
     
    337416                return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
    338417                                           "%s: Indefinite BER/CER encoding not supported for this tag (uTag=%#x)", pszErrorTag, uTag);
    339             else if (pCursor->cIndefinedRecs > 8)
     418            else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
    340419                return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
    341                                            "%s: Too many indefinite BER/CER encodings. (uTag=%#x)", pszErrorTag, uTag);
     420                                           "%s: Nested indefinite BER/CER encoding. (uTag=%#x)", pszErrorTag, uTag);
    342421            else if (pCursor->cbLeft < 2)
    343422                return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
     
    345424            else
    346425            {
    347                 pCursor->cIndefinedRecs++;
    348426                pCursor->fFlags   |= RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
    349427                pAsn1Core->fFlags |= RTASN1CORE_F_INDEFINITE_LENGTH;
    350                 cb = pCursor->cbLeft - pCursor->cIndefinedRecs * 2; /* tentatively */
     428                cb = pCursor->cbLeft - 2; /* tentatively for sequences and sets, definite for others */
    351429            }
    352430        }
     431        /* else if (cb == 0 && uTag == 0) { end of content } - callers handle this */
    353432
    354433        /* Check if the length makes sense. */
     
    474553RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
    475554{
    476     uint32_t        cbSavedLeft = pCursor->cbLeft;
    477     uint8_t const  *pbSavedCur  = pCursor->pbCur;
    478     PRTERRINFO      pErrInfo    = pCursor->pPrimary->pErrInfo;
     555    uint32_t            cbSavedLeft         = pCursor->cbLeft;
     556    uint8_t const      *pbSavedCur          = pCursor->pbCur;
     557    uint8_t const       fSavedFlags         = pCursor->fFlags;
     558    PRTERRINFO const    pErrInfo            = pCursor->pPrimary->pErrInfo;
    479559    pCursor->pPrimary->pErrInfo = NULL;
    480560
     
    482562
    483563    pCursor->pPrimary->pErrInfo = pErrInfo;
    484     pCursor->pbCur  = pbSavedCur;
    485     pCursor->cbLeft = cbSavedLeft;
     564    pCursor->pbCur              = pbSavedCur;
     565    pCursor->cbLeft             = cbSavedLeft;
     566    pCursor->fFlags             = fSavedFlags;
    486567    return rc;
    487568}
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-asn1-decoder.cpp

    r69111 r74672  
    9090    {
    9191        /*
    92          * Detect CMS octet string and open the content cursor.
    93          * Current we don't have work with any contet which is octet string,
    94          * they're all sequences, which make detection so much simpler.
     92         * Detect CMS octet string format and open the content cursor.
     93         *
     94         * Current we don't have any octent string content which, they're all
     95         * sequences, which make detection so much simpler.
    9596         */
    9697        PRTASN1OCTETSTRING  pOctetString = &pThis->Content;
  • trunk/src/VBox/Runtime/tools/RTSignTool.cpp

    r73097 r74672  
    629629                if (cVerbosity > 2)
    630630                    RTPrintf("PKCS#7 signature: %u bytes\n", cbActual);
     631                if (cVerbosity > 3)
     632                    RTPrintf("%.*Rhxd\n", cbActual, pvBuf);
    631633
    632634                /*
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