2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* $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;
|
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
2007-04-03 11:02:44 +04:00
|
|
|
#endif
|
|
|
|
#if !defined(OPENSSL_NO_EC) && (OPENSSL_VERSION_NUMBER >= 0x0090802fL)
|
|
|
|
case EVP_PKEY_EC:
|
|
|
|
return ossl_ec_new(pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ossl_raise(ePKeyError, "unsupported key type");
|
|
|
|
}
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
}
|
|
|
|
|
2011-05-14 00:10:27 +04:00
|
|
|
VALUE
|
|
|
|
ossl_pkey_new_from_file(VALUE filename)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
2011-05-14 00:10:27 +04:00
|
|
|
FILE *fp;
|
2003-07-23 20:12:24 +04:00
|
|
|
EVP_PKEY *pkey;
|
2011-05-14 00:10:27 +04:00
|
|
|
|
|
|
|
SafeStringValue(filename);
|
|
|
|
if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
|
|
|
|
ossl_raise(ePKeyError, "%s", strerror(errno));
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
2011-05-14 00:10:27 +04:00
|
|
|
|
|
|
|
pkey = PEM_read_PrivateKey(fp, NULL, ossl_pem_passwd_cb, NULL);
|
|
|
|
fclose(fp);
|
|
|
|
if (!pkey) {
|
|
|
|
ossl_raise(ePKeyError, NULL);
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
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;
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2005-03-09 13:45:42 +03:00
|
|
|
if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
|
2003-07-23 20:12:24 +04:00
|
|
|
ossl_raise(rb_eArgError, "Private key is needed.");
|
|
|
|
}
|
2005-03-09 13:45:42 +03:00
|
|
|
SafeGetPKey(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
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
SafeGetPKey(obj, pkey);
|
2005-03-09 13:45:42 +03:00
|
|
|
CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
|
|
|
|
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *
|
|
|
|
DupPrivPKeyPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2005-03-09 13:45:42 +03:00
|
|
|
if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
|
2003-07-23 20:12:24 +04:00
|
|
|
ossl_raise(rb_eArgError, "Private key is needed.");
|
|
|
|
}
|
2005-03-09 13:45:42 +03:00
|
|
|
SafeGetPKey(obj, pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* PKeyClass.new -> self
|
|
|
|
*
|
|
|
|
* Because PKey is an abstract class, actually calling this method explicitly
|
|
|
|
* will raise a +NotImplementedError+.
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-19 02:22:34 +04:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (rb_funcall(self, id_private_q, 0, NULL) != Qtrue) {
|
|
|
|
ossl_raise(rb_eArgError, "Private key is needed.");
|
|
|
|
}
|
2005-03-09 13:45:42 +03:00
|
|
|
GetPKey(self, pkey);
|
2003-07-23 20:12:24 +04:00
|
|
|
EVP_SignInit(&ctx, GetDigestPtr(digest));
|
|
|
|
StringValue(data);
|
2006-08-31 14:30:33 +04:00
|
|
|
EVP_SignUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
|
2003-09-17 13:05:02 +04:00
|
|
|
str = rb_str_new(0, EVP_PKEY_size(pkey)+16);
|
2008-07-22 19:34:23 +04:00
|
|
|
if (!EVP_SignFinal(&ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey))
|
2003-07-23 20:12:24 +04:00
|
|
|
ossl_raise(ePKeyError, NULL);
|
2009-03-13 10:45:35 +03:00
|
|
|
assert((long)buf_len <= RSTRING_LEN(str));
|
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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
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);
|
2006-08-31 14:30:33 +04:00
|
|
|
EVP_VerifyUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
|
2011-03-24 10:29:21 +03:00
|
|
|
switch (EVP_VerifyFinal(&ctx, (unsigned char *)RSTRING_PTR(sig), RSTRING_LENINT(sig), pkey)) {
|
2003-07-23 20:12:24 +04:00
|
|
|
case 0:
|
|
|
|
return Qfalse;
|
|
|
|
case 1:
|
|
|
|
return Qtrue;
|
|
|
|
default:
|
|
|
|
ossl_raise(ePKeyError, NULL);
|
|
|
|
}
|
|
|
|
return Qnil; /* dummy */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Init_ossl_pkey()
|
|
|
|
{
|
2010-12-06 03:54:44 +03:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
|
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
|
|
|
|
* to others and a private key that needs to remain secret.
|
|
|
|
*
|
|
|
|
* Messages encrypted with a public key can only be encrypted 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.
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
* validation (PKey#verify) that is common to all implementations:
|
|
|
|
* * OpenSSL::PKey::RSA
|
|
|
|
* * OpenSSL::PKey::DSA
|
|
|
|
* * OpenSSL::PKey::EC
|
|
|
|
* * OpenSSL::PKey::DH
|
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|