zig_example 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/ext/mkmf.rb +5 -2
  3. data/lib/zig_example/version.rb +1 -1
  4. metadata +3 -56
  5. data/ext/openssl/openssl_missing.c +0 -40
  6. data/ext/openssl/openssl_missing.h +0 -238
  7. data/ext/openssl/ossl.c +0 -1295
  8. data/ext/openssl/ossl.h +0 -201
  9. data/ext/openssl/ossl_asn1.c +0 -1891
  10. data/ext/openssl/ossl_asn1.h +0 -62
  11. data/ext/openssl/ossl_bio.c +0 -42
  12. data/ext/openssl/ossl_bio.h +0 -16
  13. data/ext/openssl/ossl_bn.c +0 -1344
  14. data/ext/openssl/ossl_bn.h +0 -26
  15. data/ext/openssl/ossl_cipher.c +0 -1074
  16. data/ext/openssl/ossl_cipher.h +0 -20
  17. data/ext/openssl/ossl_config.c +0 -460
  18. data/ext/openssl/ossl_config.h +0 -16
  19. data/ext/openssl/ossl_digest.c +0 -425
  20. data/ext/openssl/ossl_digest.h +0 -20
  21. data/ext/openssl/ossl_engine.c +0 -568
  22. data/ext/openssl/ossl_engine.h +0 -19
  23. data/ext/openssl/ossl_hmac.c +0 -310
  24. data/ext/openssl/ossl_hmac.h +0 -18
  25. data/ext/openssl/ossl_kdf.c +0 -311
  26. data/ext/openssl/ossl_kdf.h +0 -6
  27. data/ext/openssl/ossl_ns_spki.c +0 -405
  28. data/ext/openssl/ossl_ns_spki.h +0 -19
  29. data/ext/openssl/ossl_ocsp.c +0 -1965
  30. data/ext/openssl/ossl_ocsp.h +0 -23
  31. data/ext/openssl/ossl_pkcs12.c +0 -275
  32. data/ext/openssl/ossl_pkcs12.h +0 -13
  33. data/ext/openssl/ossl_pkcs7.c +0 -1081
  34. data/ext/openssl/ossl_pkcs7.h +0 -36
  35. data/ext/openssl/ossl_pkey.c +0 -1624
  36. data/ext/openssl/ossl_pkey.h +0 -204
  37. data/ext/openssl/ossl_pkey_dh.c +0 -440
  38. data/ext/openssl/ossl_pkey_dsa.c +0 -359
  39. data/ext/openssl/ossl_pkey_ec.c +0 -1655
  40. data/ext/openssl/ossl_pkey_rsa.c +0 -579
  41. data/ext/openssl/ossl_rand.c +0 -200
  42. data/ext/openssl/ossl_rand.h +0 -18
  43. data/ext/openssl/ossl_ssl.c +0 -3142
  44. data/ext/openssl/ossl_ssl.h +0 -36
  45. data/ext/openssl/ossl_ssl_session.c +0 -331
  46. data/ext/openssl/ossl_ts.c +0 -1539
  47. data/ext/openssl/ossl_ts.h +0 -16
  48. data/ext/openssl/ossl_x509.c +0 -256
  49. data/ext/openssl/ossl_x509.h +0 -115
  50. data/ext/openssl/ossl_x509attr.c +0 -324
  51. data/ext/openssl/ossl_x509cert.c +0 -1002
  52. data/ext/openssl/ossl_x509crl.c +0 -545
  53. data/ext/openssl/ossl_x509ext.c +0 -490
  54. data/ext/openssl/ossl_x509name.c +0 -597
  55. data/ext/openssl/ossl_x509req.c +0 -444
  56. data/ext/openssl/ossl_x509revoked.c +0 -300
  57. data/ext/openssl/ossl_x509store.c +0 -986
