Class: Ccrypto::Ruby::ECCKeyBundle

Inherits:
Object
  • Object
show all
Includes:
ECCKeyBundle, PEMStore, PKCS12Store, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/ruby/engines/ecc_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PEMStore

included, #to_pem

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_hex, #to_int_array

Methods included from PKCS12Store

included, #to_pkcs12

Constructor Details

#initialize(keypair) ⇒ ECCKeyBundle

Returns a new instance of ECCKeyBundle.



79
80
81
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 79

def initialize(keypair)
  @nativeKeypair = keypair
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 198

def method_missing(mtd, *args, &block)
  if @nativeKeypair.respond_to?(mtd)
    teLogger.debug "Sending to nativeKeypair #{mtd}"
    @nativeKeypair.send(mtd,*args, &block)
  else
    super
  end
end

Class Method Details

.from_storage(bin, &block) ⇒ Object

Raises:

  • (KeypairEngineException)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 166

def self.from_storage(bin, &block)
  raise KeypairEngineException, "Given data to load is empty" if is_empty?(bin)

  case bin
  when String
    begin
      teLogger.debug "Given String to load from storage"
      if is_pem?(bin)
        self.from_pem(bin, &block)
      else
        # binary buffer
        teLogger.debug "Given binary to load from storage"
        self.from_pkcs12(bin,&block)
      end
    rescue Ccrypto::Ruby::PKCS12Store::PKCS12StoreException => ex
      raise KeyBundleStorageException, ex
    end
  else
    raise KeyBundleStorageException, "Unsupported input type #{bin}"
  end

end

Instance Method Details

#derive_dh_shared_secret(pubKey) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 98

def derive_dh_shared_secret(pubKey)

  case pubKey
  when OpenSSL::PKey::EC::Point
    tkey = pubKey
  when Ccrypto::ECCPublicKey
    tkey = pubKey.native_pubKey
    if OpenSSL::VERSION < "3.0.0"
      tkey = tkey.public_key if not tkey.is_a?(OpenSSL::PKey::EC::Point)
    else
      tkey = OpenSSL::PKey::EC.new(tkey)
    end
  else
    raise KeypairEngineException, "Unknown public key type #{pubKey.class}" 
  end

  if OpenSSL::VERSION < "3.0.0"
    raise KeypairEngineException, "OpenSSL::PKey::EC::Point is required. Given #{tkey.inspect}" if not tkey.is_a?(OpenSSL::PKey::EC::Point)
  else
    raise KeypairEngineException, "OpenSSL::PKey::EC is required. Given #{tkey.inspect}" if not tkey.is_a?(OpenSSL::PKey::EC)
  end

  @nativeKeypair.dh_compute_key(tkey) 
end

#equal?(kp) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 189

def equal?(kp)
  if kp.respond_to?(:to_der)
    @nativeKeypair.to_der == kp.to_der
  else 
    @nativeKeypair == kp
    #false
  end
end

#is_public_key_equal?(pubKey) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 123

def is_public_key_equal?(pubKey)

  case pubKey
  when OpenSSL::PKey::EC
    targetKey = pubKey
  when ECCKeyBundle
    targetKey = pubKey.public_key
  when ECCPublicKey
    targetKey = pubKey.native_pubKey
  else
    raise KeypairEngineException, "Unknown public key type #{pubKey.class}" 
  end

  public_key.to_bin == targetKey.to_der
end

#private_keyObject



94
95
96
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 94

def private_key
  ECCPrivateKey.new(@nativeKeypair)
end

#public_keyObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 83

def public_key
  if @pubKey.nil?
    #if OpenSSL::VERSION < "3.0.0"
      @pubKey = ECCPublicKey.new(@nativeKeypair.public_key)
    #else
    #  @pubKey = ECCPublicKey.new(@nativeKeypair.public_key.to_bn)
    #end
  end
  @pubKey
end

#respond_to_missing?(mtd, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 207

def respond_to_missing?(mtd, *args, &block)
  @nativeKeypair.respond_to?(mtd)
end

#to_storage(format, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 139

def to_storage(format, &block)
  case format
  when :pkcs12, :p12
    to_pkcs12 do |key|
      case key
      when :keypair
        @nativeKeypair
      else
        block.call(key) if block
      end
    end

  when :pem 
    to_pem do |key|
      case key
      when :keypair
        @nativeKeypair
      else
        block.call(key) if block
      end
    end

  else
    raise KeyBundleStorageException, "Unknown storage format #{format}"
  end
end