]>
Commit | Line | Data |
---|---|---|
2039c421 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
8df61b50 | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 RS |
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 | |
8df61b50 | 8 | */ |
d02b48c6 RE |
9 | |
10 | #include <stdio.h> | |
11 | #include <time.h> | |
b379fe6c | 12 | #include <sys/types.h> |
d02b48c6 | 13 | |
b39fc560 | 14 | #include "internal/cryptlib.h" |
17f389bb | 15 | |
ec577822 BM |
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> | |
d5aef594 | 21 | #include <openssl/core_names.h> |
25f2138b DMSP |
22 | #include "crypto/asn1.h" |
23 | #include "crypto/evp.h" | |
d02b48c6 | 24 | |
12d99aac | 25 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
73e92de5 | 26 | |
8bb826ee | 27 | int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2, |
0f113f3e MC |
28 | ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey, |
29 | const EVP_MD *type) | |
30 | { | |
bfb0641f | 31 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
0f113f3e | 32 | unsigned char *p, *buf_in = NULL, *buf_out = NULL; |
da84249b F |
33 | int i, inl = 0, outl = 0; |
34 | size_t inll = 0, outll = 0; | |
0f113f3e | 35 | X509_ALGOR *a; |
d02b48c6 | 36 | |
6e59a892 | 37 | if (ctx == NULL) { |
9311d0c4 | 38 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
6e59a892 RL |
39 | goto err; |
40 | } | |
0f113f3e MC |
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 2459 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) || | |
56 | (a->parameter->type != V_ASN1_NULL)) { | |
57 | ASN1_TYPE_free(a->parameter); | |
58 | if ((a->parameter = ASN1_TYPE_new()) == NULL) | |
59 | goto err; | |
60 | a->parameter->type = V_ASN1_NULL; | |
61 | } | |
62 | ASN1_OBJECT_free(a->algorithm); | |
63 | a->algorithm = OBJ_nid2obj(type->pkey_type); | |
64 | if (a->algorithm == NULL) { | |
9311d0c4 | 65 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_OBJECT_TYPE); |
0f113f3e MC |
66 | goto err; |
67 | } | |
68 | if (a->algorithm->length == 0) { | |
9311d0c4 RL |
69 | ERR_raise(ERR_LIB_ASN1, |
70 | ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); | |
0f113f3e MC |
71 | goto err; |
72 | } | |
73 | } | |
74 | inl = i2d(data, NULL); | |
da84249b | 75 | if (inl <= 0) { |
9311d0c4 | 76 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
da84249b F |
77 | goto err; |
78 | } | |
79 | inll = (size_t)inl; | |
80 | buf_in = OPENSSL_malloc(inll); | |
ed576acd | 81 | outll = outl = EVP_PKEY_get_size(pkey); |
da84249b F |
82 | buf_out = OPENSSL_malloc(outll); |
83 | if (buf_in == NULL || buf_out == NULL) { | |
0f113f3e | 84 | outl = 0; |
9311d0c4 | 85 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
86 | goto err; |
87 | } | |
88 | p = buf_in; | |
d02b48c6 | 89 | |
0f113f3e | 90 | i2d(data, &p); |
6e59a892 RL |
91 | if (!EVP_SignInit_ex(ctx, type, NULL) |
92 | || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl) | |
93 | || !EVP_SignFinal(ctx, (unsigned char *)buf_out, | |
0f113f3e MC |
94 | (unsigned int *)&outl, pkey)) { |
95 | outl = 0; | |
9311d0c4 | 96 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
0f113f3e MC |
97 | goto err; |
98 | } | |
b548a1f1 | 99 | OPENSSL_free(signature->data); |
0f113f3e MC |
100 | signature->data = buf_out; |
101 | buf_out = NULL; | |
102 | signature->length = outl; | |
103 | /* | |
104 | * In the interests of compatibility, I'll make sure that the bit string | |
105 | * has a 'not-used bits' value of 0 | |
106 | */ | |
107 | signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
108 | signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
109 | err: | |
bfb0641f | 110 | EVP_MD_CTX_free(ctx); |
da84249b | 111 | OPENSSL_clear_free((char *)buf_in, inll); |
4b45c6e5 | 112 | OPENSSL_clear_free((char *)buf_out, outll); |
26a7d938 | 113 | return outl; |
0f113f3e | 114 | } |
09ab755c | 115 | |
73e92de5 DSH |
116 | #endif |
117 | ||
ded346fa DDO |
118 | int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, |
119 | ASN1_BIT_STRING *signature, const void *data, | |
120 | EVP_PKEY *pkey, const EVP_MD *md) | |
0f113f3e | 121 | { |
d8652be0 MC |
122 | return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey, |
123 | md, NULL, NULL); | |
ded346fa DDO |
124 | } |
125 | ||
d8652be0 MC |
126 | int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1, |
127 | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, | |
128 | const void *data, const ASN1_OCTET_STRING *id, | |
b4250010 | 129 | EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx, |
d8652be0 | 130 | const char *propq) |
ded346fa DDO |
131 | { |
132 | int rv = 0; | |
d8652be0 | 133 | EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq); |
6e59a892 RL |
134 | |
135 | if (ctx == NULL) { | |
9311d0c4 | 136 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
137 | return 0; |
138 | } | |
e6c2f964 | 139 | /* We can use the non _ex variant here since the pkey is already setup */ |
ded346fa DDO |
140 | if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)) |
141 | goto err; | |
c6aca19b | 142 | |
ded346fa | 143 | rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx); |
c6aca19b | 144 | |
ded346fa | 145 | err: |
ed576acd | 146 | EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx)); |
c6aca19b SF |
147 | EVP_MD_CTX_free(ctx); |
148 | return rv; | |
0f113f3e | 149 | } |
85522a07 | 150 | |
ded346fa DDO |
151 | int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, |
152 | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, | |
153 | const void *data, EVP_MD_CTX *ctx) | |
0f113f3e | 154 | { |
ded346fa | 155 | const EVP_MD *md; |
0f113f3e MC |
156 | EVP_PKEY *pkey; |
157 | unsigned char *buf_in = NULL, *buf_out = NULL; | |
158 | size_t inl = 0, outl = 0, outll = 0; | |
da84249b | 159 | int signid, paramtype, buf_len = 0; |
bc42bd62 | 160 | int rv, pkey_id; |
85522a07 | 161 | |
f6c95e46 | 162 | md = EVP_MD_CTX_get0_md(ctx); |
ed576acd | 163 | pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx)); |
09ab755c | 164 | |
7dd6de9f | 165 | if (pkey == NULL) { |
9311d0c4 | 166 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
2c91b3f5 MRA |
167 | goto err; |
168 | } | |
169 | ||
219f3ca6 | 170 | if (pkey->ameth == NULL) { |
ed576acd | 171 | EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx); |
d5aef594 RL |
172 | OSSL_PARAM params[2]; |
173 | unsigned char aid[128]; | |
174 | size_t aid_len = 0; | |
175 | ||
176 | if (pctx == NULL | |
177 | || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { | |
9311d0c4 | 178 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
d5aef594 RL |
179 | goto err; |
180 | } | |
181 | ||
182 | params[0] = | |
183 | OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, | |
184 | aid, sizeof(aid)); | |
185 | params[1] = OSSL_PARAM_construct_end(); | |
186 | ||
187 | if (EVP_PKEY_CTX_get_params(pctx, params) <= 0) | |
188 | goto err; | |
189 | ||
190 | if ((aid_len = params[0].return_size) == 0) { | |
9311d0c4 | 191 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
d5aef594 RL |
192 | goto err; |
193 | } | |
194 | ||
195 | if (algor1 != NULL) { | |
196 | const unsigned char *pp = aid; | |
197 | ||
198 | if (d2i_X509_ALGOR(&algor1, &pp, aid_len) == NULL) { | |
9311d0c4 | 199 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
d5aef594 RL |
200 | goto err; |
201 | } | |
202 | } | |
203 | ||
204 | if (algor2 != NULL) { | |
205 | const unsigned char *pp = aid; | |
206 | ||
207 | if (d2i_X509_ALGOR(&algor2, &pp, aid_len) == NULL) { | |
9311d0c4 | 208 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
d5aef594 RL |
209 | goto err; |
210 | } | |
211 | } | |
03919683 | 212 | |
d5aef594 RL |
213 | rv = 3; |
214 | } else if (pkey->ameth->item_sign) { | |
ded346fa | 215 | rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature); |
0f113f3e MC |
216 | if (rv == 1) |
217 | outl = signature->length; | |
50e735f9 MC |
218 | /*- |
219 | * Return value meanings: | |
220 | * <=0: error. | |
221 | * 1: method does everything. | |
222 | * 2: carry on as normal. | |
223 | * 3: ASN1 method sets algorithm identifiers: just sign. | |
224 | */ | |
0f113f3e | 225 | if (rv <= 0) |
9311d0c4 | 226 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
0f113f3e MC |
227 | if (rv <= 1) |
228 | goto err; | |
7dd6de9f | 229 | } else { |
0f113f3e | 230 | rv = 2; |
7dd6de9f | 231 | } |
03919683 | 232 | |
0f113f3e | 233 | if (rv == 2) { |
ded346fa | 234 | if (md == NULL) { |
9311d0c4 | 235 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
7dd6de9f DSH |
236 | goto err; |
237 | } | |
bc42bd62 PY |
238 | |
239 | pkey_id = | |
240 | #ifndef OPENSSL_NO_SM2 | |
ed576acd | 241 | EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 : |
bc42bd62 | 242 | #endif |
d5aef594 | 243 | pkey->ameth->pkey_id; |
bc42bd62 | 244 | |
ded346fa | 245 | if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) { |
9311d0c4 | 246 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
2c91b3f5 | 247 | goto err; |
7f572e95 | 248 | } |
ee1d9ec0 | 249 | |
04bc3c12 DDO |
250 | paramtype = pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL ? |
251 | V_ASN1_NULL : V_ASN1_UNDEF; | |
252 | if (algor1 != NULL | |
253 | && !X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL)) | |
254 | goto err; | |
255 | if (algor2 != NULL | |
256 | && !X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL)) | |
257 | goto err; | |
0f113f3e | 258 | } |
ee1d9ec0 | 259 | |
ded346fa | 260 | buf_len = ASN1_item_i2d(data, &buf_in, it); |
da84249b F |
261 | if (buf_len <= 0) { |
262 | outl = 0; | |
9311d0c4 | 263 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
da84249b F |
264 | goto err; |
265 | } | |
266 | inl = buf_len; | |
9767a3dc RL |
267 | if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) { |
268 | outl = 0; | |
9311d0c4 | 269 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
9767a3dc RL |
270 | goto err; |
271 | } | |
272 | outl = outll; | |
da84249b F |
273 | buf_out = OPENSSL_malloc(outll); |
274 | if (buf_in == NULL || buf_out == NULL) { | |
0f113f3e | 275 | outl = 0; |
9311d0c4 | 276 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
277 | goto err; |
278 | } | |
09ab755c | 279 | |
75394189 | 280 | if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) { |
0f113f3e | 281 | outl = 0; |
9311d0c4 | 282 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
0f113f3e MC |
283 | goto err; |
284 | } | |
b548a1f1 | 285 | OPENSSL_free(signature->data); |
0f113f3e MC |
286 | signature->data = buf_out; |
287 | buf_out = NULL; | |
288 | signature->length = outl; | |
289 | /* | |
290 | * In the interests of compatibility, I'll make sure that the bit string | |
291 | * has a 'not-used bits' value of 0 | |
292 | */ | |
293 | signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
294 | signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
295 | err: | |
da84249b | 296 | OPENSSL_clear_free((char *)buf_in, inl); |
4b45c6e5 | 297 | OPENSSL_clear_free((char *)buf_out, outll); |
26a7d938 | 298 | return outl; |
0f113f3e | 299 | } |