ASN1_item_sign.3ossl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "ASN1_ITEM_SIGN 3ossl"
  58. .TH ASN1_ITEM_SIGN 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. ASN1_item_sign, ASN1_item_sign_ex, ASN1_item_sign_ctx,
  65. ASN1_item_verify, ASN1_item_verify_ex, ASN1_item_verify_ctx \-
  66. ASN1 sign and verify
  67. .SH SYNOPSIS
  68. .IX Header "SYNOPSIS"
  69. .Vb 1
  70. \& #include <openssl/x509.h>
  71. \&
  72. \& int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
  73. \& X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  74. \& const void *data, const ASN1_OCTET_STRING *id,
  75. \& EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
  76. \& const char *propq);
  77. \&
  78. \& int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
  79. \& ASN1_BIT_STRING *signature, const void *data,
  80. \& EVP_PKEY *pkey, const EVP_MD *md);
  81. \&
  82. \& int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  83. \& X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  84. \& const void *data, EVP_MD_CTX *ctx);
  85. \&
  86. \& int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
  87. \& const ASN1_BIT_STRING *signature, const void *data,
  88. \& const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
  89. \& OSSL_LIB_CTX *libctx, const char *propq);
  90. \&
  91. \& int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
  92. \& const ASN1_BIT_STRING *signature, const void *data,
  93. \& EVP_PKEY *pkey);
  94. \&
  95. \& int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
  96. \& const ASN1_BIT_STRING *signature, const void *data,
  97. \& EVP_MD_CTX *ctx);
  98. .Ve
  99. .SH DESCRIPTION
  100. .IX Header "DESCRIPTION"
  101. \&\fBASN1_item_sign_ex()\fR is used to sign arbitrary ASN1 data using a data object
  102. \&\fIdata\fR, the ASN.1 structure \fIit\fR, private key \fIpkey\fR and message digest \fImd\fR.
  103. The data that is signed is formed by taking the data object in \fIdata\fR and
  104. converting it to der format using the ASN.1 structure \fIit\fR.
  105. The \fIdata\fR that will be signed, and a structure containing the signature may
  106. both have a copy of the \fBX509_ALGOR\fR. The \fBASN1_item_sign_ex()\fR function will
  107. write the correct \fBX509_ALGOR\fR to the structs based on the algorithms and
  108. parameters that have been set up. If one of \fIalgor1\fR or \fIalgor2\fR points to the
  109. \&\fBX509_ALGOR\fR of the \fIdata\fR to be signed, then that \fBX509_ALGOR\fR will first be
  110. written before the signature is generated.
  111. Examples of valid values that can be used by the ASN.1 structure \fIit\fR are
  112. ASN1_ITEM_rptr(X509_CINF), ASN1_ITEM_rptr(X509_REQ_INFO) and
  113. ASN1_ITEM_rptr(X509_CRL_INFO).
  114. The \fBOSSL_LIB_CTX\fR specified in \fIlibctx\fR and the property query string
  115. specified in \fIprops\fR are used when searching for algorithms in providers.
  116. The generated signature is set into \fIsignature\fR.
  117. The optional parameter \fIid\fR can be NULL, but can be set for special key types.
  118. See \fBEVP_PKEY_CTX_set1_id()\fR for further info. The output parameters <algor1> and
  119. \&\fIalgor2\fR are ignored if they are NULL.
  120. .PP
  121. \&\fBASN1_item_sign()\fR is similar to \fBASN1_item_sign_ex()\fR but uses default values of
  122. NULL for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
  123. .PP
  124. \&\fBASN1_item_sign_ctx()\fR is similar to \fBASN1_item_sign()\fR but uses the parameters
  125. contained in digest context \fIctx\fR.
  126. .PP
  127. \&\fBASN1_item_verify_ex()\fR is used to verify the signature \fIsignature\fR of internal
  128. data \fIdata\fR using the public key \fIpkey\fR and algorithm identifier \fIalg\fR.
  129. The data that is verified is formed by taking the data object in \fIdata\fR and
  130. converting it to der format using the ASN.1 structure \fIit\fR.
  131. The \fBOSSL_LIB_CTX\fR specified in \fIlibctx\fR and the property query string
  132. specified in \fIprops\fR are used when searching for algorithms in providers.
  133. The optional parameter \fIid\fR can be NULL, but can be set for special key types.
  134. See \fBEVP_PKEY_CTX_set1_id()\fR for further info.
  135. .PP
  136. \&\fBASN1_item_verify()\fR is similar to \fBASN1_item_verify_ex()\fR but uses default values of
  137. NULL for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
  138. .PP
  139. \&\fBASN1_item_verify_ctx()\fR is similar to \fBASN1_item_verify()\fR but uses the parameters
  140. contained in digest context \fIctx\fR.
  141. .SH "RETURN VALUES"
  142. .IX Header "RETURN VALUES"
  143. All sign functions return the size of the signature in bytes for success and
  144. zero for failure.
  145. .PP
  146. All verify functions return 1 if the signature is valid and 0 if the signature
  147. check fails. If the signature could not be checked at all because it was
  148. ill-formed or some other error occurred then \-1 is returned.
  149. .SH EXAMPLES
  150. .IX Header "EXAMPLES"
  151. In the following example a 'MyObject' object is signed using the key contained
  152. in an EVP_MD_CTX. The signature is written to MyObject.signature. The object is
  153. then output in DER format and then loaded back in and verified.
  154. .PP
  155. .Vb 2
  156. \& #include <openssl/x509.h>
  157. \& #include <openssl/asn1t.h>
  158. \&
  159. \& /* An object used to store the ASN1 data fields that will be signed */
  160. \& typedef struct MySignInfoObject_st
  161. \& {
  162. \& ASN1_INTEGER *version;
  163. \& X509_ALGOR sig_alg;
  164. \& } MySignInfoObject;
  165. \&
  166. \& DECLARE_ASN1_FUNCTIONS(MySignInfoObject)
  167. \& /*
  168. \& * A higher level object containing the ASN1 fields, signature alg and
  169. \& * output signature.
  170. \& */
  171. \& typedef struct MyObject_st
  172. \& {
  173. \& MySignInfoObject info;
  174. \& X509_ALGOR sig_alg;
  175. \& ASN1_BIT_STRING *signature;
  176. \& } MyObject;
  177. \&
  178. \& DECLARE_ASN1_FUNCTIONS(MyObject)
  179. \&
  180. \& /* The ASN1 definition of MySignInfoObject */
  181. \& ASN1_SEQUENCE_cb(MySignInfoObject, NULL) = {
  182. \& ASN1_SIMPLE(MySignInfoObject, version, ASN1_INTEGER)
  183. \& ASN1_EMBED(MySignInfoObject, sig_alg, X509_ALGOR),
  184. \& } ASN1_SEQUENCE_END_cb(MySignInfoObject, MySignInfoObject)
  185. \&
  186. \& /* new, free, d2i & i2d functions for MySignInfoObject */
  187. \& IMPLEMENT_ASN1_FUNCTIONS(MySignInfoObject)
  188. \&
  189. \& /* The ASN1 definition of MyObject */
  190. \& ASN1_SEQUENCE_cb(MyObject, NULL) = {
  191. \& ASN1_EMBED(MyObject, info, MySignInfoObject),
  192. \& ASN1_EMBED(MyObject, sig_alg, X509_ALGOR),
  193. \& ASN1_SIMPLE(MyObject, signature, ASN1_BIT_STRING)
  194. \& } ASN1_SEQUENCE_END_cb(MyObject, MyObject)
  195. \&
  196. \& /* new, free, d2i & i2d functions for MyObject */
  197. \& IMPLEMENT_ASN1_FUNCTIONS(MyObject)
  198. \&
  199. \& int test_asn1_item_sign_verify(const char *mdname, EVP_PKEY *pkey, long version)
  200. \& {
  201. \& int ret = 0;
  202. \& unsigned char *obj_der = NULL;
  203. \& const unsigned char *p = NULL;
  204. \& MyObject *obj = NULL, *loaded_obj = NULL;
  205. \& const ASN1_ITEM *it = ASN1_ITEM_rptr(MySignInfoObject);
  206. \& EVP_MD_CTX *sctx = NULL, *vctx = NULL;
  207. \& int len;
  208. \&
  209. \& /* Create MyObject and set its version */
  210. \& obj = MyObject_new();
  211. \& if (obj == NULL)
  212. \& goto err;
  213. \& if (!ASN1_INTEGER_set(obj\->info.version, version))
  214. \& goto err;
  215. \&
  216. \& /* Set the key and digest used for signing */
  217. \& sctx = EVP_MD_CTX_new();
  218. \& if (sctx == NULL
  219. \& || !EVP_DigestSignInit_ex(sctx, NULL, mdname, NULL, NULL, pkey))
  220. \& goto err;
  221. \&
  222. \& /*
  223. \& * it contains the mapping between ASN.1 data and an object MySignInfoObject
  224. \& * obj\->info is the \*(AqMySignInfoObject\*(Aq object that will be
  225. \& * converted into DER data and then signed.
  226. \& * obj\->signature will contain the output signature.
  227. \& * obj\->sig_alg is filled with the private key\*(Aqs signing algorithm id.
  228. \& * obj\->info.sig_alg is another copy of the signing algorithm id that sits
  229. \& * within MyObject.
  230. \& */
  231. \& len = ASN1_item_sign_ctx(it, &obj\->sig_alg, &obj\->info.sig_alg,
  232. \& obj\->signature, &obj\->info, sctx);
  233. \& if (len <= 0
  234. \& || X509_ALGOR_cmp(&obj\->sig_alg, &obj\->info.sig_alg) != 0)
  235. \& goto err;
  236. \&
  237. \& /* Output MyObject in der form */
  238. \& len = i2d_MyObject(obj, &obj_der);
  239. \& if (len <= 0)
  240. \& goto err;
  241. \&
  242. \& /* Set the key and digest used for verifying */
  243. \& vctx = EVP_MD_CTX_new();
  244. \& if (vctx == NULL
  245. \& || !EVP_DigestVerifyInit_ex(vctx, NULL, mdname, NULL, NULL, pkey))
  246. \& goto err;
  247. \&
  248. \& /* Load the der data back into an object */
  249. \& p = obj_der;
  250. \& loaded_obj = d2i_MyObject(NULL, &p, len);
  251. \& if (loaded_obj == NULL)
  252. \& goto err;
  253. \& /* Verify the loaded object */
  254. \& ret = ASN1_item_verify_ctx(it, &loaded_obj\->sig_alg, loaded_obj\->signature,
  255. \& &loaded_obj\->info, vctx);
  256. \&err:
  257. \& OPENSSL_free(obj_der);
  258. \& MyObject_free(loaded_obj);
  259. \& MyObject_free(obj);
  260. \& EVP_MD_CTX_free(sctx);
  261. \& EVP_MD_CTX_free(vctx);
  262. \& return ret;
  263. \& }
  264. .Ve
  265. .SH "SEE ALSO"
  266. .IX Header "SEE ALSO"
  267. \&\fBX509_sign\fR\|(3),
  268. \&\fBX509_verify\fR\|(3)
  269. .SH HISTORY
  270. .IX Header "HISTORY"
  271. \&\fBASN1_item_sign_ex()\fR and \fBASN1_item_verify_ex()\fR were added in OpenSSL 3.0.
  272. .SH COPYRIGHT
  273. .IX Header "COPYRIGHT"
  274. Copyright 2020\-2022 The OpenSSL Project Authors. All Rights Reserved.
  275. .PP
  276. Licensed under the Apache License 2.0 (the "License"). You may not use
  277. this file except in compliance with the License. You can obtain a copy
  278. in the file LICENSE in the source distribution or at
  279. <https://www.openssl.org/source/license.html>.