2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is licenced under the same licence as Ruby.
|
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#if !defined(_OSSL_PKEY_H_)
|
|
|
|
#define _OSSL_PKEY_H_
|
|
|
|
|
|
|
|
extern VALUE mPKey;
|
|
|
|
extern VALUE cPKey;
|
|
|
|
extern VALUE ePKeyError;
|
|
|
|
extern ID id_private_q;
|
|
|
|
|
2005-09-19 02:56:11 +04:00
|
|
|
#define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
|
|
|
|
#define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
|
|
|
|
#define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
#define WrapPKey(klass, obj, pkey) do { \
|
2011-03-07 11:44:45 +03:00
|
|
|
if (!(pkey)) { \
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
|
|
|
|
} \
|
2011-03-07 11:44:45 +03:00
|
|
|
(obj) = Data_Wrap_Struct((klass), 0, EVP_PKEY_free, (pkey)); \
|
2005-09-19 02:56:11 +04:00
|
|
|
OSSL_PKEY_SET_PUBLIC(obj); \
|
2003-07-23 20:12:24 +04:00
|
|
|
} while (0)
|
|
|
|
#define GetPKey(obj, pkey) do {\
|
2011-03-07 11:44:45 +03:00
|
|
|
Data_Get_Struct((obj), EVP_PKEY, (pkey));\
|
|
|
|
if (!(pkey)) { \
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SafeGetPKey(obj, pkey) do { \
|
2011-03-07 11:44:45 +03:00
|
|
|
OSSL_Check_Kind((obj), cPKey); \
|
|
|
|
GetPKey((obj), (pkey)); \
|
2003-07-23 20:12:24 +04:00
|
|
|
} while (0)
|
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
void ossl_generate_cb(int, int, void *);
|
2011-09-01 11:42:29 +04:00
|
|
|
#define HAVE_BN_GENCB defined(HAVE_RSA_GENERATE_KEY_EX) || defined(HAVE_DH_GENERATE_PARAMETERS_EX) || defined(HAVE_DSA_GENERATE_PARAMETERS_EX)
|
|
|
|
#if HAVE_BN_GENCB
|
|
|
|
struct ossl_generate_cb_arg {
|
|
|
|
int yield;
|
|
|
|
int stop;
|
|
|
|
int state;
|
2011-11-04 11:19:23 +04:00
|
|
|
};
|
2011-09-01 11:42:29 +04:00
|
|
|
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
|
|
|
|
void ossl_generate_cb_stop(void *ptr);
|
|
|
|
#endif
|
2003-09-12 17:46:48 +04:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
VALUE ossl_pkey_new(EVP_PKEY *);
|
2011-05-14 00:10:27 +04:00
|
|
|
VALUE ossl_pkey_new_from_file(VALUE);
|
2003-07-23 20:12:24 +04:00
|
|
|
EVP_PKEY *GetPKeyPtr(VALUE);
|
2005-03-09 13:45:42 +03:00
|
|
|
EVP_PKEY *DupPKeyPtr(VALUE);
|
2003-07-23 20:12:24 +04:00
|
|
|
EVP_PKEY *GetPrivPKeyPtr(VALUE);
|
|
|
|
EVP_PKEY *DupPrivPKeyPtr(VALUE);
|
|
|
|
void Init_ossl_pkey(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RSA
|
|
|
|
*/
|
|
|
|
extern VALUE cRSA;
|
|
|
|
extern VALUE eRSAError;
|
|
|
|
|
|
|
|
VALUE ossl_rsa_new(EVP_PKEY *);
|
|
|
|
void Init_ossl_rsa(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DSA
|
|
|
|
*/
|
|
|
|
extern VALUE cDSA;
|
|
|
|
extern VALUE eDSAError;
|
|
|
|
|
|
|
|
VALUE ossl_dsa_new(EVP_PKEY *);
|
|
|
|
void Init_ossl_dsa(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DH
|
|
|
|
*/
|
|
|
|
extern VALUE cDH;
|
|
|
|
extern VALUE eDHError;
|
2005-03-09 13:45:42 +03:00
|
|
|
extern DH *OSSL_DEFAULT_DH_512;
|
|
|
|
extern DH *OSSL_DEFAULT_DH_1024;
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
VALUE ossl_dh_new(EVP_PKEY *);
|
|
|
|
void Init_ossl_dh(void);
|
|
|
|
|
2007-04-03 11:02:44 +04:00
|
|
|
/*
|
|
|
|
* EC
|
|
|
|
*/
|
|
|
|
extern VALUE cEC;
|
|
|
|
extern VALUE eECError;
|
|
|
|
extern VALUE cEC_GROUP;
|
|
|
|
extern VALUE eEC_GROUP;
|
|
|
|
extern VALUE cEC_POINT;
|
|
|
|
extern VALUE eEC_POINT;
|
|
|
|
VALUE ossl_ec_new(EVP_PKEY *);
|
|
|
|
void Init_ossl_ec(void);
|
|
|
|
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
#define OSSL_PKEY_BN(keytype, name) \
|
2007-04-02 23:00:23 +04:00
|
|
|
/* \
|
|
|
|
* call-seq: \
|
|
|
|
* key.##name -> aBN \
|
|
|
|
*/ \
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE ossl_##keytype##_get_##name(VALUE self) \
|
|
|
|
{ \
|
|
|
|
EVP_PKEY *pkey; \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
\
|
|
|
|
GetPKey(self, pkey); \
|
|
|
|
bn = pkey->pkey.keytype->name; \
|
|
|
|
if (bn == NULL) \
|
|
|
|
return Qnil; \
|
|
|
|
return ossl_bn_new(bn); \
|
|
|
|
} \
|
2007-04-02 23:00:23 +04:00
|
|
|
/* \
|
|
|
|
* call-seq: \
|
|
|
|
* key.##name = bn -> bn \
|
|
|
|
*/ \
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
|
|
|
|
{ \
|
|
|
|
EVP_PKEY *pkey; \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
\
|
|
|
|
GetPKey(self, pkey); \
|
|
|
|
if (NIL_P(bignum)) { \
|
|
|
|
BN_clear_free(pkey->pkey.keytype->name); \
|
|
|
|
pkey->pkey.keytype->name = NULL; \
|
|
|
|
return Qnil; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
bn = GetBNPtr(bignum); \
|
|
|
|
if (pkey->pkey.keytype->name == NULL) \
|
|
|
|
pkey->pkey.keytype->name = BN_new(); \
|
|
|
|
if (pkey->pkey.keytype->name == NULL) \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
if (BN_copy(pkey->pkey.keytype->name, bn) == NULL) \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
return bignum; \
|
|
|
|
}
|
|
|
|
|
2005-03-09 13:45:42 +03:00
|
|
|
#define DEF_OSSL_PKEY_BN(class, keytype, name) \
|
|
|
|
do { \
|
2011-03-07 11:44:45 +03:00
|
|
|
rb_define_method((class), #name, ossl_##keytype##_get_##name, 0); \
|
|
|
|
rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
|
2003-07-23 20:12:24 +04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#endif /* _OSSL_PKEY_H_ */
|