@@ -1,204 +0,0 @@
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
- /* For ENGINE */
19
- #define OSSL_PKEY_SET_PRIVATE(obj) rb_ivar_set((obj), rb_intern("private"), Qtrue)
20
- #define OSSL_PKEY_IS_PRIVATE(obj) (rb_attr_get((obj), rb_intern("private")) == Qtrue)
21
-
22
- #define GetPKey(obj, pkey) do {\
23
- TypedData_Get_Struct((obj), EVP_PKEY, &ossl_evp_pkey_type, (pkey)); \
24
- if (!(pkey)) { \
25
- rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
26
- } \
27
- } while (0)
28
-
29
- /* Takes ownership of the EVP_PKEY */
30
- VALUE ossl_pkey_new(EVP_PKEY *);
31
- void ossl_pkey_check_public_key(const EVP_PKEY *);
32
- EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
33
- EVP_PKEY *GetPKeyPtr(VALUE);
34
- EVP_PKEY *DupPKeyPtr(VALUE);
35
- EVP_PKEY *GetPrivPKeyPtr(VALUE);
36
-
37
- /*
38
- * Serializes _self_ in X.509 SubjectPublicKeyInfo format and returns the
39
- * resulting String. Sub-classes use this when overriding #to_der.
40
- */
41
- VALUE ossl_pkey_export_spki(VALUE self, int to_der);
42
- /*
43
- * Serializes the private key _self_ in the traditional private key format
44
- * and returns the resulting String. Sub-classes use this when overriding
45
- * #to_der.
46
- */
47
- VALUE ossl_pkey_export_traditional(int argc, VALUE *argv, VALUE self,
48
- int to_der);
49
-
50
- void Init_ossl_pkey(void);
51
-
52
- /*
53
- * RSA
54
- */
55
- extern VALUE cRSA;
56
- extern VALUE eRSAError;
57
-
58
- void Init_ossl_rsa(void);
59
-
60
- /*
61
- * DSA
62
- */
63
- extern VALUE cDSA;
64
- extern VALUE eDSAError;
65
-
66
- void Init_ossl_dsa(void);
67
-
68
- /*
69
- * DH
70
- */
71
- extern VALUE cDH;
72
- extern VALUE eDHError;
73
-
74
- void Init_ossl_dh(void);
75
-
76
- /*
77
- * EC
78
- */
79
- extern VALUE cEC;
80
- extern VALUE eECError;
81
- extern VALUE cEC_GROUP;
82
- extern VALUE eEC_GROUP;
83
- extern VALUE cEC_POINT;
84
- extern VALUE eEC_POINT;
85
- VALUE ossl_ec_new(EVP_PKEY *);
86
- void Init_ossl_ec(void);
87
-
88
- #define OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, _name, _get) \
89
- /* \
90
- * call-seq: \
91
- * _keytype##.##_name -> aBN \
92
- */ \
93
- static VALUE ossl_##_keytype##_get_##_name(VALUE self) \
94
- { \
95
- const _type *obj; \
96
- const BIGNUM *bn; \
97
- \
98
- Get##_type(self, obj); \
99
- _get; \
100
- if (bn == NULL) \
101
- return Qnil; \
102
- return ossl_bn_new(bn); \
103
- }
104
-
105
- #define OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
106
- OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
107
- _type##_get0_##_group(obj, &bn, NULL, NULL)) \
108
- OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
109
- _type##_get0_##_group(obj, NULL, &bn, NULL)) \
110
- OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a3, \
111
- _type##_get0_##_group(obj, NULL, NULL, &bn))
112
-
113
- #define OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
114
- OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
115
- _type##_get0_##_group(obj, &bn, NULL)) \
116
- OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
117
- _type##_get0_##_group(obj, NULL, &bn))
118
-
119
- #if !OSSL_OPENSSL_PREREQ(3, 0, 0)
120
- #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
121
- /* \
122
- * call-seq: \
123
- * _keytype##.set_##_group(a1, a2, a3) -> self \
124
- */ \
125
- static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
126
- { \
127
- _type *obj; \
128
- BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
129
- BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
130
- BIGNUM *bn3 = NULL, *orig_bn3 = NIL_P(v3) ? NULL : GetBNPtr(v3);\
131
- \
132
- Get##_type(self, obj); \
133
- if ((orig_bn1 && !(bn1 = BN_dup(orig_bn1))) || \
134
- (orig_bn2 && !(bn2 = BN_dup(orig_bn2))) || \
135
- (orig_bn3 && !(bn3 = BN_dup(orig_bn3)))) { \
136
- BN_clear_free(bn1); \
137
- BN_clear_free(bn2); \
138
- BN_clear_free(bn3); \
139
- ossl_raise(eBNError, NULL); \
140
- } \
141
- \
142
- if (!_type##_set0_##_group(obj, bn1, bn2, bn3)) { \
143
- BN_clear_free(bn1); \
144
- BN_clear_free(bn2); \
145
- BN_clear_free(bn3); \
146
- ossl_raise(ePKeyError, #_type"_set0_"#_group); \
147
- } \
148
- return self; \
149
- }
150
-
151
- #define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
152
- /* \
153
- * call-seq: \
154
- * _keytype##.set_##_group(a1, a2) -> self \
155
- */ \
156
- static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
157
- { \
158
- _type *obj; \
159
- BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
160
- BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
161
- \
162
- Get##_type(self, obj); \
163
- if ((orig_bn1 && !(bn1 = BN_dup(orig_bn1))) || \
164
- (orig_bn2 && !(bn2 = BN_dup(orig_bn2)))) { \
165
- BN_clear_free(bn1); \
166
- BN_clear_free(bn2); \
167
- ossl_raise(eBNError, NULL); \
168
- } \
169
- \
170
- if (!_type##_set0_##_group(obj, bn1, bn2)) { \
171
- BN_clear_free(bn1); \
172
- BN_clear_free(bn2); \
173
- ossl_raise(ePKeyError, #_type"_set0_"#_group); \
174
- } \
175
- return self; \
176
- }
177
- #else
178
- #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
179
- static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
180
- { \
181
- rb_raise(ePKeyError, \
182
- #_keytype"#set_"#_group"= is incompatible with OpenSSL 3.0"); \
183
- }
184
-
185
- #define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
186
- static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
187
- { \
188
- rb_raise(ePKeyError, \
189
- #_keytype"#set_"#_group"= is incompatible with OpenSSL 3.0"); \
190
- }
191
- #endif
192
-
193
- #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
194
- OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
195
- OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)
196
-
197
- #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
198
- OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
199
- OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2)
200
-
201
- #define DEF_OSSL_PKEY_BN(class, keytype, name) \
202
- rb_define_method((class), #name, ossl_##keytype##_get_##name, 0)
203
-
204
- #endif /* OSSL_PKEY_H */
@@ -1,440 +0,0 @@
1
- /*
2
- * 'OpenSSL for Ruby' project
3
- * Copyright (C) 2001-2002 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
- #include "ossl.h"
11
-
12
- #if !defined(OPENSSL_NO_DH)
13
-
14
- #define GetPKeyDH(obj, pkey) do { \
15
- GetPKey((obj), (pkey)); \
16
- if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) { /* PARANOIA? */ \
17
- ossl_raise(rb_eRuntimeError, "THIS IS NOT A DH!") ; \
18
- } \
19
- } while (0)
20
- #define GetDH(obj, dh) do { \
21
- EVP_PKEY *_pkey; \
22
- GetPKeyDH((obj), _pkey); \
23
- (dh) = EVP_PKEY_get0_DH(_pkey); \
24
- } while (0)
25
-
26
- /*
27
- * Classes
28
- */
29
- VALUE cDH;
30
- VALUE eDHError;
31
-
32
- /*
33
- * Private
34
- */
35
- /*
36
- * call-seq:
37
- * DH.new -> dh
38
- * DH.new(string) -> dh
39
- * DH.new(size [, generator]) -> dh
40
- *
41
- * Creates a new instance of OpenSSL::PKey::DH.
42
- *
43
- * If called without arguments, an empty instance without any parameter or key
44
- * components is created. Use #set_pqg to manually set the parameters afterwards
45
- * (and optionally #set_key to set private and public key components).
46
- *
47
- * If a String is given, tries to parse it as a DER- or PEM- encoded parameters.
48
- * See also OpenSSL::PKey.read which can parse keys of any kinds.
49
- *
50
- * The DH.new(size [, generator]) form is an alias of DH.generate.
51
- *
52
- * +string+::
53
- * A String that contains the DER or PEM encoded key.
54
- * +size+::
55
- * See DH.generate.
56
- * +generator+::
57
- * See DH.generate.
58
- *
59
- * Examples:
60
- * # Creating an instance from scratch
61
- * # Note that this is deprecated and will not work on OpenSSL 3.0 or later.
62
- * dh = OpenSSL::PKey::DH.new
63
- * dh.set_pqg(bn_p, nil, bn_g)
64
- *
65
- * # Generating a parameters and a key pair
66
- * dh = OpenSSL::PKey::DH.new(2048) # An alias of OpenSSL::PKey::DH.generate(2048)
67
- *
68
- * # Reading DH parameters
69
- * dh_params = OpenSSL::PKey::DH.new(File.read('parameters.pem')) # loads parameters only
70
- * dh = OpenSSL::PKey.generate_key(dh_params) # generates a key pair
71
- */
72
- static VALUE
73
- ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
74
- {
75
- EVP_PKEY *pkey;
76
- int type;
77
- DH *dh;
78
- BIO *in = NULL;
79
- VALUE arg;
80
-
81
- TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
82
- if (pkey)
83
- rb_raise(rb_eTypeError, "pkey already initialized");
84
-
85
- /* The DH.new(size, generator) form is handled by lib/openssl/pkey.rb */
86
- if (rb_scan_args(argc, argv, "01", &arg) == 0) {
87
- dh = DH_new();
88
- if (!dh)
89
- ossl_raise(eDHError, "DH_new");
90
- goto legacy;
91
- }
92
-
93
- arg = ossl_to_der_if_possible(arg);
94
- in = ossl_obj2bio(&arg);
95
-
96
- /*
97
- * On OpenSSL <= 1.1.1 and current versions of LibreSSL, the generic
98
- * routine does not support DER-encoded parameters
99
- */
100
- dh = d2i_DHparams_bio(in, NULL);
101
- if (dh)
102
- goto legacy;
103
- OSSL_BIO_reset(in);
104
-
105
- pkey = ossl_pkey_read_generic(in, Qnil);
106
- BIO_free(in);
107
- if (!pkey)
108
- ossl_raise(eDHError, "could not parse pkey");
109
-
110
- type = EVP_PKEY_base_id(pkey);
111
- if (type != EVP_PKEY_DH) {
112
- EVP_PKEY_free(pkey);
113
- rb_raise(eDHError, "incorrect pkey type: %s", OBJ_nid2sn(type));
114
- }
115
- RTYPEDDATA_DATA(self) = pkey;
116
- return self;
117
-
118
- legacy:
119
- BIO_free(in);
120
- pkey = EVP_PKEY_new();
121
- if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
122
- EVP_PKEY_free(pkey);
123
- DH_free(dh);
124
- ossl_raise(eDHError, "EVP_PKEY_assign_DH");
125
- }
126
- RTYPEDDATA_DATA(self) = pkey;
127
- return self;
128
- }
129
-
130
- #ifndef HAVE_EVP_PKEY_DUP
131
- static VALUE
132
- ossl_dh_initialize_copy(VALUE self, VALUE other)
133
- {
134
- EVP_PKEY *pkey;
135
- DH *dh, *dh_other;
136
- const BIGNUM *pub, *priv;
137
-
138
- TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
139
- if (pkey)
140
- rb_raise(rb_eTypeError, "pkey already initialized");
141
- GetDH(other, dh_other);
142
-
143
- dh = DHparams_dup(dh_other);
144
- if (!dh)
145
- ossl_raise(eDHError, "DHparams_dup");
146
-
147
- DH_get0_key(dh_other, &pub, &priv);
148
- if (pub) {
149
- BIGNUM *pub2 = BN_dup(pub);
150
- BIGNUM *priv2 = BN_dup(priv);
151
-
152
- if (!pub2 || (priv && !priv2)) {
153
- BN_clear_free(pub2);
154
- BN_clear_free(priv2);
155
- ossl_raise(eDHError, "BN_dup");
156
- }
157
- DH_set0_key(dh, pub2, priv2);
158
- }
159
-
160
- pkey = EVP_PKEY_new();
161
- if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
162
- EVP_PKEY_free(pkey);
163
- DH_free(dh);
164
- ossl_raise(eDHError, "EVP_PKEY_assign_DH");
165
- }
166
- RTYPEDDATA_DATA(self) = pkey;
167
- return self;
168
- }
169
- #endif
170
-
171
- /*
172
- * call-seq:
173
- * dh.public? -> true | false
174
- *
175
- * Indicates whether this DH instance has a public key associated with it or
176
- * not. The public key may be retrieved with DH#pub_key.
177
- */
178
- static VALUE
179
- ossl_dh_is_public(VALUE self)
180
- {
181
- OSSL_3_const DH *dh;
182
- const BIGNUM *bn;
183
-
184
- GetDH(self, dh);
185
- DH_get0_key(dh, &bn, NULL);
186
-
187
- return bn ? Qtrue : Qfalse;
188
- }
189
-
190
- /*
191
- * call-seq:
192
- * dh.private? -> true | false
193
- *
194
- * Indicates whether this DH instance has a private key associated with it or
195
- * not. The private key may be retrieved with DH#priv_key.
196
- */
197
- static VALUE
198
- ossl_dh_is_private(VALUE self)
199
- {
200
- OSSL_3_const DH *dh;
201
- const BIGNUM *bn;
202
-
203
- GetDH(self, dh);
204
- DH_get0_key(dh, NULL, &bn);
205
-
206
- #if !defined(OPENSSL_NO_ENGINE)
207
- return (bn || DH_get0_engine((DH *)dh)) ? Qtrue : Qfalse;
208
- #else
209
- return bn ? Qtrue : Qfalse;
210
- #endif
211
- }
212
-
213
- /*
214
- * call-seq:
215
- * dh.export -> aString
216
- * dh.to_pem -> aString
217
- * dh.to_s -> aString
218
- *
219
- * Encodes this DH to its PEM encoding. Note that any existing per-session
220
- * public/private keys will *not* get encoded, just the Diffie-Hellman
221
- * parameters will be encoded.
222
- */
223
- static VALUE
224
- ossl_dh_export(VALUE self)
225
- {
226
- OSSL_3_const DH *dh;
227
- BIO *out;
228
- VALUE str;
229
-
230
- GetDH(self, dh);
231
- if (!(out = BIO_new(BIO_s_mem()))) {
232
- ossl_raise(eDHError, NULL);
233
- }
234
- if (!PEM_write_bio_DHparams(out, dh)) {
235
- BIO_free(out);
236
- ossl_raise(eDHError, NULL);
237
- }
238
- str = ossl_membio2str(out);
239
-
240
- return str;
241
- }
242
-
243
- /*
244
- * call-seq:
245
- * dh.to_der -> aString
246
- *
247
- * Encodes this DH to its DER encoding. Note that any existing per-session
248
- * public/private keys will *not* get encoded, just the Diffie-Hellman
249
- * parameters will be encoded.
250
-
251
- */
252
- static VALUE
253
- ossl_dh_to_der(VALUE self)
254
- {
255
- OSSL_3_const DH *dh;
256
- unsigned char *p;
257
- long len;
258
- VALUE str;
259
-
260
- GetDH(self, dh);
261
- if((len = i2d_DHparams(dh, NULL)) <= 0)
262
- ossl_raise(eDHError, NULL);
263
- str = rb_str_new(0, len);
264
- p = (unsigned char *)RSTRING_PTR(str);
265
- if(i2d_DHparams(dh, &p) < 0)
266
- ossl_raise(eDHError, NULL);
267
- ossl_str_adjust(str, p);
268
-
269
- return str;
270
- }
271
-
272
- /*
273
- * call-seq:
274
- * dh.params -> hash
275
- *
276
- * Stores all parameters of key to the hash
277
- * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
278
- * Don't use :-)) (I's up to you)
279
- */
280
- static VALUE
281
- ossl_dh_get_params(VALUE self)
282
- {
283
- OSSL_3_const DH *dh;
284
- VALUE hash;
285
- const BIGNUM *p, *q, *g, *pub_key, *priv_key;
286
-
287
- GetDH(self, dh);
288
- DH_get0_pqg(dh, &p, &q, &g);
289
- DH_get0_key(dh, &pub_key, &priv_key);
290
-
291
- hash = rb_hash_new();
292
- rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
293
- rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
294
- rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(g));
295
- rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pub_key));
296
- rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(priv_key));
297
-
298
- return hash;
299
- }
300
-
301
- /*
302
- * call-seq:
303
- * dh.params_ok? -> true | false
304
- *
305
- * Validates the Diffie-Hellman parameters associated with this instance.
306
- * It checks whether a safe prime and a suitable generator are used. If this
307
- * is not the case, +false+ is returned.
308
- *
309
- * See also the man page EVP_PKEY_param_check(3).
310
- */
311
- static VALUE
312
- ossl_dh_check_params(VALUE self)
313
- {
314
- int ret;
315
- #ifdef HAVE_EVP_PKEY_CHECK
316
- EVP_PKEY *pkey;
317
- EVP_PKEY_CTX *pctx;
318
-
319
- GetPKey(self, pkey);
320
- pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
321
- if (!pctx)
322
- ossl_raise(eDHError, "EVP_PKEY_CTX_new");
323
- ret = EVP_PKEY_param_check(pctx);
324
- EVP_PKEY_CTX_free(pctx);
325
- #else
326
- DH *dh;
327
- int codes;
328
-
329
- GetDH(self, dh);
330
- ret = DH_check(dh, &codes) == 1 && codes == 0;
331
- #endif
332
-
333
- if (ret == 1)
334
- return Qtrue;
335
- else {
336
- /* DH_check_ex() will put error entry on failure */
337
- ossl_clear_error();
338
- return Qfalse;
339
- }
340
- }
341
-
342
- /*
343
- * Document-method: OpenSSL::PKey::DH#set_pqg
344
- * call-seq:
345
- * dh.set_pqg(p, q, g) -> self
346
- *
347
- * Sets _p_, _q_, _g_ to the DH instance.
348
- */
349
- OSSL_PKEY_BN_DEF3(dh, DH, pqg, p, q, g)
350
- /*
351
- * Document-method: OpenSSL::PKey::DH#set_key
352
- * call-seq:
353
- * dh.set_key(pub_key, priv_key) -> self
354
- *
355
- * Sets _pub_key_ and _priv_key_ for the DH instance. _priv_key_ may be +nil+.
356
- */
357
- OSSL_PKEY_BN_DEF2(dh, DH, key, pub_key, priv_key)
358
-
359
- /*
360
- * INIT
361
- */
362
- void
363
- Init_ossl_dh(void)
364
- {
365
- #if 0
366
- mPKey = rb_define_module_under(mOSSL, "PKey");
367
- cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
368
- ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
369
- #endif
370
-
371
- /* Document-class: OpenSSL::PKey::DHError
372
- *
373
- * Generic exception that is raised if an operation on a DH PKey
374
- * fails unexpectedly or in case an instantiation of an instance of DH
375
- * fails due to non-conformant input data.
376
- */
377
- eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
378
- /* Document-class: OpenSSL::PKey::DH
379
- *
380
- * An implementation of the Diffie-Hellman key exchange protocol based on
381
- * discrete logarithms in finite fields, the same basis that DSA is built
382
- * on.
383
- *
384
- * === Accessor methods for the Diffie-Hellman parameters
385
- * DH#p::
386
- * The prime (an OpenSSL::BN) of the Diffie-Hellman parameters.
387
- * DH#g::
388
- * The generator (an OpenSSL::BN) g of the Diffie-Hellman parameters.
389
- * DH#pub_key::
390
- * The per-session public key (an OpenSSL::BN) matching the private key.
391
- * This needs to be passed to DH#compute_key.
392
- * DH#priv_key::
393
- * The per-session private key, an OpenSSL::BN.
394
- *
395
- * === Example of a key exchange
396
- * # you may send the parameters (der) and own public key (pub1) publicly
397
- * # to the participating party
398
- * dh1 = OpenSSL::PKey::DH.new(2048)
399
- * der = dh1.to_der
400
- * pub1 = dh1.pub_key
401
- *
402
- * # the other party generates its per-session key pair
403
- * dhparams = OpenSSL::PKey::DH.new(der)
404
- * dh2 = OpenSSL::PKey.generate_key(dhparams)
405
- * pub2 = dh2.pub_key
406
- *
407
- * symm_key1 = dh1.compute_key(pub2)
408
- * symm_key2 = dh2.compute_key(pub1)
409
- * puts symm_key1 == symm_key2 # => true
410
- */
411
- cDH = rb_define_class_under(mPKey, "DH", cPKey);
412
- rb_define_method(cDH, "initialize", ossl_dh_initialize, -1);
413
- #ifndef HAVE_EVP_PKEY_DUP
414
- rb_define_method(cDH, "initialize_copy", ossl_dh_initialize_copy, 1);
415
- #endif
416
- rb_define_method(cDH, "public?", ossl_dh_is_public, 0);
417
- rb_define_method(cDH, "private?", ossl_dh_is_private, 0);
418
- rb_define_method(cDH, "export", ossl_dh_export, 0);
419
- rb_define_alias(cDH, "to_pem", "export");
420
- rb_define_alias(cDH, "to_s", "export");
421
- rb_define_method(cDH, "to_der", ossl_dh_to_der, 0);
422
- rb_define_method(cDH, "params_ok?", ossl_dh_check_params, 0);
423
-
424
- DEF_OSSL_PKEY_BN(cDH, dh, p);
425
- DEF_OSSL_PKEY_BN(cDH, dh, q);
426
- DEF_OSSL_PKEY_BN(cDH, dh, g);
427
- DEF_OSSL_PKEY_BN(cDH, dh, pub_key);
428
- DEF_OSSL_PKEY_BN(cDH, dh, priv_key);
429
- rb_define_method(cDH, "set_pqg", ossl_dh_set_pqg, 3);
430
- rb_define_method(cDH, "set_key", ossl_dh_set_key, 2);
431
-
432
- rb_define_method(cDH, "params", ossl_dh_get_params, 0);
433
- }
434
-
435
- #else /* defined NO_DH */
436
- void
437
- Init_ossl_dh(void)
438
- {
439
- }
440
- #endif /* NO_DH */