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