/src/openssl/crypto/asn1/a_sign.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <time.h> |
12 | | #include <sys/types.h> |
13 | | |
14 | | #include "internal/cryptlib.h" |
15 | | |
16 | | #include <openssl/bn.h> |
17 | | #include <openssl/evp.h> |
18 | | #include <openssl/x509.h> |
19 | | #include <openssl/objects.h> |
20 | | #include <openssl/buffer.h> |
21 | | #include <openssl/core_names.h> |
22 | | #include "crypto/asn1.h" |
23 | | #include "crypto/evp.h" |
24 | | |
25 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
26 | | |
27 | | int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2, |
28 | | ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey, |
29 | | const EVP_MD *type) |
30 | 0 | { |
31 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
32 | 0 | unsigned char *p, *buf_in = NULL, *buf_out = NULL; |
33 | 0 | int i, inl = 0, outl = 0; |
34 | 0 | size_t inll = 0, outll = 0; |
35 | 0 | X509_ALGOR *a; |
36 | |
|
37 | 0 | if (ctx == NULL) { |
38 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
39 | 0 | goto err; |
40 | 0 | } |
41 | 0 | for (i = 0; i < 2; i++) { |
42 | 0 | if (i == 0) |
43 | 0 | a = algor1; |
44 | 0 | else |
45 | 0 | a = algor2; |
46 | 0 | if (a == NULL) |
47 | 0 | continue; |
48 | 0 | if (type->pkey_type == NID_dsaWithSHA1) { |
49 | | /* |
50 | | * special case: RFC 3370 tells us to omit 'parameters' with |
51 | | * id-dsa-with-sha1 |
52 | | */ |
53 | 0 | ASN1_TYPE_free(a->parameter); |
54 | 0 | a->parameter = NULL; |
55 | 0 | } else if ((a->parameter == NULL) || (a->parameter->type != V_ASN1_NULL)) { |
56 | 0 | ASN1_TYPE_free(a->parameter); |
57 | 0 | if ((a->parameter = ASN1_TYPE_new()) == NULL) |
58 | 0 | goto err; |
59 | 0 | a->parameter->type = V_ASN1_NULL; |
60 | 0 | } |
61 | 0 | ASN1_OBJECT_free(a->algorithm); |
62 | 0 | a->algorithm = OBJ_nid2obj(type->pkey_type); |
63 | 0 | if (a->algorithm == NULL) { |
64 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_OBJECT_TYPE); |
65 | 0 | goto err; |
66 | 0 | } |
67 | 0 | if (a->algorithm->length == 0) { |
68 | 0 | ERR_raise(ERR_LIB_ASN1, |
69 | 0 | ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); |
70 | 0 | goto err; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | inl = i2d(data, NULL); |
74 | 0 | if (inl <= 0) { |
75 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
76 | 0 | goto err; |
77 | 0 | } |
78 | 0 | inll = (size_t)inl; |
79 | 0 | buf_in = OPENSSL_malloc(inll); |
80 | 0 | outll = outl = EVP_PKEY_get_size(pkey); |
81 | 0 | buf_out = OPENSSL_malloc(outll); |
82 | 0 | if (buf_in == NULL || buf_out == NULL) { |
83 | 0 | outl = 0; |
84 | 0 | goto err; |
85 | 0 | } |
86 | 0 | p = buf_in; |
87 | |
|
88 | 0 | i2d(data, &p); |
89 | 0 | if (!EVP_SignInit_ex(ctx, type, NULL) |
90 | 0 | || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl) |
91 | 0 | || !EVP_SignFinal(ctx, (unsigned char *)buf_out, |
92 | 0 | (unsigned int *)&outl, pkey)) { |
93 | 0 | outl = 0; |
94 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
95 | 0 | goto err; |
96 | 0 | } |
97 | 0 | ASN1_STRING_set0(signature, buf_out, outl); |
98 | 0 | buf_out = NULL; |
99 | | /* |
100 | | * In the interests of compatibility, I'll make sure that the bit string |
101 | | * has a 'not-used bits' value of 0 |
102 | | */ |
103 | 0 | ossl_asn1_string_set_bits_left(signature, 0); |
104 | 0 | err: |
105 | 0 | EVP_MD_CTX_free(ctx); |
106 | 0 | OPENSSL_clear_free((char *)buf_in, inll); |
107 | 0 | OPENSSL_clear_free((char *)buf_out, outll); |
108 | 0 | return outl; |
109 | 0 | } |
110 | | |
111 | | #endif |
112 | | |
113 | | int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, |
114 | | ASN1_BIT_STRING *signature, const void *data, |
115 | | EVP_PKEY *pkey, const EVP_MD *md) |
116 | 0 | { |
117 | 0 | return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey, |
118 | 0 | md, NULL, NULL); |
119 | 0 | } |
120 | | |
121 | | int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1, |
122 | | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, |
123 | | const void *data, const ASN1_OCTET_STRING *id, |
124 | | EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx, |
125 | | const char *propq) |
126 | 0 | { |
127 | 0 | int rv = 0; |
128 | 0 | EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq); |
129 | |
|
130 | 0 | if (ctx == NULL) { |
131 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
132 | 0 | return 0; |
133 | 0 | } |
134 | | /* We can use the non _ex variant here since the pkey is already setup */ |
135 | 0 | if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)) |
136 | 0 | goto err; |
137 | | |
138 | 0 | rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx); |
139 | |
|
140 | 0 | err: |
141 | 0 | EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx)); |
142 | 0 | EVP_MD_CTX_free(ctx); |
143 | 0 | return rv; |
144 | 0 | } |
145 | | |
146 | | static int replace_algor_contents_from_DER(X509_ALGOR *algor, const unsigned char *aid, size_t len) |
147 | 0 | { |
148 | | /* as a workaround for d2i_*() freeing its first argument, using NULL instead: */ |
149 | 0 | X509_ALGOR *alg = d2i_X509_ALGOR(NULL, &aid, (long)len); |
150 | 0 | int ret; |
151 | |
|
152 | 0 | if (alg == NULL) { |
153 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR); |
154 | 0 | return 0; |
155 | 0 | } |
156 | 0 | ret = X509_ALGOR_copy(algor, alg); |
157 | 0 | X509_ALGOR_free(alg); |
158 | 0 | return ret; |
159 | 0 | } |
160 | | |
161 | | int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, |
162 | | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, |
163 | | const void *data, EVP_MD_CTX *ctx) |
164 | 0 | { |
165 | 0 | const EVP_MD *md; |
166 | 0 | EVP_PKEY *pkey; |
167 | 0 | unsigned char *buf_in = NULL, *buf_out = NULL; |
168 | 0 | size_t inl = 0, outl = 0, outll = 0; |
169 | 0 | int signid, paramtype, buf_len = 0; |
170 | 0 | int rv, pkey_id; |
171 | |
|
172 | 0 | md = EVP_MD_CTX_get0_md(ctx); |
173 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx)); |
174 | |
|
175 | 0 | if (pkey == NULL) { |
176 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
177 | 0 | goto err; |
178 | 0 | } |
179 | | |
180 | 0 | if (pkey->ameth == NULL) { |
181 | 0 | EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx); |
182 | 0 | OSSL_PARAM params[2]; |
183 | 0 | unsigned char aid[128]; |
184 | 0 | size_t aid_len = 0; |
185 | |
|
186 | 0 | if (pctx == NULL |
187 | 0 | || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { |
188 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
189 | 0 | goto err; |
190 | 0 | } |
191 | | |
192 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, |
193 | 0 | aid, sizeof(aid)); |
194 | 0 | params[1] = OSSL_PARAM_construct_end(); |
195 | |
|
196 | 0 | if (EVP_PKEY_CTX_get_params(pctx, params) <= 0) |
197 | 0 | goto err; |
198 | | |
199 | 0 | if ((aid_len = params[0].return_size) == 0) { |
200 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
201 | 0 | goto err; |
202 | 0 | } |
203 | | |
204 | 0 | if (algor1 != NULL && !replace_algor_contents_from_DER(algor1, aid, aid_len)) |
205 | 0 | goto err; |
206 | 0 | if (algor2 != NULL && !replace_algor_contents_from_DER(algor2, aid, aid_len)) |
207 | 0 | goto err; |
208 | | |
209 | 0 | rv = 3; |
210 | 0 | } else if (pkey->ameth->item_sign) { |
211 | 0 | rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature); |
212 | 0 | if (rv == 1) |
213 | 0 | outl = signature->length; |
214 | | /*- |
215 | | * Return value meanings: |
216 | | * <=0: error. |
217 | | * 1: method does everything. |
218 | | * 2: carry on as normal. |
219 | | * 3: ASN1 method sets algorithm identifiers: just sign. |
220 | | */ |
221 | 0 | if (rv <= 0) |
222 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
223 | 0 | if (rv <= 1) |
224 | 0 | goto err; |
225 | 0 | } else { |
226 | 0 | rv = 2; |
227 | 0 | } |
228 | | |
229 | 0 | if (rv == 2) { |
230 | 0 | if (md == NULL) { |
231 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
232 | 0 | goto err; |
233 | 0 | } |
234 | | |
235 | 0 | pkey_id = |
236 | 0 | #ifndef OPENSSL_NO_SM2 |
237 | 0 | EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 : |
238 | 0 | #endif |
239 | 0 | pkey->ameth->pkey_id; |
240 | |
|
241 | 0 | if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) { |
242 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
243 | 0 | goto err; |
244 | 0 | } |
245 | | |
246 | 0 | paramtype = pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL ? V_ASN1_NULL : V_ASN1_UNDEF; |
247 | 0 | if (algor1 != NULL |
248 | 0 | && !X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL)) |
249 | 0 | goto err; |
250 | 0 | if (algor2 != NULL |
251 | 0 | && !X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL)) |
252 | 0 | goto err; |
253 | 0 | } |
254 | | |
255 | 0 | buf_len = ASN1_item_i2d(data, &buf_in, it); |
256 | 0 | if (buf_len <= 0) { |
257 | 0 | outl = 0; |
258 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
259 | 0 | goto err; |
260 | 0 | } |
261 | 0 | inl = buf_len; |
262 | 0 | if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) { |
263 | 0 | outl = 0; |
264 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
265 | 0 | goto err; |
266 | 0 | } |
267 | 0 | outl = outll; |
268 | 0 | buf_out = OPENSSL_malloc(outll); |
269 | 0 | if (buf_in == NULL || buf_out == NULL) { |
270 | 0 | outl = 0; |
271 | 0 | goto err; |
272 | 0 | } |
273 | | |
274 | 0 | if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) { |
275 | 0 | outl = 0; |
276 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
277 | 0 | goto err; |
278 | 0 | } |
279 | 0 | ASN1_STRING_set0(signature, buf_out, (int)outl); |
280 | 0 | buf_out = NULL; |
281 | | /* |
282 | | * In the interests of compatibility, I'll make sure that the bit string |
283 | | * has a 'not-used bits' value of 0 |
284 | | */ |
285 | 0 | ossl_asn1_string_set_bits_left(signature, 0); |
286 | 0 | err: |
287 | 0 | OPENSSL_clear_free((char *)buf_in, inl); |
288 | 0 | OPENSSL_clear_free((char *)buf_out, outll); |
289 | 0 | return (int)outl; |
290 | 0 | } |