1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5*b077aed3SPierre ProncheryASN1_generate_nconf, ASN1_generate_v3 - ASN1 string generation functions 6e71b7053SJung-uk Kim 7e71b7053SJung-uk Kim=head1 SYNOPSIS 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim #include <openssl/asn1.h> 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf); 12e71b7053SJung-uk Kim ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf); 13e71b7053SJung-uk Kim 14e71b7053SJung-uk Kim=head1 DESCRIPTION 15e71b7053SJung-uk Kim 16e71b7053SJung-uk KimThese functions generate the ASN1 encoding of a string 17e71b7053SJung-uk Kimin an B<ASN1_TYPE> structure. 18e71b7053SJung-uk Kim 19*b077aed3SPierre ProncheryI<str> contains the string to encode. I<nconf> or I<cnf> contains 20e71b7053SJung-uk Kimthe optional configuration information where additional strings 21*b077aed3SPierre Proncherywill be read from. I<nconf> will typically come from a config 22*b077aed3SPierre Proncheryfile whereas I<cnf> is obtained from an B<X509V3_CTX> structure, 23e71b7053SJung-uk Kimwhich will typically be used by X509 v3 certificate extension 24*b077aed3SPierre Proncheryfunctions. I<cnf> or I<nconf> can be set to NULL if no additional 25e71b7053SJung-uk Kimconfiguration will be used. 26e71b7053SJung-uk Kim 27e71b7053SJung-uk Kim=head1 GENERATION STRING FORMAT 28e71b7053SJung-uk Kim 29*b077aed3SPierre ProncheryThe actual data encoded is determined by the string I<str> and 30e71b7053SJung-uk Kimthe configuration information. The general format of the string 31e71b7053SJung-uk Kimis: 32e71b7053SJung-uk Kim 33e71b7053SJung-uk Kim=over 4 34e71b7053SJung-uk Kim 35*b077aed3SPierre Pronchery=item [I<modifier>,]I<type>[:I<value>] 36e71b7053SJung-uk Kim 37e71b7053SJung-uk Kim=back 38e71b7053SJung-uk Kim 39e71b7053SJung-uk KimThat is zero or more comma separated modifiers followed by a type 40*b077aed3SPierre Proncheryfollowed by an optional colon and a value. The formats of I<type>, 41*b077aed3SPierre ProncheryI<value> and I<modifier> are explained below. 42e71b7053SJung-uk Kim 43e71b7053SJung-uk Kim=head2 Supported Types 44e71b7053SJung-uk Kim 45*b077aed3SPierre ProncheryThe supported types are listed below. 46*b077aed3SPierre ProncheryCase is not significant in the type names. 47*b077aed3SPierre ProncheryUnless otherwise specified only the B<ASCII> format is permissible. 48e71b7053SJung-uk Kim 49e71b7053SJung-uk Kim=over 4 50e71b7053SJung-uk Kim 51e71b7053SJung-uk Kim=item B<BOOLEAN>, B<BOOL> 52e71b7053SJung-uk Kim 53*b077aed3SPierre ProncheryThis encodes a boolean type. The I<value> string is mandatory and 54e71b7053SJung-uk Kimshould be B<TRUE> or B<FALSE>. Additionally B<TRUE>, B<true>, B<Y>, 55e71b7053SJung-uk KimB<y>, B<YES>, B<yes>, B<FALSE>, B<false>, B<N>, B<n>, B<NO> and B<no> 56e71b7053SJung-uk Kimare acceptable. 57e71b7053SJung-uk Kim 58e71b7053SJung-uk Kim=item B<NULL> 59e71b7053SJung-uk Kim 60*b077aed3SPierre ProncheryEncode the B<NULL> type, the I<value> string must not be present. 61e71b7053SJung-uk Kim 62e71b7053SJung-uk Kim=item B<INTEGER>, B<INT> 63e71b7053SJung-uk Kim 64*b077aed3SPierre ProncheryEncodes an ASN1 B<INTEGER> type. The I<value> string represents 65e71b7053SJung-uk Kimthe value of the integer, it can be prefaced by a minus sign and 66e71b7053SJung-uk Kimis normally interpreted as a decimal value unless the prefix B<0x> 67e71b7053SJung-uk Kimis included. 68e71b7053SJung-uk Kim 69e71b7053SJung-uk Kim=item B<ENUMERATED>, B<ENUM> 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimEncodes the ASN1 B<ENUMERATED> type, it is otherwise identical to 72e71b7053SJung-uk KimB<INTEGER>. 73e71b7053SJung-uk Kim 74e71b7053SJung-uk Kim=item B<OBJECT>, B<OID> 75e71b7053SJung-uk Kim 76*b077aed3SPierre ProncheryEncodes an ASN1 B<OBJECT IDENTIFIER>, the I<value> string can be 77e71b7053SJung-uk Kima short name, a long name or numerical format. 78e71b7053SJung-uk Kim 79e71b7053SJung-uk Kim=item B<UTCTIME>, B<UTC> 80e71b7053SJung-uk Kim 81e71b7053SJung-uk KimEncodes an ASN1 B<UTCTime> structure, the value should be in 82e71b7053SJung-uk Kimthe format B<YYMMDDHHMMSSZ>. 83e71b7053SJung-uk Kim 84e71b7053SJung-uk Kim=item B<GENERALIZEDTIME>, B<GENTIME> 85e71b7053SJung-uk Kim 86e71b7053SJung-uk KimEncodes an ASN1 B<GeneralizedTime> structure, the value should be in 87e71b7053SJung-uk Kimthe format B<YYYYMMDDHHMMSSZ>. 88e71b7053SJung-uk Kim 89e71b7053SJung-uk Kim=item B<OCTETSTRING>, B<OCT> 90e71b7053SJung-uk Kim 91*b077aed3SPierre ProncheryEncodes an ASN1 B<OCTET STRING>. I<value> represents the contents 92e71b7053SJung-uk Kimof this structure, the format strings B<ASCII> and B<HEX> can be 93*b077aed3SPierre Proncheryused to specify the format of I<value>. 94e71b7053SJung-uk Kim 95e71b7053SJung-uk Kim=item B<BITSTRING>, B<BITSTR> 96e71b7053SJung-uk Kim 97*b077aed3SPierre ProncheryEncodes an ASN1 B<BIT STRING>. I<value> represents the contents 98e71b7053SJung-uk Kimof this structure, the format strings B<ASCII>, B<HEX> and B<BITLIST> 99*b077aed3SPierre Proncherycan be used to specify the format of I<value>. 100e71b7053SJung-uk Kim 101e71b7053SJung-uk KimIf the format is anything other than B<BITLIST> the number of unused 102e71b7053SJung-uk Kimbits is set to zero. 103e71b7053SJung-uk Kim 104e71b7053SJung-uk Kim=item B<UNIVERSALSTRING>, B<UNIV>, B<IA5>, B<IA5STRING>, B<UTF8>, 105e71b7053SJung-uk KimB<UTF8String>, B<BMP>, B<BMPSTRING>, B<VISIBLESTRING>, 106e71b7053SJung-uk KimB<VISIBLE>, B<PRINTABLESTRING>, B<PRINTABLE>, B<T61>, 107e71b7053SJung-uk KimB<T61STRING>, B<TELETEXSTRING>, B<GeneralString>, B<NUMERICSTRING>, 108e71b7053SJung-uk KimB<NUMERIC> 109e71b7053SJung-uk Kim 110*b077aed3SPierre ProncheryThese encode the corresponding string types. I<value> represents the 111e71b7053SJung-uk Kimcontents of this structure. The format can be B<ASCII> or B<UTF8>. 112e71b7053SJung-uk Kim 113e71b7053SJung-uk Kim=item B<SEQUENCE>, B<SEQ>, B<SET> 114e71b7053SJung-uk Kim 115*b077aed3SPierre ProncheryFormats the result as an ASN1 B<SEQUENCE> or B<SET> type. I<value> 116e71b7053SJung-uk Kimshould be a section name which will contain the contents. The 117e71b7053SJung-uk Kimfield names in the section are ignored and the values are in the 118*b077aed3SPierre Proncherygenerated string format. If I<value> is absent then an empty SEQUENCE 119e71b7053SJung-uk Kimwill be encoded. 120e71b7053SJung-uk Kim 121e71b7053SJung-uk Kim=back 122e71b7053SJung-uk Kim 123e71b7053SJung-uk Kim=head2 Modifiers 124e71b7053SJung-uk Kim 125e71b7053SJung-uk KimModifiers affect the following structure, they can be used to 126e71b7053SJung-uk Kimadd EXPLICIT or IMPLICIT tagging, add wrappers or to change 127e71b7053SJung-uk Kimthe string format of the final type and value. The supported 128e71b7053SJung-uk Kimformats are documented below. 129e71b7053SJung-uk Kim 130e71b7053SJung-uk Kim=over 4 131e71b7053SJung-uk Kim 132e71b7053SJung-uk Kim=item B<EXPLICIT>, B<EXP> 133e71b7053SJung-uk Kim 134e71b7053SJung-uk KimAdd an explicit tag to the following structure. This string 135e71b7053SJung-uk Kimshould be followed by a colon and the tag value to use as a 136e71b7053SJung-uk Kimdecimal value. 137e71b7053SJung-uk Kim 138e71b7053SJung-uk KimBy following the number with B<U>, B<A>, B<P> or B<C> UNIVERSAL, 139e71b7053SJung-uk KimAPPLICATION, PRIVATE or CONTEXT SPECIFIC tagging can be used, 140e71b7053SJung-uk Kimthe default is CONTEXT SPECIFIC. 141e71b7053SJung-uk Kim 142e71b7053SJung-uk Kim=item B<IMPLICIT>, B<IMP> 143e71b7053SJung-uk Kim 144e71b7053SJung-uk KimThis is the same as B<EXPLICIT> except IMPLICIT tagging is used 145e71b7053SJung-uk Kiminstead. 146e71b7053SJung-uk Kim 147e71b7053SJung-uk Kim=item B<OCTWRAP>, B<SEQWRAP>, B<SETWRAP>, B<BITWRAP> 148e71b7053SJung-uk Kim 149e71b7053SJung-uk KimThe following structure is surrounded by an OCTET STRING, a SEQUENCE, 150e71b7053SJung-uk Kima SET or a BIT STRING respectively. For a BIT STRING the number of unused 151e71b7053SJung-uk Kimbits is set to zero. 152e71b7053SJung-uk Kim 153e71b7053SJung-uk Kim=item B<FORMAT> 154e71b7053SJung-uk Kim 155e71b7053SJung-uk KimThis specifies the format of the ultimate value. It should be followed 156e71b7053SJung-uk Kimby a colon and one of the strings B<ASCII>, B<UTF8>, B<HEX> or B<BITLIST>. 157e71b7053SJung-uk Kim 158e71b7053SJung-uk KimIf no format specifier is included then B<ASCII> is used. If B<UTF8> is 159e71b7053SJung-uk Kimspecified then the value string must be a valid B<UTF8> string. For B<HEX> the 160e71b7053SJung-uk Kimoutput must be a set of hex digits. B<BITLIST> (which is only valid for a BIT 161e71b7053SJung-uk KimSTRING) is a comma separated list of the indices of the set bits, all other 162e71b7053SJung-uk Kimbits are zero. 163e71b7053SJung-uk Kim 164e71b7053SJung-uk Kim=back 165e71b7053SJung-uk Kim 166610a21fdSJung-uk Kim=head1 RETURN VALUES 167610a21fdSJung-uk Kim 168610a21fdSJung-uk KimASN1_generate_nconf() and ASN1_generate_v3() return the encoded 169*b077aed3SPierre Proncherydata as an B<ASN1_TYPE> structure or NULL if an error occurred. 170610a21fdSJung-uk Kim 171610a21fdSJung-uk KimThe error codes that can be obtained by L<ERR_get_error(3)>. 172610a21fdSJung-uk Kim 173e71b7053SJung-uk Kim=head1 EXAMPLES 174e71b7053SJung-uk Kim 175e71b7053SJung-uk KimA simple IA5String: 176e71b7053SJung-uk Kim 177e71b7053SJung-uk Kim IA5STRING:Hello World 178e71b7053SJung-uk Kim 179e71b7053SJung-uk KimAn IA5String explicitly tagged: 180e71b7053SJung-uk Kim 181e71b7053SJung-uk Kim EXPLICIT:0,IA5STRING:Hello World 182e71b7053SJung-uk Kim 183e71b7053SJung-uk KimAn IA5String explicitly tagged using APPLICATION tagging: 184e71b7053SJung-uk Kim 185e71b7053SJung-uk Kim EXPLICIT:0A,IA5STRING:Hello World 186e71b7053SJung-uk Kim 187e71b7053SJung-uk KimA BITSTRING with bits 1 and 5 set and all others zero: 188e71b7053SJung-uk Kim 189e71b7053SJung-uk Kim FORMAT:BITLIST,BITSTRING:1,5 190e71b7053SJung-uk Kim 191e71b7053SJung-uk KimA more complex example using a config file to produce a 192e71b7053SJung-uk KimSEQUENCE consisting of a BOOL an OID and a UTF8String: 193e71b7053SJung-uk Kim 194e71b7053SJung-uk Kim asn1 = SEQUENCE:seq_section 195e71b7053SJung-uk Kim 196e71b7053SJung-uk Kim [seq_section] 197e71b7053SJung-uk Kim 198e71b7053SJung-uk Kim field1 = BOOLEAN:TRUE 199e71b7053SJung-uk Kim field2 = OID:commonName 200e71b7053SJung-uk Kim field3 = UTF8:Third field 201e71b7053SJung-uk Kim 202e71b7053SJung-uk KimThis example produces an RSAPrivateKey structure, this is the 203e71b7053SJung-uk Kimkey contained in the file client.pem in all OpenSSL distributions 204e71b7053SJung-uk Kim(note: the field names such as 'coeff' are ignored and are present just 205e71b7053SJung-uk Kimfor clarity): 206e71b7053SJung-uk Kim 207e71b7053SJung-uk Kim asn1=SEQUENCE:private_key 208e71b7053SJung-uk Kim [private_key] 209e71b7053SJung-uk Kim version=INTEGER:0 210e71b7053SJung-uk Kim 211e71b7053SJung-uk Kim n=INTEGER:0xBB6FE79432CC6EA2D8F970675A5A87BFBE1AFF0BE63E879F2AFFB93644\ 212e71b7053SJung-uk Kim D4D2C6D000430DEC66ABF47829E74B8C5108623A1C0EE8BE217B3AD8D36D5EB4FCA1D9 213e71b7053SJung-uk Kim 214e71b7053SJung-uk Kim e=INTEGER:0x010001 215e71b7053SJung-uk Kim 216e71b7053SJung-uk Kim d=INTEGER:0x6F05EAD2F27FFAEC84BEC360C4B928FD5F3A9865D0FCAAD291E2A52F4A\ 217e71b7053SJung-uk Kim F810DC6373278C006A0ABBA27DC8C63BF97F7E666E27C5284D7D3B1FFFE16B7A87B51D 218e71b7053SJung-uk Kim 219e71b7053SJung-uk Kim p=INTEGER:0xF3929B9435608F8A22C208D86795271D54EBDFB09DDEF539AB083DA912\ 220e71b7053SJung-uk Kim D4BD57 221e71b7053SJung-uk Kim 222e71b7053SJung-uk Kim q=INTEGER:0xC50016F89DFF2561347ED1186A46E150E28BF2D0F539A1594BBD7FE467\ 223e71b7053SJung-uk Kim 46EC4F 224e71b7053SJung-uk Kim 225e71b7053SJung-uk Kim exp1=INTEGER:0x9E7D4326C924AFC1DEA40B45650134966D6F9DFA3A7F9D698CD4ABEA\ 226e71b7053SJung-uk Kim 9C0A39B9 227e71b7053SJung-uk Kim 228e71b7053SJung-uk Kim exp2=INTEGER:0xBA84003BB95355AFB7C50DF140C60513D0BA51D637272E355E397779\ 229e71b7053SJung-uk Kim E7B2458F 230e71b7053SJung-uk Kim 231e71b7053SJung-uk Kim coeff=INTEGER:0x30B9E4F2AFA5AC679F920FC83F1F2DF1BAF1779CF989447FABC2F5\ 232e71b7053SJung-uk Kim 628657053A 233e71b7053SJung-uk Kim 234e71b7053SJung-uk KimThis example is the corresponding public key in a SubjectPublicKeyInfo 235e71b7053SJung-uk Kimstructure: 236e71b7053SJung-uk Kim 237e71b7053SJung-uk Kim # Start with a SEQUENCE 238e71b7053SJung-uk Kim asn1=SEQUENCE:pubkeyinfo 239e71b7053SJung-uk Kim 240e71b7053SJung-uk Kim # pubkeyinfo contains an algorithm identifier and the public key wrapped 241e71b7053SJung-uk Kim # in a BIT STRING 242e71b7053SJung-uk Kim [pubkeyinfo] 243e71b7053SJung-uk Kim algorithm=SEQUENCE:rsa_alg 244e71b7053SJung-uk Kim pubkey=BITWRAP,SEQUENCE:rsapubkey 245e71b7053SJung-uk Kim 246e71b7053SJung-uk Kim # algorithm ID for RSA is just an OID and a NULL 247e71b7053SJung-uk Kim [rsa_alg] 248e71b7053SJung-uk Kim algorithm=OID:rsaEncryption 249e71b7053SJung-uk Kim parameter=NULL 250e71b7053SJung-uk Kim 251e71b7053SJung-uk Kim # Actual public key: modulus and exponent 252e71b7053SJung-uk Kim [rsapubkey] 253e71b7053SJung-uk Kim n=INTEGER:0xBB6FE79432CC6EA2D8F970675A5A87BFBE1AFF0BE63E879F2AFFB93644\ 254e71b7053SJung-uk Kim D4D2C6D000430DEC66ABF47829E74B8C5108623A1C0EE8BE217B3AD8D36D5EB4FCA1D9 255e71b7053SJung-uk Kim 256e71b7053SJung-uk Kim e=INTEGER:0x010001 257e71b7053SJung-uk Kim 258e71b7053SJung-uk Kim=head1 SEE ALSO 259e71b7053SJung-uk Kim 260e71b7053SJung-uk KimL<ERR_get_error(3)> 261e71b7053SJung-uk Kim 262e71b7053SJung-uk Kim=head1 COPYRIGHT 263e71b7053SJung-uk Kim 264*b077aed3SPierre ProncheryCopyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved. 265e71b7053SJung-uk Kim 266*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 267e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 268e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 269e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 270e71b7053SJung-uk Kim 271e71b7053SJung-uk Kim=cut 272