VirtualBox

Ignore:
Timestamp:
Aug 28, 2015 1:31:29 AM (10 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
102396
Message:

IPRT: Started on accessing system certificate stores to get SSL roots for cURL.

Location:
trunk/src/VBox/Runtime/common/crypto
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/crypto/RTCrStoreCertAddFromFile.cpp

    r57548 r57572  
    100100RTDECL(int) RTCrStoreCertAddFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo)
    101101{
    102     AssertReturn(!fFlags, VERR_INVALID_FLAGS);
    103 #if 0
    104     RTCRX509CERTIFICATES Certs;
    105     int rc = RTCrX509Certificates_ReadFromFile(pszFilename, 0, &Certs, pErrInfo);
    106     if (RT_SUCCESS(rc))
    107     {
    108         for (uint32_t i = 0; i < Certs.cCerts; i++)
    109         {
    110             int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER,
    111                                               RTASN1CORE_GET_RAW_ASN1_PTR(&Certs.paCerts[i].SeqCore.Asn1Core),
    112                                               RTASN1CORE_GET_RAW_ASN1_SIZE(&Certs.paCerts[i].SeqCore.Asn1Core),
    113                                               RT_SUCCESS(rc) ? pErrInfo : NULL);
    114             if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
    115                 rc = rc2;
    116         }
    117 
    118         RTAsn1Destroy(&Certs.SetCore.Asn1Core);
    119     }
    120     return rc;
    121 #else
     102    AssertReturn(!(fFlags & ~(RTCRCERTCTX_F_ADD_IF_NOT_FOUND | RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR)), VERR_INVALID_FLAGS);
    122103
    123104    PCRTCRPEMSECTION pSectionHead;
    124     int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
     105    int rc = RTCrPemReadFile(pszFilename,
     106                             fFlags & RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR ? RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR : 0,
     107                             g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
    125108    if (RT_SUCCESS(rc))
    126109    {
     
    128111        while (pCurSec)
    129112        {
    130             int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER, pCurSec->pbData, pCurSec->cbData,
    131                                               RT_SUCCESS(rc) ? pErrInfo : NULL);
     113            int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER | (fFlags & ~RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR),
     114                                              pCurSec->pbData, pCurSec->cbData, !RTErrInfoIsSet(pErrInfo) ? pErrInfo : NULL);
    132115            if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
     116            {
    133117                rc = rc2;
     118                if (!(fFlags & RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR))
     119                    break;
     120            }
    134121            pCurSec = pCurSec->pNext;
    135122        }
     
    138125    }
    139126    return rc;
    140 #endif
    141127}
    142128
  • trunk/src/VBox/Runtime/common/crypto/pemfile.cpp

    r57358 r57572  
    125125                uint8_t const  *pbSavedContent = pbContent;
    126126                size_t  const   cbSavedContent = cbContent;
    127                 uint32_t        iMarker = 0;
    128                 while (iMarker < cMarkers)
     127                for (uint32_t iMarker = 0; iMarker < cMarkers; iMarker++)
    129128                {
    130129                    pbContent = pbSavedContent;
     
    143142                        cbContent -= cchWord;
    144143
    145                         if (!cbContent || !RT_C_IS_BLANK(*pbContent))
    146                             break;
    147                         do
    148                         {
    149                             pbContent++;
    150                             cbContent--;
    151                         } while (cbContent > 0 && RT_C_IS_BLANK(*pbContent));
     144                        if (!cbContent)
     145                            break;
     146                        if (RT_C_IS_BLANK(*pbContent))
     147                            do
     148                            {
     149                                pbContent++;
     150                                cbContent--;
     151                            } while (cbContent > 0 && RT_C_IS_BLANK(*pbContent));
     152                        else if (cWords > 1 || pbContent[0] != '-')
     153                            break;
    152154
    153155                        cWords--;
     
    175177                                if (poffEnd)
    176178                                    *poffEnd = pbContent - pbStart;
    177                                 if (*ppMatch)
     179                                if (ppMatch)
    178180                                    *ppMatch = &paMarkers[iMarker];
    179181                                return true;
     
    265267{
    266268    /*
    267      * Assume a well formed PEM file contains only 7-bit ASCII and restricts
    268      * itself to the following control characters:
     269     * Well formed PEM files should probably only contain 7-bit ASCII and
     270     * restrict thenselfs to the following control characters:
    269271     *      tab, newline, return, form feed
     272     *
     273     * However, if we wan't to read PEM files which contains human readable
     274     * certificate details before or after each base-64 section, we can't stick
     275     * to 7-bit ASCII.  We could say it must be UTF-8, but that's probably to
     276     * limited too.  So, we'll settle for detecting binary files by control
     277     * characters alone (safe enough for DER encoded stuff, I think).
    270278     */
    271279    while (cbFile-- > 0)
    272280    {
    273281        uint8_t const b = *pbFile++;
    274         if (   b >= 0x7f
    275             || (b < 32 && b != '\t' && b != '\n' && b != '\r' && b != '\f') )
     282        if (b < 32 && b != '\t' && b != '\n' && b != '\r' && b != '\f')
    276283        {
    277284            /* Ignore EOT (4), SUB (26) and NUL (0) at the end of the file. */
     
    280287                    || (   cbFile == 1
    281288                        && *pbFile == '\0')))
    282                 return true;
     289                return false;
     290
    283291            if (b == 0 && cbFile == 0)
    284                 return true;
    285             return false;
    286         }
    287     }
    288     return true;
     292                return false;
     293
     294            return true;
     295        }
     296    }
     297    return false;
    289298}
    290299
     
    328337                            PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo)
    329338{
    330     AssertReturn(!fFlags, VERR_INVALID_FLAGS);
     339    AssertReturn(!(fFlags & ~RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR), VERR_INVALID_FLAGS);
    331340
    332341    size_t      cbContent;
     
    362371                    /* Decode the section. */
    363372                    /** @todo copy the preamble as well. */
    364                     rc = rtCrPemDecodeBase64(pbContent + offBegin, offEnd - offBegin,
    365                                              (void **)&pSection->pbData, &pSection->cbData);
    366                     if (RT_FAILURE(rc))
     373                    int rc2 = rtCrPemDecodeBase64(pbContent + offBegin, offEnd - offBegin,
     374                                                  (void **)&pSection->pbData, &pSection->cbData);
     375                    if (RT_FAILURE(rc2))
    367376                    {
    368377                        pSection->pbData = NULL;
    369378                        pSection->cbData = 0;
    370                         break;
     379                        if (   rc2 == VERR_INVALID_BASE64_ENCODING
     380                            && (fFlags & RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR))
     381                            rc = -rc2;
     382                        else
     383                        {
     384                            rc = rc2;
     385                            break;
     386                        }
    371387                    }
    372388
  • trunk/src/VBox/Runtime/common/crypto/store-inmem.cpp

    r57358 r57572  
    282282    int rc;
    283283
    284     AssertMsgReturn(   fFlags == RTCRCERTCTX_F_ENC_X509_DER
    285                     || fFlags == RTCRCERTCTX_F_ENC_TAF_DER
     284    AssertMsgReturn(   (fFlags & RTCRCERTCTX_F_ENC_MASK) == RTCRCERTCTX_F_ENC_X509_DER
     285                    || (fFlags & RTCRCERTCTX_F_ENC_MASK) == RTCRCERTCTX_F_ENC_TAF_DER
    286286                    , ("Only X.509 and TAF DER are supported: %#x\n", fFlags), VERR_INVALID_FLAGS);
    287287
    288     if (pThis->cCerts + 1 > pThis->cCertsAlloc)
     288    /*
     289     * Check for duplicates if specified.
     290     */
     291    if (fFlags & RTCRCERTCTX_F_ADD_IF_NOT_FOUND)
     292    {
     293        uint32_t iCert = pThis->cCerts;
     294        while (iCert-- > 0)
     295        {
     296            PRTCRSTOREINMEMCERT pCert = pThis->papCerts[iCert];
     297            if (   pCert->Core.Public.cbEncoded == cbEncoded
     298                && pCert->Core.Public.fFlags == (fFlags & RTCRCERTCTX_F_ENC_MASK)
     299                && memcmp(pCert->Core.Public.pabEncoded, pbEncoded, cbEncoded) == 0)
     300                return VWRN_ALREADY_EXISTS;
     301        }
     302    }
     303
     304    /*
     305     * Add it.
     306     */
     307    if (pThis->cCerts + 1 <= pThis->cCertsAlloc)
     308    { /* likely */ }
     309    else
    289310    {
    290311        rc = rtCrStoreInMemGrow(pThis, pThis->cCerts + 1);
     
    293314    }
    294315
    295     rc = rtCrStoreInMemCreateCertEntry(pThis, fFlags, pbEncoded, cbEncoded, pErrInfo, &pThis->papCerts[pThis->cCerts]);
     316    rc = rtCrStoreInMemCreateCertEntry(pThis, fFlags & RTCRCERTCTX_F_ENC_MASK, pbEncoded, cbEncoded,
     317                                       pErrInfo, &pThis->papCerts[pThis->cCerts]);
    296318    if (RT_SUCCESS(rc))
    297319    {
     
    360382    return rc;
    361383}
    362 
     384RT_EXPORT_SYMBOL(RTCrStoreCreateInMem);
     385
  • trunk/src/VBox/Runtime/common/crypto/store-internal.h

    r56290 r57572  
    125125     * Adds a certificate to the store.
    126126     *
     127     * @returns IPRT status.
     128     * @retval  VWRN_ALREADY_EXISTS if the certificate is already present and
     129     *          RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
    127130     * @param   pvProvider      The provider specific data.
    128131     * @param   fFlags          RTCRCERTCTX_F_XXX.
  • trunk/src/VBox/Runtime/common/crypto/store.cpp

    r57358 r57572  
    174174    AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
    175175    AssertReturn(cbSrc > 16 && cbSrc < _1M, VERR_OUT_OF_RANGE);
    176     AssertMsgReturn(   fFlags == RTCRCERTCTX_F_ENC_X509_DER
    177                     || fFlags == RTCRCERTCTX_F_ENC_TAF_DER
     176    AssertReturn(!(fFlags & ~(RTCRCERTCTX_F_ADD_IF_NOT_FOUND | RTCRCERTCTX_F_ENC_MASK)), VERR_INVALID_FLAGS);
     177    AssertMsgReturn(   (fFlags & RTCRCERTCTX_F_ENC_MASK) == RTCRCERTCTX_F_ENC_X509_DER
     178                    || (fFlags & RTCRCERTCTX_F_ENC_MASK) == RTCRCERTCTX_F_ENC_TAF_DER
    178179                    , ("Only X.509 and TAF DER supported: %#x\n", fFlags), VERR_INVALID_FLAGS);
    179180
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