11# frozen_string_literal: true
22
3+ require "delegate"
34require "openssl"
45require "openssl/signature_algorithm/base"
56
@@ -8,17 +9,21 @@ module SignatureAlgorithm
89 class ECDSA < Base
910 BYTE_LENGTH = 8
1011
11- class SigningKey < OpenSSL ::PKey ::EC
12+ class SigningKey < DelegateClass ( OpenSSL ::PKey ::EC )
1213 def initialize ( *args )
13- super ( *args ) . generate_key
14+ super ( OpenSSL :: PKey :: EC . generate ( *args ) )
1415 end
1516
1617 def verify_key
1718 VerifyKey . new ( group , public_key . to_bn )
1819 end
1920 end
2021
21- class VerifyKey < OpenSSL ::PKey ::EC ::Point
22+ class VerifyKey < DelegateClass ( OpenSSL ::PKey ::EC ::Point )
23+ def initialize ( *args )
24+ super ( OpenSSL ::PKey ::EC ::Point . new ( *args ) )
25+ end
26+
2227 def self . deserialize ( pem_string )
2328 new ( OpenSSL ::PKey ::EC . new ( pem_string ) . public_key )
2429 end
@@ -30,10 +35,16 @@ def serialize
3035 def ec_key
3136 @ec_key ||=
3237 begin
33- ec_key = OpenSSL ::PKey ::EC . new ( group )
34- ec_key . public_key = self
35-
36- ec_key
38+ # RFC5480 SubjectPublicKeyInfo
39+ asn1 = OpenSSL ::ASN1 ::Sequence ( [
40+ OpenSSL ::ASN1 ::Sequence ( [
41+ OpenSSL ::ASN1 ::ObjectId ( "id-ecPublicKey" ) ,
42+ OpenSSL ::ASN1 ::ObjectId ( group . curve_name ) ,
43+ ] ) ,
44+ OpenSSL ::ASN1 ::BitString ( to_octet_string ( :uncompressed ) )
45+ ] )
46+
47+ OpenSSL ::PKey ::EC . new ( asn1 . to_der )
3748 end
3849 end
3950
0 commit comments