1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948ASN1_generate_nconf, ASN1_generate_v3 - ASN1 generation functions 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf); 10*2175Sjp161948 ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf); 11*2175Sjp161948 12*2175Sjp161948=head1 DESCRIPTION 13*2175Sjp161948 14*2175Sjp161948These functions generate the ASN1 encoding of a string 15*2175Sjp161948in an B<ASN1_TYPE> structure. 16*2175Sjp161948 17*2175Sjp161948B<str> contains the string to encode B<nconf> or B<cnf> contains 18*2175Sjp161948the optional configuration information where additional strings 19*2175Sjp161948will be read from. B<nconf> will typically come from a config 20*2175Sjp161948file wherease B<cnf> is obtained from an B<X509V3_CTX> structure 21*2175Sjp161948which will typically be used by X509 v3 certificate extension 22*2175Sjp161948functions. B<cnf> or B<nconf> can be set to B<NULL> if no additional 23*2175Sjp161948configuration will be used. 24*2175Sjp161948 25*2175Sjp161948=head1 GENERATION STRING FORMAT 26*2175Sjp161948 27*2175Sjp161948The actual data encoded is determined by the string B<str> and 28*2175Sjp161948the configuration information. The general format of the string 29*2175Sjp161948is: 30*2175Sjp161948 31*2175Sjp161948 B<[modifier,]type[:value]> 32*2175Sjp161948 33*2175Sjp161948That is zero or more comma separated modifiers followed by a type 34*2175Sjp161948followed by an optional colon and a value. The formats of B<type>, 35*2175Sjp161948B<value> and B<modifier> are explained below. 36*2175Sjp161948 37*2175Sjp161948=head2 SUPPORTED TYPES 38*2175Sjp161948 39*2175Sjp161948The supported types are listed below. Unless otherwise specified 40*2175Sjp161948only the B<ASCII> format is permissible. 41*2175Sjp161948 42*2175Sjp161948=over 2 43*2175Sjp161948 44*2175Sjp161948=item B<BOOLEAN>, B<BOOL> 45*2175Sjp161948 46*2175Sjp161948This encodes a boolean type. The B<value> string is mandatory and 47*2175Sjp161948should be B<TRUE> or B<FALSE>. Additionally B<TRUE>, B<true>, B<Y>, 48*2175Sjp161948B<y>, B<YES>, B<yes>, B<FALSE>, B<false>, B<N>, B<n>, B<NO> and B<no> 49*2175Sjp161948are acceptable. 50*2175Sjp161948 51*2175Sjp161948=item B<NULL> 52*2175Sjp161948 53*2175Sjp161948Encode the B<NULL> type, the B<value> string must not be present. 54*2175Sjp161948 55*2175Sjp161948=item B<INTEGER>, B<INT> 56*2175Sjp161948 57*2175Sjp161948Encodes an ASN1 B<INTEGER> type. The B<value> string represents 58*2175Sjp161948the value of the integer, it can be preceeded by a minus sign and 59*2175Sjp161948is normally interpreted as a decimal value unless the prefix B<0x> 60*2175Sjp161948is included. 61*2175Sjp161948 62*2175Sjp161948=item B<ENUMERATED>, B<ENUM> 63*2175Sjp161948 64*2175Sjp161948Encodes the ASN1 B<ENUMERATED> type, it is otherwise identical to 65*2175Sjp161948B<INTEGER>. 66*2175Sjp161948 67*2175Sjp161948=item B<OBJECT>, B<OID> 68*2175Sjp161948 69*2175Sjp161948Encodes an ASN1 B<OBJECT IDENTIFIER>, the B<value> string can be 70*2175Sjp161948a short name, a long name or numerical format. 71*2175Sjp161948 72*2175Sjp161948=item B<UTCTIME>, B<UTC> 73*2175Sjp161948 74*2175Sjp161948Encodes an ASN1 B<UTCTime> structure, the value should be in 75*2175Sjp161948the format B<YYMMDDHHMMSSZ>. 76*2175Sjp161948 77*2175Sjp161948=item B<GENERALIZEDTIME>, B<GENTIME> 78*2175Sjp161948 79*2175Sjp161948Encodes an ASN1 B<GeneralizedTime> structure, the value should be in 80*2175Sjp161948the format B<YYYYMMDDHHMMSSZ>. 81*2175Sjp161948 82*2175Sjp161948=item B<OCTETSTRING>, B<OCT> 83*2175Sjp161948 84*2175Sjp161948Emcodes an ASN1 B<OCTET STRING>. B<value> represents the contents 85*2175Sjp161948of this structure, the format strings B<ASCII> and B<HEX> can be 86*2175Sjp161948used to specify the format of B<value>. 87*2175Sjp161948 88*2175Sjp161948=item B<BITSRING>, B<BITSTR> 89*2175Sjp161948 90*2175Sjp161948Emcodes an ASN1 B<BIT STRING>. B<value> represents the contents 91*2175Sjp161948of this structure, the format strings B<ASCII>, B<HEX> and B<BITLIST> 92*2175Sjp161948can be used to specify the format of B<value>. 93*2175Sjp161948 94*2175Sjp161948If the format is anything other than B<BITLIST> the number of unused 95*2175Sjp161948bits is set to zero. 96*2175Sjp161948 97*2175Sjp161948=item B<UNIVERSALSTRING>, B<UNIV>, B<IA5>, B<IA5STRING>, B<UTF8>, 98*2175Sjp161948B<UTF8String>, B<BMP>, B<BMPSTRING>, B<VISIBLESTRING>, 99*2175Sjp161948B<VISIBLE>, B<PRINTABLESTRING>, B<PRINTABLE>, B<T61>, 100*2175Sjp161948B<T61STRING>, B<TELETEXSTRING>, B<GeneralString> 101*2175Sjp161948 102*2175Sjp161948These encode the corresponding string types. B<value> represents the 103*2175Sjp161948contents of this structure. The format can be B<ASCII> or B<UTF8>. 104*2175Sjp161948 105*2175Sjp161948=item B<SEQUENCE>, B<SEQ>, B<SET> 106*2175Sjp161948 107*2175Sjp161948Formats the result as an ASN1 B<SEQUENCE> or B<SET> type. B<value> 108*2175Sjp161948should be a section name which will contain the contents. The 109*2175Sjp161948field names in the section are ignored and the values are in the 110*2175Sjp161948generated string format. If B<value> is absent then an empty SEQUENCE 111*2175Sjp161948will be encoded. 112*2175Sjp161948 113*2175Sjp161948=back 114*2175Sjp161948 115*2175Sjp161948=head2 MODIFIERS 116*2175Sjp161948 117*2175Sjp161948Modifiers affect the following structure, they can be used to 118*2175Sjp161948add EXPLICIT or IMPLICIT tagging, add wrappers or to change 119*2175Sjp161948the string format of the final type and value. The supported 120*2175Sjp161948formats are documented below. 121*2175Sjp161948 122*2175Sjp161948=over 2 123*2175Sjp161948 124*2175Sjp161948=item B<EXPLICIT>, B<EXP> 125*2175Sjp161948 126*2175Sjp161948Add an explicit tag to the following structure. This string 127*2175Sjp161948should be followed by a colon and the tag value to use as a 128*2175Sjp161948decimal value. 129*2175Sjp161948 130*2175Sjp161948By following the number with B<U>, B<A>, B<P> or B<C> UNIVERSAL, 131*2175Sjp161948APPLICATION, PRIVATE or CONTEXT SPECIFIC tagging can be used, 132*2175Sjp161948the default is CONTEXT SPECIFIC. 133*2175Sjp161948 134*2175Sjp161948=item B<IMPLICIT>, B<IMP> 135*2175Sjp161948 136*2175Sjp161948This is the same as B<EXPLICIT> except IMPLICIT tagging is used 137*2175Sjp161948instead. 138*2175Sjp161948 139*2175Sjp161948=item B<OCTWRAP>, B<SEQWRAP>, B<SETWRAP>, B<BITWRAP> 140*2175Sjp161948 141*2175Sjp161948The following structure is surrounded by an OCTET STRING, a SEQUENCE, 142*2175Sjp161948a SET or a BIT STRING respectively. For a BIT STRING the number of unused 143*2175Sjp161948bits is set to zero. 144*2175Sjp161948 145*2175Sjp161948=item B<FORMAT> 146*2175Sjp161948 147*2175Sjp161948This specifies the format of the ultimate value. It should be followed 148*2175Sjp161948by a colon and one of the strings B<ASCII>, B<UTF8>, B<HEX> or B<BITLIST>. 149*2175Sjp161948 150*2175Sjp161948If no format specifier is included then B<ASCII> is used. If B<UTF8> is specified 151*2175Sjp161948then the value string must be a valid B<UTF8> string. For B<HEX> the output must 152*2175Sjp161948be a set of hex digits. B<BITLIST> (which is only valid for a BIT STRING) is a 153*2175Sjp161948comma separated list of set bits. 154*2175Sjp161948 155*2175Sjp161948=back 156*2175Sjp161948 157*2175Sjp161948=head1 EXAMPLES 158*2175Sjp161948 159*2175Sjp161948A simple IA5String: 160*2175Sjp161948 161*2175Sjp161948 IA5STRING:Hello World 162*2175Sjp161948 163*2175Sjp161948An IA5String explicitly tagged: 164*2175Sjp161948 165*2175Sjp161948 EXPLICIT:0,IA5STRING:Hello World 166*2175Sjp161948 167*2175Sjp161948An IA5String explicitly tagged using APPLICATION tagging: 168*2175Sjp161948 169*2175Sjp161948 EXPLICIT:0A,IA5STRING:Hello World 170*2175Sjp161948 171*2175Sjp161948A more complex example using a config file to produce a 172*2175Sjp161948SEQUENCE consiting of a BOOL an OID and a UTF8String: 173*2175Sjp161948 174*2175Sjp161948asn1 = SEQUENCE:seq_section 175*2175Sjp161948 176*2175Sjp161948[seq_section] 177*2175Sjp161948 178*2175Sjp161948field1 = BOOLEAN:TRUE 179*2175Sjp161948field2 = OID:commonName 180*2175Sjp161948field3 = UTF8:Third field 181*2175Sjp161948 182*2175Sjp161948This example produces an RSAPrivateKey structure, this is the 183*2175Sjp161948key contained in the file client.pem in all OpenSSL distributions 184*2175Sjp161948(note: the field names such as 'coeff' are ignored and are present just 185*2175Sjp161948for clarity): 186*2175Sjp161948 187*2175Sjp161948 asn1=SEQUENCE:private_key 188*2175Sjp161948 [private_key] 189*2175Sjp161948 version=INTEGER:0 190*2175Sjp161948 191*2175Sjp161948 n=INTEGER:0xBB6FE79432CC6EA2D8F970675A5A87BFBE1AFF0BE63E879F2AFFB93644\ 192*2175Sjp161948 D4D2C6D000430DEC66ABF47829E74B8C5108623A1C0EE8BE217B3AD8D36D5EB4FCA1D9 193*2175Sjp161948 194*2175Sjp161948 e=INTEGER:0x010001 195*2175Sjp161948 196*2175Sjp161948 d=INTEGER:0x6F05EAD2F27FFAEC84BEC360C4B928FD5F3A9865D0FCAAD291E2A52F4A\ 197*2175Sjp161948 F810DC6373278C006A0ABBA27DC8C63BF97F7E666E27C5284D7D3B1FFFE16B7A87B51D 198*2175Sjp161948 199*2175Sjp161948 p=INTEGER:0xF3929B9435608F8A22C208D86795271D54EBDFB09DDEF539AB083DA912\ 200*2175Sjp161948 D4BD57 201*2175Sjp161948 202*2175Sjp161948 q=INTEGER:0xC50016F89DFF2561347ED1186A46E150E28BF2D0F539A1594BBD7FE467\ 203*2175Sjp161948 46EC4F 204*2175Sjp161948 205*2175Sjp161948 exp1=INTEGER:0x9E7D4326C924AFC1DEA40B45650134966D6F9DFA3A7F9D698CD4ABEA\ 206*2175Sjp161948 9C0A39B9 207*2175Sjp161948 208*2175Sjp161948 exp2=INTEGER:0xBA84003BB95355AFB7C50DF140C60513D0BA51D637272E355E397779\ 209*2175Sjp161948 E7B2458F 210*2175Sjp161948 211*2175Sjp161948 coeff=INTEGER:0x30B9E4F2AFA5AC679F920FC83F1F2DF1BAF1779CF989447FABC2F5\ 212*2175Sjp161948 628657053A 213*2175Sjp161948 214*2175Sjp161948This example is the corresponding public key in a SubjectPublicKeyInfo 215*2175Sjp161948structure: 216*2175Sjp161948 217*2175Sjp161948 # Start with a SEQUENCE 218*2175Sjp161948 asn1=SEQUENCE:pubkeyinfo 219*2175Sjp161948 220*2175Sjp161948 # pubkeyinfo contains an algorithm identifier and the public key wrapped 221*2175Sjp161948 # in a BIT STRING 222*2175Sjp161948 [pubkeyinfo] 223*2175Sjp161948 algorithm=SEQUENCE:rsa_alg 224*2175Sjp161948 pubkey=BITWRAP,SEQUENCE:rsapubkey 225*2175Sjp161948 226*2175Sjp161948 # algorithm ID for RSA is just an OID and a NULL 227*2175Sjp161948 [rsa_alg] 228*2175Sjp161948 algorithm=OID:rsaEncryption 229*2175Sjp161948 parameter=NULL 230*2175Sjp161948 231*2175Sjp161948 # Actual public key: modulus and exponent 232*2175Sjp161948 [rsapubkey] 233*2175Sjp161948 n=INTEGER:0xBB6FE79432CC6EA2D8F970675A5A87BFBE1AFF0BE63E879F2AFFB93644\ 234*2175Sjp161948 D4D2C6D000430DEC66ABF47829E74B8C5108623A1C0EE8BE217B3AD8D36D5EB4FCA1D9 235*2175Sjp161948 236*2175Sjp161948 e=INTEGER:0x010001 237*2175Sjp161948 238*2175Sjp161948=head1 RETURN VALUES 239*2175Sjp161948 240*2175Sjp161948ASN1_generate_nconf() and ASN1_generate_v3() return the encoded 241*2175Sjp161948data as an B<ASN1_TYPE> structure or B<NULL> if an error occurred. 242*2175Sjp161948 243*2175Sjp161948The error codes that can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>. 244*2175Sjp161948 245*2175Sjp161948=head1 SEE ALSO 246*2175Sjp161948 247*2175Sjp161948L<ERR_get_error(3)|ERR_get_error(3)> 248*2175Sjp161948 249*2175Sjp161948=head1 HISTORY 250*2175Sjp161948 251*2175Sjp161948ASN1_generate_nconf() and ASN1_generate_v3() were added to OpenSSL 0.9.8 252*2175Sjp161948 253*2175Sjp161948=cut 254