FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* Encodes one attribute of the name (issuer/subject)
 *
 * name     structure to hold result of encoding
 * nameStr  value to be encoded
 * nameType type of encoding i.e CTC_UTF8
 * type     id of attribute i.e ASN_COMMON_NAME
 *
 * returns length on success
 */
Encodes one attribute of the name (issuer/subject)
name     structure to hold result of encoding
nameStr  value to be encoded
nameType type of encoding i.e CTC_UTF8
type     id of attribute i.e ASN_COMMON_NAME

returns length on success
Encodes one attribute of the name ( issuer / subject ) name structure to hold result of encoding nameStr value to be encoded nameType type of encoding i . e CTC_UTF8 type id of attribute i . e ASN_COMMON_NAME returns length on success
static int wc_EncodeName(EncodedName* name, const char* nameStr, char nameType,
        byte type)
{
    word32 idx = 0;
    if (nameStr) {
        byte firstLen[1 + MAX_LENGTH_SZ];
        byte secondLen[MAX_LENGTH_SZ];
        byte sequence[MAX_SEQ_SZ];
        byte set[MAX_SET_SZ];
        int strLen  = (int)XSTRLEN(nameStr);
        int thisLen = strLen;
        int firstSz, secondSz, seqSz, setSz;
        if (strLen == 0) { 
            name->used = 0;
            return 0;
        }
        if (ASN_COUNTRY_NAME == type && strLen != CTC_COUNTRY_SIZE) {
            WOLFSSL_MSG("Country code size error");
            return ASN_COUNTRY_SIZE_E;
        }
        secondSz = SetLength(strLen, secondLen);
        thisLen += secondSz;
        switch (type) {
            case ASN_EMAIL_NAME: 
                thisLen += EMAIL_JOINT_LEN;
                firstSz  = EMAIL_JOINT_LEN;
                break;
            case ASN_DOMAIN_COMPONENT:
                thisLen += PILOT_JOINT_LEN;
                firstSz  = PILOT_JOINT_LEN;
                break;
            default:
                thisLen++;                                 
                thisLen += JOINT_LEN;
                firstSz  = JOINT_LEN + 1;
        }
        thisLen++; 
        firstSz  = SetObjectId(firstSz, firstLen);
        thisLen += firstSz;
        seqSz = SetSequence(thisLen, sequence);
        thisLen += seqSz;
        setSz = SetSet(thisLen, set);
        thisLen += setSz;
        if (thisLen > (int)sizeof(name->encoded)) {
            return BUFFER_E;
        }
        idx = 0;
        XMEMCPY(name->encoded, set, setSz);
        idx += setSz;
        XMEMCPY(name->encoded + idx, sequence, seqSz);
        idx += seqSz;
        XMEMCPY(name->encoded + idx, firstLen, firstSz);
        idx += firstSz;
        switch (type) {
            case ASN_EMAIL_NAME:
                {
                    const byte EMAIL_OID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
                                       0x01, 0x09, 0x01, 0x16 };
                    XMEMCPY(name->encoded + idx, EMAIL_OID, sizeof(EMAIL_OID));
                    idx += (int)sizeof(EMAIL_OID);
                }
                break;
            case ASN_DOMAIN_COMPONENT:
                {
                    const byte PILOT_OID[] = { 0x09, 0x92, 0x26, 0x89,
                                    0x93, 0xF2, 0x2C, 0x64, 0x01
                    };
                    XMEMCPY(name->encoded + idx, PILOT_OID,
                                                     sizeof(PILOT_OID));
                    idx += (int)sizeof(PILOT_OID);
                    name->encoded[idx++] = type;
                    name->encoded[idx++] = nameType;
                }
                break;
            default:
                name->encoded[idx++] = 0x55;
                name->encoded[idx++] = 0x04;
                name->encoded[idx++] = type;
                name->encoded[idx++] = nameType;
        }
        XMEMCPY(name->encoded + idx, secondLen, secondSz);
        idx += secondSz;
        XMEMCPY(name->encoded + idx, nameStr, strLen);
        idx += strLen;
        name->type = type;
        name->totalLen = idx;
        name->used = 1;
    }
    else
        name->used = 0;
    return idx;
}
static int wc_EncodeName ( EncodedName * name , const char * nameStr , char nameType , byte type ) { word32 idx = 0 ; if ( nameStr ) { byte firstLen [ 1 + MAX_LENGTH_SZ ] ; byte secondLen [ MAX_LENGTH_SZ ] ; byte sequence [ MAX_SEQ_SZ ] ; byte set [ MAX_SET_SZ ] ; int strLen = ( int ) XSTRLEN ( nameStr ) ; int thisLen = strLen ; int firstSz , secondSz , seqSz , setSz ; if ( strLen == 0 ) { name -> used = 0 ; return 0 ; } if ( ASN_COUNTRY_NAME == type && strLen != CTC_COUNTRY_SIZE ) { WOLFSSL_MSG ( " " ) ; return ASN_COUNTRY_SIZE_E ; } secondSz = SetLength ( strLen , secondLen ) ; thisLen += secondSz ; switch ( type ) { case ASN_EMAIL_NAME : thisLen += EMAIL_JOINT_LEN ; firstSz = EMAIL_JOINT_LEN ; break ; case ASN_DOMAIN_COMPONENT : thisLen += PILOT_JOINT_LEN ; firstSz = PILOT_JOINT_LEN ; break ; default : thisLen ++ ; thisLen += JOINT_LEN ; firstSz = JOINT_LEN + 1 ; } thisLen ++ ; firstSz = SetObjectId ( firstSz , firstLen ) ; thisLen += firstSz ; seqSz = SetSequence ( thisLen , sequence ) ; thisLen += seqSz ; setSz = SetSet ( thisLen , set ) ; thisLen += setSz ; if ( thisLen > ( int ) sizeof ( name -> encoded ) ) { return BUFFER_E ; } idx = 0 ; XMEMCPY ( name -> encoded , set , setSz ) ; idx += setSz ; XMEMCPY ( name -> encoded + idx , sequence , seqSz ) ; idx += seqSz ; XMEMCPY ( name -> encoded + idx , firstLen , firstSz ) ; idx += firstSz ; switch ( type ) { case ASN_EMAIL_NAME : { const byte EMAIL_OID [ ] = { 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x0d , 0x01 , 0x09 , 0x01 , 0x16 } ; XMEMCPY ( name -> encoded + idx , EMAIL_OID , sizeof ( EMAIL_OID ) ) ; idx += ( int ) sizeof ( EMAIL_OID ) ; } break ; case ASN_DOMAIN_COMPONENT : { const byte PILOT_OID [ ] = { 0x09 , 0x92 , 0x26 , 0x89 , 0x93 , 0xF2 , 0x2C , 0x64 , 0x01 } ; XMEMCPY ( name -> encoded + idx , PILOT_OID , sizeof ( PILOT_OID ) ) ; idx += ( int ) sizeof ( PILOT_OID ) ; name -> encoded [ idx ++ ] = type ; name -> encoded [ idx ++ ] = nameType ; } break ; default : name -> encoded [ idx ++ ] = 0x55 ; name -> encoded [ idx ++ ] = 0x04 ; name -> encoded [ idx ++ ] = type ; name -> encoded [ idx ++ ] = nameType ; } XMEMCPY ( name -> encoded + idx , secondLen , secondSz ) ; idx += secondSz ; XMEMCPY ( name -> encoded + idx , nameStr , strLen ) ; idx += strLen ; name -> type = type ; name -> totalLen = idx ; name -> used = 1 ; } else name -> used = 0 ; return idx ; }
Encodes one attribute of the name (issuer/subject)
 name     structure to hold result of encoding
 nameStr  value to be encoded
 nameType type of encoding i.e CTC_UTF8
 type     id of attribute i.e ASN_COMMON_NAME
Encodes one attribute of the name ( issuer / subject ) name structure to hold result of encoding nameStr value to be encoded nameType type of encoding i . e CTC_UTF8 type id of attribute i . e ASN_COMMON_NAME
/* bottom up */ /* no user data for this item */ /* Restrict country code size */ /* email */ /* str type */ /* id  type */ /* store it */ /* set */ /* seq */ /* asn object id */ /* email joint id */ /* id type */ /* str type */ /* id type */ /* str type */ /* second length */ /* str value */