rubysl-openssl 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -6
- data/ext/rubysl/openssl/.gitignore +3 -0
- data/ext/rubysl/openssl/deprecation.rb +21 -0
- data/ext/rubysl/openssl/extconf.rb +45 -32
- data/ext/rubysl/openssl/openssl_missing.c +20 -7
- data/ext/rubysl/openssl/openssl_missing.h +22 -15
- data/ext/rubysl/openssl/ossl.c +610 -61
- data/ext/rubysl/openssl/ossl.h +31 -17
- data/ext/rubysl/openssl/ossl_asn1.c +974 -183
- data/ext/rubysl/openssl/ossl_asn1.h +3 -3
- data/ext/rubysl/openssl/ossl_bio.c +4 -3
- data/ext/rubysl/openssl/ossl_bio.h +1 -1
- data/ext/rubysl/openssl/ossl_bn.c +32 -28
- data/ext/rubysl/openssl/ossl_bn.h +1 -1
- data/ext/rubysl/openssl/ossl_cipher.c +494 -93
- data/ext/rubysl/openssl/ossl_cipher.h +1 -1
- data/ext/rubysl/openssl/ossl_config.c +4 -5
- data/ext/rubysl/openssl/ossl_config.h +1 -1
- data/ext/rubysl/openssl/ossl_digest.c +206 -24
- data/ext/rubysl/openssl/ossl_digest.h +1 -1
- data/ext/rubysl/openssl/ossl_engine.c +48 -26
- data/ext/rubysl/openssl/ossl_engine.h +1 -1
- data/ext/rubysl/openssl/ossl_hmac.c +40 -38
- data/ext/rubysl/openssl/ossl_hmac.h +1 -1
- data/ext/rubysl/openssl/ossl_ns_spki.c +157 -25
- data/ext/rubysl/openssl/ossl_ns_spki.h +1 -1
- data/ext/rubysl/openssl/ossl_ocsp.c +57 -40
- data/ext/rubysl/openssl/ossl_ocsp.h +1 -1
- data/ext/rubysl/openssl/ossl_pkcs12.c +15 -13
- data/ext/rubysl/openssl/ossl_pkcs12.h +1 -1
- data/ext/rubysl/openssl/ossl_pkcs5.c +108 -18
- data/ext/rubysl/openssl/ossl_pkcs7.c +44 -37
- data/ext/rubysl/openssl/ossl_pkcs7.h +1 -1
- data/ext/rubysl/openssl/ossl_pkey.c +211 -15
- data/ext/rubysl/openssl/ossl_pkey.h +19 -9
- data/ext/rubysl/openssl/ossl_pkey_dh.c +180 -47
- data/ext/rubysl/openssl/ossl_pkey_dsa.c +184 -47
- data/ext/rubysl/openssl/ossl_pkey_ec.c +177 -93
- data/ext/rubysl/openssl/ossl_pkey_rsa.c +209 -102
- data/ext/rubysl/openssl/ossl_rand.c +15 -15
- data/ext/rubysl/openssl/ossl_rand.h +1 -1
- data/ext/rubysl/openssl/ossl_ssl.c +939 -192
- data/ext/rubysl/openssl/ossl_ssl.h +6 -6
- data/ext/rubysl/openssl/ossl_ssl_session.c +78 -62
- data/ext/rubysl/openssl/ossl_version.h +2 -2
- data/ext/rubysl/openssl/ossl_x509.c +1 -1
- data/ext/rubysl/openssl/ossl_x509.h +1 -1
- data/ext/rubysl/openssl/ossl_x509attr.c +20 -19
- data/ext/rubysl/openssl/ossl_x509cert.c +169 -67
- data/ext/rubysl/openssl/ossl_x509crl.c +41 -39
- data/ext/rubysl/openssl/ossl_x509ext.c +51 -38
- data/ext/rubysl/openssl/ossl_x509name.c +139 -29
- data/ext/rubysl/openssl/ossl_x509req.c +42 -40
- data/ext/rubysl/openssl/ossl_x509revoked.c +20 -20
- data/ext/rubysl/openssl/ossl_x509store.c +99 -47
- data/ext/rubysl/openssl/ruby_missing.h +3 -16
- data/lib/openssl/bn.rb +19 -19
- data/lib/openssl/buffering.rb +222 -14
- data/lib/openssl/cipher.rb +20 -20
- data/lib/openssl/config.rb +1 -4
- data/lib/openssl/digest.rb +47 -19
- data/lib/openssl/ssl.rb +197 -1
- data/lib/openssl/x509.rb +162 -1
- data/lib/rubysl/openssl.rb +4 -8
- data/lib/rubysl/openssl/version.rb +1 -1
- data/rubysl-openssl.gemspec +1 -2
- metadata +16 -34
- data/ext/rubysl/openssl/extconf.h +0 -50
- data/lib/openssl/net/ftptls.rb +0 -53
- data/lib/openssl/net/telnets.rb +0 -251
- data/lib/openssl/pkcs7.rb +0 -25
- data/lib/openssl/ssl-internal.rb +0 -187
- data/lib/openssl/x509-internal.rb +0 -153
@@ -1,5 +1,5 @@
|
|
1
1
|
/*
|
2
|
-
* $Id
|
2
|
+
* $Id$
|
3
3
|
* 'OpenSSL for Ruby' project
|
4
4
|
* Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
|
5
5
|
* All rights reserved.
|
@@ -21,24 +21,34 @@ extern ID id_private_q;
|
|
21
21
|
#define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
|
22
22
|
|
23
23
|
#define WrapPKey(klass, obj, pkey) do { \
|
24
|
-
if (!pkey) { \
|
24
|
+
if (!(pkey)) { \
|
25
25
|
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
|
26
26
|
} \
|
27
|
-
obj = Data_Wrap_Struct(klass, 0, EVP_PKEY_free, pkey); \
|
27
|
+
(obj) = Data_Wrap_Struct((klass), 0, EVP_PKEY_free, (pkey)); \
|
28
28
|
OSSL_PKEY_SET_PUBLIC(obj); \
|
29
29
|
} while (0)
|
30
30
|
#define GetPKey(obj, pkey) do {\
|
31
|
-
Data_Get_Struct(obj, EVP_PKEY, pkey);\
|
32
|
-
if (!pkey) { \
|
31
|
+
Data_Get_Struct((obj), EVP_PKEY, (pkey));\
|
32
|
+
if (!(pkey)) { \
|
33
33
|
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
|
34
34
|
} \
|
35
35
|
} while (0)
|
36
36
|
#define SafeGetPKey(obj, pkey) do { \
|
37
|
-
OSSL_Check_Kind(obj, cPKey); \
|
38
|
-
GetPKey(obj, pkey); \
|
37
|
+
OSSL_Check_Kind((obj), cPKey); \
|
38
|
+
GetPKey((obj), (pkey)); \
|
39
39
|
} while (0)
|
40
40
|
|
41
41
|
void ossl_generate_cb(int, int, void *);
|
42
|
+
#define HAVE_BN_GENCB defined(HAVE_RSA_GENERATE_KEY_EX) || defined(HAVE_DH_GENERATE_PARAMETERS_EX) || defined(HAVE_DSA_GENERATE_PARAMETERS_EX)
|
43
|
+
#if HAVE_BN_GENCB
|
44
|
+
struct ossl_generate_cb_arg {
|
45
|
+
int yield;
|
46
|
+
int stop;
|
47
|
+
int state;
|
48
|
+
};
|
49
|
+
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
|
50
|
+
void ossl_generate_cb_stop(void *ptr);
|
51
|
+
#endif
|
42
52
|
|
43
53
|
VALUE ossl_pkey_new(EVP_PKEY *);
|
44
54
|
VALUE ossl_pkey_new_from_file(VALUE);
|
@@ -134,8 +144,8 @@ static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
|
|
134
144
|
|
135
145
|
#define DEF_OSSL_PKEY_BN(class, keytype, name) \
|
136
146
|
do { \
|
137
|
-
rb_define_method(class, #name, ossl_##keytype##_get_##name, 0); \
|
138
|
-
rb_define_method(class, #name "=", ossl_##keytype##_set_##name, 1);\
|
147
|
+
rb_define_method((class), #name, ossl_##keytype##_get_##name, 0); \
|
148
|
+
rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
|
139
149
|
} while (0)
|
140
150
|
|
141
151
|
#endif /* _OSSL_PKEY_H_ */
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/*
|
2
|
-
* $Id
|
2
|
+
* $Id$
|
3
3
|
* 'OpenSSL for Ruby' project
|
4
4
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
5
5
|
* All rights reserved.
|
@@ -13,8 +13,8 @@
|
|
13
13
|
#include "ossl.h"
|
14
14
|
|
15
15
|
#define GetPKeyDH(obj, pkey) do { \
|
16
|
-
GetPKey(obj, pkey); \
|
17
|
-
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) { /* PARANOIA? */ \
|
16
|
+
GetPKey((obj), (pkey)); \
|
17
|
+
if (EVP_PKEY_type((pkey)->type) != EVP_PKEY_DH) { /* PARANOIA? */ \
|
18
18
|
ossl_raise(rb_eRuntimeError, "THIS IS NOT A DH!") ; \
|
19
19
|
} \
|
20
20
|
} while (0)
|
@@ -42,7 +42,7 @@ dh_instance(VALUE klass, DH *dh)
|
|
42
42
|
{
|
43
43
|
EVP_PKEY *pkey;
|
44
44
|
VALUE obj;
|
45
|
-
|
45
|
+
|
46
46
|
if (!dh) {
|
47
47
|
return Qfalse;
|
48
48
|
}
|
@@ -81,20 +81,67 @@ ossl_dh_new(EVP_PKEY *pkey)
|
|
81
81
|
/*
|
82
82
|
* Private
|
83
83
|
*/
|
84
|
+
#if defined(HAVE_DH_GENERATE_PARAMETERS_EX) && HAVE_BN_GENCB
|
85
|
+
struct dh_blocking_gen_arg {
|
86
|
+
DH *dh;
|
87
|
+
int size;
|
88
|
+
int gen;
|
89
|
+
BN_GENCB *cb;
|
90
|
+
int result;
|
91
|
+
};
|
92
|
+
|
93
|
+
static void *
|
94
|
+
dh_blocking_gen(void *arg)
|
95
|
+
{
|
96
|
+
struct dh_blocking_gen_arg *gen = (struct dh_blocking_gen_arg *)arg;
|
97
|
+
gen->result = DH_generate_parameters_ex(gen->dh, gen->size, gen->gen, gen->cb);
|
98
|
+
return 0;
|
99
|
+
}
|
100
|
+
#endif
|
101
|
+
|
84
102
|
static DH *
|
85
103
|
dh_generate(int size, int gen)
|
86
104
|
{
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
105
|
+
#if defined(HAVE_DH_GENERATE_PARAMETERS_EX) && HAVE_BN_GENCB
|
106
|
+
BN_GENCB cb;
|
107
|
+
struct ossl_generate_cb_arg cb_arg;
|
108
|
+
struct dh_blocking_gen_arg gen_arg;
|
109
|
+
DH *dh = DH_new();
|
110
|
+
|
92
111
|
if (!dh) return 0;
|
93
112
|
|
94
|
-
|
113
|
+
memset(&cb_arg, 0, sizeof(struct ossl_generate_cb_arg));
|
114
|
+
if (rb_block_given_p())
|
115
|
+
cb_arg.yield = 1;
|
116
|
+
BN_GENCB_set(&cb, ossl_generate_cb_2, &cb_arg);
|
117
|
+
gen_arg.dh = dh;
|
118
|
+
gen_arg.size = size;
|
119
|
+
gen_arg.gen = gen;
|
120
|
+
gen_arg.cb = &cb;
|
121
|
+
if (cb_arg.yield == 1) {
|
122
|
+
/* we cannot release GVL when callback proc is supplied */
|
123
|
+
dh_blocking_gen(&gen_arg);
|
124
|
+
} else {
|
125
|
+
/* there's a chance to unblock */
|
126
|
+
rb_thread_call_without_gvl(dh_blocking_gen, &gen_arg, ossl_generate_cb_stop, &cb_arg);
|
127
|
+
}
|
128
|
+
|
129
|
+
if (!gen_arg.result) {
|
95
130
|
DH_free(dh);
|
131
|
+
if (cb_arg.state) rb_jump_tag(cb_arg.state);
|
96
132
|
return 0;
|
97
133
|
}
|
134
|
+
#else
|
135
|
+
DH *dh;
|
136
|
+
|
137
|
+
dh = DH_generate_parameters(size, gen, rb_block_given_p() ? ossl_generate_cb : NULL, NULL);
|
138
|
+
if (!dh) return 0;
|
139
|
+
#endif
|
140
|
+
|
141
|
+
if (!DH_generate_key(dh)) {
|
142
|
+
DH_free(dh);
|
143
|
+
return 0;
|
144
|
+
}
|
98
145
|
|
99
146
|
return dh;
|
100
147
|
}
|
@@ -103,9 +150,12 @@ dh_generate(int size, int gen)
|
|
103
150
|
* call-seq:
|
104
151
|
* DH.generate(size [, generator]) -> dh
|
105
152
|
*
|
106
|
-
*
|
107
|
-
*
|
108
|
-
*
|
153
|
+
* Creates a new DH instance from scratch by generating the private and public
|
154
|
+
* components alike.
|
155
|
+
*
|
156
|
+
* === Parameters
|
157
|
+
* * +size+ is an integer representing the desired key size. Keys smaller than 1024 bits should be considered insecure.
|
158
|
+
* * +generator+ is a small number > 1, typically 2 or 5.
|
109
159
|
*
|
110
160
|
*/
|
111
161
|
static VALUE
|
@@ -114,7 +164,7 @@ ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
|
|
114
164
|
DH *dh ;
|
115
165
|
int g = 2;
|
116
166
|
VALUE size, gen, obj;
|
117
|
-
|
167
|
+
|
118
168
|
if (rb_scan_args(argc, argv, "11", &size, &gen) == 2) {
|
119
169
|
g = NUM2INT(gen);
|
120
170
|
}
|
@@ -132,16 +182,24 @@ ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
|
|
132
182
|
* call-seq:
|
133
183
|
* DH.new([size [, generator] | string]) -> dh
|
134
184
|
*
|
135
|
-
*
|
136
|
-
*
|
137
|
-
*
|
138
|
-
*
|
185
|
+
* Either generates a DH instance from scratch or by reading already existing
|
186
|
+
* DH parameters from +string+. Note that when reading a DH instance from
|
187
|
+
* data that was encoded from a DH instance by using DH#to_pem or DH#to_der
|
188
|
+
* the result will *not* contain a public/private key pair yet. This needs to
|
189
|
+
* be generated using DH#generate_key! first.
|
190
|
+
*
|
191
|
+
* === Parameters
|
192
|
+
* * +size+ is an integer representing the desired key size. Keys smaller than 1024 bits should be considered insecure.
|
193
|
+
* * +generator+ is a small number > 1, typically 2 or 5.
|
194
|
+
* * +string+ contains the DER or PEM encoded key.
|
139
195
|
*
|
140
|
-
*
|
141
|
-
*
|
142
|
-
*
|
143
|
-
*
|
144
|
-
*
|
196
|
+
* === Examples
|
197
|
+
* DH.new # -> dh
|
198
|
+
* DH.new(1024) # -> dh
|
199
|
+
* DH.new(1024, 5) # -> dh
|
200
|
+
* #Reading DH parameters
|
201
|
+
* dh = DH.new(File.read('parameters.pem')) # -> dh, but no public/private key yet
|
202
|
+
* dh.generate_key! # -> dh with public and private key
|
145
203
|
*/
|
146
204
|
static VALUE
|
147
205
|
ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
|
@@ -169,11 +227,13 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
|
|
169
227
|
in = ossl_obj2bio(arg);
|
170
228
|
dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
|
171
229
|
if (!dh){
|
172
|
-
|
230
|
+
OSSL_BIO_reset(in);
|
173
231
|
dh = d2i_DHparams_bio(in, NULL);
|
174
232
|
}
|
175
233
|
BIO_free(in);
|
176
|
-
if (!dh)
|
234
|
+
if (!dh) {
|
235
|
+
ossl_raise(eDHError, NULL);
|
236
|
+
}
|
177
237
|
}
|
178
238
|
if (!EVP_PKEY_assign_DH(pkey, dh)) {
|
179
239
|
DH_free(dh);
|
@@ -186,6 +246,8 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
|
|
186
246
|
* call-seq:
|
187
247
|
* dh.public? -> true | false
|
188
248
|
*
|
249
|
+
* Indicates whether this DH instance has a public key associated with it or
|
250
|
+
* not. The public key may be retrieved with DH#pub_key.
|
189
251
|
*/
|
190
252
|
static VALUE
|
191
253
|
ossl_dh_is_public(VALUE self)
|
@@ -201,6 +263,8 @@ ossl_dh_is_public(VALUE self)
|
|
201
263
|
* call-seq:
|
202
264
|
* dh.private? -> true | false
|
203
265
|
*
|
266
|
+
* Indicates whether this DH instance has a private key associated with it or
|
267
|
+
* not. The private key may be retrieved with DH#priv_key.
|
204
268
|
*/
|
205
269
|
static VALUE
|
206
270
|
ossl_dh_is_private(VALUE self)
|
@@ -208,7 +272,7 @@ ossl_dh_is_private(VALUE self)
|
|
208
272
|
EVP_PKEY *pkey;
|
209
273
|
|
210
274
|
GetPKeyDH(self, pkey);
|
211
|
-
|
275
|
+
|
212
276
|
return (DH_PRIVATE(pkey->pkey.dh)) ? Qtrue : Qfalse;
|
213
277
|
}
|
214
278
|
|
@@ -216,6 +280,9 @@ ossl_dh_is_private(VALUE self)
|
|
216
280
|
* call-seq:
|
217
281
|
* dh.to_pem -> aString
|
218
282
|
*
|
283
|
+
* Encodes this DH to its PEM encoding. Note that any existing per-session
|
284
|
+
* public/private keys will *not* get encoded, just the Diffie-Hellman
|
285
|
+
* parameters will be encoded.
|
219
286
|
*/
|
220
287
|
static VALUE
|
221
288
|
ossl_dh_export(VALUE self)
|
@@ -241,10 +308,14 @@ ossl_dh_export(VALUE self)
|
|
241
308
|
* call-seq:
|
242
309
|
* dh.to_der -> aString
|
243
310
|
*
|
311
|
+
* Encodes this DH to its DER encoding. Note that any existing per-session
|
312
|
+
* public/private keys will *not* get encoded, just the Diffie-Hellman
|
313
|
+
* parameters will be encoded.
|
314
|
+
|
244
315
|
*/
|
245
316
|
static VALUE
|
246
317
|
ossl_dh_to_der(VALUE self)
|
247
|
-
{
|
318
|
+
{
|
248
319
|
EVP_PKEY *pkey;
|
249
320
|
unsigned char *p;
|
250
321
|
long len;
|
@@ -254,7 +325,7 @@ ossl_dh_to_der(VALUE self)
|
|
254
325
|
if((len = i2d_DHparams(pkey->pkey.dh, NULL)) <= 0)
|
255
326
|
ossl_raise(eDHError, NULL);
|
256
327
|
str = rb_str_new(0, len);
|
257
|
-
p = RSTRING_PTR(str);
|
328
|
+
p = (unsigned char *)RSTRING_PTR(str);
|
258
329
|
if(i2d_DHparams(pkey->pkey.dh, &p) < 0)
|
259
330
|
ossl_raise(eDHError, NULL);
|
260
331
|
ossl_str_adjust(str, p);
|
@@ -284,7 +355,7 @@ ossl_dh_get_params(VALUE self)
|
|
284
355
|
rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(pkey->pkey.dh->g));
|
285
356
|
rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pkey->pkey.dh->pub_key));
|
286
357
|
rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(pkey->pkey.dh->priv_key));
|
287
|
-
|
358
|
+
|
288
359
|
return hash;
|
289
360
|
}
|
290
361
|
|
@@ -320,7 +391,22 @@ ossl_dh_to_text(VALUE self)
|
|
320
391
|
* call-seq:
|
321
392
|
* dh.public_key -> aDH
|
322
393
|
*
|
323
|
-
*
|
394
|
+
* Returns a new DH instance that carries just the public information, i.e.
|
395
|
+
* the prime +p+ and the generator +g+, but no public/private key yet. Such
|
396
|
+
* a pair may be generated using DH#generate_key!. The "public key" needed
|
397
|
+
* for a key exchange with DH#compute_key is considered as per-session
|
398
|
+
* information and may be retrieved with DH#pub_key once a key pair has
|
399
|
+
* been generated.
|
400
|
+
* If the current instance already contains private information (and thus a
|
401
|
+
* valid public/private key pair), this information will no longer be present
|
402
|
+
* in the new instance generated by DH#public_key. This feature is helpful for
|
403
|
+
* publishing the Diffie-Hellman parameters without leaking any of the private
|
404
|
+
* per-session information.
|
405
|
+
*
|
406
|
+
* === Example
|
407
|
+
* dh = OpenSSL::PKey::DH.new(2048) # has public and private key set
|
408
|
+
* public_key = dh.public_key # contains only prime and generator
|
409
|
+
* parameters = public_key.to_der # it's safe to publish this
|
324
410
|
*/
|
325
411
|
static VALUE
|
326
412
|
ossl_dh_to_public_key(VALUE self)
|
@@ -328,7 +414,7 @@ ossl_dh_to_public_key(VALUE self)
|
|
328
414
|
EVP_PKEY *pkey;
|
329
415
|
DH *dh;
|
330
416
|
VALUE obj;
|
331
|
-
|
417
|
+
|
332
418
|
GetPKeyDH(self, pkey);
|
333
419
|
dh = DHparams_dup(pkey->pkey.dh); /* err check perfomed by dh_instance */
|
334
420
|
obj = dh_instance(CLASS_OF(self), dh);
|
@@ -344,6 +430,9 @@ ossl_dh_to_public_key(VALUE self)
|
|
344
430
|
* call-seq:
|
345
431
|
* dh.check_params -> true | false
|
346
432
|
*
|
433
|
+
* Validates the Diffie-Hellman parameters associated with this instance.
|
434
|
+
* It checks whether a safe prime and a suitable generator are used. If this
|
435
|
+
* is not the case, +false+ is returned.
|
347
436
|
*/
|
348
437
|
static VALUE
|
349
438
|
ossl_dh_check_params(VALUE self)
|
@@ -351,7 +440,7 @@ ossl_dh_check_params(VALUE self)
|
|
351
440
|
DH *dh;
|
352
441
|
EVP_PKEY *pkey;
|
353
442
|
int codes;
|
354
|
-
|
443
|
+
|
355
444
|
GetPKeyDH(self, pkey);
|
356
445
|
dh = pkey->pkey.dh;
|
357
446
|
|
@@ -364,8 +453,19 @@ ossl_dh_check_params(VALUE self)
|
|
364
453
|
|
365
454
|
/*
|
366
455
|
* call-seq:
|
367
|
-
* dh.generate_key -> self
|
456
|
+
* dh.generate_key! -> self
|
457
|
+
*
|
458
|
+
* Generates a private and public key unless a private key already exists.
|
459
|
+
* If this DH instance was generated from public DH parameters (e.g. by
|
460
|
+
* encoding the result of DH#public_key), then this method needs to be
|
461
|
+
* called first in order to generate the per-session keys before performing
|
462
|
+
* the actual key exchange.
|
368
463
|
*
|
464
|
+
* === Example
|
465
|
+
* dh = OpenSSL::PKey::DH.new(2048)
|
466
|
+
* public_key = dh.public_key #contains no private/public key yet
|
467
|
+
* public_key.generate_key!
|
468
|
+
* puts public_key.private? # => true
|
369
469
|
*/
|
370
470
|
static VALUE
|
371
471
|
ossl_dh_generate_key(VALUE self)
|
@@ -385,13 +485,12 @@ ossl_dh_generate_key(VALUE self)
|
|
385
485
|
* call-seq:
|
386
486
|
* dh.compute_key(pub_bn) -> aString
|
387
487
|
*
|
388
|
-
*
|
389
|
-
*
|
390
|
-
*
|
391
|
-
* Returns aString containing a shared secret computed from the other parties public value.
|
392
|
-
*
|
393
|
-
* See DH_compute_key() for further information.
|
488
|
+
* Returns a String containing a shared secret computed from the other party's public value.
|
489
|
+
* See DH_compute_key() for further information.
|
394
490
|
*
|
491
|
+
* === Parameters
|
492
|
+
* * +pub_bn+ is a OpenSSL::BN, *not* the DH instance returned by
|
493
|
+
* DH#public_key as that contains the DH parameters only.
|
395
494
|
*/
|
396
495
|
static VALUE
|
397
496
|
ossl_dh_compute_key(VALUE self, VALUE pub)
|
@@ -407,7 +506,7 @@ ossl_dh_compute_key(VALUE self, VALUE pub)
|
|
407
506
|
pub_key = GetBNPtr(pub);
|
408
507
|
len = DH_size(dh);
|
409
508
|
str = rb_str_new(0, len);
|
410
|
-
if ((len = DH_compute_key(RSTRING_PTR(str), pub_key, dh)) < 0) {
|
509
|
+
if ((len = DH_compute_key((unsigned char *)RSTRING_PTR(str), pub_key, dh)) < 0) {
|
411
510
|
ossl_raise(eDHError, NULL);
|
412
511
|
}
|
413
512
|
rb_str_set_len(str, len);
|
@@ -436,10 +535,10 @@ static unsigned char DEFAULT_DH_512_PRIM[] = {
|
|
436
535
|
0x08, 0x04, 0x8c, 0x52, 0x8f, 0xe3, 0x4a, 0x31,
|
437
536
|
0x44, 0x47, 0x19, 0xa1, 0x4a, 0xc8, 0x8b, 0xcb,
|
438
537
|
};
|
439
|
-
static unsigned char DEFAULT_DH_512_GEN[] = { 0x02 };
|
538
|
+
static unsigned char DEFAULT_DH_512_GEN[] = { 0x02 };
|
440
539
|
DH *OSSL_DEFAULT_DH_512 = NULL;
|
441
|
-
|
442
|
-
/*
|
540
|
+
|
541
|
+
/*
|
443
542
|
* -----BEGIN DH PARAMETERS-----
|
444
543
|
* MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
|
445
544
|
* AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
|
@@ -473,8 +572,8 @@ ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
|
|
473
572
|
DH *dh;
|
474
573
|
|
475
574
|
if ((dh = DH_new()) == NULL) ossl_raise(eDHError, NULL);
|
476
|
-
dh->p = BN_bin2bn(p, plen, NULL);
|
477
|
-
dh->g = BN_bin2bn(g, glen, NULL);
|
575
|
+
dh->p = BN_bin2bn(p, rb_long2int(plen), NULL);
|
576
|
+
dh->g = BN_bin2bn(g, rb_long2int(glen), NULL);
|
478
577
|
if (dh->p == NULL || dh->g == NULL){
|
479
578
|
DH_free(dh);
|
480
579
|
ossl_raise(eDHError, NULL);
|
@@ -489,12 +588,45 @@ ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
|
|
489
588
|
void
|
490
589
|
Init_ossl_dh()
|
491
590
|
{
|
492
|
-
#if 0
|
493
|
-
mOSSL = rb_define_module("OpenSSL");
|
591
|
+
#if 0
|
592
|
+
mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL and mPKey */
|
494
593
|
mPKey = rb_define_module_under(mOSSL, "PKey");
|
495
594
|
#endif
|
496
595
|
|
596
|
+
/* Document-class: OpenSSL::PKey::DHError
|
597
|
+
*
|
598
|
+
* Generic exception that is raised if an operation on a DH PKey
|
599
|
+
* fails unexpectedly or in case an instantiation of an instance of DH
|
600
|
+
* fails due to non-conformant input data.
|
601
|
+
*/
|
497
602
|
eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
|
603
|
+
/* Document-class: OpenSSL::PKey::DH
|
604
|
+
*
|
605
|
+
* An implementation of the Diffie-Hellman key exchange protocol based on
|
606
|
+
* discrete logarithms in finite fields, the same basis that DSA is built
|
607
|
+
* on.
|
608
|
+
*
|
609
|
+
* === Accessor methods for the Diffie-Hellman parameters
|
610
|
+
* * DH#p
|
611
|
+
* The prime (an OpenSSL::BN) of the Diffie-Hellman parameters.
|
612
|
+
* * DH#g
|
613
|
+
* The generator (an OpenSSL::BN) g of the Diffie-Hellman parameters.
|
614
|
+
* * DH#pub_key
|
615
|
+
* The per-session public key (an OpenSSL::BN) matching the private key.
|
616
|
+
* This needs to be passed to DH#compute_key.
|
617
|
+
* * DH#priv_key
|
618
|
+
* The per-session private key, an OpenSSL::BN.
|
619
|
+
*
|
620
|
+
* === Example of a key exchange
|
621
|
+
* dh1 = OpenSSL::PKey::DH.new(2048)
|
622
|
+
* params = dh1.public_key.to_der #you may send this publicly to the participating party
|
623
|
+
* dh2 = OpenSSL::PKey::DH.new(der)
|
624
|
+
* dh2.generate_key! #generate the per-session key pair
|
625
|
+
* symm_key1 = dh1.compute_key(dh2.pub_key)
|
626
|
+
* symm_key2 = dh2.compute_key(dh1.pub_key)
|
627
|
+
*
|
628
|
+
* puts symm_key1 == symm_key2 # => true
|
629
|
+
*/
|
498
630
|
cDH = rb_define_class_under(mPKey, "DH", cPKey);
|
499
631
|
rb_define_singleton_method(cDH, "generate", ossl_dh_s_generate, -1);
|
500
632
|
rb_define_method(cDH, "initialize", ossl_dh_initialize, -1);
|
@@ -509,6 +641,7 @@ Init_ossl_dh()
|
|
509
641
|
rb_define_method(cDH, "params_ok?", ossl_dh_check_params, 0);
|
510
642
|
rb_define_method(cDH, "generate_key!", ossl_dh_generate_key, 0);
|
511
643
|
rb_define_method(cDH, "compute_key", ossl_dh_compute_key, 1);
|
644
|
+
|
512
645
|
DEF_OSSL_PKEY_BN(cDH, dh, p);
|
513
646
|
DEF_OSSL_PKEY_BN(cDH, dh, g);
|
514
647
|
DEF_OSSL_PKEY_BN(cDH, dh, pub_key);
|