Removed needless variable shadowing (suggested by Doug Hogan).
[BearSSL] / src / ssl / ssl_single_ec.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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 int
28 se_choose(const br_ssl_server_policy_class **pctx,
29 const br_ssl_server_context *cc,
30 br_ssl_server_choices *choices)
31 {
32 br_ssl_server_policy_ec_context *pc;
33 const br_suite_translated *st;
34 size_t u, st_num;
35 int hash_id;
36
37 pc = (br_ssl_server_policy_ec_context *)pctx;
38 st = br_ssl_server_get_client_suites(cc, &st_num);
39 hash_id = br_ssl_choose_hash(br_ssl_server_get_client_hashes(cc));
40 choices->chain = pc->chain;
41 choices->chain_len = pc->chain_len;
42 for (u = 0; u < st_num; u ++) {
43 unsigned tt;
44
45 tt = st[u][1];
46 switch (tt >> 12) {
47 case BR_SSLKEYX_ECDH_RSA:
48 if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0
49 && pc->cert_issuer_key_type == BR_KEYTYPE_RSA)
50 {
51 choices->cipher_suite = st[u][0];
52 return 1;
53 }
54 break;
55 case BR_SSLKEYX_ECDH_ECDSA:
56 if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0
57 && pc->cert_issuer_key_type == BR_KEYTYPE_EC)
58 {
59 choices->cipher_suite = st[u][0];
60 return 1;
61 }
62 break;
63 case BR_SSLKEYX_ECDHE_ECDSA:
64 if ((pc->allowed_usages & BR_KEYTYPE_SIGN) != 0
65 && hash_id != 0)
66 {
67 choices->cipher_suite = st[u][0];
68 choices->hash_id = hash_id;
69 return 1;
70 }
71 break;
72 }
73 }
74 return 0;
75 }
76
77 static uint32_t
78 se_do_keyx(const br_ssl_server_policy_class **pctx,
79 unsigned char *data, size_t len)
80 {
81 br_ssl_server_policy_ec_context *pc;
82
83 pc = (br_ssl_server_policy_ec_context *)pctx;
84 return pc->iec->mul(data, len, pc->sk->x, pc->sk->xlen, pc->sk->curve);
85 }
86
87 static size_t
88 se_do_sign(const br_ssl_server_policy_class **pctx,
89 int hash_id, size_t hv_len, unsigned char *data, size_t len)
90 {
91 br_ssl_server_policy_ec_context *pc;
92 unsigned char hv[64];
93 const br_hash_class *hc;
94
95 pc = (br_ssl_server_policy_ec_context *)pctx;
96 hc = br_multihash_getimpl(pc->mhash, hash_id);
97 if (hc == NULL) {
98 return 0;
99 }
100 memcpy(hv, data, hv_len);
101 if (len < 139) {
102 return 0;
103 }
104 return pc->iecdsa(pc->iec, hc, hv, pc->sk, data);
105 }
106
107 static const br_ssl_server_policy_class se_policy_vtable = {
108 sizeof(br_ssl_server_policy_ec_context),
109 se_choose,
110 se_do_keyx,
111 se_do_sign
112 };
113
114 /* see bearssl_ssl.h */
115 void
116 br_ssl_server_set_single_ec(br_ssl_server_context *cc,
117 const br_x509_certificate *chain, size_t chain_len,
118 const br_ec_private_key *sk, unsigned allowed_usages,
119 unsigned cert_issuer_key_type,
120 const br_ec_impl *iec, br_ecdsa_sign iecdsa)
121 {
122 cc->chain_handler.single_ec.vtable = &se_policy_vtable;
123 cc->chain_handler.single_ec.chain = chain;
124 cc->chain_handler.single_ec.chain_len = chain_len;
125 cc->chain_handler.single_ec.sk = sk;
126 cc->chain_handler.single_ec.allowed_usages = allowed_usages;
127 cc->chain_handler.single_ec.cert_issuer_key_type = cert_issuer_key_type;
128 cc->chain_handler.single_ec.mhash = &cc->eng.mhash;
129 cc->chain_handler.single_ec.iec = iec;
130 cc->chain_handler.single_ec.iecdsa = iecdsa;
131 cc->policy_vtable = &cc->chain_handler.single_ec.vtable;
132 }