Fix: make static ECDH selectable with the br_ssl_client_set_single_ec() helper function.
[BearSSL] / ssl / ssl_single_rsa.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 sr_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_rsa_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_rsa_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 = 0;
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_RSA:
51 if ((pc->allowed_usages & BR_KEYTYPE_KEYX) != 0) {
52 choices->cipher_suite = st[u][0];
53 return 1;
54 }
55 break;
56 case BR_SSLKEYX_ECDHE_RSA:
57 if ((pc->allowed_usages & BR_KEYTYPE_SIGN) != 0
58 && hash_id != 0)
59 {
60 choices->cipher_suite = st[u][0];
61 choices->hash_id = hash_id;
62 return 1;
63 }
64 break;
65 }
66 }
67 return 0;
68 }
69
70 static uint32_t
71 sr_do_keyx(const br_ssl_server_policy_class **pctx,
72 unsigned char *data, size_t len)
73 {
74 br_ssl_server_policy_rsa_context *pc;
75
76 pc = (br_ssl_server_policy_rsa_context *)pctx;
77 return br_rsa_ssl_decrypt(pc->irsacore, pc->sk, data, len);
78 }
79
80 /*
81 * OID for hash functions in RSA signatures.
82 */
83 static const unsigned char HASH_OID_SHA1[] = {
84 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
85 };
86
87 static const unsigned char HASH_OID_SHA224[] = {
88 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
89 };
90
91 static const unsigned char HASH_OID_SHA256[] = {
92 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
93 };
94
95 static const unsigned char HASH_OID_SHA384[] = {
96 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
97 };
98
99 static const unsigned char HASH_OID_SHA512[] = {
100 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
101 };
102
103 static const unsigned char *HASH_OID[] = {
104 HASH_OID_SHA1,
105 HASH_OID_SHA224,
106 HASH_OID_SHA256,
107 HASH_OID_SHA384,
108 HASH_OID_SHA512
109 };
110
111 static size_t
112 sr_do_sign(const br_ssl_server_policy_class **pctx,
113 int hash_id, size_t hv_len, unsigned char *data, size_t len)
114 {
115 br_ssl_server_policy_rsa_context *pc;
116 unsigned char hv[64];
117 size_t sig_len;
118 const unsigned char *hash_oid;
119
120 pc = (br_ssl_server_policy_rsa_context *)pctx;
121 memcpy(hv, data, hv_len);
122 if (hash_id == 0) {
123 hash_oid = NULL;
124 } else if (hash_id >= 2 && hash_id <= 6) {
125 hash_oid = HASH_OID[hash_id - 2];
126 } else {
127 return 0;
128 }
129 sig_len = (pc->sk->n_bitlen + 7) >> 3;
130 if (len < sig_len) {
131 return 0;
132 }
133 return pc->irsasign(hash_oid, hv, hv_len, pc->sk, data) ? sig_len : 0;
134 }
135
136 static const br_ssl_server_policy_class sr_policy_vtable = {
137 sizeof(br_ssl_server_policy_rsa_context),
138 sr_choose,
139 sr_do_keyx,
140 sr_do_sign
141 };
142
143 /* see bearssl_ssl.h */
144 void
145 br_ssl_server_set_single_rsa(br_ssl_server_context *cc,
146 const br_x509_certificate *chain, size_t chain_len,
147 const br_rsa_private_key *sk, unsigned allowed_usages,
148 br_rsa_private irsacore, br_rsa_pkcs1_sign irsasign)
149 {
150 cc->chain_handler.single_rsa.vtable = &sr_policy_vtable;
151 cc->chain_handler.single_rsa.chain = chain;
152 cc->chain_handler.single_rsa.chain_len = chain_len;
153 cc->chain_handler.single_rsa.sk = sk;
154 cc->chain_handler.single_rsa.allowed_usages = allowed_usages;
155 cc->chain_handler.single_rsa.irsacore = irsacore;
156 cc->chain_handler.single_rsa.irsasign = irsasign;
157 cc->policy_vtable = &cc->chain_handler.single_rsa.vtable;
158 }