Class: Ccrypto::Java::ECCKeyBundle

Inherits:
Object
  • Object
show all
Includes:
ECCKeyBundle, DataConversion, PKCS12, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/java/engines/ecc_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PKCS12

included, #to_pkcs12

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(kp) ⇒ ECCKeyBundle

Returns a new instance of ECCKeyBundle.



39
40
41
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 39

def initialize(kp)
  @keypair = kp
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



158
159
160
161
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 158

def method_missing(mtd, *args, &block)
  teLogger.debug "Sending to native #{mtd}"
  @keypair.send(mtd, *args, &block)
end

Class Method Details

.from_storage(bin, &block) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 132

def self.from_storage(bin, &block)
 
  if is_pem?(bin)
  else
    from_pkcs12(bin, &block)
  end

end

.is_pem?(bin) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 141

def self.is_pem?(bin)
  begin
    (bin =~ /BEGIN/) != nil
  rescue ArgumentError => ex
    false
  end
end

Instance Method Details

#derive_dh_shared_secret(pubKey, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 58

def derive_dh_shared_secret(pubKey, &block)

  JCEProvider.instance.add_bc_provider

  ka = javax.crypto.KeyAgreement.getInstance("ECDH", JCEProvider::DEFProv) 
  ka.init(@keypair.private)

  case pubKey
  when ECCPublicKey
    pub = pubKey.native_pubKey
  when java.security.PublicKey
    pub = pubKey
  else
    raise KeypairEngineException, "Unsupported public key type #{pubKey.class}"
  end
  ka.doPhase(pub, true)
  #ka.doPhase(pubKey.native_pubKey, true)
  if block
    keyType = block.call(:keytype)
  else
    keyType = "AES"
  end
  keyType = "AES" if is_empty?(keyType)
  teLogger.debug "Generate secret key type #{keyType}"
  ka.generateSecret(keyType).encoded
end

#equal?(kp) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 149

def equal?(kp)
  case kp
  when Ccrypto::ECCKeyBundle
    @keypair.encoded == kp.private.encoded
  else
    false
  end
end

#is_public_key_equal?(pubKey) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 85

def is_public_key_equal?(pubKey)
  @keypair.public.encoded == pubKey.encoded
end

#native_keypairObject



43
44
45
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 43

def native_keypair
  @keypair
end

#private_keyObject



54
55
56
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 54

def private_key
  ECCPrivateKey.new(@keypair.private)
end

#public_keyObject



47
48
49
50
51
52
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 47

def public_key
  if @pubKey.nil?
    @pubKey = ECCPublicKey.new(@keypair.public)
  end
  @pubKey
end

#respond_to_missing?(mtd, incPriv = false) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 163

def respond_to_missing?(mtd, incPriv = false)
  teLogger.debug "Respond to missing #{mtd}"
  @keypair.respond_to?(mtd)
end

#to_storage(type, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 89

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

  when :jks
    to_pkcs12 do |key|
      case key
      when :storeType
        "JKS"
      when :keypair
        @keypair
      else
        block.call(key) if key
      end
    end
   
  when :pem

    header = "-----BEGIN EC PRIVATE KEY-----\n"
    footer = "\n-----END EC PRIVATE KEY-----"

    out = StringIO.new
    out.write header
    out.write to_b64_mime(@keypair.private.encoded)
    out.write footer

    out.string

  else
    raise KeypairEngineException, "Unknown storage type #{type}"
  end

end