5 Any keypair function here that gets deprecated should be moved to
12 d2i_ACCESS_DESCRIPTION,
16 d2i_ASIdentifierChoice,
21 d2i_ASN1_GENERALIZEDTIME,
22 d2i_ASN1_GENERALSTRING,
27 d2i_ASN1_OCTET_STRING,
29 d2i_ASN1_PRINTABLESTRING,
30 d2i_ASN1_SEQUENCE_ANY,
36 d2i_ASN1_UNIVERSALSTRING,
39 d2i_ASN1_VISIBLESTRING,
41 d2i_AUTHORITY_INFO_ACCESS,
43 d2i_BASIC_CONSTRAINTS,
44 d2i_CERTIFICATEPOLICIES,
46 d2i_CMS_ReceiptRequest,
60 d2i_ESS_ISSUER_SERIAL,
62 d2i_ESS_SIGNING_CERT_V2,
63 d2i_EXTENDED_KEY_USAGE,
71 d2i_ISSUING_DIST_POINT,
73 d2i_NETSCAPE_CERT_SEQUENCE,
93 d2i_OSSL_CMP_PKIHEADER,
96 d2i_OSSL_CRMF_CERTTEMPLATE,
97 d2i_OSSL_CRMF_ENCRYPTEDVALUE,
100 d2i_OSSL_CRMF_PBMPARAMETER,
101 d2i_OSSL_CRMF_PKIPUBLICATIONINFO,
102 d2i_OSSL_CRMF_SINGLEPUBINFO,
103 d2i_OSSL_IETF_ATTR_SYNTAX,
117 d2i_PKCS7_ENC_CONTENT,
119 d2i_PKCS7_ISSUER_AND_SERIAL,
120 d2i_PKCS7_RECIP_INFO,
122 d2i_PKCS7_SIGNER_INFO,
123 d2i_PKCS7_SIGN_ENVELOPE,
126 d2i_PKCS8_PRIV_KEY_INFO,
127 d2i_PKCS8_PRIV_KEY_INFO_bio,
128 d2i_PKCS8_PRIV_KEY_INFO_fp,
131 d2i_PKEY_USAGE_PERIOD,
135 d2i_PROXY_CERT_INFO_EXTENSION,
145 d2i_TS_MSG_IMPRINT_bio,
146 d2i_TS_MSG_IMPRINT_fp,
187 i2d_ACCESS_DESCRIPTION,
189 i2d_ADMISSION_SYNTAX,
191 i2d_ASIdentifierChoice,
196 i2d_ASN1_GENERALIZEDTIME,
197 i2d_ASN1_GENERALSTRING,
202 i2d_ASN1_OCTET_STRING,
204 i2d_ASN1_PRINTABLESTRING,
205 i2d_ASN1_SEQUENCE_ANY,
210 i2d_ASN1_UNIVERSALSTRING,
213 i2d_ASN1_VISIBLESTRING,
216 i2d_AUTHORITY_INFO_ACCESS,
218 i2d_BASIC_CONSTRAINTS,
219 i2d_CERTIFICATEPOLICIES,
221 i2d_CMS_ReceiptRequest,
235 i2d_ESS_ISSUER_SERIAL,
236 i2d_ESS_SIGNING_CERT,
237 i2d_ESS_SIGNING_CERT_V2,
238 i2d_EXTENDED_KEY_USAGE,
243 i2d_IPAddressOrRange,
245 i2d_ISSUER_SIGN_TOOL,
246 i2d_ISSUING_DIST_POINT,
247 i2d_NAMING_AUTHORITY,
248 i2d_NETSCAPE_CERT_SEQUENCE,
263 i2d_OCSP_REVOKEDINFO,
268 i2d_OSSL_CMP_PKIHEADER,
270 i2d_OSSL_CRMF_CERTID,
271 i2d_OSSL_CRMF_CERTTEMPLATE,
272 i2d_OSSL_CRMF_ENCRYPTEDVALUE,
275 i2d_OSSL_CRMF_PBMPARAMETER,
276 i2d_OSSL_CRMF_PKIPUBLICATIONINFO,
277 i2d_OSSL_CRMF_SINGLEPUBINFO,
278 i2d_OSSL_IETF_ATTR_SYNTAX,
292 i2d_PKCS7_ENC_CONTENT,
294 i2d_PKCS7_ISSUER_AND_SERIAL,
296 i2d_PKCS7_RECIP_INFO,
298 i2d_PKCS7_SIGNER_INFO,
299 i2d_PKCS7_SIGN_ENVELOPE,
302 i2d_PKCS8PrivateKeyInfo_bio,
303 i2d_PKCS8PrivateKeyInfo_fp,
304 i2d_PKCS8_PRIV_KEY_INFO,
305 i2d_PKCS8_PRIV_KEY_INFO_bio,
306 i2d_PKCS8_PRIV_KEY_INFO_fp,
309 i2d_PKEY_USAGE_PERIOD,
313 i2d_PROXY_CERT_INFO_EXTENSION,
323 i2d_TS_MSG_IMPRINT_bio,
324 i2d_TS_MSG_IMPRINT_fp,
365 - convert objects from/to ASN.1/DER representation
371 TYPE *d2i_TYPE(TYPE **a, const unsigned char **ppin, long length);
372 TYPE *d2i_TYPE_bio(BIO *bp, TYPE **a);
373 TYPE *d2i_TYPE_fp(FILE *fp, TYPE **a);
375 int i2d_TYPE(const TYPE *a, unsigned char **ppout);
376 int i2d_TYPE(TYPE *a, unsigned char **ppout);
377 int i2d_TYPE_fp(FILE *fp, const TYPE *a);
378 int i2d_TYPE_fp(FILE *fp, TYPE *a);
379 int i2d_TYPE_bio(BIO *bp, const TYPE *a);
380 int i2d_TYPE_bio(BIO *bp, TYPE *a);
384 In the description here, B<I<TYPE>> is used a placeholder
385 for any of the OpenSSL datatypes, such as B<X509_CRL>.
386 The function parameters I<ppin> and I<ppout> are generally
387 either both named I<pp> in the headers, or I<in> and I<out>.
389 These functions convert OpenSSL objects to and from their ASN.1/DER
390 encoding. Unlike the C structures which can have pointers to sub-objects
391 within, the DER is a serialized encoding, suitable for sending over the
392 network, writing to a file, and so on.
394 B<d2i_I<TYPE>>() attempts to decode I<len> bytes at I<*ppin>. If successful a
395 pointer to the B<I<TYPE>> structure is returned and I<*ppin> is incremented to
396 the byte following the parsed data. If I<a> is not NULL then a pointer
397 to the returned structure is also written to I<*a>. If an error occurred
398 then NULL is returned.
400 On a successful return, if I<*a> is not NULL then it is assumed that I<*a>
401 contains a valid B<I<TYPE>> structure and an attempt is made to reuse it.
402 For B<I<TYPE>> structures where it matters it is possible to set up a library
403 context on the decoded structure this way (see the B<EXAMPLES> section).
404 However using the "reuse" capability for other purposes is B<strongly
405 discouraged> (see B<BUGS> below, and the discussion in the B<RETURN VALUES>
408 B<d2i_I<TYPE>_bio>() is similar to B<d2i_I<TYPE>>() except it attempts
409 to parse data from BIO I<bp>.
411 B<d2i_I<TYPE>_fp>() is similar to B<d2i_I<TYPE>>() except it attempts
412 to parse data from FILE pointer I<fp>.
414 B<i2d_I<TYPE>>() encodes the structure pointed to by I<a> into DER format.
415 If I<ppout> is not NULL, it writes the DER encoded data to the buffer
416 at I<*ppout>, and increments it to point after the data just written.
417 If the return value is negative an error occurred, otherwise it
418 returns the length of the encoded data.
420 If I<*ppout> is NULL memory will be allocated for a buffer and the encoded
421 data written to it. In this case I<*ppout> is not incremented and it points
422 to the start of the data just written.
424 B<i2d_I<TYPE>_bio>() is similar to B<i2d_I<TYPE>>() except it writes
425 the encoding of the structure I<a> to BIO I<bp> and it
426 returns 1 for success and 0 for failure.
428 B<i2d_I<TYPE>_fp>() is similar to B<i2d_I<TYPE>>() except it writes
429 the encoding of the structure I<a> to FILE pointer I<fp> and it
430 returns 1 for success and 0 for failure.
432 These routines do not encrypt private keys and therefore offer no
433 security; use L<PEM_write_PrivateKey(3)> or similar for writing to files.
437 The letters B<i> and B<d> in B<i2d_I<TYPE>>() stand for
438 "internal" (that is, an internal C structure) and "DER" respectively.
439 So B<i2d_I<TYPE>>() converts from internal to DER.
441 The functions can also understand B<BER> forms.
443 The actual TYPE structure passed to B<i2d_I<TYPE>>() must be a valid
444 populated B<I<TYPE>> structure -- it B<cannot> simply be fed with an
445 empty structure such as that returned by TYPE_new().
447 The encoded data is in binary form and may contain embedded zeros.
448 Therefore, any FILE pointers or BIOs should be opened in binary mode.
449 Functions such as strlen() will B<not> return the correct length
450 of the encoded structure.
452 The ways that I<*ppin> and I<*ppout> are incremented after the operation
453 can trap the unwary. See the B<WARNINGS> section for some common
455 The reason for this-auto increment behaviour is to reflect a typical
456 usage of ASN1 functions: after one structure is encoded or decoded
457 another will be processed after it.
459 The following points about the data types might be useful:
465 Represents an ASN1 OBJECT IDENTIFIER.
469 Represents a PKCS#3 DH parameters structure.
473 Represents an ANSI X9.42 DH parameters structure.
477 Represents an ECDSA signature.
481 Represents an B<AlgorithmIdentifier> structure as used in IETF RFC 6960 and
486 Represents a B<Name> type as used for subject and issuer names in
487 IETF RFC 6960 and elsewhere.
491 Represents a PKCS#10 certificate request.
495 Represents the B<DigestInfo> structure defined in PKCS#1 and PKCS#7.
501 B<d2i_I<TYPE>>(), B<d2i_I<TYPE>_bio>() and B<d2i_I<TYPE>_fp>() return a valid
502 B<I<TYPE>> structure or NULL if an error occurs. If the "reuse" capability has
503 been used with a valid structure being passed in via I<a>, then the object is
504 freed in the event of error and I<*a> is set to NULL.
506 B<i2d_I<TYPE>>() returns the number of bytes successfully encoded or a negative
507 value if an error occurs.
509 B<i2d_I<TYPE>_bio>() and B<i2d_I<TYPE>_fp>() return 1 for success and 0 if an
514 Allocate and encode the DER encoding of an X509 structure:
520 len = i2d_X509(x, &buf);
524 Attempt to decode a buffer:
528 const unsigned char *p;
531 /* Set up buf and len to point to the input buffer. */
533 x = d2i_X509(NULL, &p, len);
537 Alternative technique:
541 const unsigned char *p;
544 /* Set up buf and len to point to the input buffer. */
548 if (d2i_X509(&x, &p, len) == NULL)
551 Setting up a library context and property query:
555 const unsigned char *p;
557 OSSL_LIB_CTX *libctx = ....;
558 const char *propq = ....;
560 /* Set up buf and len to point to the input buffer. */
562 x = X509_new_ex(libctx, propq);
564 if (d2i_X509(&x, &p, len) == NULL)
565 /* error, x was freed and NULL assigned to it (see RETURN VALUES) */
569 Using a temporary variable is mandatory. A common
570 mistake is to attempt to use a buffer directly as follows:
575 len = i2d_X509(x, NULL);
576 buf = OPENSSL_malloc(len);
582 This code will result in I<buf> apparently containing garbage because
583 it was incremented after the call to point after the data just written.
584 Also I<buf> will no longer contain the pointer allocated by OPENSSL_malloc()
585 and the subsequent call to OPENSSL_free() is likely to crash.
587 Another trap to avoid is misuse of the I<a> argument to B<d2i_I<TYPE>>():
591 if (d2i_X509(&x, &p, len) == NULL)
594 This will probably crash somewhere in d2i_X509(). The reason for this
595 is that the variable I<x> is uninitialized and an attempt will be made to
596 interpret its (invalid) value as an B<X509> structure, typically causing
597 a segmentation violation. If I<x> is set to NULL first then this will not
602 In some versions of OpenSSL the "reuse" behaviour of B<d2i_I<TYPE>>() when
603 I<*a> is valid is broken and some parts of the reused structure may
604 persist if they are not present in the new one. Additionally, in versions of
605 OpenSSL prior to 1.1.0, when the "reuse" behaviour is used and an error occurs
606 the behaviour is inconsistent. Some functions behaved as described here, while
607 some did not free I<*a> on error and did not set I<*a> to NULL.
609 As a result of the above issues the "reuse" behaviour is strongly discouraged.
611 B<i2d_I<TYPE>>() will not return an error in many versions of OpenSSL,
612 if mandatory fields are not initialized due to a programming error
613 then the encoded structure may contain invalid data or omit the
614 fields entirely and will not be parsed by B<d2i_I<TYPE>>(). This may be
615 fixed in future so code should not assume that B<i2d_I<TYPE>>() will
618 Any function which encodes a structure (B<i2d_I<TYPE>>(),
619 B<i2d_I<TYPE>_bio>() or B<i2d_I<TYPE>_fp>()) may return a stale encoding if the
620 structure has been modified after deserialization or previous
621 serialization. This is because some objects cache the encoding for
626 Copyright 1998-2023 The OpenSSL Project Authors. All Rights Reserved.
628 Licensed under the Apache License 2.0 (the "License"). You may not use
629 this file except in compliance with the License. You can obtain a copy
630 in the file LICENSE in the source distribution or at
631 L<https://www.openssl.org/source/license.html>.