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 cc_none0(const br_ssl_client_certificate_class
**pctx
)
34 cc_none1(const br_ssl_client_certificate_class
**pctx
, size_t len
)
41 cc_none2(const br_ssl_client_certificate_class
**pctx
,
42 const unsigned char *data
, size_t len
)
50 cc_choose(const br_ssl_client_certificate_class
**pctx
,
51 const br_ssl_client_context
*cc
, uint32_t auth_types
,
52 br_ssl_client_certificate
*choices
)
54 br_ssl_client_certificate_rsa_context
*zc
;
58 zc
= (br_ssl_client_certificate_rsa_context
*)pctx
;
59 x
= br_ssl_choose_hash((unsigned)auth_types
);
60 if (x
== 0 && (auth_types
& 1) == 0) {
61 memset(choices
, 0, sizeof *choices
);
63 choices
->auth_type
= BR_AUTH_RSA
;
65 choices
->chain
= zc
->chain
;
66 choices
->chain_len
= zc
->chain_len
;
70 * OID for hash functions in RSA signatures.
72 static const unsigned char HASH_OID_SHA1
[] = {
73 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
76 static const unsigned char HASH_OID_SHA224
[] = {
77 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
80 static const unsigned char HASH_OID_SHA256
[] = {
81 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
84 static const unsigned char HASH_OID_SHA384
[] = {
85 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
88 static const unsigned char HASH_OID_SHA512
[] = {
89 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
92 static const unsigned char *HASH_OID
[] = {
101 cc_do_sign(const br_ssl_client_certificate_class
**pctx
,
102 int hash_id
, size_t hv_len
, unsigned char *data
, size_t len
)
104 br_ssl_client_certificate_rsa_context
*zc
;
105 unsigned char hv
[64];
106 const unsigned char *hash_oid
;
109 zc
= (br_ssl_client_certificate_rsa_context
*)pctx
;
110 memcpy(hv
, data
, hv_len
);
113 } else if (hash_id
>= 2 && hash_id
<= 6) {
114 hash_oid
= HASH_OID
[hash_id
- 2];
118 sig_len
= (zc
->sk
->n_bitlen
+ 7) >> 3;
122 return zc
->irsasign(hash_oid
, hv
, hv_len
, zc
->sk
, data
) ? sig_len
: 0;
125 static const br_ssl_client_certificate_class ccert_vtable
= {
126 sizeof(br_ssl_client_certificate_rsa_context
),
127 cc_none0
, /* start_name_list */
128 cc_none1
, /* start_name */
129 cc_none2
, /* append_name */
130 cc_none0
, /* end_name */
131 cc_none0
, /* end_name_list */
137 /* see bearssl_ssl.h */
139 br_ssl_client_set_single_rsa(br_ssl_client_context
*cc
,
140 const br_x509_certificate
*chain
, size_t chain_len
,
141 const br_rsa_private_key
*sk
, br_rsa_pkcs1_sign irsasign
)
143 cc
->client_auth
.single_rsa
.vtable
= &ccert_vtable
;
144 cc
->client_auth
.single_rsa
.chain
= chain
;
145 cc
->client_auth
.single_rsa
.chain_len
= chain_len
;
146 cc
->client_auth
.single_rsa
.sk
= sk
;
147 cc
->client_auth
.single_rsa
.irsasign
= irsasign
;
148 cc
->client_auth_vtable
= &cc
->client_auth
.single_rsa
.vtable
;