12
12
#include " ssl.h"
13
13
14
14
extern " C" {
15
- int ecsdsa_sign (mbedtls_ecp_keypair* ctx, int sdsa, mbedtls_ecp_keypair* to_ctx, const unsigned char * hash, size_t hlen,
15
+ int ecsdsa_sign (mbedtls_ecp_keypair* ctx, const unsigned char * hash, size_t hlen,
16
16
unsigned char * sig, size_t * slen, int (*f_rng)(void *, unsigned char *, size_t ), void* p_rng);
17
- int ecsdsa_verify (mbedtls_ecp_keypair* ctx, int sdsa, mbedtls_ecp_keypair* to_ctx, const unsigned char * hash, size_t hlen,
17
+ int ecsdsa_verify (mbedtls_ecp_keypair* ctx, const unsigned char * hash, size_t hlen,
18
18
const unsigned char * sig, size_t slen, int (*f_rng)(void *, unsigned char *, size_t ), void* p_rng);
19
19
}
20
20
@@ -361,41 +361,25 @@ result_t ECKey::equals(PKey_base* key, bool& retVal)
361
361
return 0 ;
362
362
}
363
363
364
- result_t ECKey::sign (Buffer_base* data, PKey_base* key , obj_ptr<Buffer_base>& retVal, AsyncEvent* ac)
364
+ result_t ECKey::sdsa_sign (Buffer_base* data, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac)
365
365
{
366
366
result_t hr;
367
367
368
- if (key) {
369
- mbedtls_pk_type_t type;
370
-
371
- mbedtls_pk_context& mkey = PKey::key (key);
372
-
373
- type = mbedtls_pk_get_type (&mkey);
374
- if (type != MBEDTLS_PK_ECKEY && type != MBEDTLS_PK_SM2)
375
- return CHECK_ERROR (CALL_E_INVALIDARG);
376
-
377
- mbedtls_ecp_keypair* ecp1 = mbedtls_pk_ec (m_key);
378
- mbedtls_ecp_keypair* ecp2 = mbedtls_pk_ec (mkey);
379
- if (ecp1->grp .id != ecp2->grp .id )
380
- return CHECK_ERROR (Runtime::setError (" Public key is not valid for specified curve" ));
381
- }
382
-
383
368
int32_t ret;
384
369
exlib::string output;
385
370
size_t olen = MBEDTLS_ECDSA_MAX_LEN;
386
371
387
372
obj_ptr<Buffer> buf_data = Buffer::Cast (data);
388
373
output.resize (MBEDTLS_ECDSA_MAX_LEN);
389
374
390
- ret = ecsdsa_sign (mbedtls_pk_ec (m_key), m_alg == " ECSDSA" , key ? mbedtls_pk_ec (PKey::key (key)) : NULL ,
391
- buf_data->data (), buf_data->length (), (unsigned char *)output.c_buffer (), &olen,
392
- mbedtls_ctr_drbg_random, &g_ssl.ctr_drbg );
375
+ ret = ecsdsa_sign (mbedtls_pk_ec (m_key), buf_data->data (), buf_data->length (),
376
+ (unsigned char *)output.c_buffer (), &olen, mbedtls_ctr_drbg_random, &g_ssl.ctr_drbg );
393
377
if (ret != 0 )
394
378
return CHECK_ERROR (_ssl::setError (ret));
395
379
396
380
output.resize (olen);
397
381
398
- if (ac->m_ctx [1 ].string () == " raw" ) {
382
+ if (ac->m_ctx [0 ].string () == " raw" ) {
399
383
hr = der2bin (output, output);
400
384
if (hr < 0 )
401
385
return hr;
@@ -406,46 +390,22 @@ result_t ECKey::sign(Buffer_base* data, PKey_base* key, obj_ptr<Buffer_base>& re
406
390
return 0 ;
407
391
}
408
392
409
- result_t ECKey::verify (Buffer_base* data, Buffer_base* sign, PKey_base* key , bool & retVal, AsyncEvent* ac)
393
+ result_t ECKey::sdsa_verify (Buffer_base* data, Buffer_base* sign, bool & retVal, AsyncEvent* ac)
410
394
{
411
- if (key) {
412
- result_t hr;
413
- bool priv;
414
-
415
- mbedtls_pk_context& mkey = PKey::key (key);
416
-
417
- mbedtls_pk_type_t type = mbedtls_pk_get_type (&mkey);
418
- if (type != MBEDTLS_PK_ECKEY && type != MBEDTLS_PK_SM2)
419
- return CHECK_ERROR (CALL_E_INVALIDARG);
420
-
421
- hr = key->isPrivate (priv);
422
- if (hr < 0 )
423
- return hr;
424
-
425
- if (!priv)
426
- return CHECK_ERROR (CALL_E_INVALIDARG);
427
-
428
- mbedtls_ecp_keypair* ecp1 = mbedtls_pk_ec (m_key);
429
- mbedtls_ecp_keypair* ecp2 = mbedtls_pk_ec (mkey);
430
- if (ecp1->grp .id != ecp2->grp .id )
431
- return CHECK_ERROR (Runtime::setError (" Public key is not valid for specified curve" ));
432
- }
433
-
434
395
int32_t ret;
435
396
436
397
obj_ptr<Buffer> buf_data = Buffer::Cast (data);
437
398
438
399
exlib::string strsign;
439
400
sign->toString (strsign);
440
- if (ac->m_ctx [1 ].string () == " raw" ) {
401
+ if (ac->m_ctx [0 ].string () == " raw" ) {
441
402
result_t hr = bin2der (strsign, strsign);
442
403
if (hr < 0 )
443
404
return hr;
444
405
}
445
406
446
- ret = ecsdsa_verify (mbedtls_pk_ec (m_key), m_alg == " ECSDSA" , key ? mbedtls_pk_ec (PKey::key (key)) : NULL ,
447
- buf_data->data (), buf_data->length (), (const unsigned char *)strsign.c_str (), strsign.length (),
448
- mbedtls_ctr_drbg_random, &g_ssl.ctr_drbg );
407
+ ret = ecsdsa_verify (mbedtls_pk_ec (m_key), buf_data->data (), buf_data->length (),
408
+ (const unsigned char *)strsign.c_str (), strsign.length (), mbedtls_ctr_drbg_random, &g_ssl.ctr_drbg );
449
409
if (ret == MBEDTLS_ERR_ECP_VERIFY_FAILED || ret == MBEDTLS_ERR_SM2_BAD_SIGNATURE) {
450
410
retVal = false ;
451
411
return 0 ;
@@ -578,7 +538,7 @@ result_t ECKey::bin2der(const exlib::string& bin, exlib::string& der)
578
538
result_t ECKey::check_opts (v8::Local<v8::Object> opts, AsyncEvent* ac)
579
539
{
580
540
static const char * s_keys[] = {
581
- " to " , " format" , NULL
541
+ " format" , NULL
582
542
};
583
543
584
544
if (!ac->isSync ())
@@ -591,21 +551,15 @@ result_t ECKey::check_opts(v8::Local<v8::Object> opts, AsyncEvent* ac)
591
551
if (hr < 0 )
592
552
return hr;
593
553
594
- ac->m_ctx .resize (2 );
595
-
596
- obj_ptr<PKey_base> to;
597
- hr = GetConfigValue (isolate, opts, " to" , to, true );
598
- if (hr < 0 && hr != CALL_E_PARAMNOTOPTIONAL)
599
- return hr;
600
- ac->m_ctx [0 ] = to;
554
+ ac->m_ctx .resize (1 );
601
555
602
556
exlib::string fmt = " der" ;
603
557
hr = GetConfigValue (isolate, opts, " format" , fmt, true );
604
558
if (hr < 0 && hr != CALL_E_PARAMNOTOPTIONAL)
605
559
return hr;
606
560
if (fmt != " der" && fmt != " raw" )
607
561
return CHECK_ERROR (Runtime::setError (exlib::string (" unsupported format \' " ) + fmt + " \' ." ));
608
- ac->m_ctx [1 ] = fmt;
562
+ ac->m_ctx [0 ] = fmt;
609
563
610
564
return CHECK_ERROR (CALL_E_NOSYNC);
611
565
}
@@ -625,12 +579,8 @@ result_t ECKey::sign(Buffer_base* data, v8::Local<v8::Object> opts, obj_ptr<Buff
625
579
if (!priv)
626
580
return CHECK_ERROR (CALL_E_INVALID_CALL);
627
581
628
- obj_ptr<PKey_base> to = PKey_base::getInstance (ac->m_ctx [0 ].object ());
629
- if (m_alg == " ECSDSA" || m_alg == " SM2" )
630
- return sign (data, to, retVal, ac);
631
-
632
- if (to)
633
- return CHECK_ERROR (CALL_E_INVALID_CALL);
582
+ if (m_alg == " ECSDSA" )
583
+ return sdsa_sign (data, retVal, ac);
634
584
635
585
int32_t ret;
636
586
exlib::string output;
@@ -648,7 +598,7 @@ result_t ECKey::sign(Buffer_base* data, v8::Local<v8::Object> opts, obj_ptr<Buff
648
598
649
599
output.resize (olen);
650
600
651
- if (ac->m_ctx [1 ].string () == " raw" ) {
601
+ if (ac->m_ctx [0 ].string () == " raw" ) {
652
602
hr = der2bin (output, output);
653
603
if (hr < 0 )
654
604
return hr;
@@ -665,20 +615,16 @@ result_t ECKey::verify(Buffer_base* data, Buffer_base* sign, v8::Local<v8::Objec
665
615
if (hr < 0 )
666
616
return hr;
667
617
668
- obj_ptr<PKey_base> to = PKey_base::getInstance (ac->m_ctx [0 ].object ());
669
- if (m_alg == " ECSDSA" || m_alg == " SM2" )
670
- return verify (data, sign, to, retVal, ac);
671
-
672
- if (to)
673
- return CHECK_ERROR (CALL_E_INVALID_CALL);
618
+ if (m_alg == " ECSDSA" )
619
+ return sdsa_verify (data, sign, retVal, ac);
674
620
675
621
int32_t ret;
676
622
exlib::string strsign;
677
623
678
624
obj_ptr<Buffer> buf_data = Buffer::Cast (data);
679
625
sign->toString (strsign);
680
626
681
- if (ac->m_ctx [1 ].string () == " raw" ) {
627
+ if (ac->m_ctx [0 ].string () == " raw" ) {
682
628
hr = bin2der (strsign, strsign);
683
629
if (hr < 0 )
684
630
return hr;
0 commit comments