1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/openssl/ossl_pkey.c
nahi ef5c7c951a * ext/openssl: backport fixes in 1.9.
* r25019 by marcandre
	    * ossl_ocsp.c (ossl_ocspres_to_der): Bug fix in Response#to_def.
	      Patch by Chris Chandler [ruby-core:18411]

	  * r25017 by marcandre
	    * ossl_config.c (ossl_config_add_value_m,
	      ossl_config_set_section): Check if frozen (or untrusted for
	      $SECURE >= 4) [ruby-core:18377]

	  * r22925 by nobu
	    * ext/openssl/openssl_missing.h (i2d_of_void): cast for callbacks.
	      [ruby-core:22860]

	    * ext/openssl/ossl_engine.c (ossl_engine_s_by_id): suppress a
	      warning.

	    * ext/openssl/ossl_ssl.c (ossl_sslctx_flush_sessions): time_t may
	      be larger than long.

	    * ext/openssl/ossl_ssl_session.c (ossl_ssl_session_get_time),
	      (ossl_ssl_session_get_timeout): use TIMET2NUM() to convert
	      time_t.

	  * r22924 by nobu
	    * ext/openssl/ossl_x509ext.c (ossl_x509ext_set_value): should use
	      OPENSSL_free instead of free.  a patch from Charlie Savage at
	      [ruby-core:22858].

	  * r22918 by akr
	    * ext/openssl: suppress warnings.

	    * ext/openssl/ossl.h (OSSL_Debug): don't use gcc extention for
	      variadic macro.

	  * r22666 by akr
	    * ext/openssl/lib/openssl/buffering.rb: define Buffering module
	      under OpenSSL.  [ruby-dev:37906]

	  * r22440 by nobu
	    * ext/openssl/ossl_ocsp.c (ossl_ocspbres_verify): OCSP_basic_verify
	      returns positive value on success, not non-zero.
	      [ruby-core:21762]

	  * r22378 by akr
	    * ext/openssl: avoid cyclic require.

	    * ext/openssl/lib/openssl/ssl-internal.rb: renamed from ssl.rb

	    * ext/openssl/lib/openssl/x509-internal.rb: renamed from x509.rb.
	      [ruby-dev:38018]

	  * r22101 by nobu
	    * ext/openssl/ossl_cipher.c (add_cipher_name_to_ary): used
	      conditionally.

	  * r21510 by akr
	    * ext/openssl/ossl.c (ossl_raise): abolish a warning.

	  * r21208 by akr
	    * ext/openssl/ossl_digest.c (GetDigestPtr): use StringValueCStr
	      instead of STR2CSTR.

	    * ext/openssl/ossl_pkey_ec.c (ossl_ec_key_initialize): ditto.
	      (ossl_ec_group_initialize): ditto.

	  * r19420 by mame
	    * ext/openssl/ossl_pkey_ec.c (ossl_ec_key_to_string): comment out
	      fragments of unused code.

	  * r18975 by nobu
	    * ext/openssl/ossl_ocsp.c (ossl_ocspres_initialize): fix for
	      initialization of r18168.

	  * r18971 by nobu
	    * ext/openssl/ossl_config.c (Init_ossl_config): removed C99ism.

	  * r18944 by matz
	    * ext/openssl/ossl_config.c (Init_ossl_config): memory leak fixed.
	      a patch <shinichiro.hamaji at gmail.com> in [ruby-dev:35880].

	    * ext/openssl/ossl_x509ext.c (ossl_x509ext_set_value): ditto.

	  * r18917 by nobu
	    * ext/openssl/ossl_x509attr.c (ossl_x509attr_initialize): fix for
	      initialization of r18168.

	    * ext/openssl/ossl_ocsp.c (ossl_ocspreq_initialize): ditto.

	    * ext/openssl/ossl_x509name.c (ossl_x509name_initialize): ditto.

	  * r18283 by nobu
	    * ext/openssl/ossl_asn1.c (ossl_asn1_get_asn1type): suppress
	      warnings on platforms which int size differs from pointer size.

	  * r18181 by nobu
	    * ext/openssl/openssl_missing.h (d2i_of_void): define for older
	      versions.  [ruby-dev:35637]

	  * r18168 by nobu
	    * ext/openssl: suppress warnings.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@26835 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-03-06 21:37:39 +00:00

240 lines
4.7 KiB
C

