Skip to content

Commit 436c232

Browse files
committed
Add _ecVrfy and _ecSign
Add _ecVrfy and _ecSign to allow the user to customize the verification and signature functions (could be useful if the user wants to use BearSSLClient without ECC508) The functions can be customized through the constructor and are set to their current values as soon as the user calls setEccSlot Signed-off-by: Fabrice Fontaine <[email protected]>
1 parent 15763ab commit 436c232

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/BearSSLClient.cpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs,
4646
_numTAs(myNumTAs),
4747
_noSNI(false)
4848
{
49+
_ecVrfy = br_ecdsa_vrfy_asn1_get_default();
50+
_ecSign = br_ecdsa_sign_asn1_get_default();
51+
4952
_ecKey.curve = 0;
5053
_ecKey.x = NULL;
5154
_ecKey.xlen = 0;
@@ -192,6 +195,16 @@ void BearSSLClient::setInsecure(SNI insecure)
192195
}
193196
}
194197

198+
void BearSSLClient::setEccVrfy(br_ecdsa_vrfy vrfy)
199+
{
200+
_ecVrfy = vrfy;
201+
}
202+
203+
void BearSSLClient::setEccSign(br_ecdsa_sign sign)
204+
{
205+
_ecSign = sign;
206+
}
207+
195208
void BearSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLength)
196209
{
197210
// HACK: put the key slot info. in the br_ec_private_key structure
@@ -202,6 +215,9 @@ void BearSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLen
202215
_ecCert.data = (unsigned char*)cert;
203216
_ecCert.data_len = certLength;
204217
_ecCertDynamic = false;
218+
219+
_ecVrfy = eccX08_vrfy_asn1;
220+
_ecSign = eccX08_sign_asn1;
205221
}
206222

207223
void BearSSLClient::setEccSlot(int ecc508KeySlot, const char cert[])
@@ -267,23 +283,23 @@ int BearSSLClient::connectSSL(const char* host)
267283
// inject entropy in engine
268284
unsigned char entropy[32];
269285

270-
if (ECCX08.begin() && ECCX08.locked() && ECCX08.random(entropy, sizeof(entropy))) {
271-
// ECC508 random success, add custom ECDSA vfry and EC sign
272-
br_ssl_engine_set_ecdsa(&_sc.eng, eccX08_vrfy_asn1);
273-
br_x509_minimal_set_ecdsa(&_xc, br_ssl_engine_get_ec(&_sc.eng), br_ssl_engine_get_ecdsa(&_sc.eng));
274-
275-
// enable client auth using the ECCX08
276-
if (_ecCert.data_len && _ecKey.xlen) {
277-
br_ssl_client_set_single_ec(&_sc, &_ecCert, 1, &_ecKey, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, BR_KEYTYPE_EC, br_ec_get_default(), eccX08_sign_asn1);
278-
}
279-
} else {
286+
if (!ECCX08.begin() || !ECCX08.locked() || !ECCX08.random(entropy, sizeof(entropy))) {
280287
// no ECCX08 or random failed, fallback to pseudo random
281288
for (size_t i = 0; i < sizeof(entropy); i++) {
282289
entropy[i] = random(0, 255);
283290
}
284291
}
285292
br_ssl_engine_inject_entropy(&_sc.eng, entropy, sizeof(entropy));
286293

294+
// add custom ECDSA vfry and EC sign
295+
br_ssl_engine_set_ecdsa(&_sc.eng, _ecVrfy);
296+
br_x509_minimal_set_ecdsa(&_xc, br_ssl_engine_get_ec(&_sc.eng), br_ssl_engine_get_ecdsa(&_sc.eng));
297+
298+
// enable client auth
299+
if (_ecCert.data_len && _ecKey.xlen) {
300+
br_ssl_client_set_single_ec(&_sc, &_ecCert, 1, &_ecKey, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, BR_KEYTYPE_EC, br_ec_get_default(), _ecSign);
301+
}
302+
287303
// set the hostname used for SNI
288304
br_ssl_client_reset(&_sc, host, 0);
289305

src/BearSSLClient.h

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class BearSSLClient : public Client {
7171

7272
void setInsecure(SNI insecure) __attribute__((deprecated("INSECURE. DO NOT USE IN PRODUCTION")));
7373

74+
void setEccVrfy(br_ecdsa_vrfy vrfy);
75+
void setEccSign(br_ecdsa_sign sign);
76+
7477
void setEccSlot(int ecc508KeySlot, const byte cert[], int certLength);
7578
void setEccSlot(int ecc508KeySlot, const char cert[]);
7679

@@ -89,6 +92,9 @@ class BearSSLClient : public Client {
8992

9093
bool _noSNI;
9194

95+
br_ecdsa_vrfy _ecVrfy;
96+
br_ecdsa_sign _ecSign;
97+
9298
br_ec_private_key _ecKey;
9399
br_x509_certificate _ecCert;
94100
bool _ecCertDynamic;

0 commit comments

Comments
 (0)