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