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