mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
609103dbb5
Import Ruby/OpenSSL 2.1.0.beta1. The full commit log since v2.0.5 (imported by r59567) can be found at: https://github.com/ruby/openssl/compare/v2.0.5...v2.1.0.beta1 ---------------------------------------------------------------- Antonio Terceiro (1): test/test_ssl: explicitly accept TLS 1.1 in corresponding test Colby Swandale (1): document using secure protocol to fetch git master in Bundler Colton Jenkins (1): Add fips_mode_get to return fips_mode Kazuki Yamaguchi (85): Start preparing for 2.1.0 Remove support for OpenSSL 0.9.8 and 1.0.0 bn: refine tests bn: implement unary {plus,minus} operators for OpenSSL::BN bn: implement OpenSSL::BN#negative? Don't define main() when built with --enable-debug test: let OpenSSL::TestCase include OpenSSL::TestUtils test: prepare test PKey instances on demand Add OpenSSL.print_mem_leaks Enable OSSL_MDEBUG on CI builds ssl: move default DH parameters from OpenSSL::PKey::DH Make exceptions with the same format regardless of OpenSSL.debug ssl: show reason of 'certificate verify error' in exception message ssl: remove OpenSSL::ExtConfig::TLS_DH_anon_WITH_AES_256_GCM_SHA384 ssl: do not confuse different ex_data index registries ssl: assume SSL/SSL_CTX always have a valid reference to the Ruby object Fix RDoc markup ssl: suppress compiler warning ext/openssl/deprecation.rb: remove broken-apple-openssl extconf.rb: print informative message if OpenSSL can't be found Rakefile: compile the extension before test kdf: introduce OpenSSL::KDF module ossl.h: add NUM2UINT64T() macro kdf: add scrypt Expand rb_define_copy_func() macro Expand FPTR_TO_FD() macro Remove SafeGet*() macros cipher: rename GetCipherPtr() to ossl_evp_get_cipherbyname() digest: rename GetDigestPtr() to ossl_evp_get_digestbyname() Add ossl_str_new(), an exception-safe rb_str_new() bio: simplify ossl_membio2str() using ossl_str_new() Remove unused functions and macros Drop support for LibreSSL 2.3 ocsp: add OpenSSL::OCSP::Request#signed? asn1: infinite length -> indefinite length asn1: rearrange tests ssl: remove a needless NULL check in SSL::SSLContext#ciphers ssl: return nil in SSL::SSLSocket#cipher if session is not started asn1: remove an unnecessary function prototype asn1: require tag information when instantiating generic type asn1: initialize 'unused_bits' attribute of BitString with 0 asn1: check for illegal 'unused_bits' value of BitString asn1: disallow NULL to be passed to asn1time_to_time() asn1: avoid truncating OID in OpenSSL::ASN1::ObjectId#oid asn1: allow constructed encoding with definite length form asn1: prohibit indefinite length form for primitive encoding asn1: allow tag number to be >= 32 for universal tag class asn1: use ossl_asn1_tag() asn1: clean up OpenSSL::ASN1::Constructive#to_der asn1: harmonize OpenSSL::ASN1::*#to_der asn1: prevent EOC octets from being in the middle of the content asn1: do not treat EOC octets as part of content octets x509name: add 'loc' and 'set' kwargs to OpenSSL::X509::Name#add_entry ssl: do not call session_remove_cb during GC Backport "Merge branch 'topic/test-memory-leak'" to maint cipher: update the documentation for Cipher#auth_tag= Rakefile: let sync:to_ruby know about test/openssl/fixtures test: fix formatting test/utils: remove OpenSSL::TestUtils.silent test/utils: add SSLTestCase#tls12_supported? test/utils: have start_server yield only the port number test/utils: do not set ecdh_curves in start_server test/utils: let server_loop close socket test/utils: improve error handling in start_server test/utils: add OpenSSL::TestUtils.openssl? and .libressl? test/utils: do not use DSA certificates in SSL tests test/test_ssl: remove test_invalid_shutdown_by_gc test/test_ssl: move test_multibyte_read_write to test_pair test/test_ssl_session: rearrange tests test/test_pair, test/test_ssl: fix for TLS 1.3 ssl: remove useless call to rb_thread_wait_fd() ssl: fix NPN support ssl: mark OpenSSL::SSL::SSLContext::DEFAULT_{1024,2048} as private ssl: use 2048-bit group in the default tmp_dh_cb ssl: ensure that SSL option flags are non-negative ssl: update OpenSSL::SSL::OP_* flags ssl: prefer TLS_method() over SSLv23_method() ssl: add SSLContext#min_version= and #max_version= ssl: rework SSLContext#ssl_version= test/test_x509name: change script encoding to ASCII-8BIT x509name: refactor OpenSSL::X509::Name#to_s x509name: add OpenSSL::X509::Name#to_utf8 x509name: add OpenSSL::X509::Name#inspect x509name: update regexp in OpenSSL::X509::Name.parse Ruby/OpenSSL 2.1.0.beta1 Marcus Stollsteimer (1): Fix rdoc for core Integer class nobu (4): [DOC] {read,write}_nonblock with exception: false [DOC] keyword argument _exception_ [DOC] mark up literals Revert r57690 except for read_nonblock git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59734 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
483 lines
12 KiB
C
483 lines
12 KiB
C
/*
|
|
* 'OpenSSL for Ruby' project
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
* All rights reserved.
|
|
*/
|
|
/*
|
|
* This program is licensed under the same licence as Ruby.
|
|
* (See the file 'LICENCE'.)
|
|
*/
|
|
#include "ossl.h"
|
|
|
|
/*
|
|
* Classes
|
|
*/
|
|
VALUE mPKey;
|
|
VALUE cPKey;
|
|
VALUE ePKeyError;
|
|
static ID id_private_q;
|
|
|
|
/*
|
|
* callback for generating keys
|
|
*/
|
|
int
|
|
ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
|
|
{
|
|
VALUE ary;
|
|
struct ossl_generate_cb_arg *arg;
|
|
int state;
|
|
|
|
arg = (struct ossl_generate_cb_arg *)BN_GENCB_get_arg(cb);
|
|
if (arg->yield) {
|
|
ary = rb_ary_new2(2);
|
|
rb_ary_store(ary, 0, INT2NUM(p));
|
|
rb_ary_store(ary, 1, INT2NUM(n));
|
|
|
|
/*
|
|
* can be break by raising exception or 'break'
|
|
*/
|
|
rb_protect(rb_yield, ary, &state);
|
|
if (state) {
|
|
arg->stop = 1;
|
|
arg->state = state;
|
|
}
|
|
}
|
|
if (arg->stop) return 0;
|
|
return 1;
|
|
}
|
|
|
|
void
|
|
ossl_generate_cb_stop(void *ptr)
|
|
{
|
|
struct ossl_generate_cb_arg *arg = (struct ossl_generate_cb_arg *)ptr;
|
|
arg->stop = 1;
|
|
}
|
|
|
|
static void
|
|
ossl_evp_pkey_free(void *ptr)
|
|
{
|
|
EVP_PKEY_free(ptr);
|
|
}
|
|
|
|
/*
|
|
* Public
|
|
*/
|
|
const rb_data_type_t ossl_evp_pkey_type = {
|
|
"OpenSSL/EVP_PKEY",
|
|
{
|
|
0, ossl_evp_pkey_free,
|
|
},
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
};
|
|
|
|
static VALUE
|
|
pkey_new0(EVP_PKEY *pkey)
|
|
{
|
|
VALUE obj;
|
|
int type;
|
|
|
|
if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE)
|
|
ossl_raise(rb_eRuntimeError, "pkey is empty");
|
|
|
|
switch (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)
|
|
case EVP_PKEY_EC:
|
|
return ossl_ec_new(pkey);
|
|
#endif
|
|
default:
|
|
obj = NewPKey(cPKey);
|
|
SetPKey(obj, pkey);
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
VALUE
|
|
ossl_pkey_new(EVP_PKEY *pkey)
|
|
{
|
|
VALUE obj;
|
|
int status;
|
|
|
|
obj = rb_protect((VALUE (*)(VALUE))pkey_new0, (VALUE)pkey, &status);
|
|
if (status) {
|
|
EVP_PKEY_free(pkey);
|
|
rb_jump_tag(status);
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
/*
|
|
* call-seq:
|
|
* OpenSSL::PKey.read(string [, pwd ]) -> PKey
|
|
* OpenSSL::PKey.read(io [, pwd ]) -> PKey
|
|
*
|
|
* Reads a DER or PEM encoded string from _string_ or _io_ and returns an
|
|
* instance of the appropriate PKey class.
|
|
*
|
|
* === Parameters
|
|
* * _string+ is a DER- or PEM-encoded string containing an arbitrary private
|
|
* or public key.
|
|
* * _io_ is an instance of IO containing a DER- or PEM-encoded
|
|
* arbitrary private or public key.
|
|
* * _pwd_ is an optional password in case _string_ or _io_ is an encrypted
|
|
* PEM resource.
|
|
*/
|
|
static VALUE
|
|
ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
BIO *bio;
|
|
VALUE data, pass;
|
|
|
|
rb_scan_args(argc, argv, "11", &data, &pass);
|
|
pass = ossl_pem_passwd_value(pass);
|
|
|
|
bio = ossl_obj2bio(&data);
|
|
if (!(pkey = d2i_PrivateKey_bio(bio, NULL))) {
|
|
OSSL_BIO_reset(bio);
|
|
if (!(pkey = PEM_read_bio_PrivateKey(bio, NULL, ossl_pem_passwd_cb, (void *)pass))) {
|
|
OSSL_BIO_reset(bio);
|
|
if (!(pkey = d2i_PUBKEY_bio(bio, NULL))) {
|
|
OSSL_BIO_reset(bio);
|
|
pkey = PEM_read_bio_PUBKEY(bio, NULL, ossl_pem_passwd_cb, (void *)pass);
|
|
}
|
|
}
|
|
}
|
|
|
|
BIO_free(bio);
|
|
if (!pkey)
|
|
ossl_raise(ePKeyError, "Could not parse PKey");
|
|
|
|
return ossl_pkey_new(pkey);
|
|
}
|
|
|
|
static void
|
|
pkey_check_public_key(EVP_PKEY *pkey)
|
|
{
|
|
void *ptr;
|
|
const BIGNUM *n, *e, *pubkey;
|
|
|
|
if (EVP_PKEY_missing_parameters(pkey))
|
|
ossl_raise(ePKeyError, "parameters missing");
|
|
|
|
ptr = EVP_PKEY_get0(pkey);
|
|
switch (EVP_PKEY_base_id(pkey)) {
|
|
case EVP_PKEY_RSA:
|
|
RSA_get0_key(ptr, &n, &e, NULL);
|
|
if (n && e)
|
|
return;
|
|
break;
|
|
case EVP_PKEY_DSA:
|
|
DSA_get0_key(ptr, &pubkey, NULL);
|
|
if (pubkey)
|
|
return;
|
|
break;
|
|
case EVP_PKEY_DH:
|
|
DH_get0_key(ptr, &pubkey, NULL);
|
|
if (pubkey)
|
|
return;
|
|
break;
|
|
#if !defined(OPENSSL_NO_EC)
|
|
case EVP_PKEY_EC:
|
|
if (EC_KEY_get0_public_key(ptr))
|
|
return;
|
|
break;
|
|
#endif
|
|
default:
|
|
/* unsupported type; assuming ok */
|
|
return;
|
|
}
|
|
ossl_raise(ePKeyError, "public key missing");
|
|
}
|
|
|
|
EVP_PKEY *
|
|
GetPKeyPtr(VALUE obj)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
|
|
GetPKey(obj, pkey);
|
|
|
|
return pkey;
|
|
}
|
|
|
|
EVP_PKEY *
|
|
GetPrivPKeyPtr(VALUE obj)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
|
|
if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) {
|
|
ossl_raise(rb_eArgError, "Private key is needed.");
|
|
}
|
|
GetPKey(obj, pkey);
|
|
|
|
return pkey;
|
|
}
|
|
|
|
EVP_PKEY *
|
|
DupPKeyPtr(VALUE obj)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
|
|
GetPKey(obj, pkey);
|
|
EVP_PKEY_up_ref(pkey);
|
|
|
|
return pkey;
|
|
}
|
|
|
|
/*
|
|
* Private
|
|
*/
|
|
static VALUE
|
|
ossl_pkey_alloc(VALUE klass)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
VALUE obj;
|
|
|
|
obj = NewPKey(klass);
|
|
if (!(pkey = EVP_PKEY_new())) {
|
|
ossl_raise(ePKeyError, NULL);
|
|
}
|
|
SetPKey(obj, pkey);
|
|
|
|
return obj;
|
|
}
|
|
|
|
/*
|
|
* call-seq:
|
|
* PKeyClass.new -> self
|
|
*
|
|
* Because PKey is an abstract class, actually calling this method explicitly
|
|
* will raise a NotImplementedError.
|
|
*/
|
|
static VALUE
|
|
ossl_pkey_initialize(VALUE self)
|
|
{
|
|
if (rb_obj_is_instance_of(self, cPKey)) {
|
|
ossl_raise(rb_eTypeError, "OpenSSL::PKey::PKey can't be instantiated directly");
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/*
|
|
* call-seq:
|
|
* pkey.sign(digest, data) -> String
|
|
*
|
|
* To sign the String _data_, _digest_, an instance of OpenSSL::Digest, must
|
|
* be provided. The return value is again a String containing the signature.
|
|
* A PKeyError is raised should errors occur.
|
|
* Any previous state of the Digest instance is irrelevant to the signature
|
|
* outcome, the digest instance is reset to its initial state during the
|
|
* operation.
|
|
*
|
|
* == Example
|
|
* data = 'Sign me!'
|
|
* digest = OpenSSL::Digest::SHA256.new
|
|
* pkey = OpenSSL::PKey::RSA.new(2048)
|
|
* signature = pkey.sign(digest, data)
|
|
*/
|
|
static VALUE
|
|
ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
const EVP_MD *md;
|
|
EVP_MD_CTX *ctx;
|
|
unsigned int buf_len;
|
|
VALUE str;
|
|
int result;
|
|
|
|
pkey = GetPrivPKeyPtr(self);
|
|
md = ossl_evp_get_digestbyname(digest);
|
|
StringValue(data);
|
|
str = rb_str_new(0, EVP_PKEY_size(pkey));
|
|
|
|
ctx = EVP_MD_CTX_new();
|
|
if (!ctx)
|
|
ossl_raise(ePKeyError, "EVP_MD_CTX_new");
|
|
if (!EVP_SignInit_ex(ctx, md, NULL)) {
|
|
EVP_MD_CTX_free(ctx);
|
|
ossl_raise(ePKeyError, "EVP_SignInit_ex");
|
|
}
|
|
if (!EVP_SignUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data))) {
|
|
EVP_MD_CTX_free(ctx);
|
|
ossl_raise(ePKeyError, "EVP_SignUpdate");
|
|
}
|
|
result = EVP_SignFinal(ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey);
|
|
EVP_MD_CTX_free(ctx);
|
|
if (!result)
|
|
ossl_raise(ePKeyError, "EVP_SignFinal");
|
|
rb_str_set_len(str, buf_len);
|
|
|
|
return str;
|
|
}
|
|
|
|
/*
|
|
* call-seq:
|
|
* pkey.verify(digest, signature, data) -> String
|
|
*
|
|
* To verify the String _signature_, _digest_, an instance of
|
|
* OpenSSL::Digest, must be provided to re-compute the message digest of the
|
|
* original _data_, also a String. The return value is +true+ if the
|
|
* signature is valid, +false+ otherwise. A PKeyError is raised should errors
|
|
* occur.
|
|
* Any previous state of the Digest instance is irrelevant to the validation
|
|
* outcome, the digest instance is reset to its initial state during the
|
|
* operation.
|
|
*
|
|
* == Example
|
|
* data = 'Sign me!'
|
|
* digest = OpenSSL::Digest::SHA256.new
|
|
* pkey = OpenSSL::PKey::RSA.new(2048)
|
|
* signature = pkey.sign(digest, data)
|
|
* pub_key = pkey.public_key
|
|
* puts pub_key.verify(digest, signature, data) # => true
|
|
*/
|
|
static VALUE
|
|
ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
|
|
{
|
|
EVP_PKEY *pkey;
|
|
const EVP_MD *md;
|
|
EVP_MD_CTX *ctx;
|
|
int siglen, result;
|
|
|
|
GetPKey(self, pkey);
|
|
pkey_check_public_key(pkey);
|
|
md = ossl_evp_get_digestbyname(digest);
|
|
StringValue(sig);
|
|
siglen = RSTRING_LENINT(sig);
|
|
StringValue(data);
|
|
|
|
ctx = EVP_MD_CTX_new();
|
|
if (!ctx)
|
|
ossl_raise(ePKeyError, "EVP_MD_CTX_new");
|
|
if (!EVP_VerifyInit_ex(ctx, md, NULL)) {
|
|
EVP_MD_CTX_free(ctx);
|
|
ossl_raise(ePKeyError, "EVP_VerifyInit_ex");
|
|
}
|
|
if (!EVP_VerifyUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data))) {
|
|
EVP_MD_CTX_free(ctx);
|
|
ossl_raise(ePKeyError, "EVP_VerifyUpdate");
|
|
}
|
|
result = EVP_VerifyFinal(ctx, (unsigned char *)RSTRING_PTR(sig), siglen, pkey);
|
|
EVP_MD_CTX_free(ctx);
|
|
switch (result) {
|
|
case 0:
|
|
ossl_clear_error();
|
|
return Qfalse;
|
|
case 1:
|
|
return Qtrue;
|
|
default:
|
|
ossl_raise(ePKeyError, "EVP_VerifyFinal");
|
|
}
|
|
}
|
|
|
|
/*
|
|
* INIT
|
|
*/
|
|
void
|
|
Init_ossl_pkey(void)
|
|
{
|
|
#if 0
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
#endif
|
|
|
|
/* Document-module: OpenSSL::PKey
|
|
*
|
|
* == Asymmetric Public Key Algorithms
|
|
*
|
|
* Asymmetric public key algorithms solve the problem of establishing and
|
|
* sharing secret keys to en-/decrypt messages. The key in such an
|
|
* algorithm consists of two parts: a public key that may be distributed
|
|
* to others and a private key that needs to remain secret.
|
|
*
|
|
* Messages encrypted with a public key can only be decrypted by
|
|
* recipients that are in possession of the associated private key.
|
|
* Since public key algorithms are considerably slower than symmetric
|
|
* key algorithms (cf. OpenSSL::Cipher) they are often used to establish
|
|
* a symmetric key shared between two parties that are in possession of
|
|
* each other's public key.
|
|
*
|
|
* Asymmetric algorithms offer a lot of nice features that are used in a
|
|
* lot of different areas. A very common application is the creation and
|
|
* validation of digital signatures. To sign a document, the signatory
|
|
* generally uses a message digest algorithm (cf. OpenSSL::Digest) to
|
|
* compute a digest of the document that is then encrypted (i.e. signed)
|
|
* using the private key. Anyone in possession of the public key may then
|
|
* verify the signature by computing the message digest of the original
|
|
* document on their own, decrypting the signature using the signatory's
|
|
* public key and comparing the result to the message digest they
|
|
* previously computed. The signature is valid if and only if the
|
|
* decrypted signature is equal to this message digest.
|
|
*
|
|
* The PKey module offers support for three popular public/private key
|
|
* algorithms:
|
|
* * RSA (OpenSSL::PKey::RSA)
|
|
* * DSA (OpenSSL::PKey::DSA)
|
|
* * Elliptic Curve Cryptography (OpenSSL::PKey::EC)
|
|
* Each of these implementations is in fact a sub-class of the abstract
|
|
* PKey class which offers the interface for supporting digital signatures
|
|
* in the form of PKey#sign and PKey#verify.
|
|
*
|
|
* == Diffie-Hellman Key Exchange
|
|
*
|
|
* Finally PKey also features OpenSSL::PKey::DH, an implementation of
|
|
* the Diffie-Hellman key exchange protocol based on discrete logarithms
|
|
* in finite fields, the same basis that DSA is built on.
|
|
* The Diffie-Hellman protocol can be used to exchange (symmetric) keys
|
|
* over insecure channels without needing any prior joint knowledge
|
|
* between the participating parties. As the security of DH demands
|
|
* relatively long "public keys" (i.e. the part that is overtly
|
|
* transmitted between participants) DH tends to be quite slow. If
|
|
* security or speed is your primary concern, OpenSSL::PKey::EC offers
|
|
* another implementation of the Diffie-Hellman protocol.
|
|
*
|
|
*/
|
|
mPKey = rb_define_module_under(mOSSL, "PKey");
|
|
|
|
/* Document-class: OpenSSL::PKey::PKeyError
|
|
*
|
|
*Raised when errors occur during PKey#sign or PKey#verify.
|
|
*/
|
|
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
|
|
|
|
/* Document-class: OpenSSL::PKey::PKey
|
|
*
|
|
* An abstract class that bundles signature creation (PKey#sign) and
|
|
* validation (PKey#verify) that is common to all implementations except
|
|
* OpenSSL::PKey::DH
|
|
* * OpenSSL::PKey::RSA
|
|
* * OpenSSL::PKey::DSA
|
|
* * OpenSSL::PKey::EC
|
|
*/
|
|
cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
|
|
|
|
rb_define_module_function(mPKey, "read", ossl_pkey_new_from_data, -1);
|
|
|
|
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();
|
|
}
|