VirtualBox

Changeset 59663 in vbox


Ignore:
Timestamp:
Feb 14, 2016 8:11:06 PM (9 years ago)
Author:
vboxsync
Message:

IPRT: Added RTCrX509Certificate_VerifySignatureSelfSigned and RTAsn1EncodeToBuffer, corrected the name of RTAsn1EncodeWriteHeader (was RTAsnEncodeWriteHeader).

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/asn1.h

    r58106 r59663  
    14671467 *                              Optional.
    14681468 */
    1469 RTDECL(int) RTAsnEncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
    1470                                    PRTERRINFO pErrInfo);
    1471 
    1472 /**
    1473  * Prepares the ASN.1 structure for encoding.
    1474  *
    1475  * The preparations is mainly calculating accurate object size, but may also
    1476  * involve operations like recoding internal UTF-8 strings to the actual ASN.1
    1477  * format and other things that may require memory to allocated/reallocated.
     1469RTDECL(int) RTAsn1EncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
     1470                                    PRTERRINFO pErrInfo);
     1471
     1472/**
     1473 * Encodes and writes an ASN.1 object.
    14781474 *
    14791475 * @returns IPRT status code
     
    14881484RTDECL(int) RTAsn1EncodeWrite(PCRTASN1CORE pRoot, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
    14891485                              PRTERRINFO pErrInfo);
     1486
     1487/**
     1488 * Encodes and writes an ASN.1 object into a caller allocated memory buffer.
     1489 *
     1490 * @returns IPRT status code
     1491 * @param   pRoot               The root of the ASN.1 object tree to encode.
     1492 * @param   fFlags              Valid combination of the RTASN1ENCODE_F_XXX
     1493 *                              flags.  Must include the encoding type.
     1494 * @param   pvBuf               The output buffer.
     1495 * @param   cbBuf               The buffer size.  This should have the size
     1496 *                              returned by RTAsn1EncodePrepare().
     1497 * @param   pErrInfo            Where to store extended error information.
     1498 *                              Optional.
     1499 */
     1500RTDECL(int) RTAsn1EncodeToBuffer(PCRTASN1CORE pRoot, uint32_t fFlags, void *pvBuf, size_t cbBuf, PRTERRINFO pErrInfo);
    14901501
    14911502/** @} */
  • trunk/include/iprt/crypto/x509.h

    r59625 r59663  
    960960                                                PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPublicKey,
    961961                                                PRTERRINFO pErrInfo);
     962RTDECL(int) RTCrX509Certificate_VerifySignatureSelfSigned(PCRTCRX509CERTIFICATE pThis, PRTERRINFO pErrInfo);
    962963RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags,
    963964                                             PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo);
  • trunk/include/iprt/mangling.h

    r59625 r59663  
    23802380# define RTAsn1EncodePrepare                            RT_MANGLER(RTAsn1EncodePrepare)
    23812381# define RTAsn1EncodeRecalcHdrSize                      RT_MANGLER(RTAsn1EncodeRecalcHdrSize)
     2382# define RTAsn1EncodeToBuffer                           RT_MANGLER(RTAsn1EncodeToBuffer)
    23822383# define RTAsn1EncodeWrite                              RT_MANGLER(RTAsn1EncodeWrite)
    2383 # define RTAsnEncodeWriteHeader                         RT_MANGLER(RTAsnEncodeWriteHeader)
     2384# define RTAsn1EncodeWriteHeader                        RT_MANGLER(RTAsn1EncodeWriteHeader)
    23842385# define RTAsn1BitString_CheckSanity                    RT_MANGLER(RTAsn1BitString_CheckSanity)
    23852386# define RTAsn1BitString_Clone                          RT_MANGLER(RTAsn1BitString_Clone)
     
    31523153# define RTCrX509Validity_CheckSanity                   RT_MANGLER(RTCrX509Validity_CheckSanity)
    31533154# define RTCrX509Certificate_VerifySignature            RT_MANGLER(RTCrX509Certificate_VerifySignature)
     3155# define RTCrX509Certificate_VerifySignatureSelfSigned  RT_MANGLER(RTCrX509Certificate_VerifySignatureSelfSigned)
    31543156# define RTCrTafCertPathControls_DecodeAsn1             RT_MANGLER(RTCrTafCertPathControls_DecodeAsn1)
    31553157# define RTCrTafTrustAnchorChoice_DecodeAsn1            RT_MANGLER(RTCrTafTrustAnchorChoice_DecodeAsn1)
  • trunk/src/VBox/Runtime/common/asn1/asn1-encode.cpp

    r57358 r59663  
    3636#include <iprt/ctype.h>
    3737#include <iprt/err.h>
     38#include <iprt/string.h>
    3839
    3940#include <iprt/formats/asn1.h>
     
    7273} RTASN1ENCODEWRITEARGS;
    7374
     75/**
     76 * Argument package for rtAsn1EncodeToBufferCallback passed by
     77 * RTAsn1EncodeToBuffer.
     78 */
     79typedef struct RTASN1ENCODETOBUFARGS
     80{
     81    /** The destination buffer position (incremented while writing). */
     82    uint8_t                *pbDst;
     83    /** The size of the destination buffer left (decremented while writing). */
     84    size_t                  cbDst;
     85} RTASN1ENCODETOBUFARGS;
     86
    7487
    7588RTDECL(int) RTAsn1EncodeRecalcHdrSize(PRTASN1CORE pAsn1Core, uint32_t fFlags, PRTERRINFO pErrInfo)
     
    234247
    235248
    236 RTDECL(int) RTAsnEncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
    237                                    PRTERRINFO pErrInfo)
     249RTDECL(int) RTAsn1EncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
     250                                    PRTERRINFO pErrInfo)
    238251{
    239252    AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
     
    377390             * Generic path. Start by writing the header for this object.
    378391             */
    379             rc = RTAsnEncodeWriteHeader(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
     392            rc = RTAsn1EncodeWriteHeader(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
    380393            if (RT_SUCCESS(rc))
    381394            {
     
    426439}
    427440
     441
     442static DECLCALLBACK(int) rtAsn1EncodeToBufferCallback(const void *pvBuf, size_t cbToWrite, void *pvUser, PRTERRINFO pErrInfo)
     443{
     444    RTASN1ENCODETOBUFARGS *pArgs = (RTASN1ENCODETOBUFARGS *)pvUser;
     445    if (RT_LIKELY(pArgs->cbDst >= cbToWrite))
     446    {
     447        memcpy(pArgs->pbDst, pvBuf, cbToWrite);
     448        pArgs->cbDst -= cbToWrite;
     449        pArgs->pbDst += cbToWrite;
     450        return VINF_SUCCESS;
     451    }
     452
     453    /*
     454     * Overflow.
     455     */
     456    if (pArgs->cbDst)
     457    {
     458        memcpy(pArgs->pbDst, pvBuf, pArgs->cbDst);
     459        pArgs->pbDst -= pArgs->cbDst;
     460        pArgs->cbDst  = 0;
     461    }
     462    return VERR_BUFFER_OVERFLOW;
     463}
     464
     465
     466RTDECL(int) RTAsn1EncodeToBuffer(PCRTASN1CORE pRoot, uint32_t fFlags, void *pvBuf, size_t cbBuf, PRTERRINFO pErrInfo)
     467{
     468    RTASN1ENCODETOBUFARGS Args;
     469    Args.pbDst = (uint8_t *)pvBuf;
     470    Args.cbDst = cbBuf;
     471    return RTAsn1EncodeWrite(pRoot, fFlags, rtAsn1EncodeToBufferCallback, &Args, pErrInfo);
     472}
     473
  • trunk/src/VBox/Runtime/common/asn1/asn1-ut-bitstring.cpp

    r57358 r59663  
    272272     * First the header.
    273273     */
    274     int rc = RTAsnEncodeWriteHeader(&pThis->Asn1Core, fFlags, pfnWriter, pvUser, pErrInfo);
     274    int rc = RTAsn1EncodeWriteHeader(&pThis->Asn1Core, fFlags, pfnWriter, pvUser, pErrInfo);
    275275    if (RT_SUCCESS(rc) && rc != VINF_ASN1_NOT_ENCODED)
    276276    {
  • trunk/src/VBox/Runtime/common/asn1/asn1-ut-octetstring.cpp

    r57358 r59663  
    210210     * First the header.
    211211     */
    212     int rc = RTAsnEncodeWriteHeader(&pThis->Asn1Core, fFlags, pfnWriter, pvUser, pErrInfo);
     212    int rc = RTAsn1EncodeWriteHeader(&pThis->Asn1Core, fFlags, pfnWriter, pvUser, pErrInfo);
    213213    if (RT_SUCCESS(rc) && rc != VINF_ASN1_NOT_ENCODED)
    214214    {
  • trunk/src/VBox/Runtime/common/crypto/x509-verify.cpp

    r57358 r59663  
    3434
    3535#include <iprt/err.h>
     36#include <iprt/mem.h>
    3637#include <iprt/string.h>
    3738
     
    7677    /*
    7778     * Here we should recode the to-be-signed part as DER, but we'll ASSUME
    78      * that it's already in DER encoding.  This is safe.
     79     * that it's already in DER encoding and only does this if there the
     80     * encoded bits are missing.
    7981     */
    80     return RTCrPkixPubKeyVerifySignature(&pThis->SignatureAlgorithm.Algorithm, pParameters, pPublicKey, &pThis->SignatureValue,
    81                                          RTASN1CORE_GET_RAW_ASN1_PTR(&pThis->TbsCertificate.SeqCore.Asn1Core),
    82                                          RTASN1CORE_GET_RAW_ASN1_SIZE(&pThis->TbsCertificate.SeqCore.Asn1Core),
    83                                          pErrInfo);
     82    if (   pThis->TbsCertificate.SeqCore.Asn1Core.uData.pu8
     83        && pThis->TbsCertificate.SeqCore.Asn1Core.cb > 0)
     84        return RTCrPkixPubKeyVerifySignature(&pThis->SignatureAlgorithm.Algorithm, pParameters, pPublicKey, &pThis->SignatureValue,
     85                                             RTASN1CORE_GET_RAW_ASN1_PTR(&pThis->TbsCertificate.SeqCore.Asn1Core),
     86                                             RTASN1CORE_GET_RAW_ASN1_SIZE(&pThis->TbsCertificate.SeqCore.Asn1Core),
     87                                             pErrInfo);
     88
     89    uint32_t cbEncoded;
     90    int rc = RTAsn1EncodePrepare((PRTASN1CORE)&pThis->TbsCertificate.SeqCore.Asn1Core, RTASN1ENCODE_F_DER, &cbEncoded, pErrInfo);
     91    if (RT_SUCCESS(rc))
     92    {
     93        void *pvTbsBits = RTMemTmpAlloc(cbEncoded);
     94        if (pvTbsBits)
     95        {
     96            rc = RTAsn1EncodeToBuffer(&pThis->TbsCertificate.SeqCore.Asn1Core, RTASN1ENCODE_F_DER,
     97                                      pvTbsBits, cbEncoded, pErrInfo);
     98            if (RT_SUCCESS(rc))
     99                rc = RTCrPkixPubKeyVerifySignature(&pThis->SignatureAlgorithm.Algorithm, pParameters, pPublicKey,
     100                                                   &pThis->SignatureValue, pvTbsBits, cbEncoded, pErrInfo);
     101            else
     102                AssertRC(rc);
     103            RTMemTmpFree(pvTbsBits);
     104        }
     105        else
     106            rc = VERR_NO_TMP_MEMORY;
     107    }
     108    return rc;
    84109}
    85110
     111
     112RTDECL(int) RTCrX509Certificate_VerifySignatureSelfSigned(PCRTCRX509CERTIFICATE pThis, PRTERRINFO pErrInfo)
     113{
     114    /*
     115     * Validate the input a little.
     116     */
     117    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
     118    AssertReturn(RTCrX509Certificate_IsPresent(pThis), VERR_INVALID_PARAMETER);
     119
     120    /*
     121     * Assemble parameters for the generic verification call.
     122     */
     123    PCRTCRX509TBSCERTIFICATE const pTbsCert    = &pThis->TbsCertificate;
     124    PCRTASN1DYNTYPE                pParameters = NULL;
     125    if (   RTASN1CORE_IS_PRESENT(&pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.u.Core)
     126        && pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.enmType != RTASN1TYPE_NULL)
     127        pParameters = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
     128    return RTCrX509Certificate_VerifySignature(pThis, &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm, pParameters,
     129                                               &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey, pErrInfo);
     130}
     131
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