yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | =pod |
| 2 | |
| 3 | =head1 NAME |
| 4 | |
| 5 | d2i_ACCESS_DESCRIPTION, |
| 6 | d2i_ADMISSIONS, |
| 7 | d2i_ADMISSION_SYNTAX, |
| 8 | d2i_ASIdOrRange, |
| 9 | d2i_ASIdentifierChoice, |
| 10 | d2i_ASIdentifiers, |
| 11 | d2i_ASN1_BIT_STRING, |
| 12 | d2i_ASN1_BMPSTRING, |
| 13 | d2i_ASN1_ENUMERATED, |
| 14 | d2i_ASN1_GENERALIZEDTIME, |
| 15 | d2i_ASN1_GENERALSTRING, |
| 16 | d2i_ASN1_IA5STRING, |
| 17 | d2i_ASN1_INTEGER, |
| 18 | d2i_ASN1_NULL, |
| 19 | d2i_ASN1_OBJECT, |
| 20 | d2i_ASN1_OCTET_STRING, |
| 21 | d2i_ASN1_PRINTABLE, |
| 22 | d2i_ASN1_PRINTABLESTRING, |
| 23 | d2i_ASN1_SEQUENCE_ANY, |
| 24 | d2i_ASN1_SET_ANY, |
| 25 | d2i_ASN1_T61STRING, |
| 26 | d2i_ASN1_TIME, |
| 27 | d2i_ASN1_TYPE, |
| 28 | d2i_ASN1_UINTEGER, |
| 29 | d2i_ASN1_UNIVERSALSTRING, |
| 30 | d2i_ASN1_UTCTIME, |
| 31 | d2i_ASN1_UTF8STRING, |
| 32 | d2i_ASN1_VISIBLESTRING, |
| 33 | d2i_ASRange, |
| 34 | d2i_AUTHORITY_INFO_ACCESS, |
| 35 | d2i_AUTHORITY_KEYID, |
| 36 | d2i_BASIC_CONSTRAINTS, |
| 37 | d2i_CERTIFICATEPOLICIES, |
| 38 | d2i_CMS_ContentInfo, |
| 39 | d2i_CMS_ReceiptRequest, |
| 40 | d2i_CMS_bio, |
| 41 | d2i_CRL_DIST_POINTS, |
| 42 | d2i_DHxparams, |
| 43 | d2i_DIRECTORYSTRING, |
| 44 | d2i_DISPLAYTEXT, |
| 45 | d2i_DIST_POINT, |
| 46 | d2i_DIST_POINT_NAME, |
| 47 | d2i_DSAPrivateKey, |
| 48 | d2i_DSAPrivateKey_bio, |
| 49 | d2i_DSAPrivateKey_fp, |
| 50 | d2i_DSAPublicKey, |
| 51 | d2i_DSA_PUBKEY, |
| 52 | d2i_DSA_PUBKEY_bio, |
| 53 | d2i_DSA_PUBKEY_fp, |
| 54 | d2i_DSA_SIG, |
| 55 | d2i_DSAparams, |
| 56 | d2i_ECDSA_SIG, |
| 57 | d2i_ECPKParameters, |
| 58 | d2i_ECParameters, |
| 59 | d2i_ECPrivateKey, |
| 60 | d2i_ECPrivateKey_bio, |
| 61 | d2i_ECPrivateKey_fp, |
| 62 | d2i_EC_PUBKEY, |
| 63 | d2i_EC_PUBKEY_bio, |
| 64 | d2i_EC_PUBKEY_fp, |
| 65 | d2i_EDIPARTYNAME, |
| 66 | d2i_ESS_CERT_ID, |
| 67 | d2i_ESS_ISSUER_SERIAL, |
| 68 | d2i_ESS_SIGNING_CERT, |
| 69 | d2i_EXTENDED_KEY_USAGE, |
| 70 | d2i_GENERAL_NAME, |
| 71 | d2i_GENERAL_NAMES, |
| 72 | d2i_IPAddressChoice, |
| 73 | d2i_IPAddressFamily, |
| 74 | d2i_IPAddressOrRange, |
| 75 | d2i_IPAddressRange, |
| 76 | d2i_ISSUING_DIST_POINT, |
| 77 | d2i_NAMING_AUTHORITY, |
| 78 | d2i_NETSCAPE_CERT_SEQUENCE, |
| 79 | d2i_NETSCAPE_SPKAC, |
| 80 | d2i_NETSCAPE_SPKI, |
| 81 | d2i_NOTICEREF, |
| 82 | d2i_OCSP_BASICRESP, |
| 83 | d2i_OCSP_CERTID, |
| 84 | d2i_OCSP_CERTSTATUS, |
| 85 | d2i_OCSP_CRLID, |
| 86 | d2i_OCSP_ONEREQ, |
| 87 | d2i_OCSP_REQINFO, |
| 88 | d2i_OCSP_REQUEST, |
| 89 | d2i_OCSP_RESPBYTES, |
| 90 | d2i_OCSP_RESPDATA, |
| 91 | d2i_OCSP_RESPID, |
| 92 | d2i_OCSP_RESPONSE, |
| 93 | d2i_OCSP_REVOKEDINFO, |
| 94 | d2i_OCSP_SERVICELOC, |
| 95 | d2i_OCSP_SIGNATURE, |
| 96 | d2i_OCSP_SINGLERESP, |
| 97 | d2i_OTHERNAME, |
| 98 | d2i_PBE2PARAM, |
| 99 | d2i_PBEPARAM, |
| 100 | d2i_PBKDF2PARAM, |
| 101 | d2i_PKCS12, |
| 102 | d2i_PKCS12_BAGS, |
| 103 | d2i_PKCS12_MAC_DATA, |
| 104 | d2i_PKCS12_SAFEBAG, |
| 105 | d2i_PKCS12_bio, |
| 106 | d2i_PKCS12_fp, |
| 107 | d2i_PKCS7, |
| 108 | d2i_PKCS7_DIGEST, |
| 109 | d2i_PKCS7_ENCRYPT, |
| 110 | d2i_PKCS7_ENC_CONTENT, |
| 111 | d2i_PKCS7_ENVELOPE, |
| 112 | d2i_PKCS7_ISSUER_AND_SERIAL, |
| 113 | d2i_PKCS7_RECIP_INFO, |
| 114 | d2i_PKCS7_SIGNED, |
| 115 | d2i_PKCS7_SIGNER_INFO, |
| 116 | d2i_PKCS7_SIGN_ENVELOPE, |
| 117 | d2i_PKCS7_bio, |
| 118 | d2i_PKCS7_fp, |
| 119 | d2i_PKCS8_PRIV_KEY_INFO, |
| 120 | d2i_PKCS8_PRIV_KEY_INFO_bio, |
| 121 | d2i_PKCS8_PRIV_KEY_INFO_fp, |
| 122 | d2i_PKCS8_bio, |
| 123 | d2i_PKCS8_fp, |
| 124 | d2i_PKEY_USAGE_PERIOD, |
| 125 | d2i_POLICYINFO, |
| 126 | d2i_POLICYQUALINFO, |
| 127 | d2i_PROFESSION_INFO, |
| 128 | d2i_PROXY_CERT_INFO_EXTENSION, |
| 129 | d2i_PROXY_POLICY, |
| 130 | d2i_RSAPrivateKey, |
| 131 | d2i_RSAPrivateKey_bio, |
| 132 | d2i_RSAPrivateKey_fp, |
| 133 | d2i_RSAPublicKey, |
| 134 | d2i_RSAPublicKey_bio, |
| 135 | d2i_RSAPublicKey_fp, |
| 136 | d2i_RSA_OAEP_PARAMS, |
| 137 | d2i_RSA_PSS_PARAMS, |
| 138 | d2i_RSA_PUBKEY, |
| 139 | d2i_RSA_PUBKEY_bio, |
| 140 | d2i_RSA_PUBKEY_fp, |
| 141 | d2i_SCRYPT_PARAMS, |
| 142 | d2i_SCT_LIST, |
| 143 | d2i_SXNET, |
| 144 | d2i_SXNETID, |
| 145 | d2i_TS_ACCURACY, |
| 146 | d2i_TS_MSG_IMPRINT, |
| 147 | d2i_TS_MSG_IMPRINT_bio, |
| 148 | d2i_TS_MSG_IMPRINT_fp, |
| 149 | d2i_TS_REQ, |
| 150 | d2i_TS_REQ_bio, |
| 151 | d2i_TS_REQ_fp, |
| 152 | d2i_TS_RESP, |
| 153 | d2i_TS_RESP_bio, |
| 154 | d2i_TS_RESP_fp, |
| 155 | d2i_TS_STATUS_INFO, |
| 156 | d2i_TS_TST_INFO, |
| 157 | d2i_TS_TST_INFO_bio, |
| 158 | d2i_TS_TST_INFO_fp, |
| 159 | d2i_USERNOTICE, |
| 160 | d2i_X509, |
| 161 | d2i_X509_bio, |
| 162 | d2i_X509_fp, |
| 163 | d2i_X509_ALGOR, |
| 164 | d2i_X509_ALGORS, |
| 165 | d2i_X509_ATTRIBUTE, |
| 166 | d2i_X509_CERT_AUX, |
| 167 | d2i_X509_CINF, |
| 168 | d2i_X509_CRL, |
| 169 | d2i_X509_CRL_INFO, |
| 170 | d2i_X509_CRL_bio, |
| 171 | d2i_X509_CRL_fp, |
| 172 | d2i_X509_EXTENSION, |
| 173 | d2i_X509_EXTENSIONS, |
| 174 | d2i_X509_NAME, |
| 175 | d2i_X509_NAME_ENTRY, |
| 176 | d2i_X509_PUBKEY, |
| 177 | d2i_X509_REQ, |
| 178 | d2i_X509_REQ_INFO, |
| 179 | d2i_X509_REQ_bio, |
| 180 | d2i_X509_REQ_fp, |
| 181 | d2i_X509_REVOKED, |
| 182 | d2i_X509_SIG, |
| 183 | d2i_X509_VAL, |
| 184 | i2d_ACCESS_DESCRIPTION, |
| 185 | i2d_ADMISSIONS, |
| 186 | i2d_ADMISSION_SYNTAX, |
| 187 | i2d_ASIdOrRange, |
| 188 | i2d_ASIdentifierChoice, |
| 189 | i2d_ASIdentifiers, |
| 190 | i2d_ASN1_BIT_STRING, |
| 191 | i2d_ASN1_BMPSTRING, |
| 192 | i2d_ASN1_ENUMERATED, |
| 193 | i2d_ASN1_GENERALIZEDTIME, |
| 194 | i2d_ASN1_GENERALSTRING, |
| 195 | i2d_ASN1_IA5STRING, |
| 196 | i2d_ASN1_INTEGER, |
| 197 | i2d_ASN1_NULL, |
| 198 | i2d_ASN1_OBJECT, |
| 199 | i2d_ASN1_OCTET_STRING, |
| 200 | i2d_ASN1_PRINTABLE, |
| 201 | i2d_ASN1_PRINTABLESTRING, |
| 202 | i2d_ASN1_SEQUENCE_ANY, |
| 203 | i2d_ASN1_SET_ANY, |
| 204 | i2d_ASN1_T61STRING, |
| 205 | i2d_ASN1_TIME, |
| 206 | i2d_ASN1_TYPE, |
| 207 | i2d_ASN1_UNIVERSALSTRING, |
| 208 | i2d_ASN1_UTCTIME, |
| 209 | i2d_ASN1_UTF8STRING, |
| 210 | i2d_ASN1_VISIBLESTRING, |
| 211 | i2d_ASN1_bio_stream, |
| 212 | i2d_ASRange, |
| 213 | i2d_AUTHORITY_INFO_ACCESS, |
| 214 | i2d_AUTHORITY_KEYID, |
| 215 | i2d_BASIC_CONSTRAINTS, |
| 216 | i2d_CERTIFICATEPOLICIES, |
| 217 | i2d_CMS_ContentInfo, |
| 218 | i2d_CMS_ReceiptRequest, |
| 219 | i2d_CMS_bio, |
| 220 | i2d_CRL_DIST_POINTS, |
| 221 | i2d_DHxparams, |
| 222 | i2d_DIRECTORYSTRING, |
| 223 | i2d_DISPLAYTEXT, |
| 224 | i2d_DIST_POINT, |
| 225 | i2d_DIST_POINT_NAME, |
| 226 | i2d_DSAPrivateKey, |
| 227 | i2d_DSAPrivateKey_bio, |
| 228 | i2d_DSAPrivateKey_fp, |
| 229 | i2d_DSAPublicKey, |
| 230 | i2d_DSA_PUBKEY, |
| 231 | i2d_DSA_PUBKEY_bio, |
| 232 | i2d_DSA_PUBKEY_fp, |
| 233 | i2d_DSA_SIG, |
| 234 | i2d_DSAparams, |
| 235 | i2d_ECDSA_SIG, |
| 236 | i2d_ECPKParameters, |
| 237 | i2d_ECParameters, |
| 238 | i2d_ECPrivateKey, |
| 239 | i2d_ECPrivateKey_bio, |
| 240 | i2d_ECPrivateKey_fp, |
| 241 | i2d_EC_PUBKEY, |
| 242 | i2d_EC_PUBKEY_bio, |
| 243 | i2d_EC_PUBKEY_fp, |
| 244 | i2d_EDIPARTYNAME, |
| 245 | i2d_ESS_CERT_ID, |
| 246 | i2d_ESS_ISSUER_SERIAL, |
| 247 | i2d_ESS_SIGNING_CERT, |
| 248 | i2d_EXTENDED_KEY_USAGE, |
| 249 | i2d_GENERAL_NAME, |
| 250 | i2d_GENERAL_NAMES, |
| 251 | i2d_IPAddressChoice, |
| 252 | i2d_IPAddressFamily, |
| 253 | i2d_IPAddressOrRange, |
| 254 | i2d_IPAddressRange, |
| 255 | i2d_ISSUING_DIST_POINT, |
| 256 | i2d_NAMING_AUTHORITY, |
| 257 | i2d_NETSCAPE_CERT_SEQUENCE, |
| 258 | i2d_NETSCAPE_SPKAC, |
| 259 | i2d_NETSCAPE_SPKI, |
| 260 | i2d_NOTICEREF, |
| 261 | i2d_OCSP_BASICRESP, |
| 262 | i2d_OCSP_CERTID, |
| 263 | i2d_OCSP_CERTSTATUS, |
| 264 | i2d_OCSP_CRLID, |
| 265 | i2d_OCSP_ONEREQ, |
| 266 | i2d_OCSP_REQINFO, |
| 267 | i2d_OCSP_REQUEST, |
| 268 | i2d_OCSP_RESPBYTES, |
| 269 | i2d_OCSP_RESPDATA, |
| 270 | i2d_OCSP_RESPID, |
| 271 | i2d_OCSP_RESPONSE, |
| 272 | i2d_OCSP_REVOKEDINFO, |
| 273 | i2d_OCSP_SERVICELOC, |
| 274 | i2d_OCSP_SIGNATURE, |
| 275 | i2d_OCSP_SINGLERESP, |
| 276 | i2d_OTHERNAME, |
| 277 | i2d_PBE2PARAM, |
| 278 | i2d_PBEPARAM, |
| 279 | i2d_PBKDF2PARAM, |
| 280 | i2d_PKCS12, |
| 281 | i2d_PKCS12_BAGS, |
| 282 | i2d_PKCS12_MAC_DATA, |
| 283 | i2d_PKCS12_SAFEBAG, |
| 284 | i2d_PKCS12_bio, |
| 285 | i2d_PKCS12_fp, |
| 286 | i2d_PKCS7, |
| 287 | i2d_PKCS7_DIGEST, |
| 288 | i2d_PKCS7_ENCRYPT, |
| 289 | i2d_PKCS7_ENC_CONTENT, |
| 290 | i2d_PKCS7_ENVELOPE, |
| 291 | i2d_PKCS7_ISSUER_AND_SERIAL, |
| 292 | i2d_PKCS7_NDEF, |
| 293 | i2d_PKCS7_RECIP_INFO, |
| 294 | i2d_PKCS7_SIGNED, |
| 295 | i2d_PKCS7_SIGNER_INFO, |
| 296 | i2d_PKCS7_SIGN_ENVELOPE, |
| 297 | i2d_PKCS7_bio, |
| 298 | i2d_PKCS7_fp, |
| 299 | i2d_PKCS8PrivateKeyInfo_bio, |
| 300 | i2d_PKCS8PrivateKeyInfo_fp, |
| 301 | i2d_PKCS8_PRIV_KEY_INFO, |
| 302 | i2d_PKCS8_PRIV_KEY_INFO_bio, |
| 303 | i2d_PKCS8_PRIV_KEY_INFO_fp, |
| 304 | i2d_PKCS8_bio, |
| 305 | i2d_PKCS8_fp, |
| 306 | i2d_PKEY_USAGE_PERIOD, |
| 307 | i2d_POLICYINFO, |
| 308 | i2d_POLICYQUALINFO, |
| 309 | i2d_PROFESSION_INFO, |
| 310 | i2d_PROXY_CERT_INFO_EXTENSION, |
| 311 | i2d_PROXY_POLICY, |
| 312 | i2d_RSAPrivateKey, |
| 313 | i2d_RSAPrivateKey_bio, |
| 314 | i2d_RSAPrivateKey_fp, |
| 315 | i2d_RSAPublicKey, |
| 316 | i2d_RSAPublicKey_bio, |
| 317 | i2d_RSAPublicKey_fp, |
| 318 | i2d_RSA_OAEP_PARAMS, |
| 319 | i2d_RSA_PSS_PARAMS, |
| 320 | i2d_RSA_PUBKEY, |
| 321 | i2d_RSA_PUBKEY_bio, |
| 322 | i2d_RSA_PUBKEY_fp, |
| 323 | i2d_SCRYPT_PARAMS, |
| 324 | i2d_SCT_LIST, |
| 325 | i2d_SXNET, |
| 326 | i2d_SXNETID, |
| 327 | i2d_TS_ACCURACY, |
| 328 | i2d_TS_MSG_IMPRINT, |
| 329 | i2d_TS_MSG_IMPRINT_bio, |
| 330 | i2d_TS_MSG_IMPRINT_fp, |
| 331 | i2d_TS_REQ, |
| 332 | i2d_TS_REQ_bio, |
| 333 | i2d_TS_REQ_fp, |
| 334 | i2d_TS_RESP, |
| 335 | i2d_TS_RESP_bio, |
| 336 | i2d_TS_RESP_fp, |
| 337 | i2d_TS_STATUS_INFO, |
| 338 | i2d_TS_TST_INFO, |
| 339 | i2d_TS_TST_INFO_bio, |
| 340 | i2d_TS_TST_INFO_fp, |
| 341 | i2d_USERNOTICE, |
| 342 | i2d_X509, |
| 343 | i2d_X509_bio, |
| 344 | i2d_X509_fp, |
| 345 | i2d_X509_ALGOR, |
| 346 | i2d_X509_ALGORS, |
| 347 | i2d_X509_ATTRIBUTE, |
| 348 | i2d_X509_CERT_AUX, |
| 349 | i2d_X509_CINF, |
| 350 | i2d_X509_CRL, |
| 351 | i2d_X509_CRL_INFO, |
| 352 | i2d_X509_CRL_bio, |
| 353 | i2d_X509_CRL_fp, |
| 354 | i2d_X509_EXTENSION, |
| 355 | i2d_X509_EXTENSIONS, |
| 356 | i2d_X509_NAME, |
| 357 | i2d_X509_NAME_ENTRY, |
| 358 | i2d_X509_PUBKEY, |
| 359 | i2d_X509_REQ, |
| 360 | i2d_X509_REQ_INFO, |
| 361 | i2d_X509_REQ_bio, |
| 362 | i2d_X509_REQ_fp, |
| 363 | i2d_X509_REVOKED, |
| 364 | i2d_X509_SIG, |
| 365 | i2d_X509_VAL, |
| 366 | - convert objects from/to ASN.1/DER representation |
| 367 | |
| 368 | =head1 SYNOPSIS |
| 369 | |
| 370 | =for comment generic |
| 371 | |
| 372 | TYPE *d2i_TYPE(TYPE **a, const unsigned char **ppin, long length); |
| 373 | TYPE *d2i_TYPE_bio(BIO *bp, TYPE **a); |
| 374 | TYPE *d2i_TYPE_fp(FILE *fp, TYPE **a); |
| 375 | |
| 376 | int i2d_TYPE(TYPE *a, unsigned char **ppout); |
| 377 | int i2d_TYPE_fp(FILE *fp, TYPE *a); |
| 378 | int i2d_TYPE_bio(BIO *bp, TYPE *a); |
| 379 | |
| 380 | =head1 DESCRIPTION |
| 381 | |
| 382 | In the description here, I<TYPE> is used a placeholder |
| 383 | for any of the OpenSSL datatypes, such as I<X509_CRL>. |
| 384 | The function parameters I<ppin> and I<ppout> are generally |
| 385 | either both named I<pp> in the headers, or I<in> and I<out>. |
| 386 | |
| 387 | These functions convert OpenSSL objects to and from their ASN.1/DER |
| 388 | encoding. Unlike the C structures which can have pointers to sub-objects |
| 389 | within, the DER is a serialized encoding, suitable for sending over the |
| 390 | network, writing to a file, and so on. |
| 391 | |
| 392 | d2i_TYPE() attempts to decode B<len> bytes at B<*ppin>. If successful a |
| 393 | pointer to the B<TYPE> structure is returned and B<*ppin> is incremented to |
| 394 | the byte following the parsed data. If B<a> is not B<NULL> then a pointer |
| 395 | to the returned structure is also written to B<*a>. If an error occurred |
| 396 | then B<NULL> is returned. |
| 397 | |
| 398 | On a successful return, if B<*a> is not B<NULL> then it is assumed that B<*a> |
| 399 | contains a valid B<TYPE> structure and an attempt is made to reuse it. This |
| 400 | "reuse" capability is present for historical compatibility but its use is |
| 401 | B<strongly discouraged> (see BUGS below, and the discussion in the RETURN |
| 402 | VALUES section). |
| 403 | |
| 404 | d2i_TYPE_bio() is similar to d2i_TYPE() except it attempts |
| 405 | to parse data from BIO B<bp>. |
| 406 | |
| 407 | d2i_TYPE_fp() is similar to d2i_TYPE() except it attempts |
| 408 | to parse data from FILE pointer B<fp>. |
| 409 | |
| 410 | i2d_TYPE() encodes the structure pointed to by B<a> into DER format. |
| 411 | If B<ppout> is not B<NULL>, it writes the DER encoded data to the buffer |
| 412 | at B<*ppout>, and increments it to point after the data just written. |
| 413 | If the return value is negative an error occurred, otherwise it |
| 414 | returns the length of the encoded data. |
| 415 | |
| 416 | If B<*ppout> is B<NULL> memory will be allocated for a buffer and the encoded |
| 417 | data written to it. In this case B<*ppout> is not incremented and it points |
| 418 | to the start of the data just written. |
| 419 | |
| 420 | i2d_TYPE_bio() is similar to i2d_TYPE() except it writes |
| 421 | the encoding of the structure B<a> to BIO B<bp> and it |
| 422 | returns 1 for success and 0 for failure. |
| 423 | |
| 424 | i2d_TYPE_fp() is similar to i2d_TYPE() except it writes |
| 425 | the encoding of the structure B<a> to BIO B<bp> and it |
| 426 | returns 1 for success and 0 for failure. |
| 427 | |
| 428 | These routines do not encrypt private keys and therefore offer no |
| 429 | security; use L<PEM_write_PrivateKey(3)> or similar for writing to files. |
| 430 | |
| 431 | =head1 NOTES |
| 432 | |
| 433 | The letters B<i> and B<d> in B<i2d_TYPE> stand for |
| 434 | "internal" (that is, an internal C structure) and "DER" respectively. |
| 435 | So B<i2d_TYPE> converts from internal to DER. |
| 436 | |
| 437 | The functions can also understand B<BER> forms. |
| 438 | |
| 439 | The actual TYPE structure passed to i2d_TYPE() must be a valid |
| 440 | populated B<TYPE> structure -- it B<cannot> simply be fed with an |
| 441 | empty structure such as that returned by TYPE_new(). |
| 442 | |
| 443 | The encoded data is in binary form and may contain embedded zeros. |
| 444 | Therefore, any FILE pointers or BIOs should be opened in binary mode. |
| 445 | Functions such as strlen() will B<not> return the correct length |
| 446 | of the encoded structure. |
| 447 | |
| 448 | The ways that B<*ppin> and B<*ppout> are incremented after the operation |
| 449 | can trap the unwary. See the B<WARNINGS> section for some common |
| 450 | errors. |
| 451 | The reason for this-auto increment behaviour is to reflect a typical |
| 452 | usage of ASN1 functions: after one structure is encoded or decoded |
| 453 | another will be processed after it. |
| 454 | |
| 455 | The following points about the data types might be useful: |
| 456 | |
| 457 | =over 4 |
| 458 | |
| 459 | =item B<ASN1_OBJECT> |
| 460 | |
| 461 | Represents an ASN1 OBJECT IDENTIFIER. |
| 462 | |
| 463 | =item B<DHparams> |
| 464 | |
| 465 | Represents a PKCS#3 DH parameters structure. |
| 466 | |
| 467 | =item B<DHxparams> |
| 468 | |
| 469 | Represents an ANSI X9.42 DH parameters structure. |
| 470 | |
| 471 | =item B<DSA_PUBKEY> |
| 472 | |
| 473 | Represents a DSA public key using a B<SubjectPublicKeyInfo> structure. |
| 474 | |
| 475 | =item B<DSAPublicKey, DSAPrivateKey> |
| 476 | |
| 477 | Use a non-standard OpenSSL format and should be avoided; use B<DSA_PUBKEY>, |
| 478 | B<PEM_write_PrivateKey(3)>, or similar instead. |
| 479 | |
| 480 | =item B<ECDSA_SIG> |
| 481 | |
| 482 | Represents an ECDSA signature. |
| 483 | |
| 484 | =item B<RSAPublicKey> |
| 485 | |
| 486 | Represents a PKCS#1 RSA public key structure. |
| 487 | |
| 488 | =item B<X509_ALGOR> |
| 489 | |
| 490 | Represents an B<AlgorithmIdentifier> structure as used in IETF RFC 6960 and |
| 491 | elsewhere. |
| 492 | |
| 493 | =item B<X509_Name> |
| 494 | |
| 495 | Represents a B<Name> type as used for subject and issuer names in |
| 496 | IETF RFC 6960 and elsewhere. |
| 497 | |
| 498 | =item B<X509_REQ> |
| 499 | |
| 500 | Represents a PKCS#10 certificate request. |
| 501 | |
| 502 | =item B<X509_SIG> |
| 503 | |
| 504 | Represents the B<DigestInfo> structure defined in PKCS#1 and PKCS#7. |
| 505 | |
| 506 | =back |
| 507 | |
| 508 | =head1 RETURN VALUES |
| 509 | |
| 510 | d2i_TYPE(), d2i_TYPE_bio() and d2i_TYPE_fp() return a valid B<TYPE> structure |
| 511 | or B<NULL> if an error occurs. If the "reuse" capability has been used with |
| 512 | a valid structure being passed in via B<a>, then the object is freed in |
| 513 | the event of error and B<*a> is set to NULL. |
| 514 | |
| 515 | i2d_TYPE() returns the number of bytes successfully encoded or a negative |
| 516 | value if an error occurs. |
| 517 | |
| 518 | i2d_TYPE_bio() and i2d_TYPE_fp() return 1 for success and 0 if an error |
| 519 | occurs. |
| 520 | |
| 521 | =head1 EXAMPLES |
| 522 | |
| 523 | Allocate and encode the DER encoding of an X509 structure: |
| 524 | |
| 525 | int len; |
| 526 | unsigned char *buf; |
| 527 | |
| 528 | buf = NULL; |
| 529 | len = i2d_X509(x, &buf); |
| 530 | if (len < 0) |
| 531 | /* error */ |
| 532 | |
| 533 | Attempt to decode a buffer: |
| 534 | |
| 535 | X509 *x; |
| 536 | unsigned char *buf; |
| 537 | const unsigned char *p; |
| 538 | int len; |
| 539 | |
| 540 | /* Set up buf and len to point to the input buffer. */ |
| 541 | p = buf; |
| 542 | x = d2i_X509(NULL, &p, len); |
| 543 | if (x == NULL) |
| 544 | /* error */ |
| 545 | |
| 546 | Alternative technique: |
| 547 | |
| 548 | X509 *x; |
| 549 | unsigned char *buf; |
| 550 | const unsigned char *p; |
| 551 | int len; |
| 552 | |
| 553 | /* Set up buf and len to point to the input buffer. */ |
| 554 | p = buf; |
| 555 | x = NULL; |
| 556 | |
| 557 | if (d2i_X509(&x, &p, len) == NULL) |
| 558 | /* error */ |
| 559 | |
| 560 | =head1 WARNINGS |
| 561 | |
| 562 | Using a temporary variable is mandatory. A common |
| 563 | mistake is to attempt to use a buffer directly as follows: |
| 564 | |
| 565 | int len; |
| 566 | unsigned char *buf; |
| 567 | |
| 568 | len = i2d_X509(x, NULL); |
| 569 | buf = OPENSSL_malloc(len); |
| 570 | ... |
| 571 | i2d_X509(x, &buf); |
| 572 | ... |
| 573 | OPENSSL_free(buf); |
| 574 | |
| 575 | This code will result in B<buf> apparently containing garbage because |
| 576 | it was incremented after the call to point after the data just written. |
| 577 | Also B<buf> will no longer contain the pointer allocated by OPENSSL_malloc() |
| 578 | and the subsequent call to OPENSSL_free() is likely to crash. |
| 579 | |
| 580 | Another trap to avoid is misuse of the B<a> argument to d2i_TYPE(): |
| 581 | |
| 582 | X509 *x; |
| 583 | |
| 584 | if (d2i_X509(&x, &p, len) == NULL) |
| 585 | /* error */ |
| 586 | |
| 587 | This will probably crash somewhere in d2i_X509(). The reason for this |
| 588 | is that the variable B<x> is uninitialized and an attempt will be made to |
| 589 | interpret its (invalid) value as an B<X509> structure, typically causing |
| 590 | a segmentation violation. If B<x> is set to NULL first then this will not |
| 591 | happen. |
| 592 | |
| 593 | =head1 BUGS |
| 594 | |
| 595 | In some versions of OpenSSL the "reuse" behaviour of d2i_TYPE() when |
| 596 | B<*a> is valid is broken and some parts of the reused structure may |
| 597 | persist if they are not present in the new one. Additionally, in versions of |
| 598 | OpenSSL prior to 1.1.0, when the "reuse" behaviour is used and an error occurs |
| 599 | the behaviour is inconsistent. Some functions behaved as described here, while |
| 600 | some did not free B<*a> on error and did not set B<*a> to NULL. |
| 601 | |
| 602 | As a result of the above issues the "reuse" behaviour is strongly discouraged. |
| 603 | |
| 604 | i2d_TYPE() will not return an error in many versions of OpenSSL, |
| 605 | if mandatory fields are not initialized due to a programming error |
| 606 | then the encoded structure may contain invalid data or omit the |
| 607 | fields entirely and will not be parsed by d2i_TYPE(). This may be |
| 608 | fixed in future so code should not assume that i2d_TYPE() will |
| 609 | always succeed. |
| 610 | |
| 611 | Any function which encodes a structure (i2d_TYPE(), |
| 612 | i2d_TYPE() or i2d_TYPE()) may return a stale encoding if the |
| 613 | structure has been modified after deserialization or previous |
| 614 | serialization. This is because some objects cache the encoding for |
| 615 | efficiency reasons. |
| 616 | |
| 617 | =head1 COPYRIGHT |
| 618 | |
| 619 | Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 620 | |
| 621 | Licensed under the OpenSSL license (the "License"). You may not use |
| 622 | this file except in compliance with the License. You can obtain a copy |
| 623 | in the file LICENSE in the source distribution or at |
| 624 | L<https://www.openssl.org/source/license.html>. |
| 625 | |
| 626 | =cut |