2007-04-03 11:02:44 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006-2007 Technorama Ltd. <oss-ruby@technorama.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ossl.h"
|
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
#if !defined(OPENSSL_NO_EC)
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
#define EXPORT_PEM 0
|
|
|
|
#define EXPORT_DER 1
|
|
|
|
|
2014-12-13 02:19:13 +03:00
|
|
|
static const rb_data_type_t ossl_ec_group_type;
|
2014-12-13 02:19:24 +03:00
|
|
|
static const rb_data_type_t ossl_ec_point_type;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
#define GetPKeyEC(obj, pkey) do { \
|
2011-03-06 16:21:31 +03:00
|
|
|
GetPKey((obj), (pkey)); \
|
openssl: adapt OpenSSL::PKey to OpenSSL 1.1.0 opaque structs
* ext/openssl/openssl_missing.[ch]: Implement EVP_PKEY_get0_*() and
{RSA,DSA,EC_KEY,DH}_get0_*() functions.
OpenSSL 1.1.0 makes EVP_PKEY/RSA/DSA/DH opaque. We used to provide
setter methods for each parameter of each PKey type, for example
PKey::RSA#e=, but this is no longer possible because the new API
RSA_set0_key() requires the 'n' at the same time. This commit adds
deprecation warning to them and adds PKey::*#set_* methods as direct
wrapper for those new APIs. For example, 'rsa.e = 3' now needs to be
rewritten as 'rsa.set_key(rsa.n, 3, rsa.d)'.
[ruby-core:75225] [Feature #12324]
* ext/openssl/ossl_pkey*.[ch]: Use the new accessor functions. Implement
RSA#set_{key,factors,crt_params}, DSA#set_{key,pqg}, DH#set_{key,pqg}.
Emit a warning with rb_warning() when old setter methods are used.
* test/drb/ut_array_drbssl.rb, test/drb/ut_drb_drbssl.rb,
test/rubygems/test_gem_remote_fetcher.rb: Don't set a priv_key for DH
object that are used in tmp_dh_callback. Generating a new key pair
every time should be fine - actually the private exponent is ignored
in OpenSSL >= 1.0.2f/1.0.1r even if we explicitly set.
https://www.openssl.org/news/secadv/20160128.txt
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-06-05 18:00:47 +03:00
|
|
|
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { \
|
2007-04-03 11:02:44 +04:00
|
|
|
ossl_raise(rb_eRuntimeError, "THIS IS NOT A EC PKEY!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2016-09-08 04:09:19 +03:00
|
|
|
#define GetEC(obj, key) do { \
|
|
|
|
EVP_PKEY *_pkey; \
|
|
|
|
GetPKeyEC(obj, _pkey); \
|
|
|
|
(key) = EVP_PKEY_get0_EC_KEY(_pkey); \
|
|
|
|
} while (0)
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
#define GetECGroup(obj, group) do { \
|
|
|
|
TypedData_Get_Struct(obj, EC_GROUP, &ossl_ec_group_type, group); \
|
2011-03-06 16:21:31 +03:00
|
|
|
if ((group) == NULL) \
|
2016-09-08 04:09:19 +03:00
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP is not initialized"); \
|
|
|
|
} while (0)
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
#define GetECPoint(obj, point) do { \
|
|
|
|
TypedData_Get_Struct(obj, EC_POINT, &ossl_ec_point_type, point); \
|
2011-03-06 16:21:31 +03:00
|
|
|
if ((point) == NULL) \
|
2016-09-08 04:09:19 +03:00
|
|
|
ossl_raise(eEC_POINT, "EC_POINT is not initialized"); \
|
|
|
|
} while (0)
|
|
|
|
#define GetECPointGroup(obj, group) do { \
|
|
|
|
VALUE _group = rb_attr_get(obj, id_i_group); \
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(_group, group); \
|
2016-09-08 04:09:19 +03:00
|
|
|
} while (0)
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
VALUE cEC;
|
|
|
|
VALUE eECError;
|
|
|
|
VALUE cEC_GROUP;
|
|
|
|
VALUE eEC_GROUP;
|
|
|
|
VALUE cEC_POINT;
|
|
|
|
VALUE eEC_POINT;
|
|
|
|
|
2020-06-30 09:46:14 +03:00
|
|
|
static ID s_GFp, s_GF2m;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
static ID ID_uncompressed;
|
|
|
|
static ID ID_compressed;
|
|
|
|
static ID ID_hybrid;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
static ID id_i_group;
|
|
|
|
|
|
|
|
static VALUE ec_group_new(const EC_GROUP *group);
|
|
|
|
static VALUE ec_point_new(const EC_POINT *point, const EC_GROUP *group);
|
2016-08-29 08:47:09 +03:00
|
|
|
|
2016-05-24 19:30:15 +03:00
|
|
|
/*
|
|
|
|
* Creates a new EC_KEY on the EC group obj. arg can be an EC::Group or a String
|
|
|
|
* representing an OID.
|
|
|
|
*/
|
|
|
|
static EC_KEY *
|
|
|
|
ec_key_new_from_group(VALUE arg)
|
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
|
|
|
|
|
|
|
if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
|
|
|
|
EC_GROUP *group;
|
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(arg, group);
|
2016-05-24 19:30:15 +03:00
|
|
|
if (!(ec = EC_KEY_new()))
|
|
|
|
ossl_raise(eECError, NULL);
|
|
|
|
|
|
|
|
if (!EC_KEY_set_group(ec, group)) {
|
|
|
|
EC_KEY_free(ec);
|
|
|
|
ossl_raise(eECError, NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int nid = OBJ_sn2nid(StringValueCStr(arg));
|
|
|
|
|
|
|
|
if (nid == NID_undef)
|
|
|
|
ossl_raise(eECError, "invalid curve name");
|
|
|
|
|
|
|
|
if (!(ec = EC_KEY_new_by_curve_name(nid)))
|
|
|
|
ossl_raise(eECError, NULL);
|
|
|
|
|
|
|
|
EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
|
|
|
|
EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* EC.generate(ec_group) -> ec
|
|
|
|
* EC.generate(string) -> ec
|
|
|
|
*
|
|
|
|
* Creates a new EC instance with a new random private and public key.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_s_generate(VALUE klass, VALUE arg)
|
|
|
|
{
|
2017-03-16 10:09:35 +03:00
|
|
|
EVP_PKEY *pkey;
|
2016-05-24 19:30:15 +03:00
|
|
|
EC_KEY *ec;
|
|
|
|
VALUE obj;
|
|
|
|
|
2017-03-16 10:09:35 +03:00
|
|
|
obj = rb_obj_alloc(klass);
|
2016-05-24 19:30:15 +03:00
|
|
|
|
2017-03-16 10:09:35 +03:00
|
|
|
ec = ec_key_new_from_group(arg);
|
2021-04-12 12:32:40 +03:00
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec) != 1) {
|
|
|
|
EVP_PKEY_free(pkey);
|
2017-03-16 10:09:35 +03:00
|
|
|
EC_KEY_free(ec);
|
|
|
|
ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
|
2016-05-24 19:30:15 +03:00
|
|
|
}
|
2021-04-12 12:32:40 +03:00
|
|
|
RTYPEDDATA_DATA(obj) = pkey;
|
|
|
|
|
2016-05-24 19:30:15 +03:00
|
|
|
if (!EC_KEY_generate_key(ec))
|
|
|
|
ossl_raise(eECError, "EC_KEY_generate_key");
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::PKey::EC.new
|
|
|
|
* OpenSSL::PKey::EC.new(ec_key)
|
|
|
|
* OpenSSL::PKey::EC.new(ec_group)
|
|
|
|
* OpenSSL::PKey::EC.new("secp112r1")
|
|
|
|
* OpenSSL::PKey::EC.new(pem_string [, pwd])
|
|
|
|
* OpenSSL::PKey::EC.new(der_string)
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* Creates a new EC object from given arguments.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2021-04-12 12:32:40 +03:00
|
|
|
EC_KEY *ec;
|
|
|
|
BIO *in;
|
2007-04-03 11:02:44 +04:00
|
|
|
VALUE arg, pass;
|
2021-04-12 12:32:40 +03:00
|
|
|
int type;
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2021-04-12 12:32:40 +03:00
|
|
|
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
|
|
|
|
if (pkey)
|
|
|
|
rb_raise(rb_eTypeError, "pkey already initialized");
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "02", &arg, &pass);
|
|
|
|
if (NIL_P(arg)) {
|
2016-05-24 19:30:15 +03:00
|
|
|
if (!(ec = EC_KEY_new()))
|
2021-04-12 12:32:40 +03:00
|
|
|
ossl_raise(eECError, "EC_KEY_new");
|
|
|
|
goto legacy;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
|
|
|
|
ec = ec_key_new_from_group(arg);
|
|
|
|
goto legacy;
|
|
|
|
}
|
2016-05-24 19:30:15 +03:00
|
|
|
|
2021-04-12 12:32:40 +03:00
|
|
|
pass = ossl_pem_passwd_value(pass);
|
|
|
|
arg = ossl_to_der_if_possible(arg);
|
|
|
|
in = ossl_obj2bio(&arg);
|
|
|
|
|
|
|
|
pkey = ossl_pkey_read_generic(in, pass);
|
|
|
|
BIO_free(in);
|
|
|
|
if (!pkey) {
|
|
|
|
ossl_clear_error();
|
|
|
|
ec = ec_key_new_from_group(arg);
|
|
|
|
goto legacy;
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:32:40 +03:00
|
|
|
type = EVP_PKEY_base_id(pkey);
|
|
|
|
if (type != EVP_PKEY_EC) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
2021-04-12 12:32:40 +03:00
|
|
|
RTYPEDDATA_DATA(self) = pkey;
|
|
|
|
return self;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2021-04-12 12:32:40 +03:00
|
|
|
legacy:
|
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec) != 1) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
EC_KEY_free(ec);
|
|
|
|
ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
|
|
|
|
}
|
|
|
|
RTYPEDDATA_DATA(self) = pkey;
|
2007-04-03 11:02:44 +04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:33:59 +03:00
|
|
|
#ifndef HAVE_EVP_PKEY_DUP
|
2016-06-19 12:29:59 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EC_KEY *ec, *ec_new;
|
|
|
|
|
2021-04-12 12:32:40 +03:00
|
|
|
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
|
|
|
|
if (pkey)
|
|
|
|
rb_raise(rb_eTypeError, "pkey already initialized");
|
2017-09-03 15:35:27 +03:00
|
|
|
GetEC(other, ec);
|
2016-06-19 12:29:59 +03:00
|
|
|
|
|
|
|
ec_new = EC_KEY_dup(ec);
|
|
|
|
if (!ec_new)
|
|
|
|
ossl_raise(eECError, "EC_KEY_dup");
|
2021-04-12 12:32:40 +03:00
|
|
|
|
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec_new) != 1) {
|
|
|
|
EC_KEY_free(ec_new);
|
|
|
|
ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
|
2016-06-19 12:29:59 +03:00
|
|
|
}
|
2021-04-12 12:32:40 +03:00
|
|
|
RTYPEDDATA_DATA(self) = pkey;
|
2016-06-19 12:29:59 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2021-04-22 10:33:59 +03:00
|
|
|
#endif
|
2016-06-19 12:29:59 +03:00
|
|
|
|
2007-04-03 11:02:44 +04:00
|
|
|
/*
|
2016-09-08 04:09:19 +03:00
|
|
|
* call-seq:
|
|
|
|
* key.group => group
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-09-08 04:09:19 +03:00
|
|
|
* Returns the EC::Group that the key is associated with. Modifying the returned
|
2017-09-03 15:35:27 +03:00
|
|
|
* group does not affect _key_.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-09-08 04:09:19 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_get_group(VALUE self)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
2016-09-08 04:09:19 +03:00
|
|
|
const EC_GROUP *group;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
|
|
|
group = EC_KEY_get0_group(ec);
|
|
|
|
if (!group)
|
|
|
|
return Qnil;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
return ec_group_new(group);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-09-08 04:09:19 +03:00
|
|
|
* call-seq:
|
|
|
|
* key.group = group
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-09-08 04:09:19 +03:00
|
|
|
* Sets the EC::Group for the key. The group structure is internally copied so
|
2017-09-03 15:35:27 +03:00
|
|
|
* modification to _group_ after assigning to a key has no effect on the key.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-09-08 04:09:19 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_set_group(VALUE self, VALUE group_v)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2021-09-21 12:29:59 +03:00
|
|
|
#if OSSL_OPENSSL_PREREQ(3, 0, 0)
|
|
|
|
rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
|
|
|
|
#else
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_KEY *ec;
|
|
|
|
EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(group_v, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
if (EC_KEY_set_group(ec, group) != 1)
|
|
|
|
ossl_raise(eECError, "EC_KEY_set_group");
|
|
|
|
|
|
|
|
return group_v;
|
2021-09-21 12:29:59 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* key.private_key => OpenSSL::BN
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_KEY_get0_private_key()
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_get_private_key(VALUE self)
|
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
|
|
|
const BIGNUM *bn;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if ((bn = EC_KEY_get0_private_key(ec)) == NULL)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
return ossl_bn_new(bn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* key.private_key = openssl_bn
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_KEY_set_private_key()
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_set_private_key(VALUE self, VALUE private_key)
|
|
|
|
{
|
2021-09-21 12:29:59 +03:00
|
|
|
#if OSSL_OPENSSL_PREREQ(3, 0, 0)
|
|
|
|
rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
|
|
|
|
#else
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_KEY *ec;
|
|
|
|
BIGNUM *bn = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (!NIL_P(private_key))
|
|
|
|
bn = GetBNPtr(private_key);
|
|
|
|
|
|
|
|
switch (EC_KEY_set_private_key(ec, bn)) {
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (bn == NULL)
|
|
|
|
break;
|
2021-09-12 10:38:03 +03:00
|
|
|
/* fallthrough */
|
2007-04-03 11:02:44 +04:00
|
|
|
default:
|
|
|
|
ossl_raise(eECError, "EC_KEY_set_private_key");
|
|
|
|
}
|
|
|
|
|
|
|
|
return private_key;
|
2021-09-21 12:29:59 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* key.public_key => OpenSSL::PKey::EC::Point
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_KEY_get0_public_key()
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_get_public_key(VALUE self)
|
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
|
|
|
const EC_POINT *point;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if ((point = EC_KEY_get0_public_key(ec)) == NULL)
|
|
|
|
return Qnil;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
return ec_point_new(point, EC_KEY_get0_group(ec));
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* key.public_key = ec_point
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_KEY_set_public_key()
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_set_public_key(VALUE self, VALUE public_key)
|
|
|
|
{
|
2021-09-21 12:29:59 +03:00
|
|
|
#if OSSL_OPENSSL_PREREQ(3, 0, 0)
|
|
|
|
rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
|
|
|
|
#else
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_KEY *ec;
|
|
|
|
EC_POINT *point = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (!NIL_P(public_key))
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECPoint(public_key, point);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
switch (EC_KEY_set_public_key(ec, point)) {
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (point == NULL)
|
|
|
|
break;
|
2021-09-12 10:38:03 +03:00
|
|
|
/* fallthrough */
|
2007-04-03 11:02:44 +04:00
|
|
|
default:
|
|
|
|
ossl_raise(eECError, "EC_KEY_set_public_key");
|
|
|
|
}
|
|
|
|
|
|
|
|
return public_key;
|
2021-09-21 12:29:59 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-05-21 08:30:48 +03:00
|
|
|
* key.public? => true or false
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-05-21 08:30:48 +03:00
|
|
|
* Returns whether this EC instance has a public key. The public key
|
|
|
|
* (EC::Point) can be retrieved with EC#public_key.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-05-21 08:30:48 +03:00
|
|
|
static VALUE ossl_ec_key_is_public(VALUE self)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-05-21 08:30:48 +03:00
|
|
|
return EC_KEY_get0_public_key(ec) ? Qtrue : Qfalse;
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-05-21 08:30:48 +03:00
|
|
|
* key.private? => true or false
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-05-21 08:30:48 +03:00
|
|
|
* Returns whether this EC instance has a private key. The private key (BN) can
|
|
|
|
* be retrieved with EC#private_key.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-05-21 08:30:48 +03:00
|
|
|
static VALUE ossl_ec_key_is_private(VALUE self)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
EC_KEY *ec;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-05-21 08:30:48 +03:00
|
|
|
return EC_KEY_get0_private_key(ec) ? Qtrue : Qfalse;
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-04-16 06:24:09 +04:00
|
|
|
* key.export([cipher, pass_phrase]) => String
|
|
|
|
* key.to_pem([cipher, pass_phrase]) => String
|
2011-05-12 03:05:29 +04:00
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* Outputs the EC key in PEM encoding. If _cipher_ and _pass_phrase_ are given
|
|
|
|
* they will be used to encrypt the key. _cipher_ must be an OpenSSL::Cipher
|
2016-08-29 08:47:09 +03:00
|
|
|
* instance. Note that encryption will only be effective for a private key,
|
|
|
|
* public keys will always be encoded in plain text.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2017-06-13 18:25:43 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_export(int argc, VALUE *argv, VALUE self)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2017-06-13 18:25:43 +03:00
|
|
|
EC_KEY *ec;
|
|
|
|
|
|
|
|
GetEC(self, ec);
|
|
|
|
if (EC_KEY_get0_private_key(ec))
|
|
|
|
return ossl_pkey_export_traditional(argc, argv, self, 0);
|
|
|
|
else
|
|
|
|
return ossl_pkey_export_spki(self, 0);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* key.to_der => String
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for i2d_ECPrivateKey_bio()
|
|
|
|
*/
|
2017-06-13 18:25:43 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_key_to_der(VALUE self)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2017-06-13 18:25:43 +03:00
|
|
|
EC_KEY *ec;
|
|
|
|
|
|
|
|
GetEC(self, ec);
|
|
|
|
if (EC_KEY_get0_private_key(ec))
|
|
|
|
return ossl_pkey_export_traditional(0, NULL, self, 1);
|
|
|
|
else
|
|
|
|
return ossl_pkey_export_spki(self, 1);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-05-24 19:27:11 +03:00
|
|
|
* key.generate_key! => self
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-05-24 19:27:11 +03:00
|
|
|
* Generates a new random private and public key.
|
|
|
|
*
|
|
|
|
* See also the OpenSSL documentation for EC_KEY_generate_key()
|
|
|
|
*
|
|
|
|
* === Example
|
|
|
|
* ec = OpenSSL::PKey::EC.new("prime256v1")
|
|
|
|
* p ec.private_key # => nil
|
|
|
|
* ec.generate_key!
|
|
|
|
* p ec.private_key # => #<OpenSSL::BN XXXXXX>
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_generate_key(VALUE self)
|
|
|
|
{
|
2021-12-16 20:21:42 +03:00
|
|
|
#if OSSL_OPENSSL_PREREQ(3, 0, 0)
|
|
|
|
rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
|
|
|
|
#else
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_KEY *ec;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (EC_KEY_generate_key(ec) != 1)
|
|
|
|
ossl_raise(eECError, "EC_KEY_generate_key");
|
|
|
|
|
|
|
|
return self;
|
2021-12-16 20:21:42 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-10 08:34:51 +03:00
|
|
|
* call-seq:
|
|
|
|
* key.check_key => true
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2020-07-10 08:34:51 +03:00
|
|
|
* Raises an exception if the key is invalid.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2020-07-10 08:34:51 +03:00
|
|
|
* See also the man page EVP_PKEY_public_check(3).
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_key_check_key(VALUE self)
|
|
|
|
{
|
2020-07-10 08:34:51 +03:00
|
|
|
#ifdef HAVE_EVP_PKEY_CHECK
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EVP_PKEY_CTX *pctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
GetPKey(self, pkey);
|
|
|
|
pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
|
|
|
|
if (!pctx)
|
|
|
|
ossl_raise(eDHError, "EVP_PKEY_CTX_new");
|
|
|
|
ret = EVP_PKEY_public_check(pctx);
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
if (ret != 1)
|
|
|
|
ossl_raise(eECError, "EVP_PKEY_public_check");
|
|
|
|
#else
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_KEY *ec;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetEC(self, ec);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (EC_KEY_check_key(ec) != 1)
|
|
|
|
ossl_raise(eECError, "EC_KEY_check_key");
|
2020-07-10 08:34:51 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
/*
|
|
|
|
* OpenSSL::PKey::EC::Group
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ossl_ec_group_free(void *ptr)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2020-02-21 23:52:01 +03:00
|
|
|
EC_GROUP_free(ptr);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2014-12-13 02:19:13 +03:00
|
|
|
static const rb_data_type_t ossl_ec_group_type = {
|
|
|
|
"OpenSSL/ec_group",
|
|
|
|
{
|
|
|
|
0, ossl_ec_group_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_group_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
return TypedData_Wrap_Struct(klass, &ossl_ec_group_type, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ec_group_new(const EC_GROUP *group)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
VALUE obj;
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_GROUP *group_new;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
obj = ossl_ec_group_alloc(cEC_GROUP);
|
|
|
|
group_new = EC_GROUP_dup(group);
|
|
|
|
if (!group_new)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_dup");
|
|
|
|
RTYPEDDATA_DATA(obj) = group_new;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::PKey::EC::Group.new(ec_group)
|
|
|
|
* OpenSSL::PKey::EC::Group.new(pem_or_der_encoded)
|
|
|
|
* OpenSSL::PKey::EC::Group.new(:GFp, bignum_p, bignum_a, bignum_b)
|
|
|
|
* OpenSSL::PKey::EC::Group.new(:GF2m, bignum_p, bignum_a, bignum_b)
|
|
|
|
*
|
|
|
|
* Creates a new EC::Group object.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* If the first argument is :GFp or :GF2m, creates a new curve with given
|
|
|
|
* parameters.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE arg1, arg2, arg3, arg4;
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_GROUP *group;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
TypedData_Get_Struct(self, EC_GROUP, &ossl_ec_group_type, group);
|
|
|
|
if (group)
|
2011-06-22 12:41:08 +04:00
|
|
|
ossl_raise(rb_eRuntimeError, "EC_GROUP is already initialized");
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
switch (rb_scan_args(argc, argv, "13", &arg1, &arg2, &arg3, &arg4)) {
|
|
|
|
case 1:
|
2020-06-30 09:46:14 +03:00
|
|
|
if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
|
2007-04-03 11:02:44 +04:00
|
|
|
const EC_GROUP *arg1_group;
|
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(arg1, arg1_group);
|
2007-04-03 11:02:44 +04:00
|
|
|
if ((group = EC_GROUP_dup(arg1_group)) == NULL)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_dup");
|
|
|
|
} else {
|
2017-08-10 12:23:45 +03:00
|
|
|
BIO *in = ossl_obj2bio(&arg1);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
|
|
|
|
if (!group) {
|
2011-06-22 12:41:08 +04:00
|
|
|
OSSL_BIO_reset(in);
|
2007-04-03 11:02:44 +04:00
|
|
|
group = d2i_ECPKParameters_bio(in, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_free(in);
|
|
|
|
|
|
|
|
if (!group) {
|
2008-12-31 08:55:25 +03:00
|
|
|
const char *name = StringValueCStr(arg1);
|
2007-06-18 12:56:21 +04:00
|
|
|
int nid = OBJ_sn2nid(name);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-05-18 07:07:45 +03:00
|
|
|
ossl_clear_error(); /* ignore errors in d2i_ECPKParameters_bio() */
|
2007-06-18 12:56:21 +04:00
|
|
|
if (nid == NID_undef)
|
2016-05-23 14:40:07 +03:00
|
|
|
ossl_raise(eEC_GROUP, "unknown curve name (%"PRIsVALUE")", arg1);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
group = EC_GROUP_new_by_curve_name(nid);
|
|
|
|
if (group == NULL)
|
2016-05-23 14:40:07 +03:00
|
|
|
ossl_raise(eEC_GROUP, "unable to create curve (%"PRIsVALUE")", arg1);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
|
|
|
|
EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (SYMBOL_P(arg1)) {
|
|
|
|
ID id = SYM2ID(arg1);
|
|
|
|
EC_GROUP *(*new_curve)(const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *) = NULL;
|
|
|
|
const BIGNUM *p = GetBNPtr(arg2);
|
|
|
|
const BIGNUM *a = GetBNPtr(arg3);
|
|
|
|
const BIGNUM *b = GetBNPtr(arg4);
|
|
|
|
|
|
|
|
if (id == s_GFp) {
|
|
|
|
new_curve = EC_GROUP_new_curve_GFp;
|
2013-07-06 02:46:42 +04:00
|
|
|
#if !defined(OPENSSL_NO_EC2M)
|
2007-04-03 11:02:44 +04:00
|
|
|
} else if (id == s_GF2m) {
|
|
|
|
new_curve = EC_GROUP_new_curve_GF2m;
|
2013-07-06 02:46:42 +04:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
} else {
|
2011-06-22 12:41:08 +04:00
|
|
|
ossl_raise(rb_eArgError, "unknown symbol, must be :GFp or :GF2m");
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((group = new_curve(p, a, b, ossl_bn_ctx)) == NULL)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_new_by_GF*");
|
|
|
|
} else {
|
2011-06-22 12:41:08 +04:00
|
|
|
ossl_raise(rb_eArgError, "unknown argument, must be :GFp or :GF2m");
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-22 12:41:08 +04:00
|
|
|
ossl_raise(rb_eArgError, "wrong number of arguments");
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2021-09-11 18:44:18 +03:00
|
|
|
ASSUME(group);
|
2016-09-08 04:09:19 +03:00
|
|
|
RTYPEDDATA_DATA(self) = group;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-06-19 12:29:59 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_group_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_GROUP *group, *group_new;
|
2016-06-19 12:29:59 +03:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
TypedData_Get_Struct(self, EC_GROUP, &ossl_ec_group_type, group_new);
|
|
|
|
if (group_new)
|
2016-06-19 12:29:59 +03:00
|
|
|
ossl_raise(eEC_GROUP, "EC::Group already initialized");
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(other, group);
|
2016-06-19 12:29:59 +03:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
group_new = EC_GROUP_dup(group);
|
|
|
|
if (!group_new)
|
2016-06-19 12:29:59 +03:00
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_dup");
|
2016-09-08 04:09:19 +03:00
|
|
|
RTYPEDDATA_DATA(self) = group_new;
|
2016-06-19 12:29:59 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:11 +03:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* group1.eql?(group2) => true | false
|
|
|
|
* group1 == group2 => true | false
|
2007-06-18 12:56:21 +04:00
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* Returns +true+ if the two groups use the same curve and have the same
|
|
|
|
* parameters, +false+ otherwise.
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_eql(VALUE a, VALUE b)
|
|
|
|
{
|
|
|
|
EC_GROUP *group1 = NULL, *group2 = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(a, group1);
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(b, group2);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
if (EC_GROUP_cmp(group1, group2, ossl_bn_ctx) == 1)
|
|
|
|
return Qfalse;
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:11 +03:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* group.generator => ec_point
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* Returns the generator of the group.
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_GROUP_get0_generator()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_generator(VALUE self)
|
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_GROUP *group;
|
|
|
|
const EC_POINT *generator;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
|
|
|
generator = EC_GROUP_get0_generator(group);
|
|
|
|
if (!generator)
|
|
|
|
return Qnil;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
return ec_point_new(generator, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.set_generator(generator, order, cofactor) => self
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* Sets the curve parameters. _generator_ must be an instance of EC::Point that
|
|
|
|
* is on the curve. _order_ and _cofactor_ are integers.
|
2016-08-29 08:47:09 +03:00
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_GROUP_set_generator()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_set_generator(VALUE self, VALUE generator, VALUE order, VALUE cofactor)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
const EC_POINT *point;
|
|
|
|
const BIGNUM *o, *co;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECPoint(generator, point);
|
2007-04-03 11:02:44 +04:00
|
|
|
o = GetBNPtr(order);
|
|
|
|
co = GetBNPtr(cofactor);
|
|
|
|
|
|
|
|
if (EC_GROUP_set_generator(group, point, o, co) != 1)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_set_generator");
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.get_order => order_bn
|
|
|
|
*
|
|
|
|
* Returns the order of the group.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_get_order()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_order(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE bn_obj;
|
|
|
|
BIGNUM *bn;
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
bn_obj = ossl_bn_new(NULL);
|
|
|
|
bn = GetBNPtr(bn_obj);
|
|
|
|
|
|
|
|
if (EC_GROUP_get_order(group, bn, ossl_bn_ctx) != 1)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_get_order");
|
|
|
|
|
|
|
|
return bn_obj;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.get_cofactor => cofactor_bn
|
|
|
|
*
|
|
|
|
* Returns the cofactor of the group.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_get_cofactor()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_cofactor(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE bn_obj;
|
|
|
|
BIGNUM *bn;
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
bn_obj = ossl_bn_new(NULL);
|
|
|
|
bn = GetBNPtr(bn_obj);
|
|
|
|
|
|
|
|
if (EC_GROUP_get_cofactor(group, bn, ossl_bn_ctx) != 1)
|
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_get_cofactor");
|
|
|
|
|
|
|
|
return bn_obj;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.curve_name => String
|
|
|
|
*
|
|
|
|
* Returns the curve name (sn).
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_get_curve_name()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_curve_name(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
int nid;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (group == NULL)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
nid = EC_GROUP_get_curve_name(group);
|
|
|
|
|
|
|
|
/* BUG: an nid or asn1 object should be returned, maybe. */
|
|
|
|
return rb_str_new2(OBJ_nid2sn(nid));
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* EC.builtin_curves => [[sn, comment], ...]
|
|
|
|
*
|
|
|
|
* Obtains a list of all predefined curves by the OpenSSL. Curve names are
|
|
|
|
* returned as sn.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_get_builtin_curves().
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_s_builtin_curves(VALUE self)
|
|
|
|
{
|
|
|
|
EC_builtin_curve *curves = NULL;
|
|
|
|
int n;
|
2011-03-24 10:29:21 +03:00
|
|
|
int crv_len = rb_long2int(EC_get_builtin_curves(NULL, 0));
|
2007-04-03 11:02:44 +04:00
|
|
|
VALUE ary, ret;
|
|
|
|
|
|
|
|
curves = ALLOCA_N(EC_builtin_curve, crv_len);
|
|
|
|
if (curves == NULL)
|
|
|
|
return Qnil;
|
|
|
|
if (!EC_get_builtin_curves(curves, crv_len))
|
|
|
|
ossl_raise(rb_eRuntimeError, "EC_get_builtin_curves");
|
|
|
|
|
|
|
|
ret = rb_ary_new2(crv_len);
|
|
|
|
|
|
|
|
for (n = 0; n < crv_len; n++) {
|
|
|
|
const char *sname = OBJ_nid2sn(curves[n].nid);
|
|
|
|
const char *comment = curves[n].comment;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, rb_str_new2(sname));
|
|
|
|
rb_ary_push(ary, comment ? rb_str_new2(comment) : Qnil);
|
|
|
|
rb_ary_push(ret, ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:11 +03:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* group.asn1_flag -> Integer
|
|
|
|
*
|
|
|
|
* Returns the flags set on the group.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See also #asn1_flag=.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_asn1_flag(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
int flag;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
flag = EC_GROUP_get_asn1_flag(group);
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
return INT2NUM(flag);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.asn1_flag = flags
|
|
|
|
*
|
|
|
|
* Sets flags on the group. The flag value is used to determine how to encode
|
|
|
|
* the group: encode explicit parameters or named curve using an OID.
|
|
|
|
*
|
|
|
|
* The flag value can be either of:
|
|
|
|
*
|
|
|
|
* * EC::NAMED_CURVE
|
|
|
|
* * EC::EXPLICIT_CURVE
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_set_asn1_flag().
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_set_asn1_flag(VALUE self, VALUE flag_v)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
EC_GROUP_set_asn1_flag(group, NUM2INT(flag_v));
|
|
|
|
|
|
|
|
return flag_v;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.point_conversion_form -> Symbol
|
|
|
|
*
|
|
|
|
* Returns the form how EC::Point data is encoded as ASN.1.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See also #point_conversion_form=.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_point_conversion_form(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
point_conversion_form_t form;
|
|
|
|
VALUE ret;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
form = EC_GROUP_get_point_conversion_form(group);
|
|
|
|
|
|
|
|
switch (form) {
|
|
|
|
case POINT_CONVERSION_UNCOMPRESSED: ret = ID_uncompressed; break;
|
|
|
|
case POINT_CONVERSION_COMPRESSED: ret = ID_compressed; break;
|
|
|
|
case POINT_CONVERSION_HYBRID: ret = ID_hybrid; break;
|
2011-06-22 12:41:08 +04:00
|
|
|
default: ossl_raise(eEC_GROUP, "unsupported point conversion form: %d, this module should be updated", form);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2007-04-03 11:02:44 +04:00
|
|
|
return ID2SYM(ret);
|
|
|
|
}
|
|
|
|
|
2016-11-30 17:41:46 +03:00
|
|
|
static point_conversion_form_t
|
|
|
|
parse_point_conversion_form_symbol(VALUE sym)
|
|
|
|
{
|
|
|
|
ID id = SYM2ID(sym);
|
|
|
|
|
|
|
|
if (id == ID_uncompressed)
|
|
|
|
return POINT_CONVERSION_UNCOMPRESSED;
|
|
|
|
else if (id == ID_compressed)
|
|
|
|
return POINT_CONVERSION_COMPRESSED;
|
|
|
|
else if (id == ID_hybrid)
|
|
|
|
return POINT_CONVERSION_HYBRID;
|
|
|
|
else
|
|
|
|
ossl_raise(rb_eArgError, "unsupported point conversion form %+"PRIsVALUE
|
|
|
|
" (expected :compressed, :uncompressed, or :hybrid)", sym);
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.point_conversion_form = form
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* Sets the form how EC::Point data is encoded as ASN.1 as defined in X9.62.
|
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* _format_ can be one of these:
|
2016-08-29 08:47:09 +03:00
|
|
|
*
|
2017-09-03 15:35:27 +03:00
|
|
|
* +:compressed+::
|
2016-08-29 08:47:09 +03:00
|
|
|
* Encoded as z||x, where z is an octet indicating which solution of the
|
|
|
|
* equation y is. z will be 0x02 or 0x03.
|
2017-09-03 15:35:27 +03:00
|
|
|
* +:uncompressed+::
|
2016-08-29 08:47:09 +03:00
|
|
|
* Encoded as z||x||y, where z is an octet 0x04.
|
2017-09-03 15:35:27 +03:00
|
|
|
* +:hybrid+::
|
2016-08-29 08:47:09 +03:00
|
|
|
* Encodes as z||x||y, where z is an octet indicating which solution of the
|
|
|
|
* equation y is. z will be 0x06 or 0x07.
|
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for EC_GROUP_set_point_conversion_form()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-11-30 17:41:46 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_group_set_point_conversion_form(VALUE self, VALUE form_v)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2016-11-30 17:41:46 +03:00
|
|
|
EC_GROUP *group;
|
2007-04-03 11:02:44 +04:00
|
|
|
point_conversion_form_t form;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2016-11-30 17:41:46 +03:00
|
|
|
form = parse_point_conversion_form_symbol(form_v);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
EC_GROUP_set_point_conversion_form(group, form);
|
|
|
|
|
|
|
|
return form_v;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.seed => String or nil
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_get0_seed()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_seed(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
size_t seed_len;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
seed_len = EC_GROUP_get_seed_len(group);
|
|
|
|
|
|
|
|
if (seed_len == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
2008-07-22 19:34:23 +04:00
|
|
|
return rb_str_new((const char *)EC_GROUP_get0_seed(group), seed_len);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.seed = seed => seed
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_set_seed()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_set_seed(VALUE self, VALUE seed)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
StringValue(seed);
|
|
|
|
|
2010-09-26 17:24:52 +04:00
|
|
|
if (EC_GROUP_set_seed(group, (unsigned char *)RSTRING_PTR(seed), RSTRING_LEN(seed)) != (size_t)RSTRING_LEN(seed))
|
2007-04-03 11:02:44 +04:00
|
|
|
ossl_raise(eEC_GROUP, "EC_GROUP_set_seed");
|
|
|
|
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get/set curve GFp, GF2m */
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-10-26 09:11:23 +03:00
|
|
|
* group.degree => integer
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for EC_GROUP_get_degree()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_get_degree(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return INT2NUM(EC_GROUP_get_degree(group));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE ossl_ec_group_to_string(VALUE self, int format)
|
|
|
|
{
|
|
|
|
EC_GROUP *group;
|
|
|
|
BIO *out;
|
|
|
|
int i = -1;
|
|
|
|
VALUE str;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
if (!(out = BIO_new(BIO_s_mem())))
|
|
|
|
ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
|
|
|
|
|
|
|
|
switch(format) {
|
|
|
|
case EXPORT_PEM:
|
|
|
|
i = PEM_write_bio_ECPKParameters(out, group);
|
|
|
|
break;
|
|
|
|
case EXPORT_DER:
|
|
|
|
i = i2d_ECPKParameters_bio(out, group);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BIO_free(out);
|
2011-06-22 12:41:08 +04:00
|
|
|
ossl_raise(rb_eRuntimeError, "unknown format (internal error)");
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 1) {
|
|
|
|
BIO_free(out);
|
|
|
|
ossl_raise(eECError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
str = ossl_membio2str(out);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.to_pem => String
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
|
|
|
* See the OpenSSL documentation for PEM_write_bio_ECPKParameters()
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_to_pem(VALUE self)
|
|
|
|
{
|
|
|
|
return ossl_ec_group_to_string(self, EXPORT_PEM);
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.to_der => String
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for i2d_ECPKParameters_bio()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_to_der(VALUE self)
|
|
|
|
{
|
|
|
|
return ossl_ec_group_to_string(self, EXPORT_DER);
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* group.to_text => String
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2016-08-29 08:47:09 +03:00
|
|
|
* See the OpenSSL documentation for ECPKParameters_print()
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_group_to_text(VALUE self)
|
|
|
|
{
|
|
|
|
EC_GROUP *group;
|
|
|
|
BIO *out;
|
|
|
|
VALUE str;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECGroup(self, group);
|
2007-04-03 11:02:44 +04:00
|
|
|
if (!(out = BIO_new(BIO_s_mem()))) {
|
|
|
|
ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
|
|
|
|
}
|
|
|
|
if (!ECPKParameters_print(out, group, 0)) {
|
|
|
|
BIO_free(out);
|
|
|
|
ossl_raise(eEC_GROUP, NULL);
|
|
|
|
}
|
|
|
|
str = ossl_membio2str(out);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
/*
|
|
|
|
* OpenSSL::PKey::EC::Point
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ossl_ec_point_free(void *ptr)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_POINT_clear_free(ptr);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2014-12-13 02:19:24 +03:00
|
|
|
static const rb_data_type_t ossl_ec_point_type = {
|
2016-09-08 04:09:19 +03:00
|
|
|
"OpenSSL/EC_POINT",
|
2014-12-13 02:19:24 +03:00
|
|
|
{
|
|
|
|
0, ossl_ec_point_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_point_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
return TypedData_Wrap_Struct(klass, &ossl_ec_point_type, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ec_point_new(const EC_POINT *point, const EC_GROUP *group)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_POINT *point_new;
|
2007-04-03 11:02:44 +04:00
|
|
|
VALUE obj;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
obj = ossl_ec_point_alloc(cEC_POINT);
|
|
|
|
point_new = EC_POINT_dup(point, group);
|
|
|
|
if (!point_new)
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_dup");
|
|
|
|
RTYPEDDATA_DATA(obj) = point_new;
|
|
|
|
rb_ivar_set(obj, id_i_group, ec_group_new(group));
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2017-12-14 14:19:16 +03:00
|
|
|
static VALUE ossl_ec_point_initialize_copy(VALUE, VALUE);
|
2007-04-03 11:02:44 +04:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::PKey::EC::Point.new(point)
|
2017-12-14 14:19:16 +03:00
|
|
|
* OpenSSL::PKey::EC::Point.new(group [, encoded_point])
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2017-12-14 14:19:16 +03:00
|
|
|
* Creates a new instance of OpenSSL::PKey::EC::Point. If the only argument is
|
|
|
|
* an instance of EC::Point, a copy is returned. Otherwise, creates a point
|
|
|
|
* that belongs to _group_.
|
|
|
|
*
|
|
|
|
* _encoded_point_ is the octet string representation of the point. This
|
|
|
|
* must be either a String or an OpenSSL::BN.
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_POINT *point;
|
2017-12-14 14:19:16 +03:00
|
|
|
VALUE group_v, arg2;
|
|
|
|
const EC_GROUP *group;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
TypedData_Get_Struct(self, EC_POINT, &ossl_ec_point_type, point);
|
|
|
|
if (point)
|
2017-12-14 14:19:16 +03:00
|
|
|
rb_raise(eEC_POINT, "EC_POINT already initialized");
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2017-12-14 14:19:16 +03:00
|
|
|
rb_scan_args(argc, argv, "11", &group_v, &arg2);
|
|
|
|
if (rb_obj_is_kind_of(group_v, cEC_POINT)) {
|
|
|
|
if (argc != 1)
|
|
|
|
rb_raise(rb_eArgError, "invalid second argument");
|
|
|
|
return ossl_ec_point_initialize_copy(self, group_v);
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2017-12-14 14:19:16 +03:00
|
|
|
GetECGroup(group_v, group);
|
|
|
|
if (argc == 1) {
|
|
|
|
point = EC_POINT_new(group);
|
|
|
|
if (!point)
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_new");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (rb_obj_is_kind_of(arg2, cBN)) {
|
|
|
|
point = EC_POINT_bn2point(group, GetBNPtr(arg2), NULL, ossl_bn_ctx);
|
|
|
|
if (!point)
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_bn2point");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(arg2);
|
|
|
|
point = EC_POINT_new(group);
|
|
|
|
if (!point)
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_new");
|
|
|
|
if (!EC_POINT_oct2point(group, point,
|
|
|
|
(unsigned char *)RSTRING_PTR(arg2),
|
|
|
|
RSTRING_LEN(arg2), ossl_bn_ctx)) {
|
|
|
|
EC_POINT_free(point);
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_oct2point");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
RTYPEDDATA_DATA(self) = point;
|
2016-08-29 08:47:09 +03:00
|
|
|
rb_ivar_set(self, id_i_group, group_v);
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-06-19 12:29:59 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_ec_point_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
2016-09-08 04:09:19 +03:00
|
|
|
EC_POINT *point, *point_new;
|
2016-06-19 12:29:59 +03:00
|
|
|
EC_GROUP *group;
|
|
|
|
VALUE group_v;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
TypedData_Get_Struct(self, EC_POINT, &ossl_ec_point_type, point_new);
|
|
|
|
if (point_new)
|
2016-06-19 12:29:59 +03:00
|
|
|
ossl_raise(eEC_POINT, "EC::Point already initialized");
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECPoint(other, point);
|
2016-06-19 12:29:59 +03:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
group_v = rb_obj_dup(rb_attr_get(other, id_i_group));
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(group_v, group);
|
2016-06-19 12:29:59 +03:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
point_new = EC_POINT_dup(point, group);
|
|
|
|
if (!point_new)
|
2016-06-19 12:29:59 +03:00
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_dup");
|
2016-09-08 04:09:19 +03:00
|
|
|
RTYPEDDATA_DATA(self) = point_new;
|
2016-08-29 08:47:09 +03:00
|
|
|
rb_ivar_set(self, id_i_group, group_v);
|
2016-06-19 12:29:59 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-06-18 12:56:21 +04:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point1.eql?(point2) => true | false
|
|
|
|
* point1 == point2 => true | false
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_eql(VALUE a, VALUE b)
|
|
|
|
{
|
|
|
|
EC_POINT *point1, *point2;
|
2016-08-29 08:47:09 +03:00
|
|
|
VALUE group_v1 = rb_attr_get(a, id_i_group);
|
|
|
|
VALUE group_v2 = rb_attr_get(b, id_i_group);
|
2007-06-18 12:56:21 +04:00
|
|
|
const EC_GROUP *group;
|
|
|
|
|
|
|
|
if (ossl_ec_group_eql(group_v1, group_v2) == Qfalse)
|
|
|
|
return Qfalse;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(a, point1);
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECPoint(b, point2);
|
|
|
|
GetECGroup(group_v1, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
if (EC_POINT_cmp(group, point1, point2, ossl_bn_ctx) == 1)
|
|
|
|
return Qfalse;
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.infinity? => true | false
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_is_at_infinity(VALUE self)
|
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
switch (EC_POINT_is_at_infinity(group, point)) {
|
|
|
|
case 1: return Qtrue;
|
|
|
|
case 0: return Qfalse;
|
|
|
|
default: ossl_raise(cEC_POINT, "EC_POINT_is_at_infinity");
|
|
|
|
}
|
2012-04-14 03:45:37 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2007-06-18 12:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.on_curve? => true | false
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_is_on_curve(VALUE self)
|
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
switch (EC_POINT_is_on_curve(group, point, ossl_bn_ctx)) {
|
|
|
|
case 1: return Qtrue;
|
|
|
|
case 0: return Qfalse;
|
|
|
|
default: ossl_raise(cEC_POINT, "EC_POINT_is_on_curve");
|
|
|
|
}
|
2012-04-14 03:45:37 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2007-06-18 12:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.make_affine! => self
|
2020-06-30 10:12:14 +03:00
|
|
|
*
|
|
|
|
* This method is deprecated and should not be used. This is a no-op.
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_make_affine(VALUE self)
|
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
2020-06-30 10:12:14 +03:00
|
|
|
rb_warn("OpenSSL::PKey::EC::Point#make_affine! is deprecated");
|
|
|
|
#if !OSSL_OPENSSL_PREREQ(3, 0, 0)
|
2007-06-18 12:56:21 +04:00
|
|
|
if (EC_POINT_make_affine(group, point, ossl_bn_ctx) != 1)
|
|
|
|
ossl_raise(cEC_POINT, "EC_POINT_make_affine");
|
2020-06-30 10:12:14 +03:00
|
|
|
#endif
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.invert! => self
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_invert(VALUE self)
|
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
if (EC_POINT_invert(group, point, ossl_bn_ctx) != 1)
|
|
|
|
ossl_raise(cEC_POINT, "EC_POINT_invert");
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.set_to_infinity! => self
|
2007-06-18 12:56:21 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_set_to_infinity(VALUE self)
|
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2007-06-18 12:56:21 +04:00
|
|
|
|
|
|
|
if (EC_POINT_set_to_infinity(group, point) != 1)
|
|
|
|
ossl_raise(cEC_POINT, "EC_POINT_set_to_infinity");
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-04-03 11:02:44 +04:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
2017-12-14 14:19:16 +03:00
|
|
|
* point.to_octet_string(conversion_form) -> String
|
2016-11-30 17:41:46 +03:00
|
|
|
*
|
2017-12-14 14:19:16 +03:00
|
|
|
* Returns the octet string representation of the elliptic curve point.
|
2007-04-03 11:02:44 +04:00
|
|
|
*
|
2017-12-14 14:19:16 +03:00
|
|
|
* _conversion_form_ specifies how the point is converted. Possible values are:
|
|
|
|
*
|
|
|
|
* - +:compressed+
|
|
|
|
* - +:uncompressed+
|
|
|
|
* - +:hybrid+
|
2007-04-03 11:02:44 +04:00
|
|
|
*/
|
2016-11-30 17:41:46 +03:00
|
|
|
static VALUE
|
2017-12-14 14:19:16 +03:00
|
|
|
ossl_ec_point_to_octet_string(VALUE self, VALUE conversion_form)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
EC_POINT *point;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
point_conversion_form_t form;
|
2017-12-14 14:19:16 +03:00
|
|
|
VALUE str;
|
|
|
|
size_t len;
|
2007-04-03 11:02:44 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point);
|
|
|
|
GetECPointGroup(self, group);
|
2017-12-14 14:19:16 +03:00
|
|
|
form = parse_point_conversion_form_symbol(conversion_form);
|
|
|
|
|
|
|
|
len = EC_POINT_point2oct(group, point, form, NULL, 0, ossl_bn_ctx);
|
|
|
|
if (!len)
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_point2oct");
|
|
|
|
str = rb_str_new(NULL, (long)len);
|
|
|
|
if (!EC_POINT_point2oct(group, point, form,
|
|
|
|
(unsigned char *)RSTRING_PTR(str), len,
|
|
|
|
ossl_bn_ctx))
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_point2oct");
|
|
|
|
return str;
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
2020-02-16 09:21:29 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* point.add(point) => point
|
|
|
|
*
|
|
|
|
* Performs elliptic curve point addition.
|
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_add(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
EC_POINT *point_self, *point_other, *point_result;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
VALUE group_v = rb_attr_get(self, id_i_group);
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
GetECPoint(self, point_self);
|
|
|
|
GetECPoint(other, point_other);
|
|
|
|
GetECGroup(group_v, group);
|
|
|
|
|
|
|
|
result = rb_obj_alloc(cEC_POINT);
|
|
|
|
ossl_ec_point_initialize(1, &group_v, result);
|
|
|
|
GetECPoint(result, point_result);
|
|
|
|
|
|
|
|
if (EC_POINT_add(group, point_result, point_self, point_other, ossl_bn_ctx) != 1) {
|
|
|
|
ossl_raise(eEC_POINT, "EC_POINT_add");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-10 06:38:40 +04:00
|
|
|
/*
|
2016-08-29 08:47:09 +03:00
|
|
|
* call-seq:
|
|
|
|
* point.mul(bn1 [, bn2]) => point
|
|
|
|
* point.mul(bns, points [, bn2]) => point
|
2016-05-18 05:55:46 +03:00
|
|
|
*
|
|
|
|
* Performs elliptic curve point multiplication.
|
|
|
|
*
|
|
|
|
* The first form calculates <tt>bn1 * point + bn2 * G</tt>, where +G+ is the
|
2017-09-03 15:35:27 +03:00
|
|
|
* generator of the group of _point_. _bn2_ may be omitted, and in that case,
|
2016-05-18 05:55:46 +03:00
|
|
|
* the result is just <tt>bn1 * point</tt>.
|
|
|
|
*
|
|
|
|
* The second form calculates <tt>bns[0] * point + bns[1] * points[0] + ...
|
2017-09-03 15:35:27 +03:00
|
|
|
* + bns[-1] * points[-1] + bn2 * G</tt>. _bn2_ may be omitted. _bns_ must be
|
|
|
|
* an array of OpenSSL::BN. _points_ must be an array of
|
2016-05-18 05:55:46 +03:00
|
|
|
* OpenSSL::PKey::EC::Point. Please note that <tt>points[0]</tt> is not
|
|
|
|
* multiplied by <tt>bns[0]</tt>, but <tt>bns[1]</tt>.
|
2012-06-10 06:38:40 +04:00
|
|
|
*/
|
|
|
|
static VALUE ossl_ec_point_mul(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2016-05-18 05:55:46 +03:00
|
|
|
EC_POINT *point_self, *point_result;
|
2012-06-10 06:38:40 +04:00
|
|
|
const EC_GROUP *group;
|
2016-08-29 08:47:09 +03:00
|
|
|
VALUE group_v = rb_attr_get(self, id_i_group);
|
2016-05-18 05:55:46 +03:00
|
|
|
VALUE arg1, arg2, arg3, result;
|
|
|
|
const BIGNUM *bn_g = NULL;
|
2012-06-10 06:38:40 +04:00
|
|
|
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(self, point_self);
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECGroup(group_v, group);
|
2012-06-10 06:38:40 +04:00
|
|
|
|
2016-05-18 05:55:46 +03:00
|
|
|
result = rb_obj_alloc(cEC_POINT);
|
|
|
|
ossl_ec_point_initialize(1, &group_v, result);
|
2016-09-08 04:09:19 +03:00
|
|
|
GetECPoint(result, point_result);
|
2012-06-10 06:38:40 +04:00
|
|
|
|
2016-05-18 05:55:46 +03:00
|
|
|
rb_scan_args(argc, argv, "12", &arg1, &arg2, &arg3);
|
2016-08-29 08:47:09 +03:00
|
|
|
if (!RB_TYPE_P(arg1, T_ARRAY)) {
|
2016-05-18 05:55:46 +03:00
|
|
|
BIGNUM *bn = GetBNPtr(arg1);
|
2012-06-10 06:38:40 +04:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
if (!NIL_P(arg2))
|
|
|
|
bn_g = GetBNPtr(arg2);
|
2016-05-18 05:55:46 +03:00
|
|
|
if (EC_POINT_mul(group, point_result, bn_g, point_self, bn, ossl_bn_ctx) != 1)
|
|
|
|
ossl_raise(eEC_POINT, NULL);
|
2012-06-10 06:38:40 +04:00
|
|
|
} else {
|
2021-04-29 16:02:19 +03:00
|
|
|
#if (defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3) || defined(LIBRESSL_VERSION_NUMBER)
|
2020-05-18 09:43:18 +03:00
|
|
|
rb_raise(rb_eNotImpError, "calling #mul with arrays is not" \
|
|
|
|
"supported by this OpenSSL version");
|
|
|
|
#else
|
2016-05-18 05:55:46 +03:00
|
|
|
/*
|
|
|
|
* bignums | arg1[0] | arg1[1] | arg1[2] | ...
|
|
|
|
* points | self | arg2[0] | arg2[1] | ...
|
|
|
|
*/
|
2016-05-18 07:36:02 +03:00
|
|
|
long i, num;
|
2016-12-10 11:12:02 +03:00
|
|
|
VALUE bns_tmp, tmp_p, tmp_b;
|
2016-05-18 05:55:46 +03:00
|
|
|
const EC_POINT **points;
|
|
|
|
const BIGNUM **bignums;
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
Check_Type(arg1, T_ARRAY);
|
|
|
|
Check_Type(arg2, T_ARRAY);
|
2016-05-18 05:55:46 +03:00
|
|
|
if (RARRAY_LEN(arg1) != RARRAY_LEN(arg2) + 1) /* arg2 must be 1 larger */
|
|
|
|
ossl_raise(rb_eArgError, "bns must be 1 longer than points; see the documentation");
|
|
|
|
|
2020-05-18 09:43:18 +03:00
|
|
|
rb_warning("OpenSSL::PKey::EC::Point#mul(ary, ary) is deprecated; " \
|
|
|
|
"use #mul(bn) form instead");
|
|
|
|
|
2016-05-18 05:55:46 +03:00
|
|
|
num = RARRAY_LEN(arg1);
|
2022-10-17 10:17:27 +03:00
|
|
|
bns_tmp = rb_ary_tmp_new(num);
|
2016-05-18 05:55:46 +03:00
|
|
|
bignums = ALLOCV_N(const BIGNUM *, tmp_b, num);
|
2016-12-10 11:12:02 +03:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
VALUE item = RARRAY_AREF(arg1, i);
|
|
|
|
bignums[i] = GetBNPtr(item);
|
|
|
|
rb_ary_push(bns_tmp, item);
|
|
|
|
}
|
2016-05-18 05:55:46 +03:00
|
|
|
|
|
|
|
points = ALLOCV_N(const EC_POINT *, tmp_p, num);
|
|
|
|
points[0] = point_self; /* self */
|
|
|
|
for (i = 0; i < num - 1; i++)
|
2017-09-03 15:35:27 +03:00
|
|
|
GetECPoint(RARRAY_AREF(arg2, i), points[i + 1]);
|
2016-05-18 05:55:46 +03:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
if (!NIL_P(arg3))
|
2016-05-18 05:55:46 +03:00
|
|
|
bn_g = GetBNPtr(arg3);
|
|
|
|
|
|
|
|
if (EC_POINTs_mul(group, point_result, bn_g, num, points, bignums, ossl_bn_ctx) != 1) {
|
|
|
|
ALLOCV_END(tmp_b);
|
|
|
|
ALLOCV_END(tmp_p);
|
|
|
|
ossl_raise(eEC_POINT, NULL);
|
|
|
|
}
|
2012-06-10 06:38:40 +04:00
|
|
|
|
2016-05-18 05:55:46 +03:00
|
|
|
ALLOCV_END(tmp_b);
|
|
|
|
ALLOCV_END(tmp_p);
|
2020-05-18 09:43:18 +03:00
|
|
|
#endif
|
2012-06-10 06:38:40 +04:00
|
|
|
}
|
|
|
|
|
2016-05-18 05:55:46 +03:00
|
|
|
return result;
|
2012-06-10 06:38:40 +04:00
|
|
|
}
|
|
|
|
|
2014-10-05 03:09:32 +04:00
|
|
|
void Init_ossl_ec(void)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
2018-02-16 11:39:48 +03:00
|
|
|
#undef rb_intern
|
2016-08-29 08:47:09 +03:00
|
|
|
#if 0
|
2007-04-03 11:02:44 +04:00
|
|
|
mPKey = rb_define_module_under(mOSSL, "PKey");
|
2016-08-29 08:47:09 +03:00
|
|
|
cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
|
|
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
|
2007-04-03 11:02:44 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
eECError = rb_define_class_under(mPKey, "ECError", ePKeyError);
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
/*
|
|
|
|
* Document-class: OpenSSL::PKey::EC
|
|
|
|
*
|
|
|
|
* OpenSSL::PKey::EC provides access to Elliptic Curve Digital Signature
|
|
|
|
* Algorithm (ECDSA) and Elliptic Curve Diffie-Hellman (ECDH).
|
|
|
|
*
|
|
|
|
* === Key exchange
|
|
|
|
* ec1 = OpenSSL::PKey::EC.generate("prime256v1")
|
|
|
|
* ec2 = OpenSSL::PKey::EC.generate("prime256v1")
|
|
|
|
* # ec1 and ec2 have own private key respectively
|
|
|
|
* shared_key1 = ec1.dh_compute_key(ec2.public_key)
|
|
|
|
* shared_key2 = ec2.dh_compute_key(ec1.public_key)
|
|
|
|
*
|
|
|
|
* p shared_key1 == shared_key2 #=> true
|
|
|
|
*/
|
2007-04-03 11:02:44 +04:00
|
|
|
cEC = rb_define_class_under(mPKey, "EC", cPKey);
|
|
|
|
cEC_GROUP = rb_define_class_under(cEC, "Group", rb_cObject);
|
|
|
|
cEC_POINT = rb_define_class_under(cEC, "Point", rb_cObject);
|
|
|
|
eEC_GROUP = rb_define_class_under(cEC_GROUP, "Error", eOSSLError);
|
|
|
|
eEC_POINT = rb_define_class_under(cEC_POINT, "Error", eOSSLError);
|
|
|
|
|
|
|
|
s_GFp = rb_intern("GFp");
|
|
|
|
s_GF2m = rb_intern("GF2m");
|
|
|
|
|
|
|
|
ID_uncompressed = rb_intern("uncompressed");
|
|
|
|
ID_compressed = rb_intern("compressed");
|
|
|
|
ID_hybrid = rb_intern("hybrid");
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
rb_define_const(cEC, "NAMED_CURVE", INT2NUM(OPENSSL_EC_NAMED_CURVE));
|
|
|
|
#if defined(OPENSSL_EC_EXPLICIT_CURVE)
|
|
|
|
rb_define_const(cEC, "EXPLICIT_CURVE", INT2NUM(OPENSSL_EC_EXPLICIT_CURVE));
|
|
|
|
#endif
|
2007-06-18 12:56:21 +04:00
|
|
|
|
2007-04-03 11:02:44 +04:00
|
|
|
rb_define_singleton_method(cEC, "builtin_curves", ossl_s_builtin_curves, 0);
|
|
|
|
|
2016-05-24 19:30:15 +03:00
|
|
|
rb_define_singleton_method(cEC, "generate", ossl_ec_key_s_generate, 1);
|
2007-04-03 11:02:44 +04:00
|
|
|
rb_define_method(cEC, "initialize", ossl_ec_key_initialize, -1);
|
2021-04-22 10:33:59 +03:00
|
|
|
#ifndef HAVE_EVP_PKEY_DUP
|
2017-09-03 15:35:27 +03:00
|
|
|
rb_define_method(cEC, "initialize_copy", ossl_ec_key_initialize_copy, 1);
|
2021-04-22 10:33:59 +03:00
|
|
|
#endif
|
2007-04-03 11:02:44 +04:00
|
|
|
|
|
|
|
rb_define_method(cEC, "group", ossl_ec_key_get_group, 0);
|
|
|
|
rb_define_method(cEC, "group=", ossl_ec_key_set_group, 1);
|
|
|
|
rb_define_method(cEC, "private_key", ossl_ec_key_get_private_key, 0);
|
|
|
|
rb_define_method(cEC, "private_key=", ossl_ec_key_set_private_key, 1);
|
|
|
|
rb_define_method(cEC, "public_key", ossl_ec_key_get_public_key, 0);
|
|
|
|
rb_define_method(cEC, "public_key=", ossl_ec_key_set_public_key, 1);
|
2016-05-21 08:30:48 +03:00
|
|
|
rb_define_method(cEC, "private?", ossl_ec_key_is_private, 0);
|
|
|
|
rb_define_method(cEC, "public?", ossl_ec_key_is_public, 0);
|
|
|
|
rb_define_alias(cEC, "private_key?", "private?");
|
|
|
|
rb_define_alias(cEC, "public_key?", "public?");
|
2007-04-03 11:02:44 +04:00
|
|
|
/* rb_define_method(cEC, "", ossl_ec_key_get_, 0);
|
|
|
|
rb_define_method(cEC, "=", ossl_ec_key_set_ 1);
|
|
|
|
set/get enc_flags
|
|
|
|
set/get _conv_from
|
|
|
|
set/get asn1_flag (can use ruby to call self.group.asn1_flag)
|
|
|
|
set/get precompute_mult
|
|
|
|
*/
|
2016-05-24 19:27:11 +03:00
|
|
|
rb_define_method(cEC, "generate_key!", ossl_ec_key_generate_key, 0);
|
|
|
|
rb_define_alias(cEC, "generate_key", "generate_key!");
|
2007-04-03 11:02:44 +04:00
|
|
|
rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
|
|
|
|
|
2012-06-10 05:23:21 +04:00
|
|
|
rb_define_method(cEC, "export", ossl_ec_key_export, -1);
|
|
|
|
rb_define_alias(cEC, "to_pem", "export");
|
2007-04-03 11:02:44 +04:00
|
|
|
rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);
|
|
|
|
|
|
|
|
|
|
|
|
rb_define_alloc_func(cEC_GROUP, ossl_ec_group_alloc);
|
|
|
|
rb_define_method(cEC_GROUP, "initialize", ossl_ec_group_initialize, -1);
|
2017-09-03 15:35:27 +03:00
|
|
|
rb_define_method(cEC_GROUP, "initialize_copy", ossl_ec_group_initialize_copy, 1);
|
2007-06-18 12:56:21 +04:00
|
|
|
rb_define_method(cEC_GROUP, "eql?", ossl_ec_group_eql, 1);
|
|
|
|
rb_define_alias(cEC_GROUP, "==", "eql?");
|
2007-04-03 11:02:44 +04:00
|
|
|
/* copy/dup/cmp */
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "generator", ossl_ec_group_get_generator, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "set_generator", ossl_ec_group_set_generator, 3);
|
|
|
|
rb_define_method(cEC_GROUP, "order", ossl_ec_group_get_order, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "cofactor", ossl_ec_group_get_cofactor, 0);
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "curve_name", ossl_ec_group_get_curve_name, 0);
|
|
|
|
/* rb_define_method(cEC_GROUP, "curve_name=", ossl_ec_group_set_curve_name, 1); */
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "asn1_flag", ossl_ec_group_get_asn1_flag, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "asn1_flag=", ossl_ec_group_set_asn1_flag, 1);
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "point_conversion_form", ossl_ec_group_get_point_conversion_form, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "point_conversion_form=", ossl_ec_group_set_point_conversion_form, 1);
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "seed", ossl_ec_group_get_seed, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "seed=", ossl_ec_group_set_seed, 1);
|
|
|
|
|
|
|
|
/* get/set GFp, GF2m */
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "degree", ossl_ec_group_get_degree, 0);
|
|
|
|
|
|
|
|
/* check* */
|
|
|
|
|
|
|
|
|
|
|
|
rb_define_method(cEC_GROUP, "to_pem", ossl_ec_group_to_pem, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "to_der", ossl_ec_group_to_der, 0);
|
|
|
|
rb_define_method(cEC_GROUP, "to_text", ossl_ec_group_to_text, 0);
|
|
|
|
|
|
|
|
|
|
|
|
rb_define_alloc_func(cEC_POINT, ossl_ec_point_alloc);
|
|
|
|
rb_define_method(cEC_POINT, "initialize", ossl_ec_point_initialize, -1);
|
2017-09-03 15:35:27 +03:00
|
|
|
rb_define_method(cEC_POINT, "initialize_copy", ossl_ec_point_initialize_copy, 1);
|
2007-04-03 11:02:44 +04:00
|
|
|
rb_attr(cEC_POINT, rb_intern("group"), 1, 0, 0);
|
2007-06-18 12:56:21 +04:00
|
|
|
rb_define_method(cEC_POINT, "eql?", ossl_ec_point_eql, 1);
|
|
|
|
rb_define_alias(cEC_POINT, "==", "eql?");
|
|
|
|
|
|
|
|
rb_define_method(cEC_POINT, "infinity?", ossl_ec_point_is_at_infinity, 0);
|
|
|
|
rb_define_method(cEC_POINT, "on_curve?", ossl_ec_point_is_on_curve, 0);
|
|
|
|
rb_define_method(cEC_POINT, "make_affine!", ossl_ec_point_make_affine, 0);
|
|
|
|
rb_define_method(cEC_POINT, "invert!", ossl_ec_point_invert, 0);
|
|
|
|
rb_define_method(cEC_POINT, "set_to_infinity!", ossl_ec_point_set_to_infinity, 0);
|
2007-04-03 11:02:44 +04:00
|
|
|
/* all the other methods */
|
|
|
|
|
2017-12-14 14:19:16 +03:00
|
|
|
rb_define_method(cEC_POINT, "to_octet_string", ossl_ec_point_to_octet_string, 1);
|
2020-02-16 09:21:29 +03:00
|
|
|
rb_define_method(cEC_POINT, "add", ossl_ec_point_add, 1);
|
2012-06-10 06:38:40 +04:00
|
|
|
rb_define_method(cEC_POINT, "mul", ossl_ec_point_mul, -1);
|
2016-08-29 08:47:09 +03:00
|
|
|
|
|
|
|
id_i_group = rb_intern("@group");
|
2007-04-03 11:02:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* defined NO_EC */
|
2014-10-05 03:09:32 +04:00
|
|
|
void Init_ossl_ec(void)
|
2007-04-03 11:02:44 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* NO_EC */
|