Adapt ASN1_item_sign_ctx() for use with provided keypairs
[openssl.git] / crypto / asn1 / a_sign.c
1 /*
2  * Copyright 1995-2016 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 NO_ASN1_OLD
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         ASN1err(ASN1_F_ASN1_SIGN, 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             ASN1err(ASN1_F_ASN1_SIGN, ASN1_R_UNKNOWN_OBJECT_TYPE);
66             goto err;
67         }
68         if (a->algorithm->length == 0) {
69             ASN1err(ASN1_F_ASN1_SIGN,
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         ASN1err(ASN1_F_ASN1_SIGN, ERR_R_INTERNAL_ERROR);
77         goto err;
78     }
79     inll = (size_t)inl;
80     buf_in = OPENSSL_malloc(inll);
81     outll = outl = EVP_PKEY_size(pkey);
82     buf_out = OPENSSL_malloc(outll);
83     if (buf_in == NULL || buf_out == NULL) {
84         outl = 0;
85         ASN1err(ASN1_F_ASN1_SIGN, 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         ASN1err(ASN1_F_ASN1_SIGN, ERR_R_EVP_LIB);
97         goto err;
98     }
99     OPENSSL_free(signature->data);
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:
110     EVP_MD_CTX_free(ctx);
111     OPENSSL_clear_free((char *)buf_in, inll);
112     OPENSSL_clear_free((char *)buf_out, outll);
113     return outl;
114 }
115
116 #endif
117
118 int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
119                    X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn,
120                    EVP_PKEY *pkey, const EVP_MD *type)
121 {
122     int rv;
123     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
124
125     if (ctx == NULL) {
126         ASN1err(ASN1_F_ASN1_ITEM_SIGN, ERR_R_MALLOC_FAILURE);
127         return 0;
128     }
129     if (!EVP_DigestSignInit(ctx, NULL, type, NULL, pkey)) {
130         EVP_MD_CTX_free(ctx);
131         return 0;
132     }
133
134     rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, ctx);
135
136     EVP_MD_CTX_free(ctx);
137     return rv;
138 }
139
140 int ASN1_item_sign_ctx(const ASN1_ITEM *it,
141                        X509_ALGOR *algor1, X509_ALGOR *algor2,
142                        ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx)
143 {
144     const EVP_MD *type;
145     EVP_PKEY *pkey;
146     unsigned char *buf_in = NULL, *buf_out = NULL;
147     size_t inl = 0, outl = 0, outll = 0;
148     int signid, paramtype, buf_len = 0;
149     int rv, pkey_id;
150
151     type = EVP_MD_CTX_md(ctx);
152     pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
153
154     if (pkey == NULL) {
155         ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
156         goto err;
157     }
158
159     if (pkey->ameth == NULL) {
160         EVP_PKEY_CTX *pctx = EVP_MD_CTX_pkey_ctx(ctx);
161         OSSL_PARAM params[2];
162         unsigned char aid[128];
163         size_t aid_len = 0;
164
165         if (pctx == NULL
166             || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
167             ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
168             goto err;
169         }
170
171         params[0] =
172             OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
173                                               aid, sizeof(aid));
174         params[1] = OSSL_PARAM_construct_end();
175
176         if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
177             goto err;
178
179         if ((aid_len = params[0].return_size) == 0) {
180             ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
181                     ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
182             goto err;
183         }
184
185         if (algor1 != NULL) {
186             const unsigned char *pp = aid;
187
188             if (d2i_X509_ALGOR(&algor1, &pp, aid_len) == NULL) {
189                 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_INTERNAL_ERROR);
190                 goto err;
191             }
192         }
193
194         if (algor2 != NULL) {
195             const unsigned char *pp = aid;
196
197             if (d2i_X509_ALGOR(&algor2, &pp, aid_len) == NULL) {
198                 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_INTERNAL_ERROR);
199                 goto err;
200             }
201         }
202
203         rv = 3;
204     } else if (pkey->ameth->item_sign) {
205         rv = pkey->ameth->item_sign(ctx, it, asn, algor1, algor2, signature);
206         if (rv == 1)
207             outl = signature->length;
208         /*-
209          * Return value meanings:
210          * <=0: error.
211          *   1: method does everything.
212          *   2: carry on as normal.
213          *   3: ASN1 method sets algorithm identifiers: just sign.
214          */
215         if (rv <= 0)
216             ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
217         if (rv <= 1)
218             goto err;
219     } else {
220         rv = 2;
221     }
222
223     if (rv == 2) {
224         if (type == NULL) {
225             ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
226             goto err;
227         }
228
229         pkey_id =
230 #ifndef OPENSSL_NO_SM2
231             EVP_PKEY_id(pkey) == NID_sm2 ? NID_sm2 :
232 #endif
233             pkey->ameth->pkey_id;
234
235         if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(type), pkey_id)) {
236             ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
237                     ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
238             goto err;
239         }
240
241         if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
242             paramtype = V_ASN1_NULL;
243         else
244             paramtype = V_ASN1_UNDEF;
245
246         if (algor1)
247             X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
248         if (algor2)
249             X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
250
251     }
252
253     buf_len = ASN1_item_i2d(asn, &buf_in, it);
254     if (buf_len <= 0) {
255         outl = 0;
256         ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, 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         ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, 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         ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_MALLOC_FAILURE);
270         goto err;
271     }
272
273     if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
274         outl = 0;
275         ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
276         goto err;
277     }
278     OPENSSL_free(signature->data);
279     signature->data = buf_out;
280     buf_out = NULL;
281     signature->length = outl;
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     signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
287     signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
288  err:
289     OPENSSL_clear_free((char *)buf_in, inl);
290     OPENSSL_clear_free((char *)buf_out, outll);
291     return outl;
292 }