Ruby  2.4.2p198(2017-09-14revision59899)
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 #define SafeGetPKey(obj, pkey) do { \
38  OSSL_Check_Kind((obj), cPKey); \
39  GetPKey((obj), (pkey)); \
40 } while (0)
41 
43  int yield;
44  int stop;
45  int state;
46 };
47 int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
48 void ossl_generate_cb_stop(void *ptr);
49 
50 VALUE ossl_pkey_new(EVP_PKEY *);
51 EVP_PKEY *GetPKeyPtr(VALUE);
52 EVP_PKEY *DupPKeyPtr(VALUE);
53 EVP_PKEY *GetPrivPKeyPtr(VALUE);
54 void Init_ossl_pkey(void);
55 
56 /*
57  * RSA
58  */
59 extern VALUE cRSA;
60 extern VALUE eRSAError;
61 
62 VALUE ossl_rsa_new(EVP_PKEY *);
63 void Init_ossl_rsa(void);
64 
65 /*
66  * DSA
67  */
68 extern VALUE cDSA;
69 extern VALUE eDSAError;
70 
71 VALUE ossl_dsa_new(EVP_PKEY *);
72 void Init_ossl_dsa(void);
73 
74 /*
75  * DH
76  */
77 extern VALUE cDH;
78 extern VALUE eDHError;
79 
80 VALUE ossl_dh_new(EVP_PKEY *);
81 void Init_ossl_dh(void);
82 
83 /*
84  * EC
85  */
86 extern VALUE cEC;
87 extern VALUE eECError;
88 extern VALUE cEC_GROUP;
89 extern VALUE eEC_GROUP;
90 extern VALUE cEC_POINT;
91 extern VALUE eEC_POINT;
92 VALUE ossl_ec_new(EVP_PKEY *);
93 void Init_ossl_ec(void);
94 
95 #define OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, _name, _get) \
96 /* \
97  * call-seq: \
98  * _keytype##.##_name -> aBN \
99  */ \
100 static VALUE ossl_##_keytype##_get_##_name(VALUE self) \
101 { \
102  _type *obj; \
103  const BIGNUM *bn; \
104  \
105  Get##_type(self, obj); \
106  _get; \
107  if (bn == NULL) \
108  return Qnil; \
109  return ossl_bn_new(bn); \
110 }
111 
112 #define OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
113  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
114  _type##_get0_##_group(obj, &bn, NULL, NULL)) \
115  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
116  _type##_get0_##_group(obj, NULL, &bn, NULL)) \
117  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a3, \
118  _type##_get0_##_group(obj, NULL, NULL, &bn))
119 
120 #define OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
121  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
122  _type##_get0_##_group(obj, &bn, NULL)) \
123  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
124  _type##_get0_##_group(obj, NULL, &bn))
125 
126 #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
127 /* \
128  * call-seq: \
129  * _keytype##.set_##_group(a1, a2, a3) -> self \
130  */ \
131 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
132 { \
133  _type *obj; \
134  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
135  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
136  BIGNUM *bn3 = NULL, *orig_bn3 = NIL_P(v3) ? NULL : GetBNPtr(v3);\
137  \
138  Get##_type(self, obj); \
139  if (orig_bn1 && !(bn1 = BN_dup(orig_bn1)) || \
140  orig_bn2 && !(bn2 = BN_dup(orig_bn2)) || \
141  orig_bn3 && !(bn3 = BN_dup(orig_bn3))) { \
142  BN_clear_free(bn1); \
143  BN_clear_free(bn2); \
144  BN_clear_free(bn3); \
145  ossl_raise(eBNError, NULL); \
146  } \
147  \
148  if (!_type##_set0_##_group(obj, bn1, bn2, bn3)) { \
149  BN_clear_free(bn1); \
150  BN_clear_free(bn2); \
151  BN_clear_free(bn3); \
152  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
153  } \
154  return self; \
155 }
156 
157 #define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
158 /* \
159  * call-seq: \
160  * _keytype##.set_##_group(a1, a2) -> self \
161  */ \
162 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
163 { \
164  _type *obj; \
165  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
166  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
167  \
168  Get##_type(self, obj); \
169  if (orig_bn1 && !(bn1 = BN_dup(orig_bn1)) || \
170  orig_bn2 && !(bn2 = BN_dup(orig_bn2))) { \
171  BN_clear_free(bn1); \
172  BN_clear_free(bn2); \
173  ossl_raise(eBNError, NULL); \
174  } \
175  \
176  if (!_type##_set0_##_group(obj, bn1, bn2)) { \
177  BN_clear_free(bn1); \
178  BN_clear_free(bn2); \
179  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
180  } \
181  return self; \
182 }
183 
184 #define OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, _name) \
185 /* \
186  * call-seq: \
187  * _keytype##.##_name = bn -> bn \
188  */ \
189 static VALUE ossl_##_keytype##_set_##_name(VALUE self, VALUE bignum) \
190 { \
191  _type *obj; \
192  BIGNUM *bn; \
193  \
194  rb_warning("#"#_name"= is deprecated; use #set_"#_group); \
195  Get##_type(self, obj); \
196  if (NIL_P(bignum)) { \
197  BN_clear_free(obj->_name); \
198  obj->_name = NULL; \
199  return Qnil; \
200  } \
201  \
202  bn = GetBNPtr(bignum); \
203  if (obj->_name == NULL) \
204  obj->_name = BN_new(); \
205  if (obj->_name == NULL) \
206  ossl_raise(eBNError, NULL); \
207  if (BN_copy(obj->_name, bn) == NULL) \
208  ossl_raise(eBNError, NULL); \
209  return bignum; \
210 }
211 
212 #if defined(HAVE_OPAQUE_OPENSSL) /* OpenSSL 1.1.0 */
213 #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
214  OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
215  OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)
216 
217 #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
218  OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
219  OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2)
220 
221 #define DEF_OSSL_PKEY_BN(class, keytype, name) \
222  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0)
223 
224 #else
225 #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
226  OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
227  OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
228  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a1) \
229  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a2) \
230  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a3)
231 
232 #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
233  OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
234  OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
235  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a1) \
236  OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, a2)
237 
238 #define DEF_OSSL_PKEY_BN(class, keytype, name) do { \
239  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0);\
240  rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
241 } while (0)
242 #endif /* HAVE_OPAQUE_OPENSSL */
243 
244 #endif /* _OSSL_PKEY_H_ */
VALUE eEC_GROUP
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
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
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
VALUE eECError
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
VALUE ossl_ec_new(EVP_PKEY *)
VALUE ossl_pkey_new(EVP_PKEY *)
Definition: ossl_pkey.c:107
void ossl_generate_cb_stop(void *ptr)
Definition: ossl_pkey.c:50