1ASN1_ITEM_SIGN(3ossl) OpenSSL ASN1_ITEM_SIGN(3ossl)
2
3
4
6 ASN1_item_sign, ASN1_item_sign_ex, ASN1_item_sign_ctx,
7 ASN1_item_verify, ASN1_item_verify_ex, ASN1_item_verify_ctx - ASN1 sign
8 and verify
9
11 #include <openssl/x509.h>
12
13 int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
14 X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
15 const void *data, const ASN1_OCTET_STRING *id,
16 EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
17 const char *propq);
18
19 int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
20 ASN1_BIT_STRING *signature, const void *data,
21 EVP_PKEY *pkey, const EVP_MD *md);
22
23 int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
24 X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
25 const void *data, EVP_MD_CTX *ctx);
26
27 int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
28 const ASN1_BIT_STRING *signature, const void *data,
29 const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
30 OSSL_LIB_CTX *libctx, const char *propq);
31
32 int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
33 const ASN1_BIT_STRING *signature, const void *data,
34 EVP_PKEY *pkey);
35
36 int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
37 const ASN1_BIT_STRING *signature, const void *data,
38 EVP_MD_CTX *ctx);
39
41 ASN1_item_sign_ex() is used to sign arbitrary ASN1 data using a data
42 object data, the ASN.1 structure it, private key pkey and message
43 digest md. The data that is signed is formed by taking the data object
44 in data and converting it to der format using the ASN.1 structure it.
45 The data that will be signed, and a structure containing the signature
46 may both have a copy of the X509_ALGOR. The ASN1_item_sign_ex()
47 function will write the correct X509_ALGOR to the structs based on the
48 algorithms and parameters that have been set up. If one of algor1 or
49 algor2 points to the X509_ALGOR of the data to be signed, then that
50 X509_ALGOR will first be written before the signature is generated.
51 Examples of valid values that can be used by the ASN.1 structure it are
52 ASN1_ITEM_rptr(X509_CINF), ASN1_ITEM_rptr(X509_REQ_INFO) and
53 ASN1_ITEM_rptr(X509_CRL_INFO). The OSSL_LIB_CTX specified in libctx
54 and the property query string specified in props are used when
55 searching for algorithms in providers. The generated signature is set
56 into signature. The optional parameter id can be NULL, but can be set
57 for special key types. See EVP_PKEY_CTX_set1_id() for further info.
58 The output parameters <algor1> and algor2 are ignored if they are NULL.
59
60 ASN1_item_sign() is similar to ASN1_item_sign_ex() but uses default
61 values of NULL for the id, libctx and propq.
62
63 ASN1_item_sign_ctx() is similiar to ASN1_item_sign() but uses the
64 parameters contained in digest context ctx.
65
66 ASN1_item_verify_ex() is used to verify the signature signature of
67 internal data data using the public key pkey and algorithm identifier
68 alg. The data that is verified is formed by taking the data object in
69 data and converting it to der format using the ASN.1 structure it. The
70 OSSL_LIB_CTX specified in libctx and the property query string
71 specified in props are used when searching for algorithms in providers.
72 The optional parameter id can be NULL, but can be set for special key
73 types. See EVP_PKEY_CTX_set1_id() for further info.
74
75 ASN1_item_verify() is similar to ASN1_item_verify_ex() but uses default
76 values of NULL for the id, libctx and propq.
77
78 ASN1_item_verify_ctx() is similiar to ASN1_item_verify() but uses the
79 parameters contained in digest context ctx.
80
82 All sign functions return the size of the signature in bytes for
83 success and zero for failure.
84
85 All verify functions return 1 if the signature is valid and 0 if the
86 signature check fails. If the signature could not be checked at all
87 because it was ill-formed or some other error occurred then -1 is
88 returned.
89
91 In the following example a 'MyObject' object is signed using the key
92 contained in an EVP_MD_CTX. The signature is written to
93 MyObject.signature. The object is then output in DER format and then
94 loaded back in and verified.
95
96 #include <openssl/x509.h>
97 #include <openssl/asn1t.h>
98
99 /* An object used to store the ASN1 data fields that will be signed */
100 typedef struct MySignInfoObject_st
101 {
102 ASN1_INTEGER *version;
103 X509_ALGOR sig_alg;
104 } MySignInfoObject;
105
106 DECLARE_ASN1_FUNCTIONS(MySignInfoObject)
107 /*
108 * A higher level object containing the ASN1 fields, signature alg and
109 * output signature.
110 */
111 typedef struct MyObject_st
112 {
113 MySignInfoObject info;
114 X509_ALGOR sig_alg;
115 ASN1_BIT_STRING *signature;
116 } MyObject;
117
118 DECLARE_ASN1_FUNCTIONS(MyObject)
119
120 /* The ASN1 definition of MySignInfoObject */
121 ASN1_SEQUENCE_cb(MySignInfoObject, NULL) = {
122 ASN1_SIMPLE(MySignInfoObject, version, ASN1_INTEGER)
123 ASN1_EMBED(MySignInfoObject, sig_alg, X509_ALGOR),
124 } ASN1_SEQUENCE_END_cb(MySignInfoObject, MySignInfoObject)
125
126 /* new, free, d2i & i2d functions for MySignInfoObject */
127 IMPLEMENT_ASN1_FUNCTIONS(MySignInfoObject)
128
129 /* The ASN1 definition of MyObject */
130 ASN1_SEQUENCE_cb(MyObject, NULL) = {
131 ASN1_EMBED(MyObject, info, MySignInfoObject),
132 ASN1_EMBED(MyObject, sig_alg, X509_ALGOR),
133 ASN1_SIMPLE(MyObject, signature, ASN1_BIT_STRING)
134 } ASN1_SEQUENCE_END_cb(MyObject, MyObject)
135
136 /* new, free, d2i & i2d functions for MyObject */
137 IMPLEMENT_ASN1_FUNCTIONS(MyObject)
138
139 int test_asn1_item_sign_verify(const char *mdname, EVP_PKEY *pkey, long version)
140 {
141 int ret = 0;
142 unsigned char *obj_der = NULL;
143 const unsigned char *p = NULL;
144 MyObject *obj = NULL, *loaded_obj = NULL;
145 const ASN1_ITEM *it = ASN1_ITEM_rptr(MySignInfoObject);
146 EVP_MD_CTX *sctx = NULL, *vctx = NULL;
147 int len;
148
149 /* Create MyObject and set its version */
150 obj = MyObject_new();
151 if (obj == NULL)
152 goto err;
153 if (!ASN1_INTEGER_set(obj->info.version, version))
154 goto err;
155
156 /* Set the key and digest used for signing */
157 sctx = EVP_MD_CTX_new();
158 if (sctx == NULL
159 || !EVP_DigestSignInit_ex(sctx, NULL, mdname, NULL, NULL, pkey))
160 goto err;
161
162 /*
163 * it contains the mapping between ASN.1 data and an object MySignInfoObject
164 * obj->info is the 'MySignInfoObject' object that will be
165 * converted into DER data and then signed.
166 * obj->signature will contain the output signature.
167 * obj->sig_alg is filled with the private key's signing algorithm id.
168 * obj->info.sig_alg is another copy of the signing algorithm id that sits
169 * within MyObject.
170 */
171 len = ASN1_item_sign_ctx(it, &obj->sig_alg, &obj->info.sig_alg,
172 obj->signature, &obj->info, sctx);
173 if (len <= 0
174 || X509_ALGOR_cmp(&obj->sig_alg, &obj->info.sig_alg) != 0)
175 goto err;
176
177 /* Output MyObject in der form */
178 len = i2d_MyObject(obj, &obj_der);
179 if (len <= 0)
180 goto err;
181
182 /* Set the key and digest used for verifying */
183 vctx = EVP_MD_CTX_new();
184 if (vctx == NULL
185 || !EVP_DigestVerifyInit_ex(vctx, NULL, mdname, NULL, NULL, pkey))
186 goto err;
187
188 /* Load the der data back into an object */
189 p = obj_der;
190 loaded_obj = d2i_MyObject(NULL, &p, len);
191 if (loaded_obj == NULL)
192 goto err;
193 /* Verify the loaded object */
194 ret = ASN1_item_verify_ctx(it, &loaded_obj->sig_alg, loaded_obj->signature,
195 &loaded_obj->info, vctx);
196 err:
197 OPENSSL_free(obj_der);
198 MyObject_free(loaded_obj);
199 MyObject_free(obj);
200 EVP_MD_CTX_free(sctx);
201 EVP_MD_CTX_free(vctx);
202 return ret;
203 }
204
206 X509_sign(3), X509_verify(3)
207
209 ASN1_item_sign_ex() and ASN1_item_verify_ex() were added in OpenSSL
210 3.0.
211
213 Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
214
215 Licensed under the Apache License 2.0 (the "License"). You may not use
216 this file except in compliance with the License. You can obtain a copy
217 in the file LICENSE in the source distribution or at
218 <https://www.openssl.org/source/license.html>.
219
220
221
2223.0.5 2022-07-05 ASN1_ITEM_SIGN(3ossl)