Ruby  2.5.0dev(2017-10-22revision60238)
ossl_pkey.h
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #if !defined(_OSSL_PKEY_H_)
11 #define _OSSL_PKEY_H_
12 
13 extern VALUE mPKey;
14 extern VALUE cPKey;
15 extern VALUE ePKeyError;
17 
18 #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
19 #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
20 #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
21 
22 #define NewPKey(klass) \
23  TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, 0)
24 #define SetPKey(obj, pkey) do { \
25  if (!(pkey)) { \
26  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
27  } \
28  RTYPEDDATA_DATA(obj) = (pkey); \
29  OSSL_PKEY_SET_PUBLIC(obj); \
30 } while (0)
31 #define GetPKey(obj, pkey) do {\
32  TypedData_Get_Struct((obj), EVP_PKEY, &ossl_evp_pkey_type, (pkey)); \
33  if (!(pkey)) { \
34  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
35  } \
36 } while (0)
37 
39  int yield;
40  int stop;
41  int state;
42 };
43 int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
44 void ossl_generate_cb_stop(void *ptr);
45 
46 VALUE ossl_pkey_new(EVP_PKEY *);
47 EVP_PKEY *GetPKeyPtr(VALUE);
48 EVP_PKEY *DupPKeyPtr(VALUE);
49 EVP_PKEY *GetPrivPKeyPtr(VALUE);
50 void Init_ossl_pkey(void);
51 
52 /*
53  * RSA
54  */
55 extern VALUE cRSA;
56 extern VALUE eRSAError;
57 
58 VALUE ossl_rsa_new(EVP_PKEY *);
59 void Init_ossl_rsa(void);
60 
61 /*
62  * DSA
63  */
64 extern VALUE cDSA;
65 extern VALUE eDSAError;
66 
67 VALUE ossl_dsa_new(EVP_PKEY *);
68 void Init_ossl_dsa(void);
69 
70 /*
71  * DH
72  */
73 extern VALUE cDH;
74 extern VALUE eDHError;
75 
76 VALUE ossl_dh_new(EVP_PKEY *);
77 void Init_ossl_dh(void);
78 
79 /*
80  * EC
81  */
82 extern VALUE cEC;
83 extern VALUE eECError;
84 extern VALUE cEC_GROUP;
85 extern VALUE eEC_GROUP;
86 extern VALUE cEC_POINT;
87 extern VALUE eEC_POINT;
88 VALUE ossl_ec_new(EVP_PKEY *);
89 void Init_ossl_ec(void);
90 
91 #define OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, _name, _get) \
92 /* \
93  * call-seq: \
94  * _keytype##.##_name -> aBN \
95  */ \
96 static VALUE ossl_##_keytype##_get_##_name(VALUE self) \
97 { \
98  _type *obj; \
99  const BIGNUM *bn; \
100  \
101  Get##_type(self, obj); \
102  _get; \
103  if (bn == NULL) \
104  return Qnil; \
105  return ossl_bn_new(bn); \
106 }
107 
108 #define OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
109  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
110  _type##_get0_##_group(obj, &bn, NULL, NULL)) \
111  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
112  _type##_get0_##_group(obj, NULL, &bn, NULL)) \
113  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a3, \
114  _type##_get0_##_group(obj, NULL, NULL, &bn))
115 
116 #define OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
117  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
118  _type##_get0_##_group(obj, &bn, NULL)) \
119  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
120  _type##_get0_##_group(obj, NULL, &bn))
121 
122 #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
123 /* \
124  * call-seq: \
125  * _keytype##.set_##_group(a1, a2, a3) -> self \
126  */ \
127 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
128 { \
129  _type *obj; \
130  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
131  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
132  BIGNUM *bn3 = NULL, *orig_bn3 = NIL_P(v3) ? NULL : GetBNPtr(v3);\
133  \
134  Get##_type(self, obj); \
135  if (orig_bn1 && !(bn1 = BN_dup(orig_bn1)) || \
136  orig_bn2 && !(bn2 = BN_dup(orig_bn2)) || \
137  orig_bn3 && !(bn3 = BN_dup(orig_bn3))) { \
138  BN_clear_free(bn1); \
139  BN_clear_free(bn2); \
140  BN_clear_free(bn3); \
141  ossl_raise(eBNError, NULL); \
142  } \
143  \
144  if (!_type##_set0_##_group(obj, bn1, bn2, bn3)) { \
145  BN_clear_free(bn1); \
146  BN_clear_free(bn2); \
147  BN_clear_free(bn3); \
148  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
149  } \
150  return self; \
151 }
152 
153 #define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
154 /* \
155  * call-seq: \
156  * _keytype##.set_##_group(a1, a2) -> self \
157  */ \
158 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
159 { \
160  _type *obj; \
161  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
162  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
163  \
164  Get##_type(self, obj); \
165  if (orig_bn1 && !(bn1 = BN_dup(orig_bn1)) || \
166  orig_bn2 && !(bn2 = BN_dup(orig_bn2))) { \
167  BN_clear_free(bn1); \
168  BN_clear_free(bn2); \
169  ossl_raise(eBNError, NULL); \
170  } \
171  \
172  if (!_type##_set0_##_group(obj, bn1, bn2)) { \
173  BN_clear_free(bn1); \
174  BN_clear_free(bn2); \
175  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
176  } \
177  return self; \
178 }
179 
180 #define OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, _name) \
181 /* \
182  * call-seq: \
183  * _keytype##.##_name = bn -> bn \
184  */ \
185 static VALUE ossl_##_keytype##_set_##_name(VALUE self, VALUE bignum) \
186 { \
187  _type *obj; \
188  BIGNUM *bn; \
189  \
190  rb_warning("#"#_name"= is deprecated; use #set_"#_group); \
191  Get##_type(self, obj); \
192  if (NIL_P(bignum)) { \
193  BN_clear_free(obj->_name); \
194  obj->_name = NULL; \
195  return Qnil; \
196  } \
197  \
198  bn = GetBNPtr(bignum); \
199  if (obj->_name == NULL) \
200  obj->_name = BN_new(); \
201  if (obj->_name == NULL) \
202  ossl_raise(eBNError, NULL); \
203  if (BN_copy(obj->_name, bn) == NULL) \
204  ossl_raise(eBNError, NULL); \
205  return bignum; \
206 }
207 
208 #if defined(HAVE_OPAQUE_OPENSSL) /* OpenSSL 1.1.0 */
209 #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
210  OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
211  OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)
212 
213 #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
214  OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
215  OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2)
216 
217 #define DEF_OSSL_PKEY_BN(class, keytype, name) \
218  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0)
219 
220 #else
221 #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
222  OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
223  OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
224  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a1) \
225  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a2) \
226  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a3)
227 
228 #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
229  OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
230  OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
231  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a1) \
232  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a2)
233 
234 #define DEF_OSSL_PKEY_BN(class, keytype, name) do { \
235  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0);\
236  rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
237 } while (0)
238 #endif /* HAVE_OPAQUE_OPENSSL */
239 
240 #endif /* _OSSL_PKEY_H_ */
VALUE eEC_GROUP
Definition: ossl_pkey_ec.c:46
void Init_ossl_pkey(void)
Definition: ossl_pkey.c:389
VALUE ePKeyError
Definition: ossl_pkey.c:17
void Init_ossl_dsa(void)
VALUE ossl_dsa_new(EVP_PKEY *)
Definition: ossl_pkey_dsa.c:72
VALUE eEC_POINT
Definition: ossl_pkey_ec.c:48
VALUE ossl_rsa_new(EVP_PKEY *)
Definition: ossl_pkey_rsa.c:73
VALUE eDSAError
Definition: ossl_pkey_dsa.c:44
VALUE eRSAError
Definition: ossl_pkey_rsa.c:45
VALUE cPKey
Definition: ossl_pkey.c:16
VALUE cDH
Definition: ossl_pkey_dh.c:29
VALUE cDSA
Definition: ossl_pkey_dsa.c:43
VALUE cRSA
Definition: ossl_pkey_rsa.c:44
VALUE ossl_dh_new(EVP_PKEY *)
Definition: ossl_pkey_dh.c:58
VALUE mPKey
Definition: ossl_pkey.c:15
VALUE eDHError
Definition: ossl_pkey_dh.c:30
const rb_data_type_t ossl_evp_pkey_type
Definition: ossl_pkey.c:65
void Init_ossl_ec(void)
VALUE cEC_GROUP
Definition: ossl_pkey_ec.c:45
unsigned long VALUE
Definition: ruby.h:85
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
Definition: ossl_pkey.c:24
EVP_PKEY * GetPKeyPtr(VALUE)
Definition: ossl_pkey.c:206
VALUE cEC_POINT
Definition: ossl_pkey_ec.c:47
VALUE eECError
Definition: ossl_pkey_ec.c:44
EVP_PKEY * GetPrivPKeyPtr(VALUE)
Definition: ossl_pkey.c:216
EVP_PKEY * DupPKeyPtr(VALUE)
Definition: ossl_pkey.c:229
void Init_ossl_rsa(void)
void Init_ossl_dh(void)
Definition: ossl_pkey_dh.c:576
VALUE cEC
Definition: ossl_pkey_ec.c:43
VALUE ossl_ec_new(EVP_PKEY *)
Definition: ossl_pkey_ec.c:87
VALUE ossl_pkey_new(EVP_PKEY *)
Definition: ossl_pkey.c:107
void ossl_generate_cb_stop(void *ptr)
Definition: ossl_pkey.c:50