Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/evp/m_sigver.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||
2 | * Copyright 2006-2018 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 "internal/cryptlib.h" | - | ||||||||||||
12 | #include <openssl/evp.h> | - | ||||||||||||
13 | #include <openssl/objects.h> | - | ||||||||||||
14 | #include <openssl/x509.h> | - | ||||||||||||
15 | #include "internal/evp_int.h" | - | ||||||||||||
16 | #include "evp_locl.h" | - | ||||||||||||
17 | - | |||||||||||||
18 | static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen) | - | ||||||||||||
19 | { | - | ||||||||||||
20 | EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED); | - | ||||||||||||
21 | return 0; executed 4 times by 1 test: return 0; Executed by:
| 4 | ||||||||||||
22 | } | - | ||||||||||||
23 | - | |||||||||||||
24 | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | - | ||||||||||||
25 | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey, | - | ||||||||||||
26 | int ver) | - | ||||||||||||
27 | { | - | ||||||||||||
28 | if (ctx->pctx == NULL)
| 3-24489 | ||||||||||||
29 | ctx->pctx = EVP_PKEY_CTX_new(pkey, e); executed 24489 times by 1 test: ctx->pctx = EVP_PKEY_CTX_new(pkey, e); Executed by:
| 24489 | ||||||||||||
30 | if (ctx->pctx == NULL)
| 0-24492 | ||||||||||||
31 | return 0; never executed: return 0; | 0 | ||||||||||||
32 | - | |||||||||||||
33 | if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
| 122-24370 | ||||||||||||
34 | - | |||||||||||||
35 | if (type == NULL) {
| 2-24368 | ||||||||||||
36 | int def_nid; | - | ||||||||||||
37 | if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
| 0-2 | ||||||||||||
38 | type = EVP_get_digestbynid(def_nid); executed 2 times by 1 test: type = EVP_get_digestbyname(OBJ_nid2sn(def_nid)); Executed by:
| 2 | ||||||||||||
39 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
40 | - | |||||||||||||
41 | if (type == NULL) {
| 0-24370 | ||||||||||||
42 | EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST); | - | ||||||||||||
43 | return 0; never executed: return 0; | 0 | ||||||||||||
44 | } | - | ||||||||||||
45 | } executed 24370 times by 1 test: end of block Executed by:
| 24370 | ||||||||||||
46 | - | |||||||||||||
47 | if (ver) {
| 3194-21298 | ||||||||||||
48 | if (ctx->pctx->pmeth->verifyctx_init) {
| 0-3194 | ||||||||||||
49 | if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
| 0 | ||||||||||||
50 | return 0; never executed: return 0; | 0 | ||||||||||||
51 | ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX; | - | ||||||||||||
52 | } else if (ctx->pctx->pmeth->digestverify != 0) { never executed: end of block
| 0-3173 | ||||||||||||
53 | ctx->pctx->operation = EVP_PKEY_OP_VERIFY; | - | ||||||||||||
54 | ctx->update = update; | - | ||||||||||||
55 | } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) { executed 21 times by 1 test: end of block Executed by:
| 0-3173 | ||||||||||||
56 | return 0; never executed: return 0; | 0 | ||||||||||||
57 | } | - | ||||||||||||
58 | } else { executed 3194 times by 1 test: end of block Executed by:
| 3194 | ||||||||||||
59 | if (ctx->pctx->pmeth->signctx_init) {
| 2020-19278 | ||||||||||||
60 | if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
| 0-19278 | ||||||||||||
61 | return 0; never executed: return 0; | 0 | ||||||||||||
62 | ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX; | - | ||||||||||||
63 | } else if (ctx->pctx->pmeth->digestsign != 0) { executed 19278 times by 1 test: end of block Executed by:
| 29-19278 | ||||||||||||
64 | ctx->pctx->operation = EVP_PKEY_OP_SIGN; | - | ||||||||||||
65 | ctx->update = update; | - | ||||||||||||
66 | } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) { executed 29 times by 1 test: end of block Executed by:
| 0-1991 | ||||||||||||
67 | return 0; never executed: return 0; | 0 | ||||||||||||
68 | } | - | ||||||||||||
69 | } executed 21298 times by 1 test: end of block Executed by:
| 21298 | ||||||||||||
70 | if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
| 3-24489 | ||||||||||||
71 | return 0; executed 3 times by 1 test: return 0; Executed by:
| 3 | ||||||||||||
72 | if (pctx)
| 3930-20559 | ||||||||||||
73 | *pctx = ctx->pctx; executed 3930 times by 1 test: *pctx = ctx->pctx; Executed by:
| 3930 | ||||||||||||
74 | if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
| 120-24369 | ||||||||||||
75 | return 1; executed 120 times by 1 test: return 1; Executed by:
| 120 | ||||||||||||
76 | if (!EVP_DigestInit_ex(ctx, type, e))
| 0-24369 | ||||||||||||
77 | return 0; never executed: return 0; | 0 | ||||||||||||
78 | /* | - | ||||||||||||
79 | * This indicates the current algorithm requires | - | ||||||||||||
80 | * special treatment before hashing the tbs-message. | - | ||||||||||||
81 | */ | - | ||||||||||||
82 | if (ctx->pctx->pmeth->digest_custom != NULL)
| 3-24366 | ||||||||||||
83 | return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx); executed 3 times by 1 test: return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx); Executed by:
| 3 | ||||||||||||
84 | - | |||||||||||||
85 | return 1; executed 24366 times by 1 test: return 1; Executed by:
| 24366 | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | - | ||||||||||||
89 | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) | - | ||||||||||||
90 | { | - | ||||||||||||
91 | return do_sigver_init(ctx, pctx, type, e, pkey, 0); executed 21298 times by 1 test: return do_sigver_init(ctx, pctx, type, e, pkey, 0); Executed by:
| 21298 | ||||||||||||
92 | } | - | ||||||||||||
93 | - | |||||||||||||
94 | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | - | ||||||||||||
95 | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) | - | ||||||||||||
96 | { | - | ||||||||||||
97 | return do_sigver_init(ctx, pctx, type, e, pkey, 1); executed 3194 times by 1 test: return do_sigver_init(ctx, pctx, type, e, pkey, 1); Executed by:
| 3194 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, | - | ||||||||||||
101 | size_t *siglen) | - | ||||||||||||
102 | { | - | ||||||||||||
103 | int sctx = 0, r = 0; | - | ||||||||||||
104 | EVP_PKEY_CTX *pctx = ctx->pctx; | - | ||||||||||||
105 | if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
| 144-83271 | ||||||||||||
106 | if (!sigret)
| 72 | ||||||||||||
107 | return pctx->pmeth->signctx(pctx, sigret, siglen, ctx); executed 72 times by 1 test: return pctx->pmeth->signctx(pctx, sigret, siglen, ctx); Executed by:
| 72 | ||||||||||||
108 | if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
| 0-72 | ||||||||||||
109 | r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); never executed: r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); | 0 | ||||||||||||
110 | else { | - | ||||||||||||
111 | EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx); | - | ||||||||||||
112 | if (!dctx)
| 0-72 | ||||||||||||
113 | return 0; never executed: return 0; | 0 | ||||||||||||
114 | r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx); | - | ||||||||||||
115 | EVP_PKEY_CTX_free(dctx); | - | ||||||||||||
116 | } executed 72 times by 1 test: end of block Executed by:
| 72 | ||||||||||||
117 | return r; executed 72 times by 1 test: return r; Executed by:
| 72 | ||||||||||||
118 | } | - | ||||||||||||
119 | if (pctx->pmeth->signctx)
| 2070-81201 | ||||||||||||
120 | sctx = 1; executed 81201 times by 1 test: sctx = 1; Executed by:
| 81201 | ||||||||||||
121 | else | - | ||||||||||||
122 | sctx = 0; executed 2070 times by 1 test: sctx = 0; Executed by:
| 2070 | ||||||||||||
123 | if (sigret) {
| 109-83162 | ||||||||||||
124 | unsigned char md[EVP_MAX_MD_SIZE]; | - | ||||||||||||
125 | unsigned int mdlen = 0; | - | ||||||||||||
126 | if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
| 0-83162 | ||||||||||||
127 | if (sctx)
| 0 | ||||||||||||
128 | r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx); never executed: r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx); | 0 | ||||||||||||
129 | else | - | ||||||||||||
130 | r = EVP_DigestFinal_ex(ctx, md, &mdlen); never executed: r = EVP_DigestFinal_ex(ctx, md, &mdlen); | 0 | ||||||||||||
131 | } else { | - | ||||||||||||
132 | EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); | - | ||||||||||||
133 | if (tmp_ctx == NULL)
| 0-83162 | ||||||||||||
134 | return 0; never executed: return 0; | 0 | ||||||||||||
135 | if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
| 0-83162 | ||||||||||||
136 | EVP_MD_CTX_free(tmp_ctx); | - | ||||||||||||
137 | return 0; never executed: return 0; | 0 | ||||||||||||
138 | } | - | ||||||||||||
139 | if (sctx)
| 1991-81171 | ||||||||||||
140 | r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx, executed 81171 times by 1 test: r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx, sigret, siglen, tmp_ctx); Executed by:
| 81171 | ||||||||||||
141 | sigret, siglen, tmp_ctx); executed 81171 times by 1 test: r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx, sigret, siglen, tmp_ctx); Executed by:
| 81171 | ||||||||||||
142 | else | - | ||||||||||||
143 | r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); executed 1991 times by 1 test: r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); Executed by:
| 1991 | ||||||||||||
144 | EVP_MD_CTX_free(tmp_ctx); | - | ||||||||||||
145 | } executed 83162 times by 1 test: end of block Executed by:
| 83162 | ||||||||||||
146 | if (sctx || !r)
| 0-81171 | ||||||||||||
147 | return r; executed 81171 times by 1 test: return r; Executed by:
| 81171 | ||||||||||||
148 | if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
| 2-1989 | ||||||||||||
149 | return 0; executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||
150 | } else { executed 1989 times by 1 test: end of block Executed by:
| 1989 | ||||||||||||
151 | if (sctx) {
| 30-79 | ||||||||||||
152 | if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
| 0-30 | ||||||||||||
153 | return 0; never executed: return 0; | 0 | ||||||||||||
154 | } else { executed 30 times by 1 test: end of block Executed by:
| 30 | ||||||||||||
155 | int s = EVP_MD_size(ctx->digest); | - | ||||||||||||
156 | if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
| 0-79 | ||||||||||||
157 | return 0; never executed: return 0; | 0 | ||||||||||||
158 | } executed 79 times by 1 test: end of block Executed by:
| 79 | ||||||||||||
159 | } | - | ||||||||||||
160 | return 1; executed 2098 times by 1 test: return 1; Executed by:
| 2098 | ||||||||||||
161 | } | - | ||||||||||||
162 | - | |||||||||||||
163 | int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen, | - | ||||||||||||
164 | const unsigned char *tbs, size_t tbslen) | - | ||||||||||||
165 | { | - | ||||||||||||
166 | if (ctx->pctx->pmeth->digestsign != NULL)
| 40-1914 | ||||||||||||
167 | return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen); executed 40 times by 1 test: return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen); Executed by:
| 40 | ||||||||||||
168 | if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
| 0-1913 | ||||||||||||
169 | return 0; never executed: return 0; | 0 | ||||||||||||
170 | return EVP_DigestSignFinal(ctx, sigret, siglen); executed 1914 times by 1 test: return EVP_DigestSignFinal(ctx, sigret, siglen); Executed by:
| 1914 | ||||||||||||
171 | } | - | ||||||||||||
172 | - | |||||||||||||
173 | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, | - | ||||||||||||
174 | size_t siglen) | - | ||||||||||||
175 | { | - | ||||||||||||
176 | unsigned char md[EVP_MAX_MD_SIZE]; | - | ||||||||||||
177 | int r = 0; | - | ||||||||||||
178 | unsigned int mdlen = 0; | - | ||||||||||||
179 | int vctx = 0; | - | ||||||||||||
180 | - | |||||||||||||
181 | if (ctx->pctx->pmeth->verifyctx)
| 0-3170 | ||||||||||||
182 | vctx = 1; never executed: vctx = 1; | 0 | ||||||||||||
183 | else | - | ||||||||||||
184 | vctx = 0; executed 3170 times by 1 test: vctx = 0; Executed by:
| 3170 | ||||||||||||
185 | if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
| 0-3170 | ||||||||||||
186 | if (vctx)
| 0 | ||||||||||||
187 | r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx); never executed: r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx); | 0 | ||||||||||||
188 | else | - | ||||||||||||
189 | r = EVP_DigestFinal_ex(ctx, md, &mdlen); never executed: r = EVP_DigestFinal_ex(ctx, md, &mdlen); | 0 | ||||||||||||
190 | } else { | - | ||||||||||||
191 | EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); | - | ||||||||||||
192 | if (tmp_ctx == NULL)
| 0-3170 | ||||||||||||
193 | return -1; never executed: return -1; | 0 | ||||||||||||
194 | if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
| 0-3170 | ||||||||||||
195 | EVP_MD_CTX_free(tmp_ctx); | - | ||||||||||||
196 | return -1; never executed: return -1; | 0 | ||||||||||||
197 | } | - | ||||||||||||
198 | if (vctx)
| 0-3170 | ||||||||||||
199 | r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, never executed: r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, sig, siglen, tmp_ctx); | 0 | ||||||||||||
200 | sig, siglen, tmp_ctx); never executed: r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, sig, siglen, tmp_ctx); | 0 | ||||||||||||
201 | else | - | ||||||||||||
202 | r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); executed 3170 times by 1 test: r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); Executed by:
| 3170 | ||||||||||||
203 | EVP_MD_CTX_free(tmp_ctx); | - | ||||||||||||
204 | } executed 3170 times by 1 test: end of block Executed by:
| 3170 | ||||||||||||
205 | if (vctx || !r)
| 0-3170 | ||||||||||||
206 | return r; never executed: return r; | 0 | ||||||||||||
207 | return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen); executed 3170 times by 1 test: return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen); Executed by:
| 3170 | ||||||||||||
208 | } | - | ||||||||||||
209 | - | |||||||||||||
210 | int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, | - | ||||||||||||
211 | size_t siglen, const unsigned char *tbs, size_t tbslen) | - | ||||||||||||
212 | { | - | ||||||||||||
213 | if (ctx->pctx->pmeth->digestverify != NULL)
| 19-3098 | ||||||||||||
214 | return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen); executed 19 times by 1 test: return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen); Executed by:
| 19 | ||||||||||||
215 | if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
| 0-3098 | ||||||||||||
216 | return -1; never executed: return -1; | 0 | ||||||||||||
217 | return EVP_DigestVerifyFinal(ctx, sigret, siglen); executed 3098 times by 1 test: return EVP_DigestVerifyFinal(ctx, sigret, siglen); Executed by:
| 3098 | ||||||||||||
218 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |