2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
28 se_choose(const br_ssl_server_policy_class
**pctx
,
29 const br_ssl_server_context
*cc
,
30 br_ssl_server_choices
*choices
)
32 br_ssl_server_policy_ec_context
*pc
;
33 const br_suite_translated
*st
;
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
) {
43 choices
->chain
= pc
->chain
;
44 choices
->chain_len
= pc
->chain_len
;
45 for (u
= 0; u
< st_num
; u
++) {
50 case BR_SSLKEYX_ECDH_RSA
:
51 if ((pc
->allowed_usages
& BR_KEYTYPE_KEYX
) != 0
52 && pc
->cert_issuer_key_type
== BR_KEYTYPE_RSA
)
54 choices
->cipher_suite
= st
[u
][0];
58 case BR_SSLKEYX_ECDH_ECDSA
:
59 if ((pc
->allowed_usages
& BR_KEYTYPE_KEYX
) != 0
60 && pc
->cert_issuer_key_type
== BR_KEYTYPE_EC
)
62 choices
->cipher_suite
= st
[u
][0];
66 case BR_SSLKEYX_ECDHE_ECDSA
:
67 if ((pc
->allowed_usages
& BR_KEYTYPE_SIGN
) != 0
70 choices
->cipher_suite
= st
[u
][0];
71 choices
->hash_id
= hash_id
;
81 se_do_keyx(const br_ssl_server_policy_class
**pctx
,
82 unsigned char *data
, size_t len
)
84 br_ssl_server_policy_ec_context
*pc
;
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
);
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
)
94 br_ssl_server_policy_ec_context
*pc
;
96 const br_hash_class
*hc
;
98 pc
= (br_ssl_server_policy_ec_context
*)pctx
;
99 hc
= br_multihash_getimpl(pc
->mhash
, hash_id
);
103 memcpy(hv
, data
, hv_len
);
107 return pc
->iecdsa(pc
->iec
, hc
, hv
, pc
->sk
, data
);
110 static const br_ssl_server_policy_class se_policy_vtable
= {
111 sizeof(br_ssl_server_policy_ec_context
),
117 /* see bearssl_ssl.h */
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
)
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
;