Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/asn1/a_sign.c

raw
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 {
31 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
32 unsigned char *p, *buf_in = NULL, *buf_out = NULL;
33 int i, inl = 0, outl = 0;
34 size_t inll = 0, outll = 0;
35 X509_ALGOR *a;
36
37 if (ctx == NULL) {
38 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
39 goto err;
40 }
41 for (i = 0; i < 2; i++) {
42 if (i == 0)
43 a = algor1;
44 else
45 a = algor2;
46 if (a == NULL)
47 continue;
48 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 ASN1_TYPE_free(a->parameter);
54 a->parameter = NULL;
55 } else if ((a->parameter == NULL) || (a->parameter->type != V_ASN1_NULL)) {
56 ASN1_TYPE_free(a->parameter);
57 if ((a->parameter = ASN1_TYPE_new()) == NULL)
58 goto err;
59 a->parameter->type = V_ASN1_NULL;
60 }
61 ASN1_OBJECT_free(a->algorithm);
62 a->algorithm = OBJ_nid2obj(type->pkey_type);
63 if (a->algorithm == NULL) {
64 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_OBJECT_TYPE);
65 goto err;
66 }
67 if (a->algorithm->length == 0) {
68 ERR_raise(ERR_LIB_ASN1,
69 ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
70 goto err;
71 }
72 }
73 inl = i2d(data, NULL);
74 if (inl <= 0) {
75 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
76 goto err;
77 }
78 inll = (size_t)inl;
79 buf_in = OPENSSL_malloc(inll);
80 outll = outl = EVP_PKEY_get_size(pkey);
81 buf_out = OPENSSL_malloc(outll);
82 if (buf_in == NULL || buf_out == NULL) {
83 outl = 0;
84 goto err;
85 }
86 p = buf_in;
87
88 i2d(data, &p);
89 if (!EVP_SignInit_ex(ctx, type, NULL)
90 || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl)
91 || !EVP_SignFinal(ctx, (unsigned char *)buf_out,
92 (unsigned int *)&outl, pkey)) {
93 outl = 0;
94 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
95 goto err;
96 }
97 ASN1_STRING_set0(signature, buf_out, outl);
98 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 ossl_asn1_string_set_bits_left(signature, 0);
104 err:
105 EVP_MD_CTX_free(ctx);
106 OPENSSL_clear_free((char *)buf_in, inll);
107 OPENSSL_clear_free((char *)buf_out, outll);
108 return outl;
109 }
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 {
117 return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey,
118 md, NULL, NULL);
119 }
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 {
127 int rv = 0;
128 EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq);
129
130 if (ctx == NULL) {
131 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
132 return 0;
133 }
134 /* We can use the non _ex variant here since the pkey is already setup */
135 if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))
136 goto err;
137
138 rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx);
139
140 err:
141 EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
142 EVP_MD_CTX_free(ctx);
143 return rv;
144 }
145
146 int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
147 X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
148 const void *data, EVP_MD_CTX *ctx)
149 {
150 const EVP_MD *md;
151 EVP_PKEY *pkey;
152 unsigned char *buf_in = NULL, *buf_out = NULL;
153 size_t inl = 0, outl = 0, outll = 0;
154 int signid, paramtype, buf_len = 0;
155 int rv, pkey_id;
156
157 md = EVP_MD_CTX_get0_md(ctx);
158 pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
159
160 if (pkey == NULL) {
161 ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
162 goto err;
163 }
164
165 if (pkey->ameth == NULL) {
166 EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx);
167 OSSL_PARAM params[2];
168 unsigned char aid[128];
169 size_t aid_len = 0;
170
171 if (pctx == NULL
172 || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
173 ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
174 goto err;
175 }
176
177 params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
178 aid, sizeof(aid));
179 params[1] = OSSL_PARAM_construct_end();
180
181 if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
182 goto err;
183
184 if ((aid_len = params[0].return_size) == 0) {
185 ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
186 goto err;
187 }
188
189 if (algor1 != NULL) {
190 const unsigned char *pp = aid;
191
192 if (d2i_X509_ALGOR(&algor1, &pp, (long)aid_len) == NULL) {
193 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
194 goto err;
195 }
196 }
197
198 if (algor2 != NULL) {
199 const unsigned char *pp = aid;
200
201 if (d2i_X509_ALGOR(&algor2, &pp, (long)aid_len) == NULL) {
202 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
203 goto err;
204 }
205 }
206
207 rv = 3;
208 } else if (pkey->ameth->item_sign) {
209 rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature);
210 if (rv == 1)
211 outl = signature->length;
212 /*-
213 * Return value meanings:
214 * <=0: error.
215 * 1: method does everything.
216 * 2: carry on as normal.
217 * 3: ASN1 method sets algorithm identifiers: just sign.
218 */
219 if (rv <= 0)
220 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
221 if (rv <= 1)
222 goto err;
223 } else {
224 rv = 2;
225 }
226
227 if (rv == 2) {
228 if (md == NULL) {
229 ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
230 goto err;
231 }
232
233 pkey_id =
234 #ifndef OPENSSL_NO_SM2
235 EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 :
236 #endif
237 pkey->ameth->pkey_id;
238
239 if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) {
240 ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
241 goto err;
242 }
243
244 paramtype = pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL ? V_ASN1_NULL : V_ASN1_UNDEF;
245 if (algor1 != NULL
246 && !X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL))
247 goto err;
248 if (algor2 != NULL
249 && !X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL))
250 goto err;
251 }
252
253 buf_len = ASN1_item_i2d(data, &buf_in, it);
254 if (buf_len <= 0) {
255 outl = 0;
256 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
257 goto err;
258 }
259 inl = buf_len;
260 if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) {
261 outl = 0;
262 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
263 goto err;
264 }
265 outl = outll;
266 buf_out = OPENSSL_malloc(outll);
267 if (buf_in == NULL || buf_out == NULL) {
268 outl = 0;
269 goto err;
270 }
271
272 if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
273 outl = 0;
274 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
275 goto err;
276 }
277 ASN1_STRING_set0(signature, buf_out, (int)outl);
278 buf_out = NULL;
279 /*
280 * In the interests of compatibility, I'll make sure that the bit string
281 * has a 'not-used bits' value of 0
282 */
283 ossl_asn1_string_set_bits_left(signature, 0);
284 err:
285 OPENSSL_clear_free((char *)buf_in, inl);
286 OPENSSL_clear_free((char *)buf_out, outll);
287 return (int)outl;
288 }