VirtualBox

Ignore:
Timestamp:
May 11, 2020 11:46:40 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
137857
Message:

IPRT/crypto: Adding functions for checking whether a key or certificate can handle a given digest (size wise). Also, added OIDs, padding variants and stuff for sha512-224WithRSAEncryption and sha512-256WithRSAEncryption (RFC-8017). Note that OpenSSL does not implement these yet. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/crypto/pkcs7-sign.cpp

    r84235 r84248  
    8585
    8686RTDECL(int) RTCrPkcs7SimpleSignSignedData(uint32_t fFlags, PCRTCRX509CERTIFICATE pSigner, RTCRKEY hPrivateKey,
    87                                           void const *pvData, size_t cbData, RTCRSTORE hAdditionalCerts,
    88                                           void *pvResult, size_t *pcbResult, PRTERRINFO pErrInfo)
     87                                          void const *pvData, size_t cbData, RTDIGESTTYPE enmDigestType,
     88                                          RTCRSTORE hAdditionalCerts, void *pvResult, size_t *pcbResult, PRTERRINFO pErrInfo)
    8989{
    9090    size_t const cbResultBuf = *pcbResult;
    9191    *pcbResult = 0;
    9292    AssertReturn(!(fFlags & ~RTCRPKCS7SIGN_SD_F_VALID_MASK), VERR_INVALID_FLAGS);
    93 #if defined(IPRT_WITH_OPENSSL)
     93#ifdef IPRT_WITH_OPENSSL
    9494    AssertReturn((int)cbData >= 0 && (unsigned)cbData == cbData, VERR_TOO_MUCH_DATA);
     95
     96    /*
     97     * Resolve the digest type.
     98     */
     99    const EVP_MD *pEvpMd = NULL;
     100    if (enmDigestType != RTDIGESTTYPE_UNKNOWN)
     101    {
     102        pEvpMd = (const EVP_MD *)rtCrOpenSslConvertDigestType(enmDigestType, pErrInfo);
     103        AssertReturn(pEvpMd, pErrInfo ? pErrInfo->rc : VERR_INVALID_PARAMETER);
     104    }
    95105
    96106    /*
     
    125135                     * Do the signing.
    126136                     */
    127                     unsigned int fOsslSign = CMS_BINARY;
     137                    unsigned int fOsslSign = CMS_BINARY | CMS_PARTIAL;
    128138                    if (fFlags & RTCRPKCS7SIGN_SD_F_DEATCHED)
    129139                        fOsslSign |= CMS_DETACHED;
    130140                    if (fFlags & RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP)
    131141                        fOsslSign |= CMS_NOSMIMECAP;
    132                     CMS_ContentInfo *pCms = CMS_sign(pOsslSigner, pEvpPrivateKey, pOsslAdditionalCerts, pOsslData, fOsslSign);
    133                     if (pCms)
     142                    CMS_ContentInfo *pCms = CMS_sign(NULL, NULL, pOsslAdditionalCerts, NULL, fOsslSign);
     143                    if (pCms != NULL)
    134144                    {
    135                         /*
    136                          * Get the output and copy it into the result buffer.
    137                          */
    138                         BIO *pOsslResult = BIO_new(BIO_s_mem());
    139                         if (pOsslResult)
     145                        if (CMS_add1_signer(pCms, pOsslSigner, pEvpPrivateKey, pEvpMd, fOsslSign) != NULL)
    140146                        {
    141                             rc = i2d_CMS_bio(pOsslResult, pCms);
     147                            rc = CMS_final(pCms, pOsslData, NULL /*dcont*/, fOsslSign);
    142148                            if (rc > 0)
    143149                            {
    144                                 BUF_MEM *pBuf = NULL;
    145                                 rc = (int)BIO_get_mem_ptr(pOsslResult, &pBuf);
    146                                 if (rc > 0)
     150                                /*
     151                                 * Get the output and copy it into the result buffer.
     152                                 */
     153                                BIO *pOsslResult = BIO_new(BIO_s_mem());
     154                                if (pOsslResult)
    147155                                {
    148                                     AssertPtr(pBuf);
    149                                     size_t const cbResult = pBuf->length;
    150                                     if (   cbResultBuf >= cbResult
    151                                         && pvResult != NULL)
     156                                    rc = i2d_CMS_bio(pOsslResult, pCms);
     157                                    if (rc > 0)
    152158                                    {
    153                                         memcpy(pvResult, pBuf->data, cbResult);
    154                                         rc = VINF_SUCCESS;
     159                                        BUF_MEM *pBuf = NULL;
     160                                        rc = (int)BIO_get_mem_ptr(pOsslResult, &pBuf);
     161                                        if (rc > 0)
     162                                        {
     163                                            AssertPtr(pBuf);
     164                                            size_t const cbResult = pBuf->length;
     165                                            if (   cbResultBuf >= cbResult
     166                                                && pvResult != NULL)
     167                                            {
     168                                                memcpy(pvResult, pBuf->data, cbResult);
     169                                                rc = VINF_SUCCESS;
     170                                            }
     171                                            else
     172                                                rc = VERR_BUFFER_OVERFLOW;
     173                                            *pcbResult = cbResult;
     174                                        }
     175                                        else
     176                                            rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "BIO_get_mem_ptr");
    155177                                    }
    156178                                    else
    157                                         rc = VERR_BUFFER_OVERFLOW;
    158                                     *pcbResult = cbResult;
     179                                        rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "i2d_CMS_bio");
     180                                    BIO_free(pOsslResult);
    159181                                }
    160182                                else
    161                                     rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "BIO_get_mem_ptr");
     183                                    rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "BIO_new/BIO_s_mem");
    162184                            }
    163185                            else
    164                                 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "i2d_CMS_bio");
    165                             BIO_free(pOsslResult);
     186                                rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_final");
    166187                        }
    167188                        else
    168                             rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "BIO_new/BIO_s_mem");
     189                            rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_add1_signer");
    169190                        CMS_ContentInfo_free(pCms);
    170191                    }
     
    180201    return rc;
    181202#else
    182     RT_NOREF(fFlags, pSigner, hPrivateKey, pvData, cbData, hAdditionalCerts, pvResult, pErrInfo, cbResultBuf);
     203    RT_NOREF(fFlags, pSigner, hPrivateKey, pvData, cbData, enmDigestType, hAdditionalCerts, pvResult, pErrInfo, cbResultBuf);
    183204    *pcbResult = 0;
    184205    return VERR_NOT_IMPLEMENTED;
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