ASN1_item_sign.3ossl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .nr rF 0
  58. .if \n(.g .if rF .nr rF 1
  59. .if (\n(rF:(\n(.g==0)) \{\
  60. . if \nF \{\
  61. . de IX
  62. . tm Index:\\$1\t\\n%\t"\\$2"
  63. ..
  64. . if !\nF==2 \{\
  65. . nr % 0
  66. . nr F 2
  67. . \}
  68. . \}
  69. .\}
  70. .rr rF
  71. .\"
  72. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  73. .\" Fear. Run. Save yourself. No user-serviceable parts.
  74. . \" fudge factors for nroff and troff
  75. .if n \{\
  76. . ds #H 0
  77. . ds #V .8m
  78. . ds #F .3m
  79. . ds #[ \f1
  80. . ds #] \fP
  81. .\}
  82. .if t \{\
  83. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  84. . ds #V .6m
  85. . ds #F 0
  86. . ds #[ \&
  87. . ds #] \&
  88. .\}
  89. . \" simple accents for nroff and troff
  90. .if n \{\
  91. . ds ' \&
  92. . ds ` \&
  93. . ds ^ \&
  94. . ds , \&
  95. . ds ~ ~
  96. . ds /
  97. .\}
  98. .if t \{\
  99. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  100. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  101. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  102. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  103. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  104. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  105. .\}
  106. . \" troff and (daisy-wheel) nroff accents
  107. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  108. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  109. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  110. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  111. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  112. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  113. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  114. .ds ae a\h'-(\w'a'u*4/10)'e
  115. .ds Ae A\h'-(\w'A'u*4/10)'E
  116. . \" corrections for vroff
  117. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  118. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  119. . \" for low resolution devices (crt and lpr)
  120. .if \n(.H>23 .if \n(.V>19 \
  121. \{\
  122. . ds : e
  123. . ds 8 ss
  124. . ds o a
  125. . ds d- d\h'-1'\(ga
  126. . ds D- D\h'-1'\(hy
  127. . ds th \o'bp'
  128. . ds Th \o'LP'
  129. . ds ae ae
  130. . ds Ae AE
  131. .\}
  132. .rm #[ #] #H #V #F C
  133. .\" ========================================================================
  134. .\"
  135. .IX Title "ASN1_ITEM_SIGN 3ossl"
  136. .TH ASN1_ITEM_SIGN 3ossl "2024-09-03" "3.3.2" "OpenSSL"
  137. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  138. .\" way too many mistakes in technical documents.
  139. .if n .ad l
  140. .nh
  141. .SH "NAME"
  142. ASN1_item_sign, ASN1_item_sign_ex, ASN1_item_sign_ctx,
  143. ASN1_item_verify, ASN1_item_verify_ex, ASN1_item_verify_ctx \-
  144. ASN1 sign and verify
  145. .SH "SYNOPSIS"
  146. .IX Header "SYNOPSIS"
  147. .Vb 1
  148. \& #include <openssl/x509.h>
  149. \&
  150. \& int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
  151. \& X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  152. \& const void *data, const ASN1_OCTET_STRING *id,
  153. \& EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
  154. \& const char *propq);
  155. \&
  156. \& int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
  157. \& ASN1_BIT_STRING *signature, const void *data,
  158. \& EVP_PKEY *pkey, const EVP_MD *md);
  159. \&
  160. \& int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  161. \& X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  162. \& const void *data, EVP_MD_CTX *ctx);
  163. \&
  164. \& int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
  165. \& const ASN1_BIT_STRING *signature, const void *data,
  166. \& const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
  167. \& OSSL_LIB_CTX *libctx, const char *propq);
  168. \&
  169. \& int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
  170. \& const ASN1_BIT_STRING *signature, const void *data,
  171. \& EVP_PKEY *pkey);
  172. \&
  173. \& int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
  174. \& const ASN1_BIT_STRING *signature, const void *data,
  175. \& EVP_MD_CTX *ctx);
  176. .Ve
  177. .SH "DESCRIPTION"
  178. .IX Header "DESCRIPTION"
  179. \&\fBASN1_item_sign_ex()\fR is used to sign arbitrary \s-1ASN1\s0 data using a data object
  180. \&\fIdata\fR, the \s-1ASN.1\s0 structure \fIit\fR, private key \fIpkey\fR and message digest \fImd\fR.
  181. The data that is signed is formed by taking the data object in \fIdata\fR and
  182. converting it to der format using the \s-1ASN.1\s0 structure \fIit\fR.
  183. The \fIdata\fR that will be signed, and a structure containing the signature may
  184. both have a copy of the \fBX509_ALGOR\fR. The \fBASN1_item_sign_ex()\fR function will
  185. write the correct \fBX509_ALGOR\fR to the structs based on the algorithms and
  186. parameters that have been set up. If one of \fIalgor1\fR or \fIalgor2\fR points to the
  187. \&\fBX509_ALGOR\fR of the \fIdata\fR to be signed, then that \fBX509_ALGOR\fR will first be
  188. written before the signature is generated.
  189. Examples of valid values that can be used by the \s-1ASN.1\s0 structure \fIit\fR are
  190. ASN1_ITEM_rptr(X509_CINF), ASN1_ITEM_rptr(X509_REQ_INFO) and
  191. ASN1_ITEM_rptr(X509_CRL_INFO).
  192. The \fB\s-1OSSL_LIB_CTX\s0\fR specified in \fIlibctx\fR and the property query string
  193. specified in \fIprops\fR are used when searching for algorithms in providers.
  194. The generated signature is set into \fIsignature\fR.
  195. The optional parameter \fIid\fR can be \s-1NULL,\s0 but can be set for special key types.
  196. See \fBEVP_PKEY_CTX_set1_id()\fR for further info. The output parameters <algor1> and
  197. \&\fIalgor2\fR are ignored if they are \s-1NULL.\s0
  198. .PP
  199. \&\fBASN1_item_sign()\fR is similar to \fBASN1_item_sign_ex()\fR but uses default values of
  200. \&\s-1NULL\s0 for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
  201. .PP
  202. \&\fBASN1_item_sign_ctx()\fR is similar to \fBASN1_item_sign()\fR but uses the parameters
  203. contained in digest context \fIctx\fR.
  204. .PP
  205. \&\fBASN1_item_verify_ex()\fR is used to verify the signature \fIsignature\fR of internal
  206. data \fIdata\fR using the public key \fIpkey\fR and algorithm identifier \fIalg\fR.
  207. The data that is verified is formed by taking the data object in \fIdata\fR and
  208. converting it to der format using the \s-1ASN.1\s0 structure \fIit\fR.
  209. The \fB\s-1OSSL_LIB_CTX\s0\fR specified in \fIlibctx\fR and the property query string
  210. specified in \fIprops\fR are used when searching for algorithms in providers.
  211. The optional parameter \fIid\fR can be \s-1NULL,\s0 but can be set for special key types.
  212. See \fBEVP_PKEY_CTX_set1_id()\fR for further info.
  213. .PP
  214. \&\fBASN1_item_verify()\fR is similar to \fBASN1_item_verify_ex()\fR but uses default values of
  215. \&\s-1NULL\s0 for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
  216. .PP
  217. \&\fBASN1_item_verify_ctx()\fR is similar to \fBASN1_item_verify()\fR but uses the parameters
  218. contained in digest context \fIctx\fR.
  219. .SH "RETURN VALUES"
  220. .IX Header "RETURN VALUES"
  221. All sign functions return the size of the signature in bytes for success and
  222. zero for failure.
  223. .PP
  224. All verify functions return 1 if the signature is valid and 0 if the signature
  225. check fails. If the signature could not be checked at all because it was
  226. ill-formed or some other error occurred then \-1 is returned.
  227. .SH "EXAMPLES"
  228. .IX Header "EXAMPLES"
  229. In the following example a 'MyObject' object is signed using the key contained
  230. in an \s-1EVP_MD_CTX.\s0 The signature is written to MyObject.signature. The object is
  231. then output in \s-1DER\s0 format and then loaded back in and verified.
  232. .PP
  233. .Vb 2
  234. \& #include <openssl/x509.h>
  235. \& #include <openssl/asn1t.h>
  236. \&
  237. \& /* An object used to store the ASN1 data fields that will be signed */
  238. \& typedef struct MySignInfoObject_st
  239. \& {
  240. \& ASN1_INTEGER *version;
  241. \& X509_ALGOR sig_alg;
  242. \& } MySignInfoObject;
  243. \&
  244. \& DECLARE_ASN1_FUNCTIONS(MySignInfoObject)
  245. \& /*
  246. \& * A higher level object containing the ASN1 fields, signature alg and
  247. \& * output signature.
  248. \& */
  249. \& typedef struct MyObject_st
  250. \& {
  251. \& MySignInfoObject info;
  252. \& X509_ALGOR sig_alg;
  253. \& ASN1_BIT_STRING *signature;
  254. \& } MyObject;
  255. \&
  256. \& DECLARE_ASN1_FUNCTIONS(MyObject)
  257. \&
  258. \& /* The ASN1 definition of MySignInfoObject */
  259. \& ASN1_SEQUENCE_cb(MySignInfoObject, NULL) = {
  260. \& ASN1_SIMPLE(MySignInfoObject, version, ASN1_INTEGER)
  261. \& ASN1_EMBED(MySignInfoObject, sig_alg, X509_ALGOR),
  262. \& } ASN1_SEQUENCE_END_cb(MySignInfoObject, MySignInfoObject)
  263. \&
  264. \& /* new, free, d2i & i2d functions for MySignInfoObject */
  265. \& IMPLEMENT_ASN1_FUNCTIONS(MySignInfoObject)
  266. \&
  267. \& /* The ASN1 definition of MyObject */
  268. \& ASN1_SEQUENCE_cb(MyObject, NULL) = {
  269. \& ASN1_EMBED(MyObject, info, MySignInfoObject),
  270. \& ASN1_EMBED(MyObject, sig_alg, X509_ALGOR),
  271. \& ASN1_SIMPLE(MyObject, signature, ASN1_BIT_STRING)
  272. \& } ASN1_SEQUENCE_END_cb(MyObject, MyObject)
  273. \&
  274. \& /* new, free, d2i & i2d functions for MyObject */
  275. \& IMPLEMENT_ASN1_FUNCTIONS(MyObject)
  276. \&
  277. \& int test_asn1_item_sign_verify(const char *mdname, EVP_PKEY *pkey, long version)
  278. \& {
  279. \& int ret = 0;
  280. \& unsigned char *obj_der = NULL;
  281. \& const unsigned char *p = NULL;
  282. \& MyObject *obj = NULL, *loaded_obj = NULL;
  283. \& const ASN1_ITEM *it = ASN1_ITEM_rptr(MySignInfoObject);
  284. \& EVP_MD_CTX *sctx = NULL, *vctx = NULL;
  285. \& int len;
  286. \&
  287. \& /* Create MyObject and set its version */
  288. \& obj = MyObject_new();
  289. \& if (obj == NULL)
  290. \& goto err;
  291. \& if (!ASN1_INTEGER_set(obj\->info.version, version))
  292. \& goto err;
  293. \&
  294. \& /* Set the key and digest used for signing */
  295. \& sctx = EVP_MD_CTX_new();
  296. \& if (sctx == NULL
  297. \& || !EVP_DigestSignInit_ex(sctx, NULL, mdname, NULL, NULL, pkey))
  298. \& goto err;
  299. \&
  300. \& /*
  301. \& * it contains the mapping between ASN.1 data and an object MySignInfoObject
  302. \& * obj\->info is the \*(AqMySignInfoObject\*(Aq object that will be
  303. \& * converted into DER data and then signed.
  304. \& * obj\->signature will contain the output signature.
  305. \& * obj\->sig_alg is filled with the private key\*(Aqs signing algorithm id.
  306. \& * obj\->info.sig_alg is another copy of the signing algorithm id that sits
  307. \& * within MyObject.
  308. \& */
  309. \& len = ASN1_item_sign_ctx(it, &obj\->sig_alg, &obj\->info.sig_alg,
  310. \& obj\->signature, &obj\->info, sctx);
  311. \& if (len <= 0
  312. \& || X509_ALGOR_cmp(&obj\->sig_alg, &obj\->info.sig_alg) != 0)
  313. \& goto err;
  314. \&
  315. \& /* Output MyObject in der form */
  316. \& len = i2d_MyObject(obj, &obj_der);
  317. \& if (len <= 0)
  318. \& goto err;
  319. \&
  320. \& /* Set the key and digest used for verifying */
  321. \& vctx = EVP_MD_CTX_new();
  322. \& if (vctx == NULL
  323. \& || !EVP_DigestVerifyInit_ex(vctx, NULL, mdname, NULL, NULL, pkey))
  324. \& goto err;
  325. \&
  326. \& /* Load the der data back into an object */
  327. \& p = obj_der;
  328. \& loaded_obj = d2i_MyObject(NULL, &p, len);
  329. \& if (loaded_obj == NULL)
  330. \& goto err;
  331. \& /* Verify the loaded object */
  332. \& ret = ASN1_item_verify_ctx(it, &loaded_obj\->sig_alg, loaded_obj\->signature,
  333. \& &loaded_obj\->info, vctx);
  334. \&err:
  335. \& OPENSSL_free(obj_der);
  336. \& MyObject_free(loaded_obj);
  337. \& MyObject_free(obj);
  338. \& EVP_MD_CTX_free(sctx);
  339. \& EVP_MD_CTX_free(vctx);
  340. \& return ret;
  341. \& }
  342. .Ve
  343. .SH "SEE ALSO"
  344. .IX Header "SEE ALSO"
  345. \&\fBX509_sign\fR\|(3),
  346. \&\fBX509_verify\fR\|(3)
  347. .SH "HISTORY"
  348. .IX Header "HISTORY"
  349. \&\fBASN1_item_sign_ex()\fR and \fBASN1_item_verify_ex()\fR were added in OpenSSL 3.0.
  350. .SH "COPYRIGHT"
  351. .IX Header "COPYRIGHT"
  352. Copyright 2020\-2022 The OpenSSL Project Authors. All Rights Reserved.
  353. .PP
  354. Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
  355. this file except in compliance with the License. You can obtain a copy
  356. in the file \s-1LICENSE\s0 in the source distribution or at
  357. <https://www.openssl.org/source/license.html>.