Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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