Debian Codegraph @

upstream/debian-unstable/man3/ASN1_item_sign.3ssl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
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 3SSL"
58 .TH ASN1_ITEM_SIGN 3SSL 2026-04-07 3.6.2 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 to be signed is formed by taking the data object in \fIdata\fR
104 and converting it to DER format using the ASN.1 type structure \fIit\fR.
105 Examples of values that can be used for \fIit\fR are \fIASN1_ITEM_rptr(X509_CINF)\fR,
106 \&\fIASN1_ITEM_rptr(X509_REQ_INFO)\fR, and \fIASN1_ITEM_rptr(X509_CRL_INFO)\fR.
107 .PP
108 \&\fIsignature\fR, <algor1>, and \fIalgor2\fR are essentially output parameters.
109 The generated signature is set into \fIsignature\fR,
110 which must have been allocated beforehand, e.g., using \fR\f(BIASN1_BIT_STRING_new()\fR\fI\fR.
111 This may happen indirectly, for instance by having an ASN.1 structure like
112 \&\fBX509\fR that includes a non-optional signature field of type \fBASN1_BIT_STRING\fR.
113 The <algor1> and \fIalgor2\fR parameters are optional and support outputting
114 up to two \fBX509_ALGOR\fR values representing the signature algorithm
115 (typically including a digest algorithm, and/or possibly other parameters),
116 which is useful for structures like \fBX509\fR that must contain the same signature
117 algorithm information in two places: \fIsig_alg\fR and \fIcert_info.signature\fR.
118 Each of the <algor1> and \fIalgor2\fR pointers may be NULL, otherwise
119 they must have been allocated beforehand (e.g., using \fI\fR\f(BIX509_ALGOR_new()\fR\fI\fR,
120 or indirectly like for the \fBX509\fRfields \fIsig_alg\fR and \fIcert_info.signature\fR),
121 and the \fBASN1_item_sign_ex()\fR function sets into it the signature algorithm. This
122 is done before generating the signature value and setting it into \fIsignature\fR.
123 .PP
124 The \fBOSSL_LIB_CTX\fR specified in \fIlibctx\fR and the property query string
125 specified in \fIprops\fR are used when searching for algorithms in providers.
126 The optional parameter \fIid\fR can be NULL, but can be set for special key types.
127 See \fBEVP_PKEY_CTX_set1_id()\fR for further info.
128 .PP
129 \&\fBASN1_item_sign()\fR is similar to \fBASN1_item_sign_ex()\fR but uses default values of
130 NULL for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
131 .PP
132 \&\fBASN1_item_sign_ctx()\fR is similar to \fBASN1_item_sign()\fR but uses the parameters
133 contained in digest context \fIctx\fR.
134 .PP
135 \&\fBASN1_item_verify_ex()\fR is used to verify the signature \fIsignature\fR of internal
136 data \fIdata\fR using the public key \fIpkey\fR and algorithm identifier \fIalg\fR.
137 The data that is verified is formed by taking the data object in \fIdata\fR and
138 converting it to der format using the ASN.1 structure \fIit\fR.
139 The \fBOSSL_LIB_CTX\fR specified in \fIlibctx\fR and the property query string
140 specified in \fIprops\fR are used when searching for algorithms in providers.
141 The optional parameter \fIid\fR can be NULL, but can be set for special key types.
142 See \fBEVP_PKEY_CTX_set1_id()\fR for further info.
143 .PP
144 \&\fBASN1_item_verify()\fR is similar to \fBASN1_item_verify_ex()\fR but uses default values of
145 NULL for the \fIid\fR, \fIlibctx\fR and \fIpropq\fR.
146 .PP
147 \&\fBASN1_item_verify_ctx()\fR is similar to \fBASN1_item_verify()\fR but uses the parameters
148 contained in digest context \fIctx\fR.
149 .SH "RETURN VALUES"
150 .IX Header "RETURN VALUES"
151 All sign functions return the size of the signature in bytes for success and
152 zero for failure.
153 .PP
154 All verify functions return 1 if the signature is valid and 0 if the signature
155 check fails. If the signature could not be checked at all because it was
156 ill-formed or some other error occurred then \-1 is returned.
157 .SH EXAMPLES
158 .IX Header "EXAMPLES"
159 In the following example a 'MyObject' object is signed using the key contained
160 in an EVP_MD_CTX. The signature is written to MyObject.signature. The object is
161 then output in DER format and then loaded back in and verified.
162 .PP
163 .Vb 2
164 \& #include <openssl/x509.h>
165 \& #include <openssl/asn1t.h>
166 \&
167 \& /* An object used to store the ASN1 data fields that will be signed */
168 \& typedef struct MySignInfoObject_st
169 \& {
170 \& ASN1_INTEGER *version;
171 \& X509_ALGOR sig_alg;
172 \& } MySignInfoObject;
173 \&
174 \& DECLARE_ASN1_FUNCTIONS(MySignInfoObject)
175 \& /*
176 \& * A higher level object containing the ASN1 fields, signature alg and
177 \& * output signature.
178 \& */
179 \& typedef struct MyObject_st
180 \& {
181 \& MySignInfoObject info;
182 \& X509_ALGOR sig_alg;
183 \& ASN1_BIT_STRING *signature;
184 \& } MyObject;
185 \&
186 \& DECLARE_ASN1_FUNCTIONS(MyObject)
187 \&
188 \& /* The ASN1 definition of MySignInfoObject */
189 \& ASN1_SEQUENCE_cb(MySignInfoObject, NULL) = {
190 \& ASN1_SIMPLE(MySignInfoObject, version, ASN1_INTEGER)
191 \& ASN1_EMBED(MySignInfoObject, sig_alg, X509_ALGOR),
192 \& } ASN1_SEQUENCE_END_cb(MySignInfoObject, MySignInfoObject)
193 \&
194 \& /* new, free, d2i & i2d functions for MySignInfoObject */
195 \& IMPLEMENT_ASN1_FUNCTIONS(MySignInfoObject)
196 \&
197 \& /* The ASN1 definition of MyObject */
198 \& ASN1_SEQUENCE_cb(MyObject, NULL) = {
199 \& ASN1_EMBED(MyObject, info, MySignInfoObject),
200 \& ASN1_EMBED(MyObject, sig_alg, X509_ALGOR),
201 \& ASN1_SIMPLE(MyObject, signature, ASN1_BIT_STRING)
202 \& } ASN1_SEQUENCE_END_cb(MyObject, MyObject)
203 \&
204 \& /* new, free, d2i & i2d functions for MyObject */
205 \& IMPLEMENT_ASN1_FUNCTIONS(MyObject)
206 \&
207 \& int test_asn1_item_sign_verify(const char *mdname, EVP_PKEY *pkey, long version)
208 \& {
209 \& int ret = 0;
210 \& unsigned char *obj_der = NULL;
211 \& const unsigned char *p = NULL;
212 \& MyObject *obj = NULL, *loaded_obj = NULL;
213 \& const ASN1_ITEM *it = ASN1_ITEM_rptr(MySignInfoObject);
214 \& EVP_MD_CTX *sctx = NULL, *vctx = NULL;
215 \& int len;
216 \&
217 \& /* Create MyObject and set its version */
218 \& obj = MyObject_new();
219 \& if (obj == NULL)
220 \& goto err;
221 \& if (!ASN1_INTEGER_set(obj\->info.version, version))
222 \& goto err;
223 \&
224 \& /* Set the key and digest used for signing */
225 \& sctx = EVP_MD_CTX_new();
226 \& if (sctx == NULL
227 \& || !EVP_DigestSignInit_ex(sctx, NULL, mdname, NULL, NULL, pkey))
228 \& goto err;
229 \&
230 \& /*
231 \& * it contains the mapping between ASN.1 data and an object MySignInfoObject
232 \& * obj\->info is the \*(AqMySignInfoObject\*(Aq object that will be
233 \& * converted into DER data and then signed.
234 \& * obj\->signature will contain the output signature.
235 \& * obj\->sig_alg is filled with the private key\*(Aqs signing algorithm id.
236 \& * obj\->info.sig_alg is another copy of the signing algorithm id that sits
237 \& * within MyObject.
238 \& */
239 \& len = ASN1_item_sign_ctx(it, &obj\->sig_alg, &obj\->info.sig_alg,
240 \& obj\->signature, &obj\->info, sctx);
241 \& if (len <= 0
242 \& || X509_ALGOR_cmp(&obj\->sig_alg, &obj\->info.sig_alg) != 0)
243 \& goto err;
244 \&
245 \& /* Output MyObject in der form */
246 \& len = i2d_MyObject(obj, &obj_der);
247 \& if (len <= 0)
248 \& goto err;
249 \&
250 \& /* Set the key and digest used for verifying */
251 \& vctx = EVP_MD_CTX_new();
252 \& if (vctx == NULL
253 \& || !EVP_DigestVerifyInit_ex(vctx, NULL, mdname, NULL, NULL, pkey))
254 \& goto err;
255 \&
256 \& /* Load the der data back into an object */
257 \& p = obj_der;
258 \& loaded_obj = d2i_MyObject(NULL, &p, len);
259 \& if (loaded_obj == NULL)
260 \& goto err;
261 \& /* Verify the loaded object */
262 \& ret = ASN1_item_verify_ctx(it, &loaded_obj\->sig_alg, loaded_obj\->signature,
263 \& &loaded_obj\->info, vctx);
264 \&err:
265 \& OPENSSL_free(obj_der);
266 \& MyObject_free(loaded_obj);
267 \& MyObject_free(obj);
268 \& EVP_MD_CTX_free(sctx);
269 \& EVP_MD_CTX_free(vctx);
270 \& return ret;
271 \& }
272 .Ve
273 .SH "SEE ALSO"
274 .IX Header "SEE ALSO"
275 \&\fBX509_sign\fR\|(3),
276 \&\fBX509_verify\fR\|(3)
277 .SH HISTORY
278 .IX Header "HISTORY"
279 \&\fBASN1_item_sign_ex()\fR and \fBASN1_item_verify_ex()\fR were added in OpenSSL 3.0.
280 .SH COPYRIGHT
281 .IX Header "COPYRIGHT"
282 Copyright 2020\-2025 The OpenSSL Project Authors. All Rights Reserved.
283 .PP
284 Licensed under the Apache License 2.0 (the "License"). You may not use
285 this file except in compliance with the License. You can obtain a copy
286 in the file LICENSE in the source distribution or at
287 <https://www.openssl.org/source/license.html>.