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