File Coverage

src/ssl/ssl_ccert_single_ec.c
Criterion Covered Total %
statement 0 54 0.0
branch 0 16 0.0
condition n/a
subroutine n/a
pod n/a
total 0 70 0.0


line stmt bran cond sub pod time code
1             /*
2             * Copyright (c) 2016 Thomas Pornin
3             *
4             * Permission is hereby granted, free of charge, to any person obtaining
5             * a copy of this software and associated documentation files (the
6             * "Software"), to deal in the Software without restriction, including
7             * without limitation the rights to use, copy, modify, merge, publish,
8             * distribute, sublicense, and/or sell copies of the Software, and to
9             * permit persons to whom the Software is furnished to do so, subject to
10             * the following conditions:
11             *
12             * The above copyright notice and this permission notice shall be
13             * included in all copies or substantial portions of the Software.
14             *
15             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16             * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17             * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18             * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19             * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20             * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21             * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22             * SOFTWARE.
23             */
24              
25             #include "inner.h"
26              
27             static void
28 0           cc_none0(const br_ssl_client_certificate_class **pctx)
29             {
30             (void)pctx;
31 0           }
32              
33             static void
34 0           cc_none1(const br_ssl_client_certificate_class **pctx, size_t len)
35             {
36             (void)pctx;
37             (void)len;
38 0           }
39              
40             static void
41 0           cc_none2(const br_ssl_client_certificate_class **pctx,
42             const unsigned char *data, size_t len)
43             {
44             (void)pctx;
45             (void)data;
46             (void)len;
47 0           }
48              
49             static void
50 0           cc_choose(const br_ssl_client_certificate_class **pctx,
51             const br_ssl_client_context *cc, uint32_t auth_types,
52             br_ssl_client_certificate *choices)
53             {
54             br_ssl_client_certificate_ec_context *zc;
55             int x;
56             int scurve;
57              
58 0           zc = (br_ssl_client_certificate_ec_context *)pctx;
59 0           scurve = br_ssl_client_get_server_curve(cc);
60              
61 0 0         if ((zc->allowed_usages & BR_KEYTYPE_KEYX) != 0
62 0 0         && scurve == zc->sk->curve)
63             {
64             int x;
65              
66 0 0         x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;
67 0 0         if (((auth_types >> x) & 1) != 0) {
68 0           choices->auth_type = BR_AUTH_ECDH;
69 0           choices->hash_id = -1;
70 0           choices->chain = zc->chain;
71 0           choices->chain_len = zc->chain_len;
72 0           return;
73             }
74             }
75              
76             /*
77             * For ECDSA authentication, we must choose an appropriate
78             * hash function.
79             */
80 0           x = br_ssl_choose_hash((unsigned)(auth_types >> 8));
81 0 0         if (x == 0 || (zc->allowed_usages & BR_KEYTYPE_SIGN) == 0) {
    0          
82 0           memset(choices, 0, sizeof *choices);
83 0           return;
84             }
85 0           choices->auth_type = BR_AUTH_ECDSA;
86 0           choices->hash_id = x;
87 0           choices->chain = zc->chain;
88 0           choices->chain_len = zc->chain_len;
89             }
90              
91             static uint32_t
92 0           cc_do_keyx(const br_ssl_client_certificate_class **pctx,
93             unsigned char *data, size_t *len)
94             {
95             br_ssl_client_certificate_ec_context *zc;
96             uint32_t r;
97             size_t xoff, xlen;
98              
99 0           zc = (br_ssl_client_certificate_ec_context *)pctx;
100 0           r = zc->iec->mul(data, *len, zc->sk->x, zc->sk->xlen, zc->sk->curve);
101 0           xoff = zc->iec->xoff(zc->sk->curve, &xlen);
102 0           memmove(data, data + xoff, xlen);
103 0           *len = xlen;
104 0           return r;
105             }
106              
107             static size_t
108 0           cc_do_sign(const br_ssl_client_certificate_class **pctx,
109             int hash_id, size_t hv_len, unsigned char *data, size_t len)
110             {
111             br_ssl_client_certificate_ec_context *zc;
112             unsigned char hv[64];
113             const br_hash_class *hc;
114              
115 0           zc = (br_ssl_client_certificate_ec_context *)pctx;
116 0           memcpy(hv, data, hv_len);
117 0           hc = br_multihash_getimpl(zc->mhash, hash_id);
118 0 0         if (hc == NULL) {
119 0           return 0;
120             }
121 0 0         if (len < 139) {
122 0           return 0;
123             }
124 0           return zc->iecdsa(zc->iec, hc, hv, zc->sk, data);
125             }
126              
127             static const br_ssl_client_certificate_class ccert_vtable = {
128             sizeof(br_ssl_client_certificate_ec_context),
129             cc_none0, /* start_name_list */
130             cc_none1, /* start_name */
131             cc_none2, /* append_name */
132             cc_none0, /* end_name */
133             cc_none0, /* end_name_list */
134             cc_choose,
135             cc_do_keyx,
136             cc_do_sign
137             };
138              
139             /* see bearssl_ssl.h */
140             void
141 0           br_ssl_client_set_single_ec(br_ssl_client_context *cc,
142             const br_x509_certificate *chain, size_t chain_len,
143             const br_ec_private_key *sk, unsigned allowed_usages,
144             unsigned cert_issuer_key_type,
145             const br_ec_impl *iec, br_ecdsa_sign iecdsa)
146             {
147 0           cc->client_auth.single_ec.vtable = &ccert_vtable;
148 0           cc->client_auth.single_ec.chain = chain;
149 0           cc->client_auth.single_ec.chain_len = chain_len;
150 0           cc->client_auth.single_ec.sk = sk;
151 0           cc->client_auth.single_ec.allowed_usages = allowed_usages;
152 0           cc->client_auth.single_ec.issuer_key_type = cert_issuer_key_type;
153 0           cc->client_auth.single_ec.mhash = &cc->eng.mhash;
154 0           cc->client_auth.single_ec.iec = iec;
155 0           cc->client_auth.single_ec.iecdsa = iecdsa;
156 0           cc->client_auth_vtable = &cc->client_auth.single_ec.vtable;
157 0           }