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