2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-20 06:55:09 +03:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 20:12:24 +04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE mPKey;
|
|
|
|
VALUE cPKey;
|
|
|
|
VALUE ePKeyError;
|
2016-08-29 08:47:09 +03:00
|
|
|
static ID id_private_q;
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
/*
|
|
|
|
* callback for generating keys
|
|
|
|
*/
|
2011-09-01 11:42:29 +04:00
|
|
|
int
|
|
|
|
ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
|
|
|
|
{
|
2011-10-19 05:15:35 +04:00
|
|
|
VALUE ary;
|
2011-09-01 11:42:29 +04:00
|
|
|
struct ossl_generate_cb_arg *arg;
|
|
|
|
int state;
|
|
|
|
|
2016-06-05 18:35:12 +03:00
|
|
|
arg = (struct ossl_generate_cb_arg *)BN_GENCB_get_arg(cb);
|
2011-09-01 11:42:29 +04:00
|
|
|
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'
|
|
|
|
*/
|
2011-10-19 05:15:35 +04:00
|
|
|
rb_protect(rb_yield, ary, &state);
|
2011-09-01 11:42:29 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:19:07 +03:00
|
|
|
static void
|
|
|
|
ossl_evp_pkey_free(void *ptr)
|
|
|
|
{
|
|
|
|
EVP_PKEY_free(ptr);
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
2014-12-13 02:19:07 +03:00
|
|
|
const rb_data_type_t ossl_evp_pkey_type = {
|
|
|
|
"OpenSSL/EVP_PKEY",
|
|
|
|
{
|
|
|
|
0, ossl_evp_pkey_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2016-11-30 17:41:46 +03:00
|
|
|
static VALUE
|
|
|
|
pkey_new0(EVP_PKEY *pkey)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
2016-12-22 04:43:41 +03:00
|
|
|
VALUE obj;
|
|
|
|
int type;
|
2016-11-30 17:41:46 +03:00
|
|
|
|
2016-12-22 04:43:41 +03:00
|
|
|
if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE)
|
|
|
|
ossl_raise(rb_eRuntimeError, "pkey is empty");
|
|
|
|
|
|
|
|
switch (type) {
|
2003-07-23 20:12:24 +04:00
|
|
|
#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);
|
2007-04-03 11:02:44 +04:00
|
|
|
#endif
|
2017-09-03 15:35:27 +03:00
|
|
|
#if !defined(OPENSSL_NO_EC)
|
2007-04-03 11:02:44 +04:00
|
|
|
case EVP_PKEY_EC:
|
|
|
|
return ossl_ec_new(pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
#endif
|
|
|
|
default:
|
2016-12-22 04:43:41 +03:00
|
|
|
obj = NewPKey(cPKey);
|
|
|
|
SetPKey(obj, pkey);
|
|
|
|
return obj;
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-14 00:10:27 +04:00
|
|
|
VALUE
|
2016-11-30 17:41:46 +03:00
|
|
|
ossl_pkey_new(EVP_PKEY *pkey)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
2016-11-30 17:41:46 +03:00
|
|
|
VALUE obj;
|
|
|
|
int status;
|
2011-05-14 00:10:27 +04:00
|
|
|
|
2016-11-30 17:41:46 +03:00
|
|
|
obj = rb_protect((VALUE (*)(VALUE))pkey_new0, (VALUE)pkey, &status);
|
|
|
|
if (status) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
rb_jump_tag(status);
|
2011-05-14 00:10:27 +04:00
|
|
|
}
|
|
|
|
|
2016-11-30 17:41:46 +03:00
|
|
|
return obj;
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
2011-06-13 00:39:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-08-29 08:47:09 +03:00
|
|
|
* OpenSSL::PKey.read(string [, pwd ]) -> PKey
|
|
|
|
* OpenSSL::PKey.read(io [, pwd ]) -> PKey
|
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* Reads a DER or PEM encoded string from _string_ or _io_ and returns an
|
2016-08-29 08:47:09 +03:00
|
|
|
* instance of the appropriate PKey class.
|
2011-06-13 00:39:38 +04:00
|
|
|
*
|
|
|
|
* === Parameters
|
2017-09-03 15:35:27 +03:00
|
|
|
* * _string+ is a DER- or PEM-encoded string containing an arbitrary private
|
2016-08-29 08:47:09 +03:00
|
|
|
* or public key.
|
2017-09-03 15:35:27 +03:00
|
|
|
* * _io_ is an instance of IO containing a DER- or PEM-encoded
|
2016-08-29 08:47:09 +03:00
|
|
|
* arbitrary private or public key.
|
2017-09-03 15:35:27 +03:00
|
|
|
* * _pwd_ is an optional password in case _string_ or _io_ is an encrypted
|
2016-08-29 08:47:09 +03:00
|
|
|
* PEM resource.
|
2011-06-13 00:39:38 +04:00
|
|
|
*/
|
2011-07-01 00:20:32 +04:00
|
|
|
static VALUE
|
2011-06-13 00:39:38 +04:00
|
|
|
ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2016-05-20 18:05:25 +03:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
BIO *bio;
|
|
|
|
VALUE data, pass;
|
2011-06-13 00:39:38 +04:00
|
|
|
|
2016-05-20 18:05:25 +03:00
|
|
|
rb_scan_args(argc, argv, "11", &data, &pass);
|
|
|
|
pass = ossl_pem_passwd_value(pass);
|
2011-06-13 00:39:38 +04:00
|
|
|
|
2017-08-10 12:23:45 +03:00
|
|
|
bio = ossl_obj2bio(&data);
|
2016-05-20 18:05:25 +03:00
|
|
|
if (!(pkey = d2i_PrivateKey_bio(bio, NULL))) {
|
2011-06-22 12:41:08 +04:00
|
|
|
OSSL_BIO_reset(bio);
|
2016-05-20 18:05:25 +03:00
|
|
|
if (!(pkey = PEM_read_bio_PrivateKey(bio, NULL, ossl_pem_passwd_cb, (void *)pass))) {
|
2011-06-22 12:41:08 +04:00
|
|
|
OSSL_BIO_reset(bio);
|
2011-06-13 00:39:38 +04:00
|
|
|
if (!(pkey = d2i_PUBKEY_bio(bio, NULL))) {
|
2011-06-22 12:41:08 +04:00
|
|
|
OSSL_BIO_reset(bio);
|
2016-05-20 18:05:25 +03:00
|
|
|
pkey = PEM_read_bio_PUBKEY(bio, NULL, ossl_pem_passwd_cb, (void *)pass);
|
2011-06-13 00:39:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_free(bio);
|
|
|
|
if (!pkey)
|
2016-08-29 08:47:09 +03:00
|
|
|
ossl_raise(ePKeyError, "Could not parse PKey");
|
|
|
|
|
2011-06-13 00:39:38 +04:00
|
|
|
return ossl_pkey_new(pkey);
|
|
|
|
}
|
|
|
|
|
2017-11-25 17:12:08 +03:00
|
|
|
void
|
|
|
|
ossl_pkey_check_public_key(const EVP_PKEY *pkey)
|
2016-11-30 17:41:46 +03:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
const BIGNUM *n, *e, *pubkey;
|
|
|
|
|
|
|
|
if (EVP_PKEY_missing_parameters(pkey))
|
|
|
|
ossl_raise(ePKeyError, "parameters missing");
|
|
|
|
|
2017-11-25 17:12:08 +03:00
|
|
|
/* OpenSSL < 1.1.0 takes non-const pointer */
|
|
|
|
ptr = EVP_PKEY_get0((EVP_PKEY *)pkey);
|
2016-11-30 17:41:46 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
EVP_PKEY *
|
|
|
|
GetPKeyPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
GetPKey(obj, pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *
|
|
|
|
GetPrivPKeyPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2015-02-16 07:08:52 +03:00
|
|
|
if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) {
|
2003-07-23 20:12:24 +04:00
|
|
|
ossl_raise(rb_eArgError, "Private key is needed.");
|
|
|
|
}
|
2017-09-03 15:35:27 +03:00
|
|
|
GetPKey(obj, pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *
|
2005-03-09 13:45:42 +03:00
|
|
|
DupPKeyPtr(VALUE obj)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
GetPKey(obj, pkey);
|
2016-06-05 15:46:05 +03:00
|
|
|
EVP_PKEY_up_ref(pkey);
|
2005-03-09 13:45:42 +03:00
|
|
|
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_pkey_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 08:55:02 +03:00
|
|
|
obj = NewPKey(klass);
|
2003-07-23 20:12:24 +04:00
|
|
|
if (!(pkey = EVP_PKEY_new())) {
|
|
|
|
ossl_raise(ePKeyError, NULL);
|
|
|
|
}
|
2015-05-29 08:55:02 +03:00
|
|
|
SetPKey(obj, pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* PKeyClass.new -> self
|
|
|
|
*
|
|
|
|
* Because PKey is an abstract class, actually calling this method explicitly
|
2017-09-03 15:35:27 +03:00
|
|
|
* will raise a NotImplementedError.
|
2011-05-19 02:22:34 +04:00
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_pkey_initialize(VALUE self)
|
|
|
|
{
|
|
|
|
if (rb_obj_is_instance_of(self, cPKey)) {
|
2016-12-22 04:43:41 +03:00
|
|
|
ossl_raise(rb_eTypeError, "OpenSSL::PKey::PKey can't be instantiated directly");
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* pkey.sign(digest, data) -> String
|
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* To sign the String _data_, _digest_, an instance of OpenSSL::Digest, must
|
|
|
|
* be provided. The return value is again a String containing the signature.
|
2011-05-19 02:22:34 +04:00
|
|
|
* A PKeyError is raised should errors occur.
|
2017-09-03 15:35:27 +03:00
|
|
|
* Any previous state of the Digest instance is irrelevant to the signature
|
2011-05-19 02:22:34 +04:00
|
|
|
* 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)
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2016-06-05 18:35:12 +03:00
|
|
|
const EVP_MD *md;
|
|
|
|
EVP_MD_CTX *ctx;
|
2008-07-22 19:34:23 +04:00
|
|
|
unsigned int buf_len;
|
2003-07-23 20:12:24 +04:00
|
|
|
VALUE str;
|
2015-11-13 08:01:15 +03:00
|
|
|
int result;
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
pkey = GetPrivPKeyPtr(self);
|
2017-09-03 15:35:27 +03:00
|
|
|
md = ossl_evp_get_digestbyname(digest);
|
2003-07-23 20:12:24 +04:00
|
|
|
StringValue(data);
|
2016-11-30 17:41:46 +03:00
|
|
|
str = rb_str_new(0, EVP_PKEY_size(pkey));
|
2016-06-05 18:35:12 +03:00
|
|
|
|
|
|
|
ctx = EVP_MD_CTX_new();
|
|
|
|
if (!ctx)
|
|
|
|
ossl_raise(ePKeyError, "EVP_MD_CTX_new");
|
2016-11-30 17:41:46 +03:00
|
|
|
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");
|
|
|
|
}
|
2016-06-05 18:35:12 +03:00
|
|
|
result = EVP_SignFinal(ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey);
|
|
|
|
EVP_MD_CTX_free(ctx);
|
2015-11-13 08:01:15 +03:00
|
|
|
if (!result)
|
2016-11-30 17:41:46 +03:00
|
|
|
ossl_raise(ePKeyError, "EVP_SignFinal");
|
2006-08-31 14:30:33 +04:00
|
|
|
rb_str_set_len(str, buf_len);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* pkey.verify(digest, signature, data) -> String
|
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* To verify the String _signature_, _digest_, an instance of
|
2011-05-19 02:22:34 +04:00
|
|
|
* OpenSSL::Digest, must be provided to re-compute the message digest of the
|
2017-09-03 15:35:27 +03:00
|
|
|
* original _data_, also a String. The return value is +true+ if the
|
2011-05-19 02:22:34 +04:00
|
|
|
* signature is valid, +false+ otherwise. A PKeyError is raised should errors
|
|
|
|
* occur.
|
2017-09-03 15:35:27 +03:00
|
|
|
* Any previous state of the Digest instance is irrelevant to the validation
|
2011-05-19 02:22:34 +04:00
|
|
|
* 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
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2016-06-05 18:35:12 +03:00
|
|
|
const EVP_MD *md;
|
|
|
|
EVP_MD_CTX *ctx;
|
2016-11-30 17:41:46 +03:00
|
|
|
int siglen, result;
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
GetPKey(self, pkey);
|
2017-11-25 17:12:08 +03:00
|
|
|
ossl_pkey_check_public_key(pkey);
|
2017-09-03 15:35:27 +03:00
|
|
|
md = ossl_evp_get_digestbyname(digest);
|
2003-07-23 20:12:24 +04:00
|
|
|
StringValue(sig);
|
2016-11-30 17:41:46 +03:00
|
|
|
siglen = RSTRING_LENINT(sig);
|
2003-07-23 20:12:24 +04:00
|
|
|
StringValue(data);
|
2016-06-05 18:35:12 +03:00
|
|
|
|
|
|
|
ctx = EVP_MD_CTX_new();
|
|
|
|
if (!ctx)
|
|
|
|
ossl_raise(ePKeyError, "EVP_MD_CTX_new");
|
2016-11-30 17:41:46 +03:00
|
|
|
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);
|
2016-06-05 18:35:12 +03:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2014-04-16 04:51:18 +04:00
|
|
|
switch (result) {
|
2003-07-23 20:12:24 +04:00
|
|
|
case 0:
|
2016-08-29 08:47:09 +03:00
|
|
|
ossl_clear_error();
|
2003-07-23 20:12:24 +04:00
|
|
|
return Qfalse;
|
|
|
|
case 1:
|
|
|
|
return Qtrue;
|
|
|
|
default:
|
2016-11-30 17:41:46 +03:00
|
|
|
ossl_raise(ePKeyError, "EVP_VerifyFinal");
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 09:25:32 +04:00
|
|
|
Init_ossl_pkey(void)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
2018-02-16 11:39:48 +03:00
|
|
|
#undef rb_intern
|
2010-12-06 03:54:44 +03:00
|
|
|
#if 0
|
2016-08-29 08:47:09 +03:00
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
2007-03-12 05:01:19 +03:00
|
|
|
#endif
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/* 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
|
2011-05-19 04:07:25 +04:00
|
|
|
* to others and a private key that needs to remain secret.
|
2011-05-19 02:22:34 +04:00
|
|
|
*
|
2016-02-03 02:25:40 +03:00
|
|
|
* Messages encrypted with a public key can only be decrypted by
|
2011-05-19 02:22:34 +04:00
|
|
|
* 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
|
|
|
|
*
|
2011-05-19 04:07:25 +04:00
|
|
|
* Finally PKey also features OpenSSL::PKey::DH, an implementation of
|
2011-05-19 02:22:34 +04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
mPKey = rb_define_module_under(mOSSL, "PKey");
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/* Document-class: OpenSSL::PKey::PKeyError
|
|
|
|
*
|
|
|
|
*Raised when errors occur during PKey#sign or PKey#verify.
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/* Document-class: OpenSSL::PKey::PKey
|
|
|
|
*
|
|
|
|
* An abstract class that bundles signature creation (PKey#sign) and
|
2011-08-16 23:59:38 +04:00
|
|
|
* validation (PKey#verify) that is common to all implementations except
|
|
|
|
* OpenSSL::PKey::DH
|
2011-05-19 02:22:34 +04:00
|
|
|
* * OpenSSL::PKey::RSA
|
|
|
|
* * OpenSSL::PKey::DSA
|
|
|
|
* * OpenSSL::PKey::EC
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2011-06-13 00:39:38 +04:00
|
|
|
rb_define_module_function(mPKey, "read", ossl_pkey_new_from_data, -1);
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
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);
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
id_private_q = rb_intern("private?");
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
2007-04-03 11:02:44 +04:00
|
|
|
* INIT rsa, dsa, dh, ec
|
2003-07-23 20:12:24 +04:00
|
|
|
*/
|
|
|
|
Init_ossl_rsa();
|
|
|
|
Init_ossl_dsa();
|
|
|
|
Init_ossl_dh();
|
2007-04-03 11:02:44 +04:00
|
|
|
Init_ossl_ec();
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|