Path: blob/main/contrib/bearssl/src/ssl/ssl_ccert_single_ec.c
39483 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526static void27cc_none0(const br_ssl_client_certificate_class **pctx)28{29(void)pctx;30}3132static void33cc_none1(const br_ssl_client_certificate_class **pctx, size_t len)34{35(void)pctx;36(void)len;37}3839static void40cc_none2(const br_ssl_client_certificate_class **pctx,41const unsigned char *data, size_t len)42{43(void)pctx;44(void)data;45(void)len;46}4748static void49cc_choose(const br_ssl_client_certificate_class **pctx,50const br_ssl_client_context *cc, uint32_t auth_types,51br_ssl_client_certificate *choices)52{53br_ssl_client_certificate_ec_context *zc;54int x;55int scurve;5657zc = (br_ssl_client_certificate_ec_context *)pctx;58scurve = br_ssl_client_get_server_curve(cc);5960if ((zc->allowed_usages & BR_KEYTYPE_KEYX) != 061&& scurve == zc->sk->curve)62{63int x;6465x = (zc->issuer_key_type == BR_KEYTYPE_RSA) ? 16 : 17;66if (((auth_types >> x) & 1) != 0) {67choices->auth_type = BR_AUTH_ECDH;68choices->hash_id = -1;69choices->chain = zc->chain;70choices->chain_len = zc->chain_len;71}72}7374/*75* For ECDSA authentication, we must choose an appropriate76* hash function.77*/78x = br_ssl_choose_hash((unsigned)(auth_types >> 8));79if (x == 0 || (zc->allowed_usages & BR_KEYTYPE_SIGN) == 0) {80memset(choices, 0, sizeof *choices);81return;82}83choices->auth_type = BR_AUTH_ECDSA;84choices->hash_id = x;85choices->chain = zc->chain;86choices->chain_len = zc->chain_len;87}8889static uint32_t90cc_do_keyx(const br_ssl_client_certificate_class **pctx,91unsigned char *data, size_t *len)92{93br_ssl_client_certificate_ec_context *zc;94uint32_t r;95size_t xoff, xlen;9697zc = (br_ssl_client_certificate_ec_context *)pctx;98r = zc->iec->mul(data, *len, zc->sk->x, zc->sk->xlen, zc->sk->curve);99xoff = zc->iec->xoff(zc->sk->curve, &xlen);100memmove(data, data + xoff, xlen);101*len = xlen;102return r;103}104105static size_t106cc_do_sign(const br_ssl_client_certificate_class **pctx,107int hash_id, size_t hv_len, unsigned char *data, size_t len)108{109br_ssl_client_certificate_ec_context *zc;110unsigned char hv[64];111const br_hash_class *hc;112113zc = (br_ssl_client_certificate_ec_context *)pctx;114memcpy(hv, data, hv_len);115hc = br_multihash_getimpl(zc->mhash, hash_id);116if (hc == NULL) {117return 0;118}119if (len < 139) {120return 0;121}122return zc->iecdsa(zc->iec, hc, hv, zc->sk, data);123}124125static const br_ssl_client_certificate_class ccert_vtable = {126sizeof(br_ssl_client_certificate_ec_context),127cc_none0, /* start_name_list */128cc_none1, /* start_name */129cc_none2, /* append_name */130cc_none0, /* end_name */131cc_none0, /* end_name_list */132cc_choose,133cc_do_keyx,134cc_do_sign135};136137/* see bearssl_ssl.h */138void139br_ssl_client_set_single_ec(br_ssl_client_context *cc,140const br_x509_certificate *chain, size_t chain_len,141const br_ec_private_key *sk, unsigned allowed_usages,142unsigned cert_issuer_key_type,143const br_ec_impl *iec, br_ecdsa_sign iecdsa)144{145cc->client_auth.single_ec.vtable = &ccert_vtable;146cc->client_auth.single_ec.chain = chain;147cc->client_auth.single_ec.chain_len = chain_len;148cc->client_auth.single_ec.sk = sk;149cc->client_auth.single_ec.allowed_usages = allowed_usages;150cc->client_auth.single_ec.issuer_key_type = cert_issuer_key_type;151cc->client_auth.single_ec.mhash = &cc->eng.mhash;152cc->client_auth.single_ec.iec = iec;153cc->client_auth.single_ec.iecdsa = iecdsa;154cc->client_auth_vtable = &cc->client_auth.single_ec.vtable;155}156157158