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