d2i_X509.pod 15 KB

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