FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* encode info from cert into DER encoded format */
encode info from cert into DER encoded format
encode info from cert into DER encoded format
static int EncodeCertReq(Cert* cert, DerCert* der, RsaKey* rsaKey,
                         ecc_key* eccKey, ed25519_key* ed25519Key,
                         ed448_key* ed448Key)
{
    (void)eccKey;
    (void)ed25519Key;
    (void)ed448Key;
    if (cert == NULL || der == NULL)
        return BAD_FUNC_ARG;
    if (rsaKey == NULL && eccKey == NULL && ed25519Key == NULL &&
                                                             ed448Key == NULL) {
            return PUBLIC_KEY_E;
    }
    XMEMSET(der, 0, sizeof(DerCert));
    der->versionSz = SetMyVersion(cert->version, der->version, FALSE);
    der->subjectSz = SetName(der->subject, sizeof(der->subject), &cert->subject);
    if (der->subjectSz <= 0)
        return SUBJECT_E;
#ifndef NO_RSA
    if (cert->keyType == RSA_KEY) {
        if (rsaKey == NULL)
            return PUBLIC_KEY_E;
        der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey,
                                           sizeof(der->publicKey), 1);
    }
#endif
#ifdef HAVE_ECC
    if (cert->keyType == ECC_KEY) {
        if (eccKey == NULL)
            return PUBLIC_KEY_E;
        der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey, 1);
    }
#endif
#ifdef HAVE_ED25519
    if (cert->keyType == ED25519_KEY) {
        if (ed25519Key == NULL)
            return PUBLIC_KEY_E;
        der->publicKeySz = SetEd25519PublicKey(der->publicKey, ed25519Key, 1);
    }
#endif
#ifdef HAVE_ED448
    if (cert->keyType == ED448_KEY) {
        if (ed448Key == NULL)
            return PUBLIC_KEY_E;
        der->publicKeySz = SetEd448PublicKey(der->publicKey, ed448Key, 1);
    }
#endif
    if (der->publicKeySz <= 0)
        return PUBLIC_KEY_E;
    der->extensionsSz = 0;
    if (cert->isCA) {
        der->caSz = SetCa(der->ca, sizeof(der->ca));
        if (der->caSz <= 0)
            return CA_TRUE_E;
        der->extensionsSz += der->caSz;
    }
    else
        der->caSz = 0;
#ifdef WOLFSSL_CERT_EXT
    if (cert->skidSz) {
        if (cert->skidSz > (int)min(CTC_MAX_SKID_SIZE, sizeof(der->skid)))
            return SKID_E;
        der->skidSz = SetSKID(der->skid, sizeof(der->skid),
                              cert->skid, cert->skidSz);
        if (der->skidSz <= 0)
            return SKID_E;
        der->extensionsSz += der->skidSz;
    }
    else
        der->skidSz = 0;
    if (cert->keyUsage != 0){
        der->keyUsageSz = SetKeyUsage(der->keyUsage, sizeof(der->keyUsage),
                                      cert->keyUsage);
        if (der->keyUsageSz <= 0)
            return KEYUSAGE_E;
        der->extensionsSz += der->keyUsageSz;
    }
    else
        der->keyUsageSz = 0;
    if (cert->extKeyUsage != 0){
        der->extKeyUsageSz = SetExtKeyUsage(cert, der->extKeyUsage,
                                sizeof(der->extKeyUsage), cert->extKeyUsage);
        if (der->extKeyUsageSz <= 0)
            return EXTKEYUSAGE_E;
        der->extensionsSz += der->extKeyUsageSz;
    }
    else
        der->extKeyUsageSz = 0;
#endif 
    if (der->extensionsSz > 0) {
        int ret;
        der->extensionsSz = SetSequence(der->extensionsSz, der->extensions);
        if (der->extensionsSz <= 0)
            return EXTENSIONS_E;
        if (der->caSz) {
            ret = SetExtensions(der->extensions, sizeof(der->extensions),
                                &der->extensionsSz,
                                der->ca, der->caSz);
            if (ret <= 0)
                return EXTENSIONS_E;
        }
#ifdef WOLFSSL_CERT_EXT
        if (der->skidSz) {
            ret = SetExtensions(der->extensions, sizeof(der->extensions),
                                &der->extensionsSz,
                                der->skid, der->skidSz);
            if (ret <= 0)
                return EXTENSIONS_E;
        }
        if (der->akidSz) {
            ret = SetExtensions(der->extensions, sizeof(der->extensions),
                                &der->extensionsSz,
                                der->akid, der->akidSz);
            if (ret <= 0)
                return EXTENSIONS_E;
        }
        if (der->keyUsageSz) {
            ret = SetExtensions(der->extensions, sizeof(der->extensions),
                                &der->extensionsSz,
                                der->keyUsage, der->keyUsageSz);
            if (ret <= 0)
                return EXTENSIONS_E;
        }
        if (der->extKeyUsageSz) {
            ret = SetExtensions(der->extensions, sizeof(der->extensions),
                                &der->extensionsSz,
                                der->extKeyUsage, der->extKeyUsageSz);
            if (ret <= 0)
                return EXTENSIONS_E;
        }
#endif 
    }
    der->attribSz = SetReqAttrib(der->attrib, cert->challengePw,
                                 cert->challengePwPrintableString,
                                 der->extensionsSz);
    if (der->attribSz <= 0)
        return REQ_ATTRIBUTE_E;
    der->total = der->versionSz + der->subjectSz + der->publicKeySz +
        der->extensionsSz + der->attribSz;
    return 0;
}
static int EncodeCertReq ( Cert * cert , DerCert * der , RsaKey * rsaKey , ecc_key * eccKey , ed25519_key * ed25519Key , ed448_key * ed448Key ) { ( void ) eccKey ; ( void ) ed25519Key ; ( void ) ed448Key ; if ( cert == NULL || der == NULL ) return BAD_FUNC_ARG ; if ( rsaKey == NULL && eccKey == NULL && ed25519Key == NULL && ed448Key == NULL ) { return PUBLIC_KEY_E ; } XMEMSET ( der , 0 , sizeof ( DerCert ) ) ; der -> versionSz = SetMyVersion ( cert -> version , der -> version , FALSE ) ; der -> subjectSz = SetName ( der -> subject , sizeof ( der -> subject ) , & cert -> subject ) ; if ( der -> subjectSz <= 0 ) return SUBJECT_E ; #ifndef NO_RSA if ( cert -> keyType == RSA_KEY ) { if ( rsaKey == NULL ) return PUBLIC_KEY_E ; der -> publicKeySz = SetRsaPublicKey ( der -> publicKey , rsaKey , sizeof ( der -> publicKey ) , 1 ) ; } #endif #ifdef HAVE_ECC if ( cert -> keyType == ECC_KEY ) { if ( eccKey == NULL ) return PUBLIC_KEY_E ; der -> publicKeySz = SetEccPublicKey ( der -> publicKey , eccKey , 1 ) ; } #endif #ifdef HAVE_ED25519 if ( cert -> keyType == ED25519_KEY ) { if ( ed25519Key == NULL ) return PUBLIC_KEY_E ; der -> publicKeySz = SetEd25519PublicKey ( der -> publicKey , ed25519Key , 1 ) ; } #endif #ifdef HAVE_ED448 if ( cert -> keyType == ED448_KEY ) { if ( ed448Key == NULL ) return PUBLIC_KEY_E ; der -> publicKeySz = SetEd448PublicKey ( der -> publicKey , ed448Key , 1 ) ; } #endif if ( der -> publicKeySz <= 0 ) return PUBLIC_KEY_E ; der -> extensionsSz = 0 ; if ( cert -> isCA ) { der -> caSz = SetCa ( der -> ca , sizeof ( der -> ca ) ) ; if ( der -> caSz <= 0 ) return CA_TRUE_E ; der -> extensionsSz += der -> caSz ; } else der -> caSz = 0 ; #ifdef WOLFSSL_CERT_EXT if ( cert -> skidSz ) { if ( cert -> skidSz > ( int ) min ( CTC_MAX_SKID_SIZE , sizeof ( der -> skid ) ) ) return SKID_E ; der -> skidSz = SetSKID ( der -> skid , sizeof ( der -> skid ) , cert -> skid , cert -> skidSz ) ; if ( der -> skidSz <= 0 ) return SKID_E ; der -> extensionsSz += der -> skidSz ; } else der -> skidSz = 0 ; if ( cert -> keyUsage != 0 ) { der -> keyUsageSz = SetKeyUsage ( der -> keyUsage , sizeof ( der -> keyUsage ) , cert -> keyUsage ) ; if ( der -> keyUsageSz <= 0 ) return KEYUSAGE_E ; der -> extensionsSz += der -> keyUsageSz ; } else der -> keyUsageSz = 0 ; if ( cert -> extKeyUsage != 0 ) { der -> extKeyUsageSz = SetExtKeyUsage ( cert , der -> extKeyUsage , sizeof ( der -> extKeyUsage ) , cert -> extKeyUsage ) ; if ( der -> extKeyUsageSz <= 0 ) return EXTKEYUSAGE_E ; der -> extensionsSz += der -> extKeyUsageSz ; } else der -> extKeyUsageSz = 0 ; #endif if ( der -> extensionsSz > 0 ) { int ret ; der -> extensionsSz = SetSequence ( der -> extensionsSz , der -> extensions ) ; if ( der -> extensionsSz <= 0 ) return EXTENSIONS_E ; if ( der -> caSz ) { ret = SetExtensions ( der -> extensions , sizeof ( der -> extensions ) , & der -> extensionsSz , der -> ca , der -> caSz ) ; if ( ret <= 0 ) return EXTENSIONS_E ; } #ifdef WOLFSSL_CERT_EXT if ( der -> skidSz ) { ret = SetExtensions ( der -> extensions , sizeof ( der -> extensions ) , & der -> extensionsSz , der -> skid , der -> skidSz ) ; if ( ret <= 0 ) return EXTENSIONS_E ; } if ( der -> akidSz ) { ret = SetExtensions ( der -> extensions , sizeof ( der -> extensions ) , & der -> extensionsSz , der -> akid , der -> akidSz ) ; if ( ret <= 0 ) return EXTENSIONS_E ; } if ( der -> keyUsageSz ) { ret = SetExtensions ( der -> extensions , sizeof ( der -> extensions ) , & der -> extensionsSz , der -> keyUsage , der -> keyUsageSz ) ; if ( ret <= 0 ) return EXTENSIONS_E ; } if ( der -> extKeyUsageSz ) { ret = SetExtensions ( der -> extensions , sizeof ( der -> extensions ) , & der -> extensionsSz , der -> extKeyUsage , der -> extKeyUsageSz ) ; if ( ret <= 0 ) return EXTENSIONS_E ; } #endif } der -> attribSz = SetReqAttrib ( der -> attrib , cert -> challengePw , cert -> challengePwPrintableString , der -> extensionsSz ) ; if ( der -> attribSz <= 0 ) return REQ_ATTRIBUTE_E ; der -> total = der -> versionSz + der -> subjectSz + der -> publicKeySz + der -> extensionsSz + der -> attribSz ; return 0 ; }
encode info from cert into DER encoded format
encode info from cert into DER encoded format
/* init */ /* version */ /* subject name */ /* public key */ /* set the extensions */ /* CA */ /* SKID */ /* check the provided SKID size */ /* Key Usage */ /* Extended Key Usage */ /* WOLFSSL_CERT_EXT */ /* put extensions */ /* put the start of sequence (ID, Size) */ /* put CA */ /* put SKID */ /* put AKID */ /* put KeyUsage */ /* put ExtendedKeyUsage */ /* WOLFSSL_CERT_EXT */