FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* encode CertName into output, return total bytes written */
encode CertName into output, return total bytes written
encode CertName into output return total bytes written
int SetName(byte* output, word32 outputSz, CertName* name)
{
    int          totalBytes = 0, i, idx;
#ifdef WOLFSSL_SMALL_STACK
    EncodedName* names = NULL;
#else
    EncodedName  names[NAME_ENTRIES];
#endif
#ifdef WOLFSSL_MULTI_ATTRIB
    EncodedName addNames[CTC_MAX_ATTRIB];
    int j, type;
#endif
    if (output == NULL || name == NULL)
        return BAD_FUNC_ARG;
    if (outputSz < 3)
        return BUFFER_E;
#ifdef WOLFSSL_SMALL_STACK
    names = (EncodedName*)XMALLOC(sizeof(EncodedName) * NAME_ENTRIES, NULL,
                                                       DYNAMIC_TYPE_TMP_BUFFER);
    if (names == NULL)
        return MEMORY_E;
#endif
    for (i = 0; i < NAME_ENTRIES; i++) {
        int ret;
        const char* nameStr = GetOneCertName(name, i);
        ret = wc_EncodeName(&names[i], nameStr, GetNameType(name, i),
                          GetCertNameId(i));
        if (ret < 0) {
        #ifdef WOLFSSL_SMALL_STACK
            XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        #endif
            WOLFSSL_MSG("EncodeName failed");
            return BUFFER_E;
        }
        totalBytes += ret;
    }
#ifdef WOLFSSL_MULTI_ATTRIB
    for (i = 0; i < CTC_MAX_ATTRIB; i++) {
        if (name->name[i].sz > 0) {
            int ret;
            ret = wc_EncodeName(&addNames[i], name->name[i].value,
                        name->name[i].type, name->name[i].id);
            if (ret < 0) {
            #ifdef WOLFSSL_SMALL_STACK
                XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
            #endif
                WOLFSSL_MSG("EncodeName on multiple attributes failed\n");
                return BUFFER_E;
            }
            totalBytes += ret;
        }
        else {
            addNames[i].used = 0;
        }
    }
#endif 
    idx = SetSequence(totalBytes, output);
    totalBytes += idx;
    if (totalBytes > ASN_NAME_MAX) {
#ifdef WOLFSSL_SMALL_STACK
        XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
        WOLFSSL_MSG("Total Bytes is greater than ASN_NAME_MAX");
        return BUFFER_E;
    }
    for (i = 0; i < NAME_ENTRIES; i++) {
    #ifdef WOLFSSL_MULTI_ATTRIB
        type = GetCertNameId(i);
        if (type == ASN_ORGUNIT_NAME) {
            type = ASN_DOMAIN_COMPONENT;
            for (j = 0; j < CTC_MAX_ATTRIB; j++) {
                if (name->name[j].sz > 0 && type == name->name[j].id) {
                    if (outputSz < (word32)(idx+addNames[j].totalLen)) {
                    #ifdef WOLFSSL_SMALL_STACK
                        XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
                    #endif
                        WOLFSSL_MSG("Not enough space left for DC value");
                        return BUFFER_E;
                    }
                    XMEMCPY(output + idx, addNames[j].encoded,
                            addNames[j].totalLen);
                    idx += addNames[j].totalLen;
                }
            }
            type = ASN_ORGUNIT_NAME;
        }
        for (j = 0; j < CTC_MAX_ATTRIB; j++) {
            if (name->name[j].sz > 0 && type == name->name[j].id) {
                if (outputSz < (word32)(idx+addNames[j].totalLen)) {
                #ifdef WOLFSSL_SMALL_STACK
                    XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
                #endif
                    return BUFFER_E;
                }
                XMEMCPY(output + idx, addNames[j].encoded,
                        addNames[j].totalLen);
                idx += addNames[j].totalLen;
            }
        }
    #endif 
        if (names[i].used) {
            if (outputSz < (word32)(idx+names[i].totalLen)) {
#ifdef WOLFSSL_SMALL_STACK
                XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
                return BUFFER_E;
            }
            XMEMCPY(output + idx, names[i].encoded, names[i].totalLen);
            idx += names[i].totalLen;
        }
    }
#ifdef WOLFSSL_SMALL_STACK
    XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
    return totalBytes;
}
int SetName ( byte * output , word32 outputSz , CertName * name ) { int totalBytes = 0 , i , idx ; #ifdef WOLFSSL_SMALL_STACK EncodedName * names = NULL ; #else EncodedName names [ NAME_ENTRIES ] ; #endif #ifdef WOLFSSL_MULTI_ATTRIB EncodedName addNames [ CTC_MAX_ATTRIB ] ; int j , type ; #endif if ( output == NULL || name == NULL ) return BAD_FUNC_ARG ; if ( outputSz < 3 ) return BUFFER_E ; #ifdef WOLFSSL_SMALL_STACK names = ( EncodedName * ) XMALLOC ( sizeof ( EncodedName ) * NAME_ENTRIES , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( names == NULL ) return MEMORY_E ; #endif for ( i = 0 ; i < NAME_ENTRIES ; i ++ ) { int ret ; const char * nameStr = GetOneCertName ( name , i ) ; ret = wc_EncodeName ( & names [ i ] , nameStr , GetNameType ( name , i ) , GetCertNameId ( i ) ) ; if ( ret < 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif WOLFSSL_MSG ( " " ) ; return BUFFER_E ; } totalBytes += ret ; } #ifdef WOLFSSL_MULTI_ATTRIB for ( i = 0 ; i < CTC_MAX_ATTRIB ; i ++ ) { if ( name -> name [ i ] . sz > 0 ) { int ret ; ret = wc_EncodeName ( & addNames [ i ] , name -> name [ i ] . value , name -> name [ i ] . type , name -> name [ i ] . id ) ; if ( ret < 0 ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif WOLFSSL_MSG ( " \n " ) ; return BUFFER_E ; } totalBytes += ret ; } else { addNames [ i ] . used = 0 ; } } #endif idx = SetSequence ( totalBytes , output ) ; totalBytes += idx ; if ( totalBytes > ASN_NAME_MAX ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif WOLFSSL_MSG ( " " ) ; return BUFFER_E ; } for ( i = 0 ; i < NAME_ENTRIES ; i ++ ) { #ifdef WOLFSSL_MULTI_ATTRIB type = GetCertNameId ( i ) ; if ( type == ASN_ORGUNIT_NAME ) { type = ASN_DOMAIN_COMPONENT ; for ( j = 0 ; j < CTC_MAX_ATTRIB ; j ++ ) { if ( name -> name [ j ] . sz > 0 && type == name -> name [ j ] . id ) { if ( outputSz < ( word32 ) ( idx + addNames [ j ] . totalLen ) ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif WOLFSSL_MSG ( " " ) ; return BUFFER_E ; } XMEMCPY ( output + idx , addNames [ j ] . encoded , addNames [ j ] . totalLen ) ; idx += addNames [ j ] . totalLen ; } } type = ASN_ORGUNIT_NAME ; } for ( j = 0 ; j < CTC_MAX_ATTRIB ; j ++ ) { if ( name -> name [ j ] . sz > 0 && type == name -> name [ j ] . id ) { if ( outputSz < ( word32 ) ( idx + addNames [ j ] . totalLen ) ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return BUFFER_E ; } XMEMCPY ( output + idx , addNames [ j ] . encoded , addNames [ j ] . totalLen ) ; idx += addNames [ j ] . totalLen ; } } #endif if ( names [ i ] . used ) { if ( outputSz < ( word32 ) ( idx + names [ i ] . totalLen ) ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return BUFFER_E ; } XMEMCPY ( output + idx , names [ i ] . encoded , names [ i ] . totalLen ) ; idx += names [ i ] . totalLen ; } } #ifdef WOLFSSL_SMALL_STACK XFREE ( names , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif return totalBytes ; }
encode CertName into output, return total bytes written
encode CertName into output return total bytes written
/* WOLFSSL_MULTI_ATTRIB */ /* header */ /* list all DC values before OUs */ /* write all similar types to the buffer */ /* WOLFSSL_MULTI_ATTRIB */