@@ -926,62 +926,114 @@ size_t GroupOrderSize(ManagedEVPPKey key) {
926926 return BN_num_bytes (order.get ());
927927}
928928
929+ // TODO(@jasnell): Reconcile with ConvertSignatureToP1363 in crypto_sig.cc
930+ // TODO(@jasnell): Move out of crypto_ec since this is also doing DSA now also
929931ByteSource ConvertToWebCryptoSignature (
930- ManagedEVPPKey key,
932+ const ManagedEVPPKey& key,
931933 const ByteSource& signature) {
932934 const unsigned char * data =
933935 reinterpret_cast <const unsigned char *>(signature.get ());
934- EcdsaSigPointer ecsig (d2i_ECDSA_SIG (nullptr , &data, signature.size ()));
935-
936- if (!ecsig)
937- return ByteSource ();
938-
939- size_t order_size_bytes = GroupOrderSize (key);
940- char * outdata = MallocOpenSSL<char >(order_size_bytes * 2 );
941- ByteSource out = ByteSource::Allocated (outdata, order_size_bytes * 2 );
942- unsigned char * ptr = reinterpret_cast <unsigned char *>(outdata);
943936
937+ ECDSASigPointer ecsig;
938+ DsaSigPointer dsasig;
944939 const BIGNUM* pr;
945940 const BIGNUM* ps;
946- ECDSA_SIG_get0 (ecsig. get (), &pr, &ps) ;
941+ size_t len = 0 ;
947942
948- if (!BN_bn2binpad (pr, ptr, order_size_bytes) ||
949- !BN_bn2binpad (ps, ptr + order_size_bytes, order_size_bytes)) {
943+ switch (EVP_PKEY_id (key.get ())) {
944+ case EVP_PKEY_EC: {
945+ ecsig = ECDSASigPointer (d2i_ECDSA_SIG (nullptr , &data, signature.size ()));
946+
947+ if (!ecsig)
948+ return ByteSource ();
949+
950+ len = GroupOrderSize (key);
951+
952+ ECDSA_SIG_get0 (ecsig.get (), &pr, &ps);
953+ break ;
954+ }
955+ case EVP_PKEY_DSA: {
956+ dsasig = DsaSigPointer (d2i_DSA_SIG (nullptr , &data, signature.size ()));
957+
958+ if (!dsasig)
959+ return ByteSource ();
960+
961+ DSA_SIG_get0 (dsasig.get (), &pr, &ps);
962+ len = BN_num_bytes (pr);
963+ }
964+ }
965+
966+ CHECK_GT (len, 0 );
967+
968+ char * outdata = MallocOpenSSL<char >(len * 2 );
969+ ByteSource out = ByteSource::Allocated (outdata, len * 2 );
970+ unsigned char * ptr = reinterpret_cast <unsigned char *>(outdata);
971+
972+ if (!BN_bn2binpad (pr, ptr, len) || !BN_bn2binpad (ps, ptr + len, len)) {
950973 return ByteSource ();
951974 }
952975 return out;
953976}
954977
978+ // TODO(@jasnell): Reconcile with ConvertSignatureToDER in crypto_sig.cc
979+ // TODO(@jasnell): Move out of crypto_ec since this is also doing DSA now also
955980ByteSource ConvertFromWebCryptoSignature (
956- ManagedEVPPKey key,
981+ const ManagedEVPPKey& key,
957982 const ByteSource& signature) {
958- size_t order_size_bytes = GroupOrderSize (key);
983+ BignumPointer r (BN_new ());
984+ BignumPointer s (BN_new ());
985+ const unsigned char * sig = signature.data <unsigned char >();
959986
960- // If the size of the signature is incorrect, verification
961- // will fail.
962- if (signature.size () != 2 * order_size_bytes)
963- return ByteSource (); // Empty!
987+ switch (EVP_PKEY_id (key.get ())) {
988+ case EVP_PKEY_EC: {
989+ size_t order_size_bytes = GroupOrderSize (key);
964990
965- EcdsaSigPointer ecsig (ECDSA_SIG_new ());
966- if (!ecsig)
967- return ByteSource ();
991+ // If the size of the signature is incorrect, verification
992+ // will fail.
993+ if (signature.size () != 2 * order_size_bytes)
994+ return ByteSource (); // Empty!
968995
969- BignumPointer r (BN_new ());
970- BignumPointer s (BN_new ());
996+ ECDSASigPointer ecsig (ECDSA_SIG_new ());
997+ if (!ecsig)
998+ return ByteSource ();
971999
972- const unsigned char * sig = signature.data <unsigned char >();
1000+ if (!BN_bin2bn (sig, order_size_bytes, r.get ()) ||
1001+ !BN_bin2bn (sig + order_size_bytes, order_size_bytes, s.get ()) ||
1002+ !ECDSA_SIG_set0 (ecsig.get (), r.release (), s.release ())) {
1003+ return ByteSource ();
1004+ }
9731005
974- if (!BN_bin2bn (sig, order_size_bytes, r.get ()) ||
975- !BN_bin2bn (sig + order_size_bytes, order_size_bytes, s.get ()) ||
976- !ECDSA_SIG_set0 (ecsig.get (), r.release (), s.release ())) {
977- return ByteSource ();
978- }
1006+ int size = i2d_ECDSA_SIG (ecsig.get (), nullptr );
1007+ char * data = MallocOpenSSL<char >(size);
1008+ unsigned char * ptr = reinterpret_cast <unsigned char *>(data);
1009+ CHECK_EQ (i2d_ECDSA_SIG (ecsig.get (), &ptr), size);
1010+ return ByteSource::Allocated (data, size);
1011+ }
1012+ case EVP_PKEY_DSA: {
1013+ size_t len = signature.size () / 2 ;
1014+
1015+ if (signature.size () != 2 * len)
1016+ return ByteSource ();
1017+
1018+ DsaSigPointer dsasig (DSA_SIG_new ());
1019+ if (!dsasig)
1020+ return ByteSource ();
1021+
1022+ if (!BN_bin2bn (sig, len, r.get ()) ||
1023+ !BN_bin2bn (sig + len, len, s.get ()) ||
1024+ !DSA_SIG_set0 (dsasig.get (), r.release (), s.release ())) {
1025+ return ByteSource ();
1026+ }
9791027
980- int size = i2d_ECDSA_SIG (ecsig.get (), nullptr );
981- char * data = MallocOpenSSL<char >(size);
982- unsigned char * ptr = reinterpret_cast <unsigned char *>(data);
983- CHECK_EQ (i2d_ECDSA_SIG (ecsig.get (), &ptr), size);
984- return ByteSource::Allocated (data, size);
1028+ int size = i2d_DSA_SIG (dsasig.get (), nullptr );
1029+ char * data = MallocOpenSSL<char >(size);
1030+ unsigned char * ptr = reinterpret_cast <unsigned char *>(data);
1031+ CHECK_EQ (i2d_DSA_SIG (dsasig.get (), &ptr), size);
1032+ return ByteSource::Allocated (data, size);
1033+ }
1034+ default :
1035+ UNREACHABLE ();
1036+ }
9851037}
9861038
9871039} // namespace crypto
0 commit comments