Class: Ccrypto::Ruby::ECCPublicKey

Inherits:
ECCPublicKey
  • Object
show all
Defined in:
lib/ccrypto/ruby/engines/ecc_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_key(bin) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 46

def self.to_key(bin)
  if OpenSSL::VERSION > "3.0.0"
    ek = OpenSSL::PKey::EC.new(bin)
  else
    seq = OpenSSL::ASN1Object.decode(bin).value
    envp = ASN1Object.decode(seq[0]).value
    raise KeypairEngineException, "Not ECC public key" if envp != "2.8.8.8.128.0"
    ver = ASN1Object.decode(seq[1]).value
    raise KeypairEngineException, "Unsupported version" if ver != 0x0100
    cv = ASN1Object.decode(seq[2]).value
    curve = ECCConst.invert[cv]
    raise KeypairEngineException, "Unknown curve '#{curve}'" if curve.nil?
    kv = ASN1Object.decode(seq[3]).value

    ek = OpenSSL::PKey::EC::Point.new(OpenSSL::PKey::EC::Group.new(curve), kv)
  end

  ECCPublicKey.new(ek)
end

Instance Method Details

#to_binObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 29

def to_bin
  if OpenSSL::VERSION < "3.0.0"
    @native_pubKey.to_der
  else
    const = ECCConst[@native_pubKey.group.curve_name]
    # At 01 April 2023
    # at Ruby 3.2.1/OpenSSL gem 3.1.0/OpenSSL 3.0.2
    # The gem has bug that the encoding is incorrect
    OpenSSL::ASN1::Sequence.new([
      OpenSSL::ASN1::ObjectId.new("2.8.8.128.0"),
      OpenSSL::ASN1::Integer.new(0x0100),
      OpenSSL::ASN1::Integer.new(const),
      OpenSSL::ASN1::BitString.new(@native_pubKey.to_bn)
    ]).to_der
  end
end