lib/ccrypto/java/engines/rsa_engine.rb in ccrypto-java-0.1.0 vs lib/ccrypto/java/engines/rsa_engine.rb in ccrypto-java-0.2.0
- old
+ new
@@ -1,37 +1,104 @@
require_relative '../data_conversion'
-require_relative '../keybundle_store/pkcs12'
-#require_relative '../keybundle_store/pem_store'
module Ccrypto
module Java
class RSAPublicKey < Ccrypto::RSAPublicKey
+ include DataConversion
def to_bin
@native_pubKey.encoded
end
def self.to_key(bin)
pubKey = java.security.KeyFactory.getInstance("RSA", "BC").generatePublic(java.security.spec.X509EncodedKeySpec.new(bin))
RSAPublicKey.new(pubKey)
end
+ def to_pem
+ cont = ["-----BEGIN RSA PUBLIC KEY-----\n"]
+ cont << to_b64(to_bin)
+ cont << "\n-----END RSA PUBLIC KEY-----"
+ cont.join
+ end
+
+ def self.from_pem(str)
+ if str =~ /RSA PUBLIC/
+ cont = str.lines[1..-2].join.strip
+ to_key(from_b64(cont))
+ else
+ raise KeypairEngineException, "Not an RSA public key"
+ end
+ end
+
def method_missing(mtd, *args, &block)
@native_pubKey.send(mtd, *args, &block)
end
end # RSAPublicKey
+
+ class RSAPrivateKey < Ccrypto::RSAPrivateKey
+ include DataConversion
+
+ def self.to_key(bin, &block)
+ if block
+ prov = block.call(:jce_provider)
+ else
+ prov = JCEProvider::BCProv
+ end
+
+ kf = java.security.KeyFactory.getInstance("RSA",prov)
+ priv = kf.generate_private(java.security.spec.PKCS8EncodedKeySpec.new(bin))
+ RSAPrivateKey.new(priv)
+
+ end
+
+ def to_pem
+ cont = ["-----BEGIN RSA PRIVATE KEY-----\n"]
+ cont << to_b64(@native_privKey.encoded)
+ cont << "\n-----END RSA PRIVATE KEY-----"
+ cont.join
+ end
+
+ def self.from_pem(str)
+ if str =~ /RSA PRIVATE/
+ cont = str.lines[1..-2].join.strip
+ to_key(from_b64(cont))
+ else
+ raise KeypairEngineException, "Not an RSA private key"
+ end
+ end
+
+ def to_bin
+ @native_privKey.encoded
+ end
+
+ def equals?(privKey)
+ if not @native_privKey.nil?
+ case privKey
+ when RSAPrivateKey
+ @native_privKey.encoded == privKey.to_bin
+ else
+ logger.warn "Unmatched private key : (native) #{@native_privKey} vs. (subject) #{privKey}"
+ false
+ end
+ else
+ logger.warn "RSAPrivateKey equals? returned false because native_privKey is nil"
+ false
+ end
+ end
+ alias_method :key_equals?, :equals?
+
+ end # class RSAPrivateKey
+
class RSAKeyBundle
include Ccrypto::RSAKeyBundle
include TR::CondUtils
- include PKCS12
- #include PEMStore
-
include TeLogger::TeLogHelper
teLogger_tag :j_rsa_keybundle
def initialize(kp)
@@ -50,59 +117,42 @@
@privKey = RSAPrivateKey.new(@nativeKeypair.private)
end
@privKey
end
- def to_storage(type, &block)
-
- case type
- when :p12, :pkcs12
- to_pkcs12 do |key|
+ def write_keystore(type, &block)
+ ksType = Keystore.map_keystore_type(type)
+ case ksType
+ when :pkcs12
+ Keystore::PKCS12Keystore.to_p12 do |key, *val|
case key
when :keypair
@nativeKeypair
else
block.call(key) if block
end
end
-
when :jks
- to_pkcs12 do |key|
+ Keystore::JKSKeystore.to_jks do |key, *val|
case key
- when :storeType
- "JKS"
when :keypair
@nativeKeypair
else
block.call(key) if block
end
end
- end
- end
-
- def self.from_storage(bin, &block)
-
- if is_pem?(bin)
else
- from_pkcs12(bin, &block)
+ raise Ccrypto::Keystore::KeystoreException, "Unsupported keystore type '#{type}' for engine '#{self.class.name}'"
end
-
end
- def self.is_pem?(bin)
- begin
- (bin =~ /BEGIN/) != nil
- rescue ArgumentError => ex
- false
- end
- end
-
+
def equal?(kp)
case kp
when Ccrypto::RSAKeyBundle
- @nativeKeypair.encoded == kp.private.encoded
+ private_key.encoded == kp.private_key.encoded
else
false
end
end
@@ -123,9 +173,13 @@
include DataConversion
include TeLogger::TeLogHelper
teLogger_tag :j_rsa
+
+ def self.supported_params
+ [1024,2048,4096,8192]
+ end
def initialize(*args, &block)
@config = args.first
raise KeypairEngineException, "1st parameter must be a #{Ccrypto::KeypairConfig.class} object" if not @config.is_a?(Ccrypto::KeypairConfig)