Ruby  2.0.0p594(2014-10-27revision48167)
ossl_pkey.h
Go to the documentation of this file.
1 /*
2  * $Id: ossl_pkey.h 33634 2011-11-04 07:19:23Z nobu $
3  * 'OpenSSL for Ruby' project
4  * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
5  * All rights reserved.
6  */
7 /*
8  * This program is licenced under the same licence as Ruby.
9  * (See the file 'LICENCE'.)
10  */
11 #if !defined(_OSSL_PKEY_H_)
12 #define _OSSL_PKEY_H_
13 
14 extern VALUE mPKey;
15 extern VALUE cPKey;
16 extern VALUE ePKeyError;
17 extern ID id_private_q;
18 
19 #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
20 #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
21 #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
22 
23 #define WrapPKey(klass, obj, pkey) do { \
24  if (!(pkey)) { \
25  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
26  } \
27  (obj) = Data_Wrap_Struct((klass), 0, EVP_PKEY_free, (pkey)); \
28  OSSL_PKEY_SET_PUBLIC(obj); \
29 } while (0)
30 #define GetPKey(obj, pkey) do {\
31  Data_Get_Struct((obj), EVP_PKEY, (pkey));\
32  if (!(pkey)) { \
33  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
34  } \
35 } while (0)
36 #define SafeGetPKey(obj, pkey) do { \
37  OSSL_Check_Kind((obj), cPKey); \
38  GetPKey((obj), (pkey)); \
39 } while (0)
40 
41 void ossl_generate_cb(int, int, void *);
42 #define HAVE_BN_GENCB defined(HAVE_RSA_GENERATE_KEY_EX) || defined(HAVE_DH_GENERATE_PARAMETERS_EX) || defined(HAVE_DSA_GENERATE_PARAMETERS_EX)
43 #if HAVE_BN_GENCB
44 struct ossl_generate_cb_arg {
45  int yield;
46  int stop;
47  int state;
48 };
49 int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
50 void ossl_generate_cb_stop(void *ptr);
51 #endif
52 
53 VALUE ossl_pkey_new(EVP_PKEY *);
55 EVP_PKEY *GetPKeyPtr(VALUE);
56 EVP_PKEY *DupPKeyPtr(VALUE);
57 EVP_PKEY *GetPrivPKeyPtr(VALUE);
58 EVP_PKEY *DupPrivPKeyPtr(VALUE);
59 void Init_ossl_pkey(void);
60 
61 /*
62  * RSA
63  */
64 extern VALUE cRSA;
65 extern VALUE eRSAError;
66 
67 VALUE ossl_rsa_new(EVP_PKEY *);
68 void Init_ossl_rsa(void);
69 
70 /*
71  * DSA
72  */
73 extern VALUE cDSA;
74 extern VALUE eDSAError;
75 
76 VALUE ossl_dsa_new(EVP_PKEY *);
77 void Init_ossl_dsa(void);
78 
79 /*
80  * DH
81  */
82 extern VALUE cDH;
83 extern VALUE eDHError;
84 extern DH *OSSL_DEFAULT_DH_512;
85 extern DH *OSSL_DEFAULT_DH_1024;
86 
87 VALUE ossl_dh_new(EVP_PKEY *);
88 void Init_ossl_dh(void);
89 
90 /*
91  * EC
92  */
93 extern VALUE cEC;
94 extern VALUE eECError;
95 extern VALUE cEC_GROUP;
96 extern VALUE eEC_GROUP;
97 extern VALUE cEC_POINT;
98 extern VALUE eEC_POINT;
99 VALUE ossl_ec_new(EVP_PKEY *);
100 void Init_ossl_ec(void);
101 
102 
103 #define OSSL_PKEY_BN(keytype, name) \
104 /* \
105  * call-seq: \
106  * key.##name -> aBN \
107  */ \
108 static VALUE ossl_##keytype##_get_##name(VALUE self) \
109 { \
110  EVP_PKEY *pkey; \
111  BIGNUM *bn; \
112  \
113  GetPKey(self, pkey); \
114  bn = pkey->pkey.keytype->name; \
115  if (bn == NULL) \
116  return Qnil; \
117  return ossl_bn_new(bn); \
118 } \
119 /* \
120  * call-seq: \
121  * key.##name = bn -> bn \
122  */ \
123 static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
124 { \
125  EVP_PKEY *pkey; \
126  BIGNUM *bn; \
127  \
128  GetPKey(self, pkey); \
129  if (NIL_P(bignum)) { \
130  BN_clear_free(pkey->pkey.keytype->name); \
131  pkey->pkey.keytype->name = NULL; \
132  return Qnil; \
133  } \
134  \
135  bn = GetBNPtr(bignum); \
136  if (pkey->pkey.keytype->name == NULL) \
137  pkey->pkey.keytype->name = BN_new(); \
138  if (pkey->pkey.keytype->name == NULL) \
139  ossl_raise(eBNError, NULL); \
140  if (BN_copy(pkey->pkey.keytype->name, bn) == NULL) \
141  ossl_raise(eBNError, NULL); \
142  return bignum; \
143 }
144 
145 #define DEF_OSSL_PKEY_BN(class, keytype, name) \
146 do { \
147  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0); \
148  rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
149 } while (0)
150 
151 #endif /* _OSSL_PKEY_H_ */
ssize_t n
Definition: bigdecimal.c:5676
ID id_private_q
Definition: ossl_pkey.c:19
Win32OLEIDispatch * p
Definition: win32ole.c:786
VALUE eEC_GROUP
VALUE mPKey
Definition: ossl_pkey.c:16
VALUE ePKeyError
Definition: ossl_pkey.c:18
EVP_PKEY * GetPrivPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:184
void Init_ossl_dsa(void)
VALUE ossl_dsa_new(EVP_PKEY *)
Definition: ossl_pkey_dsa.c:56
EVP_PKEY * DupPrivPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:208
VALUE eEC_POINT
VALUE ossl_rsa_new(EVP_PKEY *)
Definition: ossl_pkey_rsa.c:56
void Init_ossl_pkey()
Definition: ossl_pkey.c:345
int state
Definition: tcltklib.c:1461
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:76
VALUE eDSAError
Definition: ossl_pkey_dsa.c:29
VALUE eRSAError
Definition: ossl_pkey_rsa.c:29
VALUE cDH
Definition: ossl_pkey_dh.c:34
DH * OSSL_DEFAULT_DH_512
Definition: ossl_pkey_dh.c:539
void ossl_generate_cb(int p, int n, void *arg)
Definition: ossl_pkey.c:25
VALUE cDSA
Definition: ossl_pkey_dsa.c:28
VALUE cRSA
Definition: ossl_pkey_rsa.c:28
unsigned long ID
Definition: ripper.y:105
VALUE ossl_dh_new(EVP_PKEY *)
Definition: ossl_pkey_dh.c:62
VALUE eDHError
Definition: ossl_pkey_dh.c:35
void Init_ossl_ec(void)
VALUE cEC_GROUP
VALUE ossl_pkey_new_from_file(VALUE filename)
Definition: ossl_pkey.c:106
return ptr
Definition: tcltklib.c:784
VALUE cEC_POINT
VALUE eECError
EVP_PKEY * GetPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:174
EVP_PKEY * DupPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:197
void Init_ossl_rsa(void)
void Init_ossl_dh(void)
Definition: ossl_pkey_dh.c:589
unsigned long VALUE
Definition: ripper.y:104
VALUE cEC
VALUE cPKey
Definition: ossl_pkey.c:17
DH * OSSL_DEFAULT_DH_1024
Definition: ossl_pkey_dh.c:567
VALUE ossl_ec_new(EVP_PKEY *)