mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/openssl/ossl_pkey.h, ossl_pkey_rsa.c, ossl_pkey_dsa.c:
an instance variable "private" is added to OpenSSL::PKey class. this ivar is a flag that shows whether there is a private key in the instance. * ext/openssl/ossl_engine.c: (ossl_engine_load_privkey): set private key flag. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9218 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8885a8720d
commit
246f5cfe67
6 changed files with 42 additions and 23 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
|||
Mon Sep 19 07:45:37 2005 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
||||
|
||||
* ext/openssl/ossl_pkey.h, ossl_pkey_rsa.c, ossl_pkey_dsa.c:
|
||||
an instance variable "private" is added to OpenSSL::PKey class.
|
||||
this ivar is a flag that shows whether there is a private key
|
||||
in the instance.
|
||||
|
||||
* ext/openssl/ossl_engine.c: (ossl_engine_load_privkey): set private
|
||||
key flag.
|
||||
|
||||
* test/openssl/test_pkey_rsa.rb: add test about private detection.
|
||||
|
||||
Mon Sep 19 06:38:03 2005 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
* lib/fileutils.rb: method renaming: collect_methods ->
|
||||
|
|
|
@ -217,7 +217,7 @@ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
|
|||
{
|
||||
ENGINE *e;
|
||||
EVP_PKEY *pkey;
|
||||
VALUE id, data;
|
||||
VALUE id, data, obj;
|
||||
char *sid, *sdata;
|
||||
|
||||
rb_scan_args(argc, argv, "02", &id, &data);
|
||||
|
@ -230,8 +230,10 @@ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
|
|||
pkey = ENGINE_load_private_key(e, sid, NULL, sdata);
|
||||
#endif
|
||||
if (!pkey) ossl_raise(eEngineError, NULL);
|
||||
obj = ossl_pkey_new(pkey);
|
||||
OSSL_PKEY_SET_PRIVATE(obj);
|
||||
|
||||
return ossl_pkey_new(pkey);
|
||||
return obj;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -242,8 +244,8 @@ ossl_engine_load_pubkey(int argc, VALUE *argv, VALUE self)
|
|||
VALUE id, data;
|
||||
char *sid, *sdata;
|
||||
|
||||
rb_scan_args(argc, argv, "11", &id, &data);
|
||||
sid = StringValuePtr(id);
|
||||
rb_scan_args(argc, argv, "02", &id, &data);
|
||||
sid = NIL_P(id) ? NULL : StringValuePtr(id);
|
||||
sdata = NIL_P(data) ? NULL : StringValuePtr(data);
|
||||
GetEngine(self, e);
|
||||
#if OPENSSL_VERSION_NUMBER < 0x00907000L
|
||||
|
|
|
@ -16,11 +16,16 @@ extern VALUE cPKey;
|
|||
extern VALUE ePKeyError;
|
||||
extern ID id_private_q;
|
||||
|
||||
#define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
|
||||
#define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
|
||||
#define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
|
||||
|
||||
#define WrapPKey(klass, obj, pkey) do { \
|
||||
if (!pkey) { \
|
||||
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
|
||||
} \
|
||||
obj = Data_Wrap_Struct(klass, 0, EVP_PKEY_free, pkey); \
|
||||
OSSL_PKEY_SET_PUBLIC(obj); \
|
||||
} while (0)
|
||||
#define GetPKey(obj, pkey) do {\
|
||||
Data_Get_Struct(obj, EVP_PKEY, pkey);\
|
||||
|
|
|
@ -20,13 +20,7 @@
|
|||
} while (0)
|
||||
|
||||
#define DSA_HAS_PRIVATE(dsa) ((dsa)->priv_key)
|
||||
|
||||
#ifdef OSSL_ENGINE_ENABLED
|
||||
# define DSA_PRIVATE(dsa) (DSA_HAS_PRIVATE(dsa) || (dsa)->engine)
|
||||
#else
|
||||
# define DSA_PRIVATE(dsa) DSA_HAS_PRIVATE(dsa)
|
||||
#endif
|
||||
|
||||
#define DSA_PRIVATE(obj,dsa) (DSA_HAS_PRIVATE(dsa)||OSSL_PKEY_IS_PRIVATE(obj))
|
||||
|
||||
/*
|
||||
* Classes
|
||||
|
@ -190,7 +184,7 @@ ossl_dsa_is_private(VALUE self)
|
|||
|
||||
GetPKeyDSA(self, pkey);
|
||||
|
||||
return (DSA_PRIVATE(pkey->pkey.dsa)) ? Qtrue : Qfalse;
|
||||
return (DSA_PRIVATE(self, pkey->pkey.dsa)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -336,7 +330,7 @@ ossl_dsa_sign(VALUE self, VALUE data)
|
|||
|
||||
GetPKeyDSA(self, pkey);
|
||||
StringValue(data);
|
||||
if (!DSA_PRIVATE(pkey->pkey.dsa)) {
|
||||
if (!DSA_PRIVATE(self, pkey->pkey.dsa)) {
|
||||
ossl_raise(eDSAError, "Private DSA key needed!");
|
||||
}
|
||||
str = rb_str_new(0, ossl_dsa_buf_size(pkey));
|
||||
|
|
|
@ -20,12 +20,7 @@
|
|||
} while (0)
|
||||
|
||||
#define RSA_HAS_PRIVATE(rsa) ((rsa)->p && (rsa)->q)
|
||||
|
||||
#ifdef OSSL_ENGINE_ENABLED
|
||||
# define RSA_PRIVATE(rsa) (RSA_HAS_PRIVATE(rsa) || (rsa)->engine)
|
||||
#else
|
||||
# define RSA_PRIVATE(rsa) RSA_HAS_PRIVATE(rsa)
|
||||
#endif
|
||||
#define RSA_PRIVATE(obj,rsa) (RSA_HAS_PRIVATE(rsa)||OSSL_PKEY_IS_PRIVATE(obj))
|
||||
|
||||
/*
|
||||
* Classes
|
||||
|
@ -181,8 +176,8 @@ ossl_rsa_is_private(VALUE self)
|
|||
EVP_PKEY *pkey;
|
||||
|
||||
GetPKeyRSA(self, pkey);
|
||||
|
||||
return (RSA_PRIVATE(pkey->pkey.rsa)) ? Qtrue : Qfalse;
|
||||
|
||||
return (RSA_PRIVATE(self, pkey->pkey.rsa)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -303,7 +298,7 @@ ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self)
|
|||
VALUE str, buffer, padding;
|
||||
|
||||
GetPKeyRSA(self, pkey);
|
||||
if (!RSA_PRIVATE(pkey->pkey.rsa)) {
|
||||
if (!RSA_PRIVATE(self, pkey->pkey.rsa)) {
|
||||
ossl_raise(eRSAError, "private key needed.");
|
||||
}
|
||||
rb_scan_args(argc, argv, "11", &buffer, &padding);
|
||||
|
@ -328,7 +323,7 @@ ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self)
|
|||
VALUE str, buffer, padding;
|
||||
|
||||
GetPKeyRSA(self, pkey);
|
||||
if (!RSA_PRIVATE(pkey->pkey.rsa)) {
|
||||
if (!RSA_PRIVATE(self, pkey->pkey.rsa)) {
|
||||
ossl_raise(eRSAError, "private key needed.");
|
||||
}
|
||||
rb_scan_args(argc, argv, "11", &buffer, &padding);
|
||||
|
|
|
@ -33,6 +33,17 @@ class OpenSSL::TestPKeyRSA < Test::Unit::TestCase
|
|||
assert_raise(ArgumentError){ key.private_encrypt("hi", 1, nil) }
|
||||
assert_raise(OpenSSL::PKey::RSAError){ key.private_encrypt(plain0, 666) }
|
||||
end
|
||||
|
||||
def test_private
|
||||
key = OpenSSL::PKey::RSA.new(512, 3)
|
||||
assert(key.private?)
|
||||
key2 = OpenSSL::PKey::RSA.new(key.to_der)
|
||||
assert(key2.private?)
|
||||
key3 = key.public_key
|
||||
assert(!key3.private?)
|
||||
key4 = OpenSSL::PKey::RSA.new(key3.to_der)
|
||||
assert(!key4.private?)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue