d2i_X509.pod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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_ECPKParameters,
  52. d2i_EDIPARTYNAME,
  53. d2i_ESS_CERT_ID,
  54. d2i_ESS_CERT_ID_V2,
  55. d2i_ESS_ISSUER_SERIAL,
  56. d2i_ESS_SIGNING_CERT,
  57. d2i_ESS_SIGNING_CERT_V2,
  58. d2i_EXTENDED_KEY_USAGE,
  59. d2i_GENERAL_NAME,
  60. d2i_GENERAL_NAMES,
  61. d2i_IPAddressChoice,
  62. d2i_IPAddressFamily,
  63. d2i_IPAddressOrRange,
  64. d2i_IPAddressRange,
  65. d2i_ISSUER_SIGN_TOOL,
  66. d2i_ISSUING_DIST_POINT,
  67. d2i_NAMING_AUTHORITY,
  68. d2i_NETSCAPE_CERT_SEQUENCE,
  69. d2i_NETSCAPE_SPKAC,
  70. d2i_NETSCAPE_SPKI,
  71. d2i_NOTICEREF,
  72. d2i_OCSP_BASICRESP,
  73. d2i_OCSP_CERTID,
  74. d2i_OCSP_CERTSTATUS,
  75. d2i_OCSP_CRLID,
  76. d2i_OCSP_ONEREQ,
  77. d2i_OCSP_REQINFO,
  78. d2i_OCSP_REQUEST,
  79. d2i_OCSP_RESPBYTES,
  80. d2i_OCSP_RESPDATA,
  81. d2i_OCSP_RESPID,
  82. d2i_OCSP_RESPONSE,
  83. d2i_OCSP_REVOKEDINFO,
  84. d2i_OCSP_SERVICELOC,
  85. d2i_OCSP_SIGNATURE,
  86. d2i_OCSP_SINGLERESP,
  87. d2i_OSSL_CMP_MSG,
  88. d2i_OSSL_CMP_PKIHEADER,
  89. d2i_OSSL_CMP_PKISI,
  90. d2i_OSSL_CRMF_CERTID,
  91. d2i_OSSL_CRMF_CERTTEMPLATE,
  92. d2i_OSSL_CRMF_ENCRYPTEDVALUE,
  93. d2i_OSSL_CRMF_MSG,
  94. d2i_OSSL_CRMF_MSGS,
  95. d2i_OSSL_CRMF_PBMPARAMETER,
  96. d2i_OSSL_CRMF_PKIPUBLICATIONINFO,
  97. d2i_OSSL_CRMF_SINGLEPUBINFO,
  98. d2i_OTHERNAME,
  99. d2i_PBE2PARAM,
  100. d2i_PBEPARAM,
  101. d2i_PBKDF2PARAM,
  102. d2i_PKCS12,
  103. d2i_PKCS12_BAGS,
  104. d2i_PKCS12_MAC_DATA,
  105. d2i_PKCS12_SAFEBAG,
  106. d2i_PKCS12_bio,
  107. d2i_PKCS12_fp,
  108. d2i_PKCS7,
  109. d2i_PKCS7_DIGEST,
  110. d2i_PKCS7_ENCRYPT,
  111. d2i_PKCS7_ENC_CONTENT,
  112. d2i_PKCS7_ENVELOPE,
  113. d2i_PKCS7_ISSUER_AND_SERIAL,
  114. d2i_PKCS7_RECIP_INFO,
  115. d2i_PKCS7_SIGNED,
  116. d2i_PKCS7_SIGNER_INFO,
  117. d2i_PKCS7_SIGN_ENVELOPE,
  118. d2i_PKCS7_bio,
  119. d2i_PKCS7_fp,
  120. d2i_PKCS8_PRIV_KEY_INFO,
  121. d2i_PKCS8_PRIV_KEY_INFO_bio,
  122. d2i_PKCS8_PRIV_KEY_INFO_fp,
  123. d2i_PKCS8_bio,
  124. d2i_PKCS8_fp,
  125. d2i_PKEY_USAGE_PERIOD,
  126. d2i_POLICYINFO,
  127. d2i_POLICYQUALINFO,
  128. d2i_PROFESSION_INFO,
  129. d2i_PROXY_CERT_INFO_EXTENSION,
  130. d2i_PROXY_POLICY,
  131. d2i_RSA_OAEP_PARAMS,
  132. d2i_RSA_PSS_PARAMS,
  133. d2i_SCRYPT_PARAMS,
  134. d2i_SCT_LIST,
  135. d2i_SXNET,
  136. d2i_SXNETID,
  137. d2i_TS_ACCURACY,
  138. d2i_TS_MSG_IMPRINT,
  139. d2i_TS_MSG_IMPRINT_bio,
  140. d2i_TS_MSG_IMPRINT_fp,
  141. d2i_TS_REQ,
  142. d2i_TS_REQ_bio,
  143. d2i_TS_REQ_fp,
  144. d2i_TS_RESP,
  145. d2i_TS_RESP_bio,
  146. d2i_TS_RESP_fp,
  147. d2i_TS_STATUS_INFO,
  148. d2i_TS_TST_INFO,
  149. d2i_TS_TST_INFO_bio,
  150. d2i_TS_TST_INFO_fp,
  151. d2i_USERNOTICE,
  152. d2i_X509,
  153. d2i_X509_bio,
  154. d2i_X509_fp,
  155. d2i_X509_ALGOR,
  156. d2i_X509_ALGORS,
  157. d2i_X509_ATTRIBUTE,
  158. d2i_X509_CERT_AUX,
  159. d2i_X509_CINF,
  160. d2i_X509_CRL,
  161. d2i_X509_CRL_INFO,
  162. d2i_X509_CRL_bio,
  163. d2i_X509_CRL_fp,
  164. d2i_X509_EXTENSION,
  165. d2i_X509_EXTENSIONS,
  166. d2i_X509_NAME,
  167. d2i_X509_NAME_ENTRY,
  168. d2i_X509_PUBKEY,
  169. d2i_X509_PUBKEY_bio,
  170. d2i_X509_PUBKEY_fp,
  171. d2i_X509_REQ,
  172. d2i_X509_REQ_INFO,
  173. d2i_X509_REQ_bio,
  174. d2i_X509_REQ_fp,
  175. d2i_X509_REVOKED,
  176. d2i_X509_SIG,
  177. d2i_X509_VAL,
  178. i2d_ACCESS_DESCRIPTION,
  179. i2d_ADMISSIONS,
  180. i2d_ADMISSION_SYNTAX,
  181. i2d_ASIdOrRange,
  182. i2d_ASIdentifierChoice,
  183. i2d_ASIdentifiers,
  184. i2d_ASN1_BIT_STRING,
  185. i2d_ASN1_BMPSTRING,
  186. i2d_ASN1_ENUMERATED,
  187. i2d_ASN1_GENERALIZEDTIME,
  188. i2d_ASN1_GENERALSTRING,
  189. i2d_ASN1_IA5STRING,
  190. i2d_ASN1_INTEGER,
  191. i2d_ASN1_NULL,
  192. i2d_ASN1_OBJECT,
  193. i2d_ASN1_OCTET_STRING,
  194. i2d_ASN1_PRINTABLE,
  195. i2d_ASN1_PRINTABLESTRING,
  196. i2d_ASN1_SEQUENCE_ANY,
  197. i2d_ASN1_SET_ANY,
  198. i2d_ASN1_T61STRING,
  199. i2d_ASN1_TIME,
  200. i2d_ASN1_TYPE,
  201. i2d_ASN1_UNIVERSALSTRING,
  202. i2d_ASN1_UTCTIME,
  203. i2d_ASN1_UTF8STRING,
  204. i2d_ASN1_VISIBLESTRING,
  205. i2d_ASN1_bio_stream,
  206. i2d_ASRange,
  207. i2d_AUTHORITY_INFO_ACCESS,
  208. i2d_AUTHORITY_KEYID,
  209. i2d_BASIC_CONSTRAINTS,
  210. i2d_CERTIFICATEPOLICIES,
  211. i2d_CMS_ContentInfo,
  212. i2d_CMS_ReceiptRequest,
  213. i2d_CMS_bio,
  214. i2d_CRL_DIST_POINTS,
  215. i2d_DHxparams,
  216. i2d_DIRECTORYSTRING,
  217. i2d_DISPLAYTEXT,
  218. i2d_DIST_POINT,
  219. i2d_DIST_POINT_NAME,
  220. i2d_DSA_SIG,
  221. i2d_ECDSA_SIG,
  222. i2d_ECPKParameters,
  223. i2d_EDIPARTYNAME,
  224. i2d_ESS_CERT_ID,
  225. i2d_ESS_CERT_ID_V2,
  226. i2d_ESS_ISSUER_SERIAL,
  227. i2d_ESS_SIGNING_CERT,
  228. i2d_ESS_SIGNING_CERT_V2,
  229. i2d_EXTENDED_KEY_USAGE,
  230. i2d_GENERAL_NAME,
  231. i2d_GENERAL_NAMES,
  232. i2d_IPAddressChoice,
  233. i2d_IPAddressFamily,
  234. i2d_IPAddressOrRange,
  235. i2d_IPAddressRange,
  236. i2d_ISSUER_SIGN_TOOL,
  237. i2d_ISSUING_DIST_POINT,
  238. i2d_NAMING_AUTHORITY,
  239. i2d_NETSCAPE_CERT_SEQUENCE,
  240. i2d_NETSCAPE_SPKAC,
  241. i2d_NETSCAPE_SPKI,
  242. i2d_NOTICEREF,
  243. i2d_OCSP_BASICRESP,
  244. i2d_OCSP_CERTID,
  245. i2d_OCSP_CERTSTATUS,
  246. i2d_OCSP_CRLID,
  247. i2d_OCSP_ONEREQ,
  248. i2d_OCSP_REQINFO,
  249. i2d_OCSP_REQUEST,
  250. i2d_OCSP_RESPBYTES,
  251. i2d_OCSP_RESPDATA,
  252. i2d_OCSP_RESPID,
  253. i2d_OCSP_RESPONSE,
  254. i2d_OCSP_REVOKEDINFO,
  255. i2d_OCSP_SERVICELOC,
  256. i2d_OCSP_SIGNATURE,
  257. i2d_OCSP_SINGLERESP,
  258. i2d_OSSL_CMP_MSG,
  259. i2d_OSSL_CMP_PKIHEADER,
  260. i2d_OSSL_CMP_PKISI,
  261. i2d_OSSL_CRMF_CERTID,
  262. i2d_OSSL_CRMF_CERTTEMPLATE,
  263. i2d_OSSL_CRMF_ENCRYPTEDVALUE,
  264. i2d_OSSL_CRMF_MSG,
  265. i2d_OSSL_CRMF_MSGS,
  266. i2d_OSSL_CRMF_PBMPARAMETER,
  267. i2d_OSSL_CRMF_PKIPUBLICATIONINFO,
  268. i2d_OSSL_CRMF_SINGLEPUBINFO,
  269. i2d_OTHERNAME,
  270. i2d_PBE2PARAM,
  271. i2d_PBEPARAM,
  272. i2d_PBKDF2PARAM,
  273. i2d_PKCS12,
  274. i2d_PKCS12_BAGS,
  275. i2d_PKCS12_MAC_DATA,
  276. i2d_PKCS12_SAFEBAG,
  277. i2d_PKCS12_bio,
  278. i2d_PKCS12_fp,
  279. i2d_PKCS7,
  280. i2d_PKCS7_DIGEST,
  281. i2d_PKCS7_ENCRYPT,
  282. i2d_PKCS7_ENC_CONTENT,
  283. i2d_PKCS7_ENVELOPE,
  284. i2d_PKCS7_ISSUER_AND_SERIAL,
  285. i2d_PKCS7_NDEF,
  286. i2d_PKCS7_RECIP_INFO,
  287. i2d_PKCS7_SIGNED,
  288. i2d_PKCS7_SIGNER_INFO,
  289. i2d_PKCS7_SIGN_ENVELOPE,
  290. i2d_PKCS7_bio,
  291. i2d_PKCS7_fp,
  292. i2d_PKCS8PrivateKeyInfo_bio,
  293. i2d_PKCS8PrivateKeyInfo_fp,
  294. i2d_PKCS8_PRIV_KEY_INFO,
  295. i2d_PKCS8_PRIV_KEY_INFO_bio,
  296. i2d_PKCS8_PRIV_KEY_INFO_fp,
  297. i2d_PKCS8_bio,
  298. i2d_PKCS8_fp,
  299. i2d_PKEY_USAGE_PERIOD,
  300. i2d_POLICYINFO,
  301. i2d_POLICYQUALINFO,
  302. i2d_PROFESSION_INFO,
  303. i2d_PROXY_CERT_INFO_EXTENSION,
  304. i2d_PROXY_POLICY,
  305. i2d_RSA_OAEP_PARAMS,
  306. i2d_RSA_PSS_PARAMS,
  307. i2d_SCRYPT_PARAMS,
  308. i2d_SCT_LIST,
  309. i2d_SXNET,
  310. i2d_SXNETID,
  311. i2d_TS_ACCURACY,
  312. i2d_TS_MSG_IMPRINT,
  313. i2d_TS_MSG_IMPRINT_bio,
  314. i2d_TS_MSG_IMPRINT_fp,
  315. i2d_TS_REQ,
  316. i2d_TS_REQ_bio,
  317. i2d_TS_REQ_fp,
  318. i2d_TS_RESP,
  319. i2d_TS_RESP_bio,
  320. i2d_TS_RESP_fp,
  321. i2d_TS_STATUS_INFO,
  322. i2d_TS_TST_INFO,
  323. i2d_TS_TST_INFO_bio,
  324. i2d_TS_TST_INFO_fp,
  325. i2d_USERNOTICE,
  326. i2d_X509,
  327. i2d_X509_bio,
  328. i2d_X509_fp,
  329. i2d_X509_ALGOR,
  330. i2d_X509_ALGORS,
  331. i2d_X509_ATTRIBUTE,
  332. i2d_X509_CERT_AUX,
  333. i2d_X509_CINF,
  334. i2d_X509_CRL,
  335. i2d_X509_CRL_INFO,
  336. i2d_X509_CRL_bio,
  337. i2d_X509_CRL_fp,
  338. i2d_X509_EXTENSION,
  339. i2d_X509_EXTENSIONS,
  340. i2d_X509_NAME,
  341. i2d_X509_NAME_ENTRY,
  342. i2d_X509_PUBKEY,
  343. i2d_X509_PUBKEY_bio,
  344. i2d_X509_PUBKEY_fp,
  345. i2d_X509_REQ,
  346. i2d_X509_REQ_INFO,
  347. i2d_X509_REQ_bio,
  348. i2d_X509_REQ_fp,
  349. i2d_X509_REVOKED,
  350. i2d_X509_SIG,
  351. i2d_X509_VAL,
  352. - convert objects from/to ASN.1/DER representation
  353. =head1 SYNOPSIS
  354. =for openssl generic
  355. TYPE *d2i_TYPE(TYPE **a, const unsigned char **ppin, long length);
  356. TYPE *d2i_TYPE_bio(BIO *bp, TYPE **a);
  357. TYPE *d2i_TYPE_fp(FILE *fp, TYPE **a);
  358. int i2d_TYPE(const TYPE *a, unsigned char **ppout);
  359. int i2d_TYPE(TYPE *a, unsigned char **ppout);
  360. int i2d_TYPE_fp(FILE *fp, const TYPE *a);
  361. int i2d_TYPE_fp(FILE *fp, TYPE *a);
  362. int i2d_TYPE_bio(BIO *bp, const TYPE *a);
  363. int i2d_TYPE_bio(BIO *bp, TYPE *a);
  364. =head1 DESCRIPTION
  365. In the description here, B<I<TYPE>> is used a placeholder
  366. for any of the OpenSSL datatypes, such as B<X509_CRL>.
  367. The function parameters I<ppin> and I<ppout> are generally
  368. either both named I<pp> in the headers, or I<in> and I<out>.
  369. These functions convert OpenSSL objects to and from their ASN.1/DER
  370. encoding. Unlike the C structures which can have pointers to sub-objects
  371. within, the DER is a serialized encoding, suitable for sending over the
  372. network, writing to a file, and so on.
  373. B<d2i_I<TYPE>>() attempts to decode I<len> bytes at I<*ppin>. If successful a
  374. pointer to the B<I<TYPE>> structure is returned and I<*ppin> is incremented to
  375. the byte following the parsed data. If I<a> is not NULL then a pointer
  376. to the returned structure is also written to I<*a>. If an error occurred
  377. then NULL is returned.
  378. On a successful return, if I<*a> is not NULL then it is assumed that I<*a>
  379. contains a valid B<I<TYPE>> structure and an attempt is made to reuse it.
  380. For B<I<TYPE>> structures where it matters it is possible to set up a library
  381. context on the decoded structure this way (see the B<EXAMPLES> section).
  382. However using the "reuse" capability for other purposes is B<strongly
  383. discouraged> (see B<BUGS> below, and the discussion in the B<RETURN VALUES>
  384. 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. Setting up a library context and property query:
  482. X509 *x;
  483. unsigned char *buf;
  484. const unsigned char *p;
  485. int len;
  486. OSSL_LIB_CTX *libctx = ....;
  487. const char *propq = ....;
  488. /* Set up buf and len to point to the input buffer. */
  489. p = buf;
  490. x = X509_new_ex(libctx, propq);
  491. if (d2i_X509(&x, &p, len) == NULL)
  492. /* error, x was freed and NULL assigned to it (see RETURN VALUES) */
  493. =head1 WARNINGS
  494. Using a temporary variable is mandatory. A common
  495. mistake is to attempt to use a buffer directly as follows:
  496. int len;
  497. unsigned char *buf;
  498. len = i2d_X509(x, NULL);
  499. buf = OPENSSL_malloc(len);
  500. ...
  501. i2d_X509(x, &buf);
  502. ...
  503. OPENSSL_free(buf);
  504. This code will result in I<buf> apparently containing garbage because
  505. it was incremented after the call to point after the data just written.
  506. Also I<buf> will no longer contain the pointer allocated by OPENSSL_malloc()
  507. and the subsequent call to OPENSSL_free() is likely to crash.
  508. Another trap to avoid is misuse of the I<a> argument to B<d2i_I<TYPE>>():
  509. X509 *x;
  510. if (d2i_X509(&x, &p, len) == NULL)
  511. /* error */
  512. This will probably crash somewhere in d2i_X509(). The reason for this
  513. is that the variable I<x> is uninitialized and an attempt will be made to
  514. interpret its (invalid) value as an B<X509> structure, typically causing
  515. a segmentation violation. If I<x> is set to NULL first then this will not
  516. happen.
  517. =head1 BUGS
  518. In some versions of OpenSSL the "reuse" behaviour of B<d2i_I<TYPE>>() when
  519. I<*a> is valid is broken and some parts of the reused structure may
  520. persist if they are not present in the new one. Additionally, in versions of
  521. OpenSSL prior to 1.1.0, when the "reuse" behaviour is used and an error occurs
  522. the behaviour is inconsistent. Some functions behaved as described here, while
  523. some did not free I<*a> on error and did not set I<*a> to NULL.
  524. As a result of the above issues the "reuse" behaviour is strongly discouraged.
  525. B<i2d_I<TYPE>>() will not return an error in many versions of OpenSSL,
  526. if mandatory fields are not initialized due to a programming error
  527. then the encoded structure may contain invalid data or omit the
  528. fields entirely and will not be parsed by B<d2i_I<TYPE>>(). This may be
  529. fixed in future so code should not assume that B<i2d_I<TYPE>>() will
  530. always succeed.
  531. Any function which encodes a structure (B<i2d_I<TYPE>>(),
  532. B<i2d_I<TYPE>_bio>() or B<i2d_I<TYPE>_fp>()) may return a stale encoding if the
  533. structure has been modified after deserialization or previous
  534. serialization. This is because some objects cache the encoding for
  535. efficiency reasons.
  536. =head1 COPYRIGHT
  537. Copyright 1998-2023 The OpenSSL Project Authors. All Rights Reserved.
  538. Licensed under the Apache License 2.0 (the "License"). You may not use
  539. this file except in compliance with the License. You can obtain a copy
  540. in the file LICENSE in the source distribution or at
  541. L<https://www.openssl.org/source/license.html>.
  542. =cut