/src/openssl/crypto/asn1/a_sign.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/asn1_int.h" |
22 | | #include "internal/evp_int.h" |
23 | | |
24 | | #ifndef NO_ASN1_OLD |
25 | | |
26 | | int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2, |
27 | | ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey, |
28 | | const EVP_MD *type) |
29 | 0 | { |
30 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
31 | 0 | unsigned char *p, *buf_in = NULL, *buf_out = NULL; |
32 | 0 | int i, inl = 0, outl = 0, outll = 0; |
33 | 0 | X509_ALGOR *a; |
34 | 0 |
|
35 | 0 | if (ctx == NULL) { |
36 | 0 | ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE); |
37 | 0 | goto err; |
38 | 0 | } |
39 | 0 | for (i = 0; i < 2; i++) { |
40 | 0 | if (i == 0) |
41 | 0 | a = algor1; |
42 | 0 | else |
43 | 0 | a = algor2; |
44 | 0 | if (a == NULL) |
45 | 0 | continue; |
46 | 0 | if (type->pkey_type == NID_dsaWithSHA1) { |
47 | 0 | /* |
48 | 0 | * special case: RFC 2459 tells us to omit 'parameters' with |
49 | 0 | * id-dsa-with-sha1 |
50 | 0 | */ |
51 | 0 | ASN1_TYPE_free(a->parameter); |
52 | 0 | a->parameter = NULL; |
53 | 0 | } else if ((a->parameter == NULL) || |
54 | 0 | (a->parameter->type != V_ASN1_NULL)) { |
55 | 0 | ASN1_TYPE_free(a->parameter); |
56 | 0 | if ((a->parameter = ASN1_TYPE_new()) == NULL) |
57 | 0 | goto err; |
58 | 0 | a->parameter->type = V_ASN1_NULL; |
59 | 0 | } |
60 | 0 | ASN1_OBJECT_free(a->algorithm); |
61 | 0 | a->algorithm = OBJ_nid2obj(type->pkey_type); |
62 | 0 | if (a->algorithm == NULL) { |
63 | 0 | ASN1err(ASN1_F_ASN1_SIGN, ASN1_R_UNKNOWN_OBJECT_TYPE); |
64 | 0 | goto err; |
65 | 0 | } |
66 | 0 | if (a->algorithm->length == 0) { |
67 | 0 | ASN1err(ASN1_F_ASN1_SIGN, |
68 | 0 | ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); |
69 | 0 | goto err; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | inl = i2d(data, NULL); |
73 | 0 | buf_in = OPENSSL_malloc((unsigned int)inl); |
74 | 0 | outll = outl = EVP_PKEY_size(pkey); |
75 | 0 | buf_out = OPENSSL_malloc((unsigned int)outl); |
76 | 0 | if ((buf_in == NULL) || (buf_out == NULL)) { |
77 | 0 | outl = 0; |
78 | 0 | ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE); |
79 | 0 | goto err; |
80 | 0 | } |
81 | 0 | p = buf_in; |
82 | 0 |
|
83 | 0 | i2d(data, &p); |
84 | 0 | if (!EVP_SignInit_ex(ctx, type, NULL) |
85 | 0 | || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl) |
86 | 0 | || !EVP_SignFinal(ctx, (unsigned char *)buf_out, |
87 | 0 | (unsigned int *)&outl, pkey)) { |
88 | 0 | outl = 0; |
89 | 0 | ASN1err(ASN1_F_ASN1_SIGN, ERR_R_EVP_LIB); |
90 | 0 | goto err; |
91 | 0 | } |
92 | 0 | OPENSSL_free(signature->data); |
93 | 0 | signature->data = buf_out; |
94 | 0 | buf_out = NULL; |
95 | 0 | signature->length = outl; |
96 | 0 | /* |
97 | 0 | * In the interests of compatibility, I'll make sure that the bit string |
98 | 0 | * has a 'not-used bits' value of 0 |
99 | 0 | */ |
100 | 0 | signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
101 | 0 | signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
102 | 0 | err: |
103 | 0 | EVP_MD_CTX_free(ctx); |
104 | 0 | OPENSSL_clear_free((char *)buf_in, (unsigned int)inl); |
105 | 0 | OPENSSL_clear_free((char *)buf_out, outll); |
106 | 0 | return outl; |
107 | 0 | } |
108 | | |
109 | | #endif |
110 | | |
111 | | int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, |
112 | | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn, |
113 | | EVP_PKEY *pkey, const EVP_MD *type) |
114 | 0 | { |
115 | 0 | int rv; |
116 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
117 | 0 |
|
118 | 0 | if (ctx == NULL) { |
119 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN, ERR_R_MALLOC_FAILURE); |
120 | 0 | return 0; |
121 | 0 | } |
122 | 0 | if (!EVP_DigestSignInit(ctx, NULL, type, NULL, pkey)) { |
123 | 0 | EVP_MD_CTX_free(ctx); |
124 | 0 | return 0; |
125 | 0 | } |
126 | 0 | |
127 | 0 | rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, ctx); |
128 | 0 |
|
129 | 0 | EVP_MD_CTX_free(ctx); |
130 | 0 | return rv; |
131 | 0 | } |
132 | | |
133 | | int ASN1_item_sign_ctx(const ASN1_ITEM *it, |
134 | | X509_ALGOR *algor1, X509_ALGOR *algor2, |
135 | | ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx) |
136 | 0 | { |
137 | 0 | const EVP_MD *type; |
138 | 0 | EVP_PKEY *pkey; |
139 | 0 | unsigned char *buf_in = NULL, *buf_out = NULL; |
140 | 0 | size_t inl = 0, outl = 0, outll = 0; |
141 | 0 | int signid, paramtype; |
142 | 0 | int rv; |
143 | 0 |
|
144 | 0 | type = EVP_MD_CTX_md(ctx); |
145 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx)); |
146 | 0 |
|
147 | 0 | if (pkey == NULL) { |
148 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED); |
149 | 0 | goto err; |
150 | 0 | } |
151 | 0 |
|
152 | 0 | if (pkey->ameth == NULL) { |
153 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
154 | 0 | goto err; |
155 | 0 | } |
156 | 0 |
|
157 | 0 | if (pkey->ameth->item_sign) { |
158 | 0 | rv = pkey->ameth->item_sign(ctx, it, asn, algor1, algor2, signature); |
159 | 0 | if (rv == 1) |
160 | 0 | outl = signature->length; |
161 | 0 | /*- |
162 | 0 | * Return value meanings: |
163 | 0 | * <=0: error. |
164 | 0 | * 1: method does everything. |
165 | 0 | * 2: carry on as normal. |
166 | 0 | * 3: ASN1 method sets algorithm identifiers: just sign. |
167 | 0 | */ |
168 | 0 | if (rv <= 0) |
169 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB); |
170 | 0 | if (rv <= 1) |
171 | 0 | goto err; |
172 | 0 | } else { |
173 | 0 | rv = 2; |
174 | 0 | } |
175 | 0 |
|
176 | 0 | if (rv == 2) { |
177 | 0 | if (type == NULL) { |
178 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED); |
179 | 0 | goto err; |
180 | 0 | } |
181 | 0 | if (!OBJ_find_sigid_by_algs(&signid, |
182 | 0 | EVP_MD_nid(type), |
183 | 0 | pkey->ameth->pkey_id)) { |
184 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, |
185 | 0 | ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
186 | 0 | goto err; |
187 | 0 | } |
188 | 0 |
|
189 | 0 | if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL) |
190 | 0 | paramtype = V_ASN1_NULL; |
191 | 0 | else |
192 | 0 | paramtype = V_ASN1_UNDEF; |
193 | 0 |
|
194 | 0 | if (algor1) |
195 | 0 | X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL); |
196 | 0 | if (algor2) |
197 | 0 | X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL); |
198 | 0 |
|
199 | 0 | } |
200 | 0 |
|
201 | 0 | inl = ASN1_item_i2d(asn, &buf_in, it); |
202 | 0 | outll = outl = EVP_PKEY_size(pkey); |
203 | 0 | buf_out = OPENSSL_malloc((unsigned int)outl); |
204 | 0 | if ((buf_in == NULL) || (buf_out == NULL)) { |
205 | 0 | outl = 0; |
206 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_MALLOC_FAILURE); |
207 | 0 | goto err; |
208 | 0 | } |
209 | 0 |
|
210 | 0 | if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) { |
211 | 0 | outl = 0; |
212 | 0 | ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB); |
213 | 0 | goto err; |
214 | 0 | } |
215 | 0 | OPENSSL_free(signature->data); |
216 | 0 | signature->data = buf_out; |
217 | 0 | buf_out = NULL; |
218 | 0 | signature->length = outl; |
219 | 0 | /* |
220 | 0 | * In the interests of compatibility, I'll make sure that the bit string |
221 | 0 | * has a 'not-used bits' value of 0 |
222 | 0 | */ |
223 | 0 | signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
224 | 0 | signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
225 | 0 | err: |
226 | 0 | OPENSSL_clear_free((char *)buf_in, (unsigned int)inl); |
227 | 0 | OPENSSL_clear_free((char *)buf_out, outll); |
228 | 0 | return outl; |
229 | 0 | } |