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