FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* PKCS#8 from RFC 5208
 * This function takes in a DER key and converts it to PKCS#8 format. Used
 * in creating PKCS#12 shrouded key bags.
 * Reverse of ToTraditional
 *
 * PrivateKeyInfo ::= SEQUENCE {
 *  version Version,
 *  privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
 *  privateKey          PrivateKey,
 *  attributes          optional
 *  }
 *  Version ::= INTEGER
 *  PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
 *  PrivateKey ::= OCTET STRING
 *
 * out      buffer to place result in
 * outSz    size of out buffer
 * key      buffer with DER key
 * keySz    size of key buffer
 * algoID   algorithm ID i.e. RSAk
 * curveOID ECC curve oid if used. Should be NULL for RSA keys.
 * oidSz    size of curve oid. Is set to 0 if curveOID is NULL.
 *
 * Returns the size of PKCS#8 placed into out. In error cases returns negative
 * values.
 */
PKCS#8 from RFC 5208
This function takes in a DER key and converts it to PKCS#8 format. Used
in creating PKCS#12 shrouded key bags.



out      buffer to place result in
outSz    size of out buffer
key      buffer with DER key
keySz    size of key buffer
algoID   algorithm ID i.e. RSAk
curveOID ECC curve oid if used. Should be NULL for RSA keys.
oidSz    size of curve oid. Is set to 0 if curveOID is NULL.

Returns the size of PKCS#8 placed into out. In error cases returns negative
values.
PKCS#8 from RFC 5208 This function takes in a DER key and converts it to PKCS#8 format . Used in creating PKCS#12 shrouded key bags . out buffer to place result in outSz size of out buffer key buffer with DER key keySz size of key buffer algoID algorithm ID i . e . RSAk curveOID ECC curve oid if used . Should be NULL for RSA keys . oidSz size of curve oid . Is set to 0 if curveOID is NULL . Returns the size of PKCS#8 placed into out . In error cases returns negative values .
int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
        int algoID, const byte* curveOID, word32 oidSz)
{
        word32 keyIdx = 0;
        word32 tmpSz  = 0;
        word32 sz;
        if (out == NULL && outSz != NULL) {
            *outSz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
                     + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2;
            if (curveOID != NULL)
                *outSz += oidSz + MAX_LENGTH_SZ + 1;
            WOLFSSL_MSG("Checking size of PKCS8");
            return LENGTH_ONLY_E;
        }
        WOLFSSL_ENTER("wc_CreatePKCS8Key()");
        if (key == NULL || out == NULL || outSz == NULL) {
            return BAD_FUNC_ARG;
        }
        if (curveOID != NULL) {
            if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
                   + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 3 + oidSz + MAX_LENGTH_SZ))
                return BUFFER_E;
        }
        else {
            oidSz = 0; 
            if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
                      + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2))
                return BUFFER_E;
        }
        keyIdx += MAX_SEQ_SZ; 
        sz = SetMyVersion(PKCS8v0, out + keyIdx, 0);
        tmpSz += sz; keyIdx += sz;
        sz = 0; 
        if (curveOID != NULL && oidSz > 0) {
            byte buf[MAX_LENGTH_SZ];
            sz = SetLength(oidSz, buf);
            sz += 1; 
        }
        sz = SetAlgoID(algoID, out + keyIdx, oidKeyType, oidSz + sz);
        tmpSz += sz; keyIdx += sz;
        if (curveOID != NULL && oidSz > 0) {
            sz = SetObjectId(oidSz, out + keyIdx);
            keyIdx += sz; tmpSz += sz;
            XMEMCPY(out + keyIdx, curveOID, oidSz);
            keyIdx += oidSz; tmpSz += oidSz;
        }
        sz = SetOctetString(keySz, out + keyIdx);
        keyIdx += sz; tmpSz += sz;
        XMEMCPY(out + keyIdx, key, keySz);
        tmpSz += keySz;
        sz = SetSequence(tmpSz, out);
        XMEMMOVE(out + sz, out + MAX_SEQ_SZ, tmpSz);
        return tmpSz + sz;
}
int wc_CreatePKCS8Key ( byte * out , word32 * outSz , byte * key , word32 keySz , int algoID , const byte * curveOID , word32 oidSz ) { word32 keyIdx = 0 ; word32 tmpSz = 0 ; word32 sz ; if ( out == NULL && outSz != NULL ) { * outSz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2 ; if ( curveOID != NULL ) * outSz += oidSz + MAX_LENGTH_SZ + 1 ; WOLFSSL_MSG ( " " ) ; return LENGTH_ONLY_E ; } WOLFSSL_ENTER ( " " ) ; if ( key == NULL || out == NULL || outSz == NULL ) { return BAD_FUNC_ARG ; } if ( curveOID != NULL ) { if ( * outSz < ( keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 3 + oidSz + MAX_LENGTH_SZ ) ) return BUFFER_E ; } else { oidSz = 0 ; if ( * outSz < ( keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2 ) ) return BUFFER_E ; } keyIdx += MAX_SEQ_SZ ; sz = SetMyVersion ( PKCS8v0 , out + keyIdx , 0 ) ; tmpSz += sz ; keyIdx += sz ; sz = 0 ; if ( curveOID != NULL && oidSz > 0 ) { byte buf [ MAX_LENGTH_SZ ] ; sz = SetLength ( oidSz , buf ) ; sz += 1 ; } sz = SetAlgoID ( algoID , out + keyIdx , oidKeyType , oidSz + sz ) ; tmpSz += sz ; keyIdx += sz ; if ( curveOID != NULL && oidSz > 0 ) { sz = SetObjectId ( oidSz , out + keyIdx ) ; keyIdx += sz ; tmpSz += sz ; XMEMCPY ( out + keyIdx , curveOID , oidSz ) ; keyIdx += oidSz ; tmpSz += oidSz ; } sz = SetOctetString ( keySz , out + keyIdx ) ; keyIdx += sz ; tmpSz += sz ; XMEMCPY ( out + keyIdx , key , keySz ) ; tmpSz += keySz ; sz = SetSequence ( tmpSz , out ) ; XMEMMOVE ( out + sz , out + MAX_SEQ_SZ , tmpSz ) ; return tmpSz + sz ; }
PKCS#8 from RFC 5208
 This function takes in a DER key and converts it to PKCS#8 format.
PKCS#8 from RFC 5208 This function takes in a DER key and converts it to PKCS#8 format .
/* If out is NULL then return the max size needed
         * + 2 for ASN_OBJECT_ID and ASN_OCTET_STRING tags */ /* check the buffer has enough room for largest possible size */ /* with no curveOID oid size must be 0 */ /* PrivateKeyInfo ::= SEQUENCE */ /* save room for sequence */ /*  version Version
         *  no header information just INTEGER */ /*  privateKeyAlgorithm PrivateKeyAlgorithmIdentifier */ /* set sz to 0 and get privateKey oid buffer size needed */ /* plus one for ASN object id */ /*  privateKey          PrivateKey *
         * pkcs8 ecc uses slightly different format. Places curve oid in
         * buffer */ /*  attributes          optional
         * No attributes currently added */ /* rewind and add sequence */