/*
* $Id$
* 'OpenSSL for Ruby' project
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
* All rights reserved.
*/
/*
* This program is licenced under the same licence as Ruby.
* (See the file 'LICENCE'.)
*/
#include "ossl.h"
/*
* Classes
*/
VALUE mPKey;
VALUE cPKey;
VALUE ePKeyError;
ID id_private_q;
/*
* callback for generating keys
*/
void
ossl_generate_cb(int p, int n, void *arg)
{
VALUE ary;
ary = rb_ary_new2(2);
rb_ary_store(ary, 0, INT2NUM(p));
rb_ary_store(ary, 1, INT2NUM(n));
rb_yield(ary);
}
/*
* Public
*/
VALUE
ossl_pkey_new(EVP_PKEY *pkey)
{
if (!pkey) {
ossl_raise(ePKeyError, "Cannot make new key from NULL.");
}
switch (EVP_PKEY_type(pkey->type)) {
#if !defined(OPENSSL_NO_RSA)
case EVP_PKEY_RSA:
return ossl_rsa_new(pkey);
#endif
#if !defined(OPENSSL_NO_DSA)
case EVP_PKEY_DSA:
return ossl_dsa_new(pkey);
#endif
#if !defined(OPENSSL_NO_DH)
case EVP_PKEY_DH:
return ossl_dh_new(pkey);
#endif
#if !defined(OPENSSL_NO_EC) && (OPENSSL_VERSION_NUMBER >= 0x0090802fL)
case EVP_PKEY_EC:
return ossl_ec_new(pkey);
#endif
default:
ossl_raise(ePKeyError, "unsupported key type");
}
return Qnil; /* not reached */
}
VALUE
ossl_pkey_new_from_file(VALUE filename)
{
FILE *fp;
EVP_PKEY *pkey;
SafeStringValue(filename);
if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
ossl_raise(ePKeyError, "%s", strerror(errno));
}
pkey = PEM_read_PrivateKey(fp, NULL, ossl_pem_passwd_cb, NULL);
fclose(fp);
if (!pkey) {
ossl_raise(ePKeyError, NULL);
}
return ossl_pkey_new(pkey);
}
EVP_PKEY *
GetPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;
SafeGetPKey(obj, pkey);
return pkey;
}
EVP_PKEY *
GetPrivPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;
if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
}
SafeGetPKey(obj, pkey);
return pkey;
}
EVP_PKEY *
DupPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;
SafeGetPKey(obj, pkey);
CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
return pkey;
}
EVP_PKEY *
DupPrivPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;
if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
}
SafeGetPKey(obj, pkey);
CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
return pkey;
}
/*
* Private
*/
static VALUE
ossl_pkey_alloc(VALUE klass)
{
EVP_PKEY *pkey;
VALUE obj;
if (!(pkey = EVP_PKEY_new())) {
ossl_raise(ePKeyError, NULL);
}
WrapPKey(klass, obj, pkey);
return obj;
}
static VALUE
ossl_pkey_initialize(VALUE self)
{
if (rb_obj_is_instance_of(self, cPKey)) {
ossl_raise(rb_eNotImpError, "OpenSSL::PKey::PKey is an abstract class.");
}
return self;
}
static VALUE
ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
{
EVP_PKEY *pkey;
EVP_MD_CTX ctx;
unsigned int buf_len;
VALUE str;
if (rb_funcall(self, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
}
GetPKey(self, pkey);
EVP_SignInit(&ctx, GetDigestPtr(digest));
StringValue(data);
EVP_SignUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
str = rb_str_new(0, EVP_PKEY_size(pkey)+16);
if (!EVP_SignFinal(&ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey))
ossl_raise(ePKeyError, NULL);
assert((long)buf_len <= RSTRING_LEN(str));
rb_str_set_len(str, buf_len);
return str;
}
static VALUE
ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
{
EVP_PKEY *pkey;
EVP_MD_CTX ctx;
GetPKey(self, pkey);
EVP_VerifyInit(&ctx, GetDigestPtr(digest));
StringValue(sig);
StringValue(data);
EVP_VerifyUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
switch (EVP_VerifyFinal(&ctx, (unsigned char *)RSTRING_PTR(sig), RSTRING_LEN(sig), pkey)) {
case 0:
return Qfalse;
case 1:
return Qtrue;
default:
ossl_raise(ePKeyError, NULL);
}
return Qnil; /* dummy */
}
/*
* INIT
*/
void
Init_ossl_pkey()
{
#if 0 /* let rdoc know about mOSSL */
mOSSL = rb_define_module("OpenSSL");
#endif
mPKey = rb_define_module_under(mOSSL, "PKey");
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
rb_define_alloc_func(cPKey, ossl_pkey_alloc);
rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0);
rb_define_method(cPKey, "sign", ossl_pkey_sign, 2);
rb_define_method(cPKey, "verify", ossl_pkey_verify, 3);
id_private_q = rb_intern("private?");
/*
* INIT rsa, dsa, dh, ec
*/
Init_ossl_rsa();
Init_ossl_dsa();
Init_ossl_dh();
Init_ossl_ec();
}