Path: blob/main/contrib/bearssl/src/ssl/ssl_ccert_single_rsa.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_rsa_context *zc;54int x;5556(void)cc;57zc = (br_ssl_client_certificate_rsa_context *)pctx;58x = br_ssl_choose_hash((unsigned)auth_types);59if (x == 0 && (auth_types & 1) == 0) {60memset(choices, 0, sizeof *choices);61}62choices->auth_type = BR_AUTH_RSA;63choices->hash_id = x;64choices->chain = zc->chain;65choices->chain_len = zc->chain_len;66}6768/*69* OID for hash functions in RSA signatures.70*/71static const unsigned char HASH_OID_SHA1[] = {720x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A73};7475static const unsigned char HASH_OID_SHA224[] = {760x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0477};7879static const unsigned char HASH_OID_SHA256[] = {800x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0181};8283static const unsigned char HASH_OID_SHA384[] = {840x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0285};8687static const unsigned char HASH_OID_SHA512[] = {880x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0389};9091static const unsigned char *HASH_OID[] = {92HASH_OID_SHA1,93HASH_OID_SHA224,94HASH_OID_SHA256,95HASH_OID_SHA384,96HASH_OID_SHA51297};9899static size_t100cc_do_sign(const br_ssl_client_certificate_class **pctx,101int hash_id, size_t hv_len, unsigned char *data, size_t len)102{103br_ssl_client_certificate_rsa_context *zc;104unsigned char hv[64];105const unsigned char *hash_oid;106size_t sig_len;107108zc = (br_ssl_client_certificate_rsa_context *)pctx;109memcpy(hv, data, hv_len);110if (hash_id == 0) {111hash_oid = NULL;112} else if (hash_id >= 2 && hash_id <= 6) {113hash_oid = HASH_OID[hash_id - 2];114} else {115return 0;116}117sig_len = (zc->sk->n_bitlen + 7) >> 3;118if (len < sig_len) {119return 0;120}121return zc->irsasign(hash_oid, hv, hv_len, zc->sk, data) ? sig_len : 0;122}123124static const br_ssl_client_certificate_class ccert_vtable = {125sizeof(br_ssl_client_certificate_rsa_context),126cc_none0, /* start_name_list */127cc_none1, /* start_name */128cc_none2, /* append_name */129cc_none0, /* end_name */130cc_none0, /* end_name_list */131cc_choose,1320,133cc_do_sign134};135136/* see bearssl_ssl.h */137void138br_ssl_client_set_single_rsa(br_ssl_client_context *cc,139const br_x509_certificate *chain, size_t chain_len,140const br_rsa_private_key *sk, br_rsa_pkcs1_sign irsasign)141{142cc->client_auth.single_rsa.vtable = &ccert_vtable;143cc->client_auth.single_rsa.chain = chain;144cc->client_auth.single_rsa.chain_len = chain_len;145cc->client_auth.single_rsa.sk = sk;146cc->client_auth.single_rsa.irsasign = irsasign;147cc->client_auth_vtable = &cc->client_auth.single_rsa.vtable;148}149150151