Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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
13extern VALUE mPKey;
14extern VALUE cPKey;
15extern 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;
41 int state;
42};
43int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
44void ossl_generate_cb_stop(void *ptr);
45
46VALUE ossl_pkey_new(EVP_PKEY *);
47void ossl_pkey_check_public_key(const EVP_PKEY *);
48EVP_PKEY *GetPKeyPtr(VALUE);
49EVP_PKEY *DupPKeyPtr(VALUE);
50EVP_PKEY *GetPrivPKeyPtr(VALUE);
51void Init_ossl_pkey(void);
52
53/*
54 * RSA
55 */
56extern VALUE cRSA;
57extern VALUE eRSAError;
58
59VALUE ossl_rsa_new(EVP_PKEY *);
60void Init_ossl_rsa(void);
61
62/*
63 * DSA
64 */
65extern VALUE cDSA;
66extern VALUE eDSAError;
67
68VALUE ossl_dsa_new(EVP_PKEY *);
69void Init_ossl_dsa(void);
70
71/*
72 * DH
73 */
74extern VALUE cDH;
75extern VALUE eDHError;
76
77VALUE ossl_dh_new(EVP_PKEY *);
78void Init_ossl_dh(void);
79
80/*
81 * EC
82 */
83extern VALUE cEC;
84extern VALUE eECError;
85extern VALUE cEC_GROUP;
86extern VALUE eEC_GROUP;
87extern VALUE cEC_POINT;
88extern VALUE eEC_POINT;
89VALUE ossl_ec_new(EVP_PKEY *);
90void 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 */ \
97static 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 */ \
128static 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 */ \
159static 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 } \
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 */ \
186static 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)
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_ */
struct RIMemo * ptr
Definition: debug.c:65
VALUE cDSA
Definition: ossl_pkey_dsa.c:43
EVP_PKEY * DupPKeyPtr(VALUE)
Definition: ossl_pkey.c:252
VALUE cPKey
Definition: ossl_pkey.c:16
EVP_PKEY * GetPrivPKeyPtr(VALUE)
Definition: ossl_pkey.c:239
VALUE cEC
Definition: ossl_pkey_ec.c:43
VALUE eDHError
Definition: ossl_pkey_dh.c:30
VALUE cEC_GROUP
Definition: ossl_pkey_ec.c:45
void Init_ossl_dh(void)
Definition: ossl_pkey_dh.c:576
void Init_ossl_ec(void)
VALUE cRSA
Definition: ossl_pkey_rsa.c:44
VALUE ossl_pkey_new(EVP_PKEY *)
Definition: ossl_pkey.c:129
const rb_data_type_t ossl_evp_pkey_type
Definition: ossl_pkey.c:87
VALUE cEC_POINT
Definition: ossl_pkey_ec.c:47
VALUE ossl_dsa_new(EVP_PKEY *)
Definition: ossl_pkey_dsa.c:72
VALUE mPKey
Definition: ossl_pkey.c:15
VALUE ossl_ec_new(EVP_PKEY *)
Definition: ossl_pkey_ec.c:87
void Init_ossl_rsa(void)
void Init_ossl_dsa(void)
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
Definition: ossl_pkey.c:39
void ossl_generate_cb_stop(void *ptr)
Definition: ossl_pkey.c:72
VALUE eECError
Definition: ossl_pkey_ec.c:44
VALUE ossl_dh_new(EVP_PKEY *)
Definition: ossl_pkey_dh.c:58
VALUE ossl_rsa_new(EVP_PKEY *)
Definition: ossl_pkey_rsa.c:73
void Init_ossl_pkey(void)
Definition: ossl_pkey.c:412
void ossl_pkey_check_public_key(const EVP_PKEY *)
Definition: ossl_pkey.c:189
VALUE eEC_GROUP
Definition: ossl_pkey_ec.c:46
VALUE eRSAError
Definition: ossl_pkey_rsa.c:45
VALUE cDH
Definition: ossl_pkey_dh.c:29
VALUE ePKeyError
Definition: ossl_pkey.c:17
VALUE eDSAError
Definition: ossl_pkey_dsa.c:44
EVP_PKEY * GetPKeyPtr(VALUE)
Definition: ossl_pkey.c:229
VALUE eEC_POINT
Definition: ossl_pkey_ec.c:48
const char size_t n
unsigned long VALUE