d2i_X509.pod 15 KB

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