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