FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* convert der buffer to pem into output, can't do inplace, der and output
   need to be different */
convert der buffer to pem into output, can't do inplace, der and output
need to be different
convert der buffer to pem into output can ' t do inplace der and output need to be different
int wc_DerToPemEx(const byte* der, word32 derSz, byte* output, word32 outSz,
             byte *cipher_info, int type)
{
    const char* headerStr = NULL;
    const char* footerStr = NULL;
#ifdef WOLFSSL_SMALL_STACK
    char* header = NULL;
    char* footer = NULL;
#else
    char header[MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE];
    char footer[MAX_X509_HEADER_SZ];
#endif
    int headerLen = MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE;
    int footerLen = MAX_X509_HEADER_SZ;
    int i;
    int err;
    int outLen;   
    (void)cipher_info;
    if (der == output)      
        return BAD_FUNC_ARG;
    err = wc_PemGetHeaderFooter(type, &headerStr, &footerStr);
    if (err != 0)
        return err;
#ifdef WOLFSSL_SMALL_STACK
    header = (char*)XMALLOC(headerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
    if (header == NULL)
        return MEMORY_E;
    footer = (char*)XMALLOC(footerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
    if (footer == NULL) {
        XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        return MEMORY_E;
    }
#endif
    XSTRNCPY(header, headerStr, headerLen - 1);
    header[headerLen - 2] = 0;
    XSTRNCPY(footer, footerStr, footerLen - 1);
    footer[footerLen - 2] = 0;
    XSTRNCAT(header, "\n", 2);
    XSTRNCAT(footer, "\n", 2);
#ifdef WOLFSSL_ENCRYPTED_KEYS
    err = wc_EncryptedInfoAppend(header, headerLen, (char*)cipher_info);
    if (err != 0) {
    #ifdef WOLFSSL_SMALL_STACK
        XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
    #endif
        return err;
    }
#endif
    headerLen = (int)XSTRLEN(header);
    footerLen = (int)XSTRLEN(footer);
    if (!output && outSz == 0) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        outLen = 0;
        if ((err = Base64_Encode(der, derSz, NULL, (word32*)&outLen))
                != LENGTH_ONLY_E) {
            return err;
        }
        return headerLen + footerLen + outLen;
    }
    if (!der || !output) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        return BAD_FUNC_ARG;
    }
    if (outSz < headerLen + footerLen + derSz) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        return BAD_FUNC_ARG;
    }
    XMEMCPY(output, header, headerLen);
    i = headerLen;
#ifdef WOLFSSL_SMALL_STACK
    XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
    outLen = outSz - (headerLen + footerLen);  
    if ( (err = Base64_Encode(der, derSz, output + i, (word32*)&outLen)) < 0) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        return err;
    }
    i += outLen;
    if ( (i + footerLen) > (int)outSz) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        return BAD_FUNC_ARG;
    }
    XMEMCPY(output + i, footer, footerLen);
#ifdef WOLFSSL_SMALL_STACK
    XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
    return outLen + headerLen + footerLen;
}
int wc_DerToPemEx ( const byte * der , word32 derSz , byte * output , word32 outSz , byte * cipher_info , int type ) { const char * headerStr = NULL ; const char * footerStr = NULL ; #ifdef WOLFSSL_SMALL_STACK char * header = NULL ; char * footer = NULL ; #else char header [ MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE ] ; char footer [ MAX_X509_HEADER_SZ ] ; #endif int headerLen = MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE ; int footerLen = MAX_X509_HEADER_SZ ; int i ; int err ; int outLen ; ( void ) cipher_info ; if ( der == output ) return BAD_FUNC_ARG ; err = wc_PemGetHeaderFooter ( type , & headerStr , & footerStr ) ; if ( err != 0 ) return err ; #ifdef WOLFSSL_SMALL_STACK header = ( char * ) XMALLOC ( headerLen , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( header == NULL ) return MEMORY_E ; footer = ( char * ) XMALLOC ( footerLen , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( footer == NULL ) { XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; return MEMORY_E ; } #endif XSTRNCPY ( header , headerStr , headerLen - 1 ) ; header [ headerLen - 2 ] = 0 ; XSTRNCPY ( footer , footerStr , footerLen - 1 ) ; footer [ footerLen - 2 ] = 0 ; XSTRNCAT ( header , " \n " , 2 ) ; XSTRNCAT ( footer , " \n " , 2 ) ; #ifdef WOLFSSL_ENCRYPTED_KEYS err = wc_EncryptedInfoAppend ( header , headerLen , ( char * ) cipher_info ) ; if ( err != 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return err ; } #endif headerLen = ( int ) XSTRLEN ( header ) ; footerLen = ( int ) XSTRLEN ( footer ) ; if ( ! output && outSz == 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif outLen = 0 ; if ( ( err = Base64_Encode ( der , derSz , NULL , ( word32 * ) & outLen ) ) != LENGTH_ONLY_E ) { return err ; } return headerLen + footerLen + outLen ; } if ( ! der || ! output ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return BAD_FUNC_ARG ; } if ( outSz < headerLen + footerLen + derSz ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return BAD_FUNC_ARG ; } XMEMCPY ( output , header , headerLen ) ; i = headerLen ; #ifdef WOLFSSL_SMALL_STACK XFREE ( header , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif outLen = outSz - ( headerLen + footerLen ) ; if ( ( err = Base64_Encode ( der , derSz , output + i , ( word32 * ) & outLen ) ) < 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return err ; } i += outLen ; if ( ( i + footerLen ) > ( int ) outSz ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return BAD_FUNC_ARG ; } XMEMCPY ( output + i , footer , footerLen ) ; #ifdef WOLFSSL_SMALL_STACK XFREE ( footer , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return outLen + headerLen + footerLen ; }
convert der buffer to pem into output, can't do inplace, der and output
need to be different
convert der buffer to pem into output can ' t do inplace der and output need to be different
/* return length or error */ /* no in place conversion */ /* build header and footer based on type */ /* add new line to end */ /* if null output and 0 size passed in then return size needed */ /* don't even try if outSz too short */ /* header */ /* body */ /* input to Base64_Encode */ /* footer */