VirtualBox

Ignore:
Timestamp:
May 14, 2020 5:40:35 PM (5 years ago)
Author:
vboxsync
Message:

IPRT/crypto: Adding RTAsn1EncodeQueryRawBits to deal with getting encoded bytes cheaply if possible and always safely. Fixed another place using RTASN1CORE_GET_RAW_ASN1_PTR and assuming input was decoded and had valid data pointers. Added RTCrStoreCertAddPkcs7 and RTCrStoreCertAddX509 for more conveniently adding decoded certs to stores. Added RTCRPKCS7VERIFY_SD_F_TRUST_ALL_CERTS to the PKCS7 verification code. Added RTCrPkcs7_ReadFromBuffer. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/crypto/iprt-openssl.cpp

    r84248 r84310  
    7070{
    7171    const unsigned char *pabEncoded;
    72 
    73     /*
    74      * ASSUME that if the certificate has data pointers, it's been parsed out
    75      * of a binary blob and we can safely access that here.
    76      */
    77     if (pCert->SeqCore.Asn1Core.uData.pv)
     72    uint32_t             cbEncoded;
     73    void                *pvFree;
     74    int rc = RTAsn1EncodeQueryRawBits(RTCrX509Certificate_GetAsn1Core(pCert),
     75                                      (const uint8_t **)&pabEncoded, &cbEncoded, &pvFree, pErrInfo);
     76    if (RT_SUCCESS(rc))
    7877    {
    79         pabEncoded = (const unsigned char *)RTASN1CORE_GET_RAW_ASN1_PTR(&pCert->SeqCore.Asn1Core);
    80         uint32_t cbEncoded  = RTASN1CORE_GET_RAW_ASN1_SIZE(&pCert->SeqCore.Asn1Core);
    81         X509    *pOsslCert  = NULL;
    82         if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
     78        X509 *pOsslCert = NULL;
     79        X509 *pOsslCertRet = d2i_X509(&pOsslCert, &pabEncoded, cbEncoded);
     80        RTMemTmpFree(pvFree);
     81        if (pOsslCertRet == pOsslCert)
    8382        {
    8483            *ppvOsslCert = pOsslCert;
    8584            return VINF_SUCCESS;
    8685        }
     86        rc = RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
     87
    8788    }
    88     /*
    89      * Otherwise, we'll have to encode it into a temporary buffer that openssl
    90      * can decode into its structures.
    91      */
    92     else
    93     {
    94         PRTASN1CORE pNonConstCore = (PRTASN1CORE)&pCert->SeqCore.Asn1Core;
    95         uint32_t    cbEncoded     = 0;
    96         int rc = RTAsn1EncodePrepare(pNonConstCore, RTASN1ENCODE_F_DER, &cbEncoded, pErrInfo);
    97         AssertRCReturn(rc, rc);
    98 
    99         void * const pvEncoded = RTMemTmpAllocZ(cbEncoded);
    100         AssertReturn(pvEncoded, VERR_NO_TMP_MEMORY);
    101 
    102         rc = RTAsn1EncodeToBuffer(pNonConstCore, RTASN1ENCODE_F_DER, pvEncoded, cbEncoded, pErrInfo);
    103         if (RT_SUCCESS(rc))
    104         {
    105             pabEncoded = (const unsigned char *)pvEncoded;
    106             X509 *pOsslCert = NULL;
    107             if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
    108             {
    109                 *ppvOsslCert = pOsslCert;
    110                 RTMemTmpFree(pvEncoded);
    111                 return VINF_SUCCESS;
    112             }
    113         }
    114         else
    115         {
    116             RTMemTmpFree(pvEncoded);
    117             return rc;
    118         }
    119     }
    120 
    12189    *ppvOsslCert = NULL;
    122     return RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
     90    return rc;
    12391}
    12492
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