VirtualBox

Changeset 52600 in vbox for trunk/src


Ignore:
Timestamp:
Sep 4, 2014 10:59:00 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
95876
Message:

IPRT: Added support for microsoft timestamp counter signatures. This required making the PKCS #7 code accept some of the CMS (RFC-5652) stuff.

Location:
trunk/src/VBox
Files:
6 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/Makefile.kmk

    r52406 r52600  
    281281        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/taf-init.cpp \
    282282        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/taf-sanity.cpp \
     283        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/tsp-asn1-decoder.cpp \
     284        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/tsp-core.cpp \
     285        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/tsp-init.cpp \
     286        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/tsp-sanity.cpp \
    283287        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-md2.cpp \
    284288        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-md5.cpp \
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyImage-win.cpp

    r52541 r52600  
    10041004
    10051005    uint32_t fFlags = RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT
     1006                    | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_MS_TIMESTAMP_IF_PRESENT
    10061007                    | RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY;
    10071008#ifndef IN_RING0
    10081009    if (!g_fHaveOtherRoots)
    10091010#endif
    1010         fFlags |= RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED;
     1011        fFlags |= RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED | RTCRPKCS7VERIFY_SD_F_USE_MS_TIMESTAMP_UNVERIFIED;
    10111012    return RTCrPkcs7VerifySignedData(pContentInfo, fFlags, g_hSpcAndNtKernelSuppStore, g_hSpcAndNtKernelRootStore,
    10121013                                     &ValidationTime, supHardNtViCertVerifyCallback, pNtViRdr, pErrInfo);
  • trunk/src/VBox/Runtime/Makefile.kmk

    r52443 r52600  
    359359        common/crypto/taf-init.cpp \
    360360        common/crypto/taf-sanity.cpp \
     361        common/crypto/tsp-asn1-decoder.cpp \
     362        common/crypto/tsp-core.cpp \
     363        common/crypto/tsp-init.cpp \
     364        common/crypto/tsp-sanity.cpp \
    361365        common/crypto/store.cpp \
    362366        common/crypto/store-inmem.cpp \
     
    19201924        common/crypto/taf-init.cpp \
    19211925        common/crypto/taf-sanity.cpp \
     1926        common/crypto/tsp-asn1-decoder.cpp \
     1927        common/crypto/tsp-core.cpp \
     1928        common/crypto/tsp-init.cpp \
     1929        common/crypto/tsp-sanity.cpp \
    19221930        common/checksum/alt-md2.cpp \
    19231931        common/checksum/alt-sha1.cpp \
     
    27342742spc-template.o spc-template.obj: spc-core.o spc-asn1-decoder.o spc-sanity.o spc-init.o
    27352743taf-template.o taf-template.obj: taf-core.o taf-asn1-decoder.o taf-sanity.o taf-init.o
     2744tsp-template.o tsp-template.obj: tsp-core.o tsp-asn1-decoder.o tsp-sanity.o tsp-init.o
    27362745x509-template.o x509-template.obj: x509-core.o x509-asn1-decoder.o x509-sanity.o x509-init.o
    27372746pkcs7-template.o pkcs7-template.obj: pkcs7-core.o pkcs7-asn1-decoder.o pkcs7-sanity.o pkcs7-init.o
  • trunk/src/VBox/Runtime/common/asn1/asn1-cursor.cpp

    r52533 r52600  
    2828*   Header Files                                                               *
    2929*******************************************************************************/
     30#define RT_STRICT
    3031#include "internal/iprt.h"
    3132#include <iprt/asn1.h>
     
    4748 *
    4849 * For reference, 'RTSignTool verify-exe RTSignTool.exe', requires a value of 15
    49  * to work without hitting the limit.
     50 * to work without hitting the limit for signatures with simple timestamps, and
     51 * 23 (amd64/rel = ~3KB) for the new microsoft timestamp counter signatures.
    5052 */
    5153#ifdef IN_RING3
  • trunk/src/VBox/Runtime/common/asn1/asn1-dump.cpp

    r51858 r52600  
    8484static void rtAsn1DumpPrintIdent(PRTASN1DUMPDATA pData, uint32_t uDepth)
    8585{
    86     uint32_t i = 0;
    87     uDepth *= 2;
    88     while (i < uDepth)
     86    uint32_t cchLeft = uDepth * 2;
     87    while (cchLeft > 0)
    8988    {
    9089        static char const s_szSpaces[] = "                                        ";
    91         uint32_t cch = RT_MIN(uDepth, sizeof(s_szSpaces) - 1);
     90        uint32_t cch = RT_MIN(cchLeft, sizeof(s_szSpaces) - 1);
    9291        rtAsn1DumpPrintf(pData, &s_szSpaces[sizeof(s_szSpaces) - 1 - cch]);
    93         i += cch;
     92        cchLeft -= cch;
    9493    }
    9594}
  • trunk/src/VBox/Runtime/common/asn1/asn1-ut-time-decode.cpp

    r51770 r52600  
    178178     * Check the dot.
    179179     */
    180     if (*pchFraction == '.')
     180    if (*pchFraction != '.')
    181181        return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
    182182                                   "%s: Expected GeneralizedTime fraction dot, found: '%c' ('%.*s')",
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-asn1-decoder.cpp

    r52533 r52600  
    3535#include <iprt/string.h>
    3636#include <iprt/crypto/spc.h>
     37#include <iprt/crypto/tsp.h>
    3738
    3839#include "pkcs7-internal.h"
     
    4243 * PKCS #7 ContentInfo
    4344 */
     45typedef enum RTCRPKCS7CONTENTINFOCHOICE
     46{
     47    RTCRPKCS7CONTENTINFOCHOICE_INVALID = 0,
     48    RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN,
     49    RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA,
     50    RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT,
     51    RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO,
     52    RTCRPKCS7CONTENTINFOCHOICE_END,
     53    RTCRPKCS7CONTENTINFOCHOICE_32BIT_HACK = 0x7fffffff
     54} RTCRPKCS7CONTENTINFOCHOICE;
    4455
    4556static int rtCrPkcs7ContentInfo_DecodeExtra(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTCRPKCS7CONTENTINFO pThis,
     
    4859    pThis->u.pCore = NULL;
    4960
    50     int             rc;
    51     RTASN1CURSOR    ContentCursor;
     61    /*
     62     * Figure the type.
     63     */
     64    RTCRPKCS7CONTENTINFOCHOICE  enmChoice;
     65    size_t                      cbContent = 0;
    5266    if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRPKCS7SIGNEDDATA_OID) == 0)
    5367    {
    54         rc = RTAsn1MemAllocZ(&pThis->Content.EncapsulatedAllocation, (void **)&pThis->Content.pEncapsulated,
    55                              sizeof(*pThis->u.pSignedData));
    56         if (RT_SUCCESS(rc))
    57         {
    58             pThis->u.pCore = pThis->Content.pEncapsulated;
    59             rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->Content.Asn1Core, &ContentCursor, "Content");
    60             if (RT_SUCCESS(rc))
    61                 rc = RTCrPkcs7SignedData_DecodeAsn1(&ContentCursor, 0, pThis->u.pSignedData, "SignedData");
    62         }
     68        enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA;
     69        cbContent = sizeof(*pThis->u.pSignedData);
    6370    }
    6471    else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRSPCINDIRECTDATACONTENT_OID) == 0)
    6572    {
    66         rc = RTAsn1MemAllocZ(&pThis->Content.EncapsulatedAllocation, (void **)&pThis->Content.pEncapsulated,
    67                              sizeof(*pThis->u.pIndirectDataContent));
     73        enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT;
     74        cbContent = sizeof(*pThis->u.pIndirectDataContent);
     75    }
     76    else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRTSPTSTINFO_OID) == 0)
     77    {
     78        enmChoice = RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO;
     79        cbContent = sizeof(*pThis->u.pTstInfo);
     80    }
     81    else
     82    {
     83        enmChoice = RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN;
     84        cbContent = 0;
     85    }
     86
     87    int rc = VINF_SUCCESS;
     88    if (enmChoice != RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN)
     89    {
     90        /*
     91         * Detect CMS octet string and open the content cursor.
     92         * Current we don't have work with any contet which is octet string,
     93         * they're all sequences, which make detection so much simpler.
     94         */
     95        PRTASN1OCTETSTRING  pOctetString = &pThis->Content;
     96        RTASN1CURSOR        ContentCursor;
     97        rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->Content.Asn1Core, &ContentCursor, "Content");
     98        if (   RT_SUCCESS(rc)
     99            && RTAsn1CursorIsNextEx(&ContentCursor, ASN1_TAG_OCTET_STRING, ASN1_TAGFLAG_PRIMITIVE | ASN1_TAGCLASS_UNIVERSAL))
     100        {
     101            rc = RTAsn1MemAllocZ(&pThis->Content.EncapsulatedAllocation, (void **)&pThis->Content.pEncapsulated,
     102                                 sizeof(*pOctetString));
     103            if (RT_SUCCESS(rc))
     104            {
     105                pThis->pCmsContent = pOctetString = (PRTASN1OCTETSTRING)pThis->Content.pEncapsulated;
     106                rc = RTAsn1OctetString_DecodeAsn1(&ContentCursor, 0, pOctetString, "CmsContent");
     107                if (RT_SUCCESS(rc))
     108                    rc = RTAsn1CursorCheckEnd(&ContentCursor);
     109                if (RT_SUCCESS(rc))
     110                    rc = RTAsn1CursorInitSubFromCore(pCursor, &pOctetString->Asn1Core, &ContentCursor, "CmsContent");
     111            }
     112        }
    68113        if (RT_SUCCESS(rc))
    69114        {
    70             pThis->u.pCore = pThis->Content.pEncapsulated;
    71             rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->Content.Asn1Core, &ContentCursor, "Content");
     115            /*
     116             * Allocate memory for the decoded content.
     117             */
     118            rc = RTAsn1MemAllocZ(&pOctetString->EncapsulatedAllocation, (void **)&pOctetString->pEncapsulated, cbContent);
    72119            if (RT_SUCCESS(rc))
    73                 rc = RTCrSpcIndirectDataContent_DecodeAsn1(&ContentCursor, 0, pThis->u.pIndirectDataContent, "IndirectDataContent");
     120            {
     121                pThis->u.pCore = pOctetString->pEncapsulated;
     122
     123                /*
     124                 * Decode it.
     125                 */
     126                switch (enmChoice)
     127                {
     128                    case RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA:
     129                        rc = RTCrPkcs7SignedData_DecodeAsn1(&ContentCursor, 0, pThis->u.pSignedData, "SignedData");
     130                        break;
     131                    case RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT:
     132                        rc = RTCrSpcIndirectDataContent_DecodeAsn1(&ContentCursor, 0, pThis->u.pIndirectDataContent,
     133                                                                   "IndirectDataContent");
     134                        break;
     135                    case RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO:
     136                        rc = RTCrTspTstInfo_DecodeAsn1(&ContentCursor, 0, pThis->u.pTstInfo, "TstInfo");
     137                        break;
     138                    default:
     139                        AssertFailed();
     140                        rc = VERR_IPE_NOT_REACHED_DEFAULT_CASE;
     141                        break;
     142                }
     143                if (RT_SUCCESS(rc))
     144                    rc = RTAsn1CursorCheckEnd(&ContentCursor);
     145                if (RT_SUCCESS(rc))
     146                    return VINF_SUCCESS;
     147
     148                RTAsn1MemFree(&pOctetString->EncapsulatedAllocation, pOctetString->pEncapsulated);
     149                pOctetString->pEncapsulated = NULL;
     150                pThis->u.pCore = NULL;
     151            }
    74152        }
    75153    }
    76     else
    77         return VINF_SUCCESS;
    78 
    79     if (RT_SUCCESS(rc))
    80         rc = RTAsn1CursorCheckEnd(&ContentCursor);
    81     if (RT_SUCCESS(rc))
    82         return VINF_SUCCESS;
    83154    return rc;
    84155}
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-core.cpp

    r52537 r52600  
    3434#include <iprt/err.h>
    3535#include <iprt/string.h>
     36#include <iprt/crypto/tsp.h>
    3637
    3738#include "pkcs7-internal.h"
     
    127128
    128129
     130RTDECL(PCRTASN1TIME) RTCrPkcs7SignerInfo_GetMsTimestamp(PCRTCRPKCS7SIGNERINFO pThis, PCRTCRPKCS7CONTENTINFO *ppContentInfo)
     131{
     132    /*
     133     * Assume there is only one, so no need to enumerate anything here.
     134     */
     135    uint32_t             cAttrsLeft = pThis->UnauthenticatedAttributes.cItems;
     136    PCRTCRPKCS7ATTRIBUTE pAttr      = pThis->UnauthenticatedAttributes.paItems;
     137    while (cAttrsLeft-- > 0)
     138    {
     139        if (pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_MS_TIMESTAMP)
     140        {
     141            uint32_t                cLeft        = pAttr->uValues.pContentInfos->cItems;
     142            PCRTCRPKCS7CONTENTINFO  pContentInfo = &pAttr->uValues.pContentInfos->paItems[0];
     143            while (cLeft-- > 0)
     144            {
     145                if (RTAsn1ObjId_CompareWithString(&pContentInfo->ContentType, RTCRPKCS7SIGNEDDATA_OID) == 0)
     146                {
     147                    if (RTAsn1ObjId_CompareWithString(&pContentInfo->u.pSignedData->ContentInfo.ContentType,
     148                                                      RTCRTSPTSTINFO_OID) == 0)
     149                    {
     150                        if (ppContentInfo)
     151                            *ppContentInfo = pContentInfo;
     152                        return &pContentInfo->u.pSignedData->ContentInfo.u.pTstInfo->GenTime;
     153                    }
     154                }
     155
     156                pContentInfo++;
     157            }
     158        }
     159        pAttr++;
     160    }
     161
     162    /*
     163     * No signature was found.
     164     */
     165    if (ppContentInfo)
     166        *ppContentInfo = NULL;
     167
     168    return NULL;
     169}
     170
     171
    129172/*
    130173 * PCKS #7 ContentInfo.
     
    138181
    139182/*
     183 * Set of some kind of certificate supported by PKCS #7 or CMS.
     184 */
     185
     186RTDECL(PCRTCRX509CERTIFICATE)
     187RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(PCRTCRPKCS7SETOFCERTS pCertificates,
     188                                                    PCRTCRX509NAME pIssuer, PCRTASN1INTEGER pSerialNumber)
     189{
     190    for (uint32_t i = 0; i < pCertificates->cItems; i++)
     191        if (   pCertificates->paItems[i].enmChoice == RTCRPKCS7CERTCHOICE_X509
     192            && RTCrX509Certificate_MatchIssuerAndSerialNumber(pCertificates->paItems[i].u.pX509Cert, pIssuer, pSerialNumber))
     193            return pCertificates->paItems[i].u.pX509Cert;
     194    return NULL;
     195}
     196
     197
     198/*
    140199 * Generate the standard core code.
    141200 */
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-sanity.cpp

    r51770 r52600  
    4747    //RTAsn1Dump(&pSignedData->SeqCore.Asn1Core, 0, 0, RTAsn1DumpStrmPrintfV, g_pStdOut);
    4848
    49     if (RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V1) != 0)
     49    if (   RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V1) != 0
     50        && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V3) != 0
     51        && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V4) != 0
     52        && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V5) != 0)
    5053        return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNED_DATA_VERSION, "SignedData version is %llu, expected %u",
    5154                             pSignedData->Version.uValue.u, RTCRPKCS7SIGNEDDATA_V1);
     
    8285                            "SignedData.Certifcates is empty, expected at least one certificate");
    8386
    84     if (pSignedData->Certificates.cItems > 0)
    85     {
    86         int rc = RTCrX509Certificates_CheckSanity(&pSignedData->Certificates, 0, pErrInfo, "SignedData.T0.Certificates");
    87         if (RT_FAILURE(rc))
    88             return rc;
    89     }
    90 
    9187    /*
    9288     * Crls.
     
    127123
    128124        PCRTCRX509CERTIFICATE pCert;
    129         pCert = RTCrX509Certificates_FindByIssuerAndSerialNumber(&pSignedData->Certificates,
    130                                                                  &pSignerInfo->IssuerAndSerialNumber.Name,
    131                                                                  &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
     125        pCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSignedData->Certificates,
     126                                                                    &pSignerInfo->IssuerAndSerialNumber.Name,
     127                                                                    &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
    132128        if (!pCert && (fFlags & RTCRPKCS7SIGNEDDATA_SANITY_F_SIGNING_CERT_PRESENT))
    133129            return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNER_CERT_NOT_SHIPPED,
     
    146142
    147143        /* Digest encryption algorithm. */
     144#if 0  /** @todo Unimportant: Seen timestamp signatures specifying pkcs1-Sha256WithRsaEncryption in SignerInfo and just RSA in the certificate.  Figure out how to compare the two. */
    148145        if (   pCert
    149146            && RTCrX509AlgorithmIdentifier_Compare(&pSignerInfo->DigestEncryptionAlgorithm,
     
    153150                                 i, pSignerInfo->DigestEncryptionAlgorithm.Algorithm.szObjId,
    154151                                 pCert->TbsCertificate.SubjectPublicKeyInfo.Algorithm.Algorithm.szObjId);
     152#endif
    155153
    156154        /* Authenticated attributes we know. */
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-template.h

    r52503 r52600  
    5959RTASN1TMPL_MEMBER_DYN(          uValues,    pSigningTime,   RTASN1SETOFTIMES,           RTAsn1SetOfTimes,           Allocation,
    6060    enmType, RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME,   RTAsn1ObjId_CompareWithString(&pThis->Type, RTCR_PKCS9_ID_SIGNING_TIME_OID) == 0);
     61RTASN1TMPL_MEMBER_DYN(          uValues,    pContentInfos,  RTCRPKCS7SETOFCONTENTINFOS, RTCrPkcs7SetOfContentInfos, Allocation,
     62    enmType, RTCRPKCS7ATTRIBUTETYPE_MS_TIMESTAMP,   RTAsn1ObjId_CompareWithString(&pThis->Type, RTCR_PKCS9_ID_MS_TIMESTAMP) == 0);
    6163RTASN1TMPL_MEMBER_DYN_DEFAULT(  uValues,    pCores,         RTASN1SETOFCORES,           RTAsn1SetOfCores,           Allocation,
    6264    enmType, RTCRPKCS7ATTRIBUTETYPE_UNKNOWN);
     
    122124RTASN1TMPL_MEMBER(              DigestAlgorithms,           RTCRX509ALGORITHMIDENTIFIERS,   RTCrX509AlgorithmIdentifiers);
    123125RTASN1TMPL_MEMBER(              ContentInfo,                RTCRPKCS7CONTENTINFO,           RTCrPkcs7ContentInfo);
    124 RTASN1TMPL_MEMBER_OPT_ITAG(     Certificates,               RTCRX509CERTIFICATES,           RTCrX509Certificates,   0);
     126RTASN1TMPL_MEMBER_OPT_ITAG(     Certificates,               RTCRPKCS7SETOFCERTS,            RTCrPkcs7SetOfCerts,    0);
    125127RTASN1TMPL_MEMBER_OPT_ITAG(     Crls,                       RTASN1CORE,                     RTAsn1Core,             1);
    126128RTASN1TMPL_MEMBER(              SignerInfos,                RTCRPKCS7SIGNERINFOS,           RTCrPkcs7SignerInfos);
    127129RTASN1TMPL_EXEC_CHECK_SANITY(   rc = rtCrPkcs7SignedData_CheckSanityExtra(pThis, fFlags, pErrInfo, pszErrorTag) ) /* no ; */
    128130RTASN1TMPL_END_SEQCORE();
     131#undef RTASN1TMPL_TYPE
     132#undef RTASN1TMPL_EXT_NAME
     133#undef RTASN1TMPL_INT_NAME
     134
     135
     136/*
     137 * Set of PCKS #7 SignedData.
     138 */
     139#define RTASN1TMPL_TYPE         RTCRPKCS7SETOFSIGNEDDATA
     140#define RTASN1TMPL_EXT_NAME     RTCrPkcs7SetOfSignedData
     141#define RTASN1TMPL_INT_NAME     rtCrPkcs7SetOfSignedData
     142RTASN1TMPL_SET_OF(RTCRPKCS7SIGNEDDATA, RTCrPkcs7SignedData);
    129143#undef RTASN1TMPL_TYPE
    130144#undef RTASN1TMPL_EXT_NAME
     
    163177#undef RTASN1TMPL_INT_NAME
    164178
     179
     180/*
     181 * Set of PCKS #7 ContentInfo.
     182 */
     183#define RTASN1TMPL_TYPE         RTCRPKCS7SETOFCONTENTINFOS
     184#define RTASN1TMPL_EXT_NAME     RTCrPkcs7SetOfContentInfos
     185#define RTASN1TMPL_INT_NAME     rtCrPkcs7SetOfContentInfos
     186RTASN1TMPL_SET_OF(RTCRPKCS7CONTENTINFO, RTCrPkcs7ContentInfo);
     187#undef RTASN1TMPL_TYPE
     188#undef RTASN1TMPL_EXT_NAME
     189#undef RTASN1TMPL_INT_NAME
     190
     191
     192/*
     193 * One PKCS #7 ExtendedCertificateOrCertificate or a CMS CertificateChoices (sic).
     194 */
     195#define RTASN1TMPL_TYPE         RTCRPKCS7CERT
     196#define RTASN1TMPL_EXT_NAME     RTCrPkcs7Cert
     197#define RTASN1TMPL_INT_NAME     rtCrPkcs7Cert
     198RTASN1TMPL_BEGIN_PCHOICE();
     199RTASN1TMPL_PCHOICE_ITAG_UC(     ASN1_TAG_SEQUENCE, RTCRPKCS7CERTCHOICE_X509, u.pX509Cert, X509Cert,     RTCRX509CERTIFICATE, RTCrX509Certificate);
     200RTASN1TMPL_PCHOICE_ITAG(        0, RTCRPKCS7CERTCHOICE_EXTENDED_PKCS6, u.pExtendedCert,   ExtendedCert, RTASN1CORE, RTAsn1Core);
     201RTASN1TMPL_PCHOICE_ITAG(        1, RTCRPKCS7CERTCHOICE_AC_V1,          u.pAcV1,           AcV1,         RTASN1CORE, RTAsn1Core);
     202RTASN1TMPL_PCHOICE_ITAG(        2, RTCRPKCS7CERTCHOICE_AC_V2,          u.pAcV2,           AcV2,         RTASN1CORE, RTAsn1Core);
     203RTASN1TMPL_PCHOICE_ITAG(        3, RTCRPKCS7CERTCHOICE_OTHER,          u.pOtherCert,      OtherCert,    RTASN1CORE, RTAsn1Core);
     204RTASN1TMPL_END_PCHOICE();
     205#undef RTASN1TMPL_TYPE
     206#undef RTASN1TMPL_EXT_NAME
     207#undef RTASN1TMPL_INT_NAME
     208
     209
     210/*
     211 * Set of PKCS #7 ExtendedCertificateOrCertificate or a CMS CertificateChoices.
     212 */
     213#define RTASN1TMPL_TYPE         RTCRPKCS7SETOFCERTS
     214#define RTASN1TMPL_EXT_NAME     RTCrPkcs7SetOfCerts
     215#define RTASN1TMPL_INT_NAME     rtCrPkcs7SetOfCerts
     216RTASN1TMPL_SET_OF(RTCRPKCS7CERT, RTCrPkcs7Cert);
     217#undef RTASN1TMPL_TYPE
     218#undef RTASN1TMPL_EXT_NAME
     219#undef RTASN1TMPL_INT_NAME
     220
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-verify.cpp

    r52545 r52600  
    7171        if (RT_SUCCESS(rcOssl))
    7272        {
    73             for (uint32_t i = 0; i < pContentInfo->u.pSignedData->Certificates.cItems; i++)
    74                 rtCrOpenSslAddX509CertToStack(pAddCerts, &pContentInfo->u.pSignedData->Certificates.paItems[i]);
     73            PCRTCRPKCS7SETOFCERTS pCerts = &pContentInfo->u.pSignedData->Certificates;
     74            for (uint32_t i = 0; i < pCerts->cItems; i++)
     75                if (pCerts->paItems[i].enmChoice == RTCRPKCS7CERTCHOICE_X509)
     76                    rtCrOpenSslAddX509CertToStack(pAddCerts, pCerts->paItems[i].u.pX509Cert);
    7577
    7678
     
    398400    {
    399401        hSignerCertSrc = NULL;
    400         pSignerCert = RTCrX509Certificates_FindByIssuerAndSerialNumber(&pSignedData->Certificates,
    401                                                                        &pSignerInfo->IssuerAndSerialNumber.Name,
    402                                                                        &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
     402        pSignerCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSignedData->Certificates,
     403                                                                          &pSignerInfo->IssuerAndSerialNumber.Name,
     404                                                                          &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
    403405        if (!pSignerCert)
    404406            return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNED_DATA_CERT_NOT_FOUND,
     
    426428                rc = RTCrX509CertPathsSetUntrustedStore(hCertPaths, hAdditionalCerts);
    427429            if (pSignedData->Certificates.cItems > 0 && RT_SUCCESS(rc))
    428                 rc = RTCrX509CertPathsSetUntrustedArray(hCertPaths,
    429                                                         pSignedData->Certificates.paItems,
    430                                                         pSignedData->Certificates.cItems);
     430                rc = RTCrX509CertPathsSetUntrustedSet(hCertPaths, &pSignedData->Certificates);
    431431            if (RT_SUCCESS(rc))
    432432            {
     
    586586        return rc;
    587587
    588     AssertReturn(!(fFlags & RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE), VERR_INVALID_FLAGS);
    589 
    590588    /*
    591589     * Hash the content info.
     
    640638             * Validate the signed infos.
    641639             */
     640            uint32_t fPrimaryVccFlags = !(fFlags & RTCRPKCS7VERIFY_SD_F_USAGE_TIMESTAMPING)
     641                                      ? RTCRPKCS7VCC_F_SIGNED_DATA : RTCRPKCS7VCC_F_TIMESTAMP;
    642642            rc = VERR_CR_PKCS7_NO_SIGNER_INFOS;
    643643            for (i = 0; i < pSignedData->SignerInfos.cItems; i++)
     
    651651                /*
    652652                 * See if we can find a trusted signing time.
     653                 * (Note that while it would make sense splitting up this function,
     654                 * we need to carry a lot of arguments around, so better not.)
    653655                 */
    654656                bool                    fDone              = false;
     
    667669                            rc = rtCrPkcs7VerifySignerInfo(pSignerInfo, pSignedData, hThisDigest, fFlags,
    668670                                                           hAdditionalCerts, hTrustedCerts, &ThisValidationTime,
    669                                                            pfnVerifyCert, RTCRPKCS7VCC_F_SIGNED_DATA | RTCRPKCS7VCC_F_TIMESTAMP,
     671                                                           pfnVerifyCert, fPrimaryVccFlags | RTCRPKCS7VCC_F_TIMESTAMP,
    670672                                                           pvUser, pErrInfo);
    671673                        }
     
    680682                                rc = rtCrPkcs7VerifySignerInfo(pSignerInfo, pSignedData, hThisDigest, fFlags, hAdditionalCerts,
    681683                                                               hTrustedCerts, &ThisValidationTime,
    682                                                                pfnVerifyCert, RTCRPKCS7VCC_F_SIGNED_DATA, pvUser, pErrInfo);
     684                                                               pfnVerifyCert, fPrimaryVccFlags, pvUser, pErrInfo);
    683685                        }
    684686                        fDone = RT_SUCCESS(rc)
     
    693695
    694696                /*
     697                 * If not luck, check for microsoft timestamp counter signatures.
     698                 */
     699                if (!fDone && !(fFlags & RTCRPKCS7VERIFY_SD_F_IGNORE_MS_TIMESTAMP))
     700                {
     701                    PCRTCRPKCS7CONTENTINFO pSignedTimestamp = NULL;
     702                    pSignedTime = RTCrPkcs7SignerInfo_GetMsTimestamp(pSignerInfo, &pSignedTimestamp);
     703                    if (pSignedTime)
     704                    {
     705                        RTTIMESPEC ThisValidationTime;
     706                        if (RT_LIKELY(RTTimeImplode(&ThisValidationTime, &pSignedTime->Time)))
     707                        {
     708                            rc = VINF_SUCCESS;
     709                            if (!(fFlags & RTCRPKCS7VERIFY_SD_F_USE_MS_TIMESTAMP_UNVERIFIED))
     710                                rc = RTCrPkcs7VerifySignedData(pSignedTimestamp,
     711                                                               fFlags | RTCRPKCS7VERIFY_SD_F_IGNORE_MS_TIMESTAMP
     712                                                               | RTCRPKCS7VERIFY_SD_F_USAGE_TIMESTAMPING,
     713                                                               hAdditionalCerts, hTrustedCerts, &ThisValidationTime,
     714                                                               pfnVerifyCert, pvUser, pErrInfo);
     715
     716                            if (RT_SUCCESS(rc))
     717                                rc = rtCrPkcs7VerifySignerInfo(pSignerInfo, pSignedData, hThisDigest, fFlags, hAdditionalCerts,
     718                                                               hTrustedCerts, &ThisValidationTime,
     719                                                               pfnVerifyCert, fPrimaryVccFlags, pvUser, pErrInfo);
     720                            fDone = RT_SUCCESS(rc)
     721                                 || (fFlags & RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_MS_TIMESTAMP_IF_PRESENT);
     722                        }
     723                        else
     724                        {
     725                            rc = RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_3, "RTTimeImplode failed");
     726                            fDone = true;
     727                        }
     728
     729                    }
     730                }
     731
     732                /*
    695733                 * No valid signing time found, use the one specified instead.
    696734                 */
    697735                if (!fDone)
    698736                    rc = rtCrPkcs7VerifySignerInfo(pSignerInfo, pSignedData, hThisDigest, fFlags, hAdditionalCerts, hTrustedCerts,
    699                                                    pValidationTime, pfnVerifyCert, RTCRPKCS7VCC_F_SIGNED_DATA, pvUser, pErrInfo);
     737                                                   pValidationTime, pfnVerifyCert, fPrimaryVccFlags, pvUser, pErrInfo);
    700738                RTCrDigestRelease(hThisDigest);
    701739                if (RT_FAILURE(rc))
     
    721759     * Verify using OpenSSL and combine the results (should be identical).
    722760     */
     761    /** @todo figure out how to verify MS timstamp signatures using OpenSSL. */
     762    if (fFlags & RTCRPKCS7VERIFY_SD_F_USAGE_TIMESTAMPING)
     763        return rc;
    723764    int rcOssl = rtCrPkcs7VerifySignedDataUsingOpenSsl(pContentInfo, fFlags, hAdditionalCerts, hTrustedCerts,
    724765                                                       pvContent, cbContent, RT_SUCCESS(rc) ? pErrInfo : NULL);
  • trunk/src/VBox/Runtime/common/crypto/x509-certpaths.cpp

    r51823 r52600  
    3939#include <iprt/list.h>
    4040#include <iprt/time.h>
     41#include <iprt/crypto/pkcs7.h> /* PCRTCRPKCS7SETOFCERTS */
    4142#include <iprt/crypto/store.h>
    4243
     
    9293#define RTCRX509CERTPATHNODE_SRC_NONE               0
    9394#define RTCRX509CERTPATHNODE_SRC_TARGET             1
    94 #define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY    2
    95 #define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE    3
    96 #define RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE      4
    97 #define RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT       5
     95#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET      2
     96#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY    3
     97#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE    4
     98#define RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE      5
     99#define RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT       6
    98100#define RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc)   ((uSrc) >= RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE)
    99101/** @} */
     
    164166    /** Store of untrusted certificates. */
    165167    RTCRSTORE                       hUntrustedStore;
    166     /** Array of untrusted certificates, typically from the protocol (like the
    167      *  certificates member of PKCS \#7 SignedData). */
     168    /** Array of untrusted certificates, typically from the protocol. */
    168169    PCRTCRX509CERTIFICATE           paUntrustedCerts;
    169170    /** Number of entries in paUntrusted. */
    170171    uint32_t                        cUntrustedCerts;
     172    /** Set of untrusted PKCS \#7 / CMS certificatess. */
     173    PCRTCRPKCS7SETOFCERTS           pUntrustedCertsSet;
    171174
    172175    /** UTC time we're going to validate the path at, requires
     
    364367            pThis->hUntrustedStore              = NIL_RTCRSTORE;
    365368            pThis->paUntrustedCerts             = NULL; /* Referencing user memory. */
     369            pThis->pUntrustedCertsSet           = NULL; /* Referencing user memory. */
    366370            pThis->papInitialUserPolicySet      = NULL; /* Referencing user memory. */
    367371            pThis->pInitialPermittedSubtrees    = NULL; /* Referencing user memory. */
     
    438442    pThis->paUntrustedCerts = paCerts;
    439443    pThis->cUntrustedCerts  = cCerts;
     444    return VINF_SUCCESS;
     445}
     446
     447
     448RTDECL(int) RTCrX509CertPathsSetUntrustedSet(RTCRX509CERTPATHS hCertPaths, PCRTCRPKCS7SETOFCERTS pSetOfCerts)
     449{
     450    PRTCRX509CERTPATHSINT pThis = hCertPaths;
     451    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     452    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
     453
     454    pThis->pUntrustedCertsSet = pSetOfCerts;
    440455    return VINF_SUCCESS;
    441456}
     
    694709            if (RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(&pThis->paUntrustedCerts[i], pIssuer))
    695710                rtCrX509CertPathsAddIssuer(pThis, pNode, &pThis->paUntrustedCerts[i], NULL,
    696                                                  RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY);
     711                                           RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY);
     712
     713    /** @todo Rainy day: Should abstract the untrusted array and set so we don't get
     714     *        unnecessary PKCS7/CMS header dependencies. */
     715
     716    /*
     717     * Untrusted set.
     718     */
     719    if (pThis->pUntrustedCertsSet)
     720    {
     721        uint32_t const  cCerts  = pThis->pUntrustedCertsSet->cItems;
     722        PCRTCRPKCS7CERT paCerts = pThis->pUntrustedCertsSet->paItems;
     723        for (uint32_t i = 0; i < cCerts; i++)
     724            if (   paCerts[i].enmChoice == RTCRPKCS7CERTCHOICE_X509
     725                && RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(paCerts[i].u.pX509Cert, pIssuer))
     726                rtCrX509CertPathsAddIssuer(pThis, pNode, paCerts[i].u.pX509Cert, NULL, RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET);
     727    }
    697728}
    698729
     
    10841115    {
    10851116        case RTCRX509CERTPATHNODE_SRC_TARGET:           return "target";
     1117        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET:    return "untrusted_set";
    10861118        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY:  return "untrusted_array";
    10871119        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE:  return "untrusted_store";
  • trunk/src/VBox/Runtime/tools/RTSignTool.cpp

    r52538 r52600  
    183183                    PCRTCRPKCS7ISSUERANDSERIALNUMBER pISN = &pSd->SignerInfos.paItems[0].IssuerAndSerialNumber;
    184184                    PCRTCRX509CERTIFICATE pCert;
    185                     pCert = RTCrX509Certificates_FindByIssuerAndSerialNumber(&pSd->Certificates,
    186                                                                             &pISN->Name, &pISN->SerialNumber);
     185                    pCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSd->Certificates,
     186                                                                                &pISN->Name, &pISN->SerialNumber);
    187187                    if (pCert)
    188188                    {
     
    404404            return RTCrPkcs7VerifySignedData(pContentInfo,
    405405                                             RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
    406                                              | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT,
     406                                             | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT
     407                                             | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_MS_TIMESTAMP_IF_PRESENT,
    407408                                             pState->hAdditionalStore, pState->hRootStore, &ValidationTime,
    408409                                             VerifyExecCertVerifyCallback, pState, pErrInfo);
Note: See TracChangeset for help on using the changeset viewer.

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