rubysl-openssl 0.0.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +7 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/ext/rubysl/openssl/extconf.h +50 -0
  7. data/ext/rubysl/openssl/extconf.rb +144 -0
  8. data/ext/rubysl/openssl/openssl_missing.c +343 -0
  9. data/ext/rubysl/openssl/openssl_missing.h +191 -0
  10. data/ext/rubysl/openssl/ossl.c +552 -0
  11. data/ext/rubysl/openssl/ossl.h +233 -0
  12. data/ext/rubysl/openssl/ossl_asn1.c +1160 -0
  13. data/ext/rubysl/openssl/ossl_asn1.h +59 -0
  14. data/ext/rubysl/openssl/ossl_bio.c +86 -0
  15. data/ext/rubysl/openssl/ossl_bio.h +21 -0
  16. data/ext/rubysl/openssl/ossl_bn.c +852 -0
  17. data/ext/rubysl/openssl/ossl_bn.h +25 -0
  18. data/ext/rubysl/openssl/ossl_cipher.c +569 -0
  19. data/ext/rubysl/openssl/ossl_cipher.h +22 -0
  20. data/ext/rubysl/openssl/ossl_config.c +75 -0
  21. data/ext/rubysl/openssl/ossl_config.h +22 -0
  22. data/ext/rubysl/openssl/ossl_digest.c +259 -0
  23. data/ext/rubysl/openssl/ossl_digest.h +22 -0
  24. data/ext/rubysl/openssl/ossl_engine.c +411 -0
  25. data/ext/rubysl/openssl/ossl_engine.h +20 -0
  26. data/ext/rubysl/openssl/ossl_hmac.c +268 -0
  27. data/ext/rubysl/openssl/ossl_hmac.h +19 -0
  28. data/ext/rubysl/openssl/ossl_ns_spki.c +257 -0
  29. data/ext/rubysl/openssl/ossl_ns_spki.h +21 -0
  30. data/ext/rubysl/openssl/ossl_ocsp.c +769 -0
  31. data/ext/rubysl/openssl/ossl_ocsp.h +24 -0
  32. data/ext/rubysl/openssl/ossl_pkcs12.c +210 -0
  33. data/ext/rubysl/openssl/ossl_pkcs12.h +15 -0
  34. data/ext/rubysl/openssl/ossl_pkcs5.c +99 -0
  35. data/ext/rubysl/openssl/ossl_pkcs5.h +6 -0
  36. data/ext/rubysl/openssl/ossl_pkcs7.c +1039 -0
  37. data/ext/rubysl/openssl/ossl_pkcs7.h +22 -0
  38. data/ext/rubysl/openssl/ossl_pkey.c +240 -0
  39. data/ext/rubysl/openssl/ossl_pkey.h +141 -0
  40. data/ext/rubysl/openssl/ossl_pkey_dh.c +532 -0
  41. data/ext/rubysl/openssl/ossl_pkey_dsa.c +484 -0
  42. data/ext/rubysl/openssl/ossl_pkey_ec.c +1593 -0
  43. data/ext/rubysl/openssl/ossl_pkey_rsa.c +593 -0
  44. data/ext/rubysl/openssl/ossl_rand.c +202 -0
  45. data/ext/rubysl/openssl/ossl_rand.h +20 -0
  46. data/ext/rubysl/openssl/ossl_ssl.c +1484 -0
  47. data/ext/rubysl/openssl/ossl_ssl.h +36 -0
  48. data/ext/rubysl/openssl/ossl_ssl_session.c +307 -0
  49. data/ext/rubysl/openssl/ossl_version.h +16 -0
  50. data/ext/rubysl/openssl/ossl_x509.c +104 -0
  51. data/ext/rubysl/openssl/ossl_x509.h +114 -0
  52. data/ext/rubysl/openssl/ossl_x509attr.c +274 -0
  53. data/ext/rubysl/openssl/ossl_x509cert.c +764 -0
  54. data/ext/rubysl/openssl/ossl_x509crl.c +535 -0
  55. data/ext/rubysl/openssl/ossl_x509ext.c +458 -0
  56. data/ext/rubysl/openssl/ossl_x509name.c +399 -0
  57. data/ext/rubysl/openssl/ossl_x509req.c +466 -0
  58. data/ext/rubysl/openssl/ossl_x509revoked.c +229 -0
  59. data/ext/rubysl/openssl/ossl_x509store.c +625 -0
  60. data/ext/rubysl/openssl/ruby_missing.h +41 -0
  61. data/lib/openssl.rb +1 -0
  62. data/lib/openssl/bn.rb +35 -0
  63. data/lib/openssl/buffering.rb +241 -0
  64. data/lib/openssl/cipher.rb +65 -0
  65. data/lib/openssl/config.rb +316 -0
  66. data/lib/openssl/digest.rb +61 -0
  67. data/lib/openssl/net/ftptls.rb +53 -0
  68. data/lib/openssl/net/telnets.rb +251 -0
  69. data/lib/openssl/pkcs7.rb +25 -0
  70. data/lib/openssl/ssl-internal.rb +187 -0
  71. data/lib/openssl/ssl.rb +1 -0
  72. data/lib/openssl/x509-internal.rb +153 -0
  73. data/lib/openssl/x509.rb +1 -0
  74. data/lib/rubysl/openssl.rb +28 -0
  75. data/lib/rubysl/openssl/version.rb +5 -0
  76. data/rubysl-openssl.gemspec +19 -18
  77. data/spec/cipher_spec.rb +16 -0
  78. data/spec/config/freeze_spec.rb +17 -0
  79. data/spec/hmac/digest_spec.rb +15 -0
  80. data/spec/hmac/hexdigest_spec.rb +15 -0
  81. data/spec/random/pseudo_bytes_spec.rb +5 -0
  82. data/spec/random/random_bytes_spec.rb +5 -0
  83. data/spec/random/shared/random_bytes.rb +28 -0
  84. data/spec/shared/constants.rb +11 -0
  85. data/spec/x509/name/parse_spec.rb +47 -0
  86. metadata +153 -89
  87. data/lib/rubysl-openssl.rb +0 -7
  88. data/lib/rubysl-openssl/version.rb +0 -5
@@ -0,0 +1,22 @@
1
+ /*
2
+ * $Id: ossl_pkcs7.h 12496 2007-06-08 15:02:04Z technorama $
3
+ * 'OpenSSL for Ruby' project
4
+ * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5
+ * All rights reserved.
6
+ */
7
+ /*
8
+ * This program is licenced under the same licence as Ruby.
9
+ * (See the file 'LICENCE'.)
10
+ */
11
+ #if !defined(_OSSL_PKCS7_H_)
12
+ #define _OSSL_PKCS7_H_
13
+
14
+ extern VALUE cPKCS7;
15
+ extern VALUE cPKCS7Signer;
16
+ extern VALUE cPKCS7Recipient;
17
+ extern VALUE ePKCS7Error;
18
+
19
+ void Init_ossl_pkcs7(void);
20
+
21
+ #endif /* _OSSL_PKCS7_H_ */
22
+
@@ -0,0 +1,240 @@
1
+ /*
2
+ * $Id: ossl_pkey.c 28004 2010-05-24 23:58:49Z shyouhei $
3
+ * 'OpenSSL for Ruby' project
4
+ * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5
+ * All rights reserved.
6
+ */
7
+ /*
8
+ * This program is licenced under the same licence as Ruby.
9
+ * (See the file 'LICENCE'.)
10
+ */
11
+ #include "ossl.h"
12
+
13
+ /*
14
+ * Classes
15
+ */
16
+ VALUE mPKey;
17
+ VALUE cPKey;
18
+ VALUE ePKeyError;
19
+ ID id_private_q;
20
+
21
+ /*
22
+ * callback for generating keys
23
+ */
24
+ void
25
+ ossl_generate_cb(int p, int n, void *arg)
26
+ {
27
+ VALUE ary;
28
+
29
+ ary = rb_ary_new2(2);
30
+ rb_ary_store(ary, 0, INT2NUM(p));
31
+ rb_ary_store(ary, 1, INT2NUM(n));
32
+
33
+ rb_yield(ary);
34
+ }
35
+
36
+ /*
37
+ * Public
38
+ */
39
+ VALUE
40
+ ossl_pkey_new(EVP_PKEY *pkey)
41
+ {
42
+ if (!pkey) {
43
+ ossl_raise(ePKeyError, "Cannot make new key from NULL.");
44
+ }
45
+ switch (EVP_PKEY_type(pkey->type)) {
46
+ #if !defined(OPENSSL_NO_RSA)
47
+ case EVP_PKEY_RSA:
48
+ return ossl_rsa_new(pkey);
49
+ #endif
50
+ #if !defined(OPENSSL_NO_DSA)
51
+ case EVP_PKEY_DSA:
52
+ return ossl_dsa_new(pkey);
53
+ #endif
54
+ #if !defined(OPENSSL_NO_DH)
55
+ case EVP_PKEY_DH:
56
+ return ossl_dh_new(pkey);
57
+ #endif
58
+ #if !defined(OPENSSL_NO_EC) && (OPENSSL_VERSION_NUMBER >= 0x0090802fL)
59
+ case EVP_PKEY_EC:
60
+ return ossl_ec_new(pkey);
61
+ #endif
62
+ default:
63
+ ossl_raise(ePKeyError, "unsupported key type");
64
+ }
65
+ return Qnil; /* not reached */
66
+ }
67
+
68
+ VALUE
69
+ ossl_pkey_new_from_file(VALUE filename)
70
+ {
71
+ FILE *fp;
72
+ EVP_PKEY *pkey;
73
+
74
+ SafeStringValue(filename);
75
+ if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
76
+ ossl_raise(ePKeyError, "%s", strerror(errno));
77
+ }
78
+
79
+ pkey = PEM_read_PrivateKey(fp, NULL, ossl_pem_passwd_cb, NULL);
80
+ fclose(fp);
81
+ if (!pkey) {
82
+ ossl_raise(ePKeyError, NULL);
83
+ }
84
+
85
+ return ossl_pkey_new(pkey);
86
+ }
87
+
88
+ EVP_PKEY *
89
+ GetPKeyPtr(VALUE obj)
90
+ {
91
+ EVP_PKEY *pkey;
92
+
93
+ SafeGetPKey(obj, pkey);
94
+
95
+ return pkey;
96
+ }
97
+
98
+ EVP_PKEY *
99
+ GetPrivPKeyPtr(VALUE obj)
100
+ {
101
+ EVP_PKEY *pkey;
102
+
103
+ if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
104
+ ossl_raise(rb_eArgError, "Private key is needed.");
105
+ }
106
+ SafeGetPKey(obj, pkey);
107
+
108
+ return pkey;
109
+ }
110
+
111
+ EVP_PKEY *
112
+ DupPKeyPtr(VALUE obj)
113
+ {
114
+ EVP_PKEY *pkey;
115
+
116
+ SafeGetPKey(obj, pkey);
117
+ CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
118
+
119
+ return pkey;
120
+ }
121
+
122
+ EVP_PKEY *
123
+ DupPrivPKeyPtr(VALUE obj)
124
+ {
125
+ EVP_PKEY *pkey;
126
+
127
+ if (rb_funcall(obj, id_private_q, 0, NULL) != Qtrue) {
128
+ ossl_raise(rb_eArgError, "Private key is needed.");
129
+ }
130
+ SafeGetPKey(obj, pkey);
131
+ CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
132
+
133
+ return pkey;
134
+ }
135
+
136
+ /*
137
+ * Private
138
+ */
139
+ static VALUE
140
+ ossl_pkey_alloc(VALUE klass)
141
+ {
142
+ EVP_PKEY *pkey;
143
+ VALUE obj;
144
+
145
+ if (!(pkey = EVP_PKEY_new())) {
146
+ ossl_raise(ePKeyError, NULL);
147
+ }
148
+ WrapPKey(klass, obj, pkey);
149
+
150
+ return obj;
151
+ }
152
+
153
+ static VALUE
154
+ ossl_pkey_initialize(VALUE self)
155
+ {
156
+ if (rb_obj_is_instance_of(self, cPKey)) {
157
+ ossl_raise(rb_eNotImpError, "OpenSSL::PKey::PKey is an abstract class.");
158
+ }
159
+ return self;
160
+ }
161
+
162
+ static VALUE
163
+ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
164
+ {
165
+ EVP_PKEY *pkey;
166
+ EVP_MD_CTX ctx;
167
+ int buf_len;
168
+ VALUE str;
169
+
170
+ if (rb_funcall(self, id_private_q, 0, NULL) != Qtrue) {
171
+ ossl_raise(rb_eArgError, "Private key is needed.");
172
+ }
173
+ GetPKey(self, pkey);
174
+ EVP_SignInit(&ctx, GetDigestPtr(digest));
175
+ StringValue(data);
176
+ EVP_SignUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
177
+ str = rb_str_new(0, EVP_PKEY_size(pkey)+16);
178
+ if (!EVP_SignFinal(&ctx, RSTRING_PTR(str), &buf_len, pkey))
179
+ ossl_raise(ePKeyError, NULL);
180
+ assert((long)buf_len <= RSTRING_LEN(str));
181
+ rb_str_set_len(str, buf_len);
182
+
183
+ return str;
184
+ }
185
+
186
+ static VALUE
187
+ ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
188
+ {
189
+ EVP_PKEY *pkey;
190
+ EVP_MD_CTX ctx;
191
+
192
+ GetPKey(self, pkey);
193
+ EVP_VerifyInit(&ctx, GetDigestPtr(digest));
194
+ StringValue(sig);
195
+ StringValue(data);
196
+ EVP_VerifyUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
197
+ switch (EVP_VerifyFinal(&ctx, RSTRING_PTR(sig), RSTRING_LEN(sig), pkey)) {
198
+ case 0:
199
+ return Qfalse;
200
+ case 1:
201
+ return Qtrue;
202
+ default:
203
+ ossl_raise(ePKeyError, NULL);
204
+ }
205
+ return Qnil; /* dummy */
206
+ }
207
+
208
+ /*
209
+ * INIT
210
+ */
211
+ void
212
+ Init_ossl_pkey()
213
+ {
214
+ #if 0 /* let rdoc know about mOSSL */
215
+ mOSSL = rb_define_module("OpenSSL");
216
+ #endif
217
+
218
+ mPKey = rb_define_module_under(mOSSL, "PKey");
219
+
220
+ ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
221
+
222
+ cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
223
+
224
+ rb_define_alloc_func(cPKey, ossl_pkey_alloc);
225
+ rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0);
226
+
227
+ rb_define_method(cPKey, "sign", ossl_pkey_sign, 2);
228
+ rb_define_method(cPKey, "verify", ossl_pkey_verify, 3);
229
+
230
+ id_private_q = rb_intern("private?");
231
+
232
+ /*
233
+ * INIT rsa, dsa, dh, ec
234
+ */
235
+ Init_ossl_rsa();
236
+ Init_ossl_dsa();
237
+ Init_ossl_dh();
238
+ Init_ossl_ec();
239
+ }
240
+
@@ -0,0 +1,141 @@
1
+ /*
2
+ * $Id: ossl_pkey.h 12496 2007-06-08 15:02:04Z technorama $
3
+ * 'OpenSSL for Ruby' project
4
+ * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
5
+ * All rights reserved.
6
+ */
7
+ /*
8
+ * This program is licenced under the same licence as Ruby.
9
+ * (See the file 'LICENCE'.)
10
+ */
11
+ #if !defined(_OSSL_PKEY_H_)
12
+ #define _OSSL_PKEY_H_
13
+
14
+ extern VALUE mPKey;
15
+ extern VALUE cPKey;
16
+ extern VALUE ePKeyError;
17
+ extern ID id_private_q;
18
+
19
+ #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
20
+ #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
21
+ #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
22
+
23
+ #define WrapPKey(klass, obj, pkey) do { \
24
+ if (!pkey) { \
25
+ rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
26
+ } \
27
+ obj = Data_Wrap_Struct(klass, 0, EVP_PKEY_free, pkey); \
28
+ OSSL_PKEY_SET_PUBLIC(obj); \
29
+ } while (0)
30
+ #define GetPKey(obj, pkey) do {\
31
+ Data_Get_Struct(obj, EVP_PKEY, pkey);\
32
+ if (!pkey) { \
33
+ rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
34
+ } \
35
+ } while (0)
36
+ #define SafeGetPKey(obj, pkey) do { \
37
+ OSSL_Check_Kind(obj, cPKey); \
38
+ GetPKey(obj, pkey); \
39
+ } while (0)
40
+
41
+ void ossl_generate_cb(int, int, void *);
42
+
43
+ VALUE ossl_pkey_new(EVP_PKEY *);
44
+ VALUE ossl_pkey_new_from_file(VALUE);
45
+ EVP_PKEY *GetPKeyPtr(VALUE);
46
+ EVP_PKEY *DupPKeyPtr(VALUE);
47
+ EVP_PKEY *GetPrivPKeyPtr(VALUE);
48
+ EVP_PKEY *DupPrivPKeyPtr(VALUE);
49
+ void Init_ossl_pkey(void);
50
+
51
+ /*
52
+ * RSA
53
+ */
54
+ extern VALUE cRSA;
55
+ extern VALUE eRSAError;
56
+
57
+ VALUE ossl_rsa_new(EVP_PKEY *);
58
+ void Init_ossl_rsa(void);
59
+
60
+ /*
61
+ * DSA
62
+ */
63
+ extern VALUE cDSA;
64
+ extern VALUE eDSAError;
65
+
66
+ VALUE ossl_dsa_new(EVP_PKEY *);
67
+ void Init_ossl_dsa(void);
68
+
69
+ /*
70
+ * DH
71
+ */
72
+ extern VALUE cDH;
73
+ extern VALUE eDHError;
74
+ extern DH *OSSL_DEFAULT_DH_512;
75
+ extern DH *OSSL_DEFAULT_DH_1024;
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
+
93
+ #define OSSL_PKEY_BN(keytype, name) \
94
+ /* \
95
+ * call-seq: \
96
+ * key.##name -> aBN \
97
+ */ \
98
+ static VALUE ossl_##keytype##_get_##name(VALUE self) \
99
+ { \
100
+ EVP_PKEY *pkey; \
101
+ BIGNUM *bn; \
102
+ \
103
+ GetPKey(self, pkey); \
104
+ bn = pkey->pkey.keytype->name; \
105
+ if (bn == NULL) \
106
+ return Qnil; \
107
+ return ossl_bn_new(bn); \
108
+ } \
109
+ /* \
110
+ * call-seq: \
111
+ * key.##name = bn -> bn \
112
+ */ \
113
+ static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
114
+ { \
115
+ EVP_PKEY *pkey; \
116
+ BIGNUM *bn; \
117
+ \
118
+ GetPKey(self, pkey); \
119
+ if (NIL_P(bignum)) { \
120
+ BN_clear_free(pkey->pkey.keytype->name); \
121
+ pkey->pkey.keytype->name = NULL; \
122
+ return Qnil; \
123
+ } \
124
+ \
125
+ bn = GetBNPtr(bignum); \
126
+ if (pkey->pkey.keytype->name == NULL) \
127
+ pkey->pkey.keytype->name = BN_new(); \
128
+ if (pkey->pkey.keytype->name == NULL) \
129
+ ossl_raise(eBNError, NULL); \
130
+ if (BN_copy(pkey->pkey.keytype->name, bn) == NULL) \
131
+ ossl_raise(eBNError, NULL); \
132
+ return bignum; \
133
+ }
134
+
135
+ #define DEF_OSSL_PKEY_BN(class, keytype, name) \
136
+ do { \
137
+ rb_define_method(class, #name, ossl_##keytype##_get_##name, 0); \
138
+ rb_define_method(class, #name "=", ossl_##keytype##_set_##name, 1);\
139
+ } while (0)
140
+
141
+ #endif /* _OSSL_PKEY_H_ */
@@ -0,0 +1,532 @@
1
+ /*
2
+ * $Id: ossl_pkey_dh.c 28004 2010-05-24 23:58:49Z shyouhei $
3
+ * 'OpenSSL for Ruby' project
4
+ * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5
+ * All rights reserved.
6
+ */
7
+ /*
8
+ * This program is licenced under the same licence as Ruby.
9
+ * (See the file 'LICENCE'.)
10
+ */
11
+ #if !defined(OPENSSL_NO_DH)
12
+
13
+ #include "ossl.h"
14
+
15
+ #define GetPKeyDH(obj, pkey) do { \
16
+ GetPKey(obj, pkey); \
17
+ if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) { /* PARANOIA? */ \
18
+ ossl_raise(rb_eRuntimeError, "THIS IS NOT A DH!") ; \
19
+ } \
20
+ } while (0)
21
+
22
+ #define DH_HAS_PRIVATE(dh) ((dh)->priv_key)
23
+
24
+ #ifdef OSSL_ENGINE_ENABLED
25
+ # define DH_PRIVATE(dh) (DH_HAS_PRIVATE(dh) || (dh)->engine)
26
+ #else
27
+ # define DH_PRIVATE(dh) DH_HAS_PRIVATE(dh)
28
+ #endif
29
+
30
+
31
+ /*
32
+ * Classes
33
+ */
34
+ VALUE cDH;
35
+ VALUE eDHError;
36
+
37
+ /*
38
+ * Public
39
+ */
40
+ static VALUE
41
+ dh_instance(VALUE klass, DH *dh)
42
+ {
43
+ EVP_PKEY *pkey;
44
+ VALUE obj;
45
+
46
+ if (!dh) {
47
+ return Qfalse;
48
+ }
49
+ if (!(pkey = EVP_PKEY_new())) {
50
+ return Qfalse;
51
+ }
52
+ if (!EVP_PKEY_assign_DH(pkey, dh)) {
53
+ EVP_PKEY_free(pkey);
54
+ return Qfalse;
55
+ }
56
+ WrapPKey(klass, obj, pkey);
57
+
58
+ return obj;
59
+ }
60
+
61
+ VALUE
62
+ ossl_dh_new(EVP_PKEY *pkey)
63
+ {
64
+ VALUE obj;
65
+
66
+ if (!pkey) {
67
+ obj = dh_instance(cDH, DH_new());
68
+ } else {
69
+ if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) {
70
+ ossl_raise(rb_eTypeError, "Not a DH key!");
71
+ }
72
+ WrapPKey(cDH, obj, pkey);
73
+ }
74
+ if (obj == Qfalse) {
75
+ ossl_raise(eDHError, NULL);
76
+ }
77
+
78
+ return obj;
79
+ }
80
+
81
+ /*
82
+ * Private
83
+ */
84
+ static DH *
85
+ dh_generate(int size, int gen)
86
+ {
87
+ DH *dh;
88
+
89
+ dh = DH_generate_parameters(size, gen,
90
+ rb_block_given_p() ? ossl_generate_cb : NULL,
91
+ NULL);
92
+ if (!dh) return 0;
93
+
94
+ if (!DH_generate_key(dh)) {
95
+ DH_free(dh);
96
+ return 0;
97
+ }
98
+
99
+ return dh;
100
+ }
101
+
102
+ /*
103
+ * call-seq:
104
+ * DH.generate(size [, generator]) -> dh
105
+ *
106
+ * === Parameters
107
+ * * +size+ is an integer representing the desired key size. Keys smaller than 1024 should be considered insecure.
108
+ * * +generator+ is a small number > 1, typically 2 or 5.
109
+ *
110
+ */
111
+ static VALUE
112
+ ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
113
+ {
114
+ DH *dh ;
115
+ int g = 2;
116
+ VALUE size, gen, obj;
117
+
118
+ if (rb_scan_args(argc, argv, "11", &size, &gen) == 2) {
119
+ g = NUM2INT(gen);
120
+ }
121
+ dh = dh_generate(NUM2INT(size), g);
122
+ obj = dh_instance(klass, dh);
123
+ if (obj == Qfalse) {
124
+ DH_free(dh);
125
+ ossl_raise(eDHError, NULL);
126
+ }
127
+
128
+ return obj;
129
+ }
130
+
131
+ /*
132
+ * call-seq:
133
+ * DH.new([size [, generator] | string]) -> dh
134
+ *
135
+ * === Parameters
136
+ * * +size+ is an integer representing the desired key size. Keys smaller than 1024 should be considered insecure.
137
+ * * +generator+ is a small number > 1, typically 2 or 5.
138
+ * * +string+ contains the DER or PEM encoded key.
139
+ *
140
+ * === Examples
141
+ * * DH.new -> dh
142
+ * * DH.new(1024) -> dh
143
+ * * DH.new(1024, 5) -> dh
144
+ * * DH.new(File.read('key.pem')) -> dh
145
+ */
146
+ static VALUE
147
+ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
148
+ {
149
+ EVP_PKEY *pkey;
150
+ DH *dh;
151
+ int g = 2;
152
+ BIO *in;
153
+ VALUE arg, gen;
154
+
155
+ GetPKey(self, pkey);
156
+ if(rb_scan_args(argc, argv, "02", &arg, &gen) == 0) {
157
+ dh = DH_new();
158
+ }
159
+ else if (FIXNUM_P(arg)) {
160
+ if (!NIL_P(gen)) {
161
+ g = NUM2INT(gen);
162
+ }
163
+ if (!(dh = dh_generate(FIX2INT(arg), g))) {
164
+ ossl_raise(eDHError, NULL);
165
+ }
166
+ }
167
+ else {
168
+ arg = ossl_to_der_if_possible(arg);
169
+ in = ossl_obj2bio(arg);
170
+ dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
171
+ if (!dh){
172
+ BIO_reset(in);
173
+ dh = d2i_DHparams_bio(in, NULL);
174
+ }
175
+ BIO_free(in);
176
+ if (!dh) ossl_raise(eDHError, NULL);
177
+ }
178
+ if (!EVP_PKEY_assign_DH(pkey, dh)) {
179
+ DH_free(dh);
180
+ ossl_raise(eDHError, NULL);
181
+ }
182
+ return self;
183
+ }
184
+
185
+ /*
186
+ * call-seq:
187
+ * dh.public? -> true | false
188
+ *
189
+ */
190
+ static VALUE
191
+ ossl_dh_is_public(VALUE self)
192
+ {
193
+ EVP_PKEY *pkey;
194
+
195
+ GetPKeyDH(self, pkey);
196
+
197
+ return (pkey->pkey.dh->pub_key) ? Qtrue : Qfalse;
198
+ }
199
+
200
+ /*
201
+ * call-seq:
202
+ * dh.private? -> true | false
203
+ *
204
+ */
205
+ static VALUE
206
+ ossl_dh_is_private(VALUE self)
207
+ {
208
+ EVP_PKEY *pkey;
209
+
210
+ GetPKeyDH(self, pkey);
211
+
212
+ return (DH_PRIVATE(pkey->pkey.dh)) ? Qtrue : Qfalse;
213
+ }
214
+
215
+ /*
216
+ * call-seq:
217
+ * dh.to_pem -> aString
218
+ *
219
+ */
220
+ static VALUE
221
+ ossl_dh_export(VALUE self)
222
+ {
223
+ EVP_PKEY *pkey;
224
+ BIO *out;
225
+ VALUE str;
226
+
227
+ GetPKeyDH(self, pkey);
228
+ if (!(out = BIO_new(BIO_s_mem()))) {
229
+ ossl_raise(eDHError, NULL);
230
+ }
231
+ if (!PEM_write_bio_DHparams(out, pkey->pkey.dh)) {
232
+ BIO_free(out);
233
+ ossl_raise(eDHError, NULL);
234
+ }
235
+ str = ossl_membio2str(out);
236
+
237
+ return str;
238
+ }
239
+
240
+ /*
241
+ * call-seq:
242
+ * dh.to_der -> aString
243
+ *
244
+ */
245
+ static VALUE
246
+ ossl_dh_to_der(VALUE self)
247
+ {
248
+ EVP_PKEY *pkey;
249
+ unsigned char *p;
250
+ long len;
251
+ VALUE str;
252
+
253
+ GetPKeyDH(self, pkey);
254
+ if((len = i2d_DHparams(pkey->pkey.dh, NULL)) <= 0)
255
+ ossl_raise(eDHError, NULL);
256
+ str = rb_str_new(0, len);
257
+ p = RSTRING_PTR(str);
258
+ if(i2d_DHparams(pkey->pkey.dh, &p) < 0)
259
+ ossl_raise(eDHError, NULL);
260
+ ossl_str_adjust(str, p);
261
+
262
+ return str;
263
+ }
264
+
265
+ /*
266
+ * call-seq:
267
+ * dh.params -> hash
268
+ *
269
+ * Stores all parameters of key to the hash
270
+ * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
271
+ * Don't use :-)) (I's up to you)
272
+ */
273
+ static VALUE
274
+ ossl_dh_get_params(VALUE self)
275
+ {
276
+ EVP_PKEY *pkey;
277
+ VALUE hash;
278
+
279
+ GetPKeyDH(self, pkey);
280
+
281
+ hash = rb_hash_new();
282
+
283
+ rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(pkey->pkey.dh->p));
284
+ rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(pkey->pkey.dh->g));
285
+ rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pkey->pkey.dh->pub_key));
286
+ rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(pkey->pkey.dh->priv_key));
287
+
288
+ return hash;
289
+ }
290
+
291
+ /*
292
+ * call-seq:
293
+ * dh.to_text -> aString
294
+ *
295
+ * Prints all parameters of key to buffer
296
+ * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
297
+ * Don't use :-)) (I's up to you)
298
+ */
299
+ static VALUE
300
+ ossl_dh_to_text(VALUE self)
301
+ {
302
+ EVP_PKEY *pkey;
303
+ BIO *out;
304
+ VALUE str;
305
+
306
+ GetPKeyDH(self, pkey);
307
+ if (!(out = BIO_new(BIO_s_mem()))) {
308
+ ossl_raise(eDHError, NULL);
309
+ }
310
+ if (!DHparams_print(out, pkey->pkey.dh)) {
311
+ BIO_free(out);
312
+ ossl_raise(eDHError, NULL);
313
+ }
314
+ str = ossl_membio2str(out);
315
+
316
+ return str;
317
+ }
318
+
319
+ /*
320
+ * call-seq:
321
+ * dh.public_key -> aDH
322
+ *
323
+ * Makes new instance DH PUBLIC_KEY from PRIVATE_KEY
324
+ */
325
+ static VALUE
326
+ ossl_dh_to_public_key(VALUE self)
327
+ {
328
+ EVP_PKEY *pkey;
329
+ DH *dh;
330
+ VALUE obj;
331
+
332
+ GetPKeyDH(self, pkey);
333
+ dh = DHparams_dup(pkey->pkey.dh); /* err check perfomed by dh_instance */
334
+ obj = dh_instance(CLASS_OF(self), dh);
335
+ if (obj == Qfalse) {
336
+ DH_free(dh);
337
+ ossl_raise(eDHError, NULL);
338
+ }
339
+
340
+ return obj;
341
+ }
342
+
343
+ /*
344
+ * call-seq:
345
+ * dh.check_params -> true | false
346
+ *
347
+ */
348
+ static VALUE
349
+ ossl_dh_check_params(VALUE self)
350
+ {
351
+ DH *dh;
352
+ EVP_PKEY *pkey;
353
+ int codes;
354
+
355
+ GetPKeyDH(self, pkey);
356
+ dh = pkey->pkey.dh;
357
+
358
+ if (!DH_check(dh, &codes)) {
359
+ return Qfalse;
360
+ }
361
+
362
+ return codes == 0 ? Qtrue : Qfalse;
363
+ }
364
+
365
+ /*
366
+ * call-seq:
367
+ * dh.generate_key -> self
368
+ *
369
+ */
370
+ static VALUE
371
+ ossl_dh_generate_key(VALUE self)
372
+ {
373
+ DH *dh;
374
+ EVP_PKEY *pkey;
375
+
376
+ GetPKeyDH(self, pkey);
377
+ dh = pkey->pkey.dh;
378
+
379
+ if (!DH_generate_key(dh))
380
+ ossl_raise(eDHError, "Failed to generate key");
381
+ return self;
382
+ }
383
+
384
+ /*
385
+ * call-seq:
386
+ * dh.compute_key(pub_bn) -> aString
387
+ *
388
+ * === Parameters
389
+ * * +pub_bn+ is a OpenSSL::BN.
390
+ *
391
+ * Returns aString containing a shared secret computed from the other parties public value.
392
+ *
393
+ * See DH_compute_key() for further information.
394
+ *
395
+ */
396
+ static VALUE
397
+ ossl_dh_compute_key(VALUE self, VALUE pub)
398
+ {
399
+ DH *dh;
400
+ EVP_PKEY *pkey;
401
+ BIGNUM *pub_key;
402
+ VALUE str;
403
+ int len;
404
+
405
+ GetPKeyDH(self, pkey);
406
+ dh = pkey->pkey.dh;
407
+ pub_key = GetBNPtr(pub);
408
+ len = DH_size(dh);
409
+ str = rb_str_new(0, len);
410
+ if ((len = DH_compute_key(RSTRING_PTR(str), pub_key, dh)) < 0) {
411
+ ossl_raise(eDHError, NULL);
412
+ }
413
+ rb_str_set_len(str, len);
414
+
415
+ return str;
416
+ }
417
+
418
+ OSSL_PKEY_BN(dh, p)
419
+ OSSL_PKEY_BN(dh, g)
420
+ OSSL_PKEY_BN(dh, pub_key)
421
+ OSSL_PKEY_BN(dh, priv_key)
422
+
423
+ /*
424
+ * -----BEGIN DH PARAMETERS-----
425
+ * MEYCQQD0zXHljRg/mJ9PYLACLv58Cd8VxBxxY7oEuCeURMiTqEhMym16rhhKgZG2
426
+ * zk2O9uUIBIxSj+NKMURHGaFKyIvLAgEC
427
+ * -----END DH PARAMETERS-----
428
+ */
429
+ static unsigned char DEFAULT_DH_512_PRIM[] = {
430
+ 0xf4, 0xcd, 0x71, 0xe5, 0x8d, 0x18, 0x3f, 0x98,
431
+ 0x9f, 0x4f, 0x60, 0xb0, 0x02, 0x2e, 0xfe, 0x7c,
432
+ 0x09, 0xdf, 0x15, 0xc4, 0x1c, 0x71, 0x63, 0xba,
433
+ 0x04, 0xb8, 0x27, 0x94, 0x44, 0xc8, 0x93, 0xa8,
434
+ 0x48, 0x4c, 0xca, 0x6d, 0x7a, 0xae, 0x18, 0x4a,
435
+ 0x81, 0x91, 0xb6, 0xce, 0x4d, 0x8e, 0xf6, 0xe5,
436
+ 0x08, 0x04, 0x8c, 0x52, 0x8f, 0xe3, 0x4a, 0x31,
437
+ 0x44, 0x47, 0x19, 0xa1, 0x4a, 0xc8, 0x8b, 0xcb,
438
+ };
439
+ static unsigned char DEFAULT_DH_512_GEN[] = { 0x02 };
440
+ DH *OSSL_DEFAULT_DH_512 = NULL;
441
+
442
+ /*
443
+ * -----BEGIN DH PARAMETERS-----
444
+ * MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
445
+ * AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
446
+ * T4h7KZ/2zmjvV+eF8kBUHBJAojUlzxKj4QeO2x20FP9X5xmNUXeDAgEC
447
+ * -----END DH PARAMETERS-----
448
+ */
449
+ static unsigned char DEFAULT_DH_1024_PRIM[] = {
450
+ 0x9d, 0x25, 0x39, 0x5c, 0xb4, 0x54, 0x8a, 0xff,
451
+ 0x25, 0xe6, 0xd6, 0x9f, 0x4c, 0xc3, 0xc1, 0x8d,
452
+ 0xa1, 0xfa, 0xba, 0x88, 0x4c, 0x53, 0xa9, 0x74,
453
+ 0xda, 0xfa, 0xba, 0x0b, 0x20, 0xbe, 0x40, 0xd7,
454
+ 0xba, 0xe7, 0x1d, 0x70, 0x28, 0x61, 0x60, 0x4c,
455
+ 0x49, 0x01, 0x5f, 0xd9, 0x0f, 0x60, 0x16, 0x3d,
456
+ 0xba, 0xd3, 0xa9, 0x5e, 0xfa, 0x98, 0x64, 0x60,
457
+ 0x26, 0x0e, 0x04, 0x75, 0xd8, 0x13, 0xd7, 0x31,
458
+ 0xb4, 0x8e, 0xad, 0xeb, 0x9c, 0x57, 0x4c, 0x8f,
459
+ 0x65, 0xf3, 0x90, 0x16, 0x31, 0xdc, 0x15, 0x6f,
460
+ 0x7d, 0x1d, 0x00, 0xae, 0x76, 0xf2, 0xd1, 0x11,
461
+ 0xd1, 0x4f, 0x88, 0x7b, 0x29, 0x9f, 0xf6, 0xce,
462
+ 0x68, 0xef, 0x57, 0xe7, 0x85, 0xf2, 0x40, 0x54,
463
+ 0x1c, 0x12, 0x40, 0xa2, 0x35, 0x25, 0xcf, 0x12,
464
+ 0xa3, 0xe1, 0x07, 0x8e, 0xdb, 0x1d, 0xb4, 0x14,
465
+ 0xff, 0x57, 0xe7, 0x19, 0x8d, 0x51, 0x77, 0x83
466
+ };
467
+ static unsigned char DEFAULT_DH_1024_GEN[] = { 0x02 };
468
+ DH *OSSL_DEFAULT_DH_1024 = NULL;
469
+
470
+ static DH*
471
+ ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
472
+ {
473
+ DH *dh;
474
+
475
+ 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);
478
+ if (dh->p == NULL || dh->g == NULL){
479
+ DH_free(dh);
480
+ ossl_raise(eDHError, NULL);
481
+ }
482
+
483
+ return dh;
484
+ }
485
+
486
+ /*
487
+ * INIT
488
+ */
489
+ void
490
+ Init_ossl_dh()
491
+ {
492
+ #if 0 /* let rdoc know about mOSSL and mPKey */
493
+ mOSSL = rb_define_module("OpenSSL");
494
+ mPKey = rb_define_module_under(mOSSL, "PKey");
495
+ #endif
496
+
497
+ eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
498
+ cDH = rb_define_class_under(mPKey, "DH", cPKey);
499
+ rb_define_singleton_method(cDH, "generate", ossl_dh_s_generate, -1);
500
+ rb_define_method(cDH, "initialize", ossl_dh_initialize, -1);
501
+ rb_define_method(cDH, "public?", ossl_dh_is_public, 0);
502
+ rb_define_method(cDH, "private?", ossl_dh_is_private, 0);
503
+ rb_define_method(cDH, "to_text", ossl_dh_to_text, 0);
504
+ rb_define_method(cDH, "export", ossl_dh_export, 0);
505
+ rb_define_alias(cDH, "to_pem", "export");
506
+ rb_define_alias(cDH, "to_s", "export");
507
+ rb_define_method(cDH, "to_der", ossl_dh_to_der, 0);
508
+ rb_define_method(cDH, "public_key", ossl_dh_to_public_key, 0);
509
+ rb_define_method(cDH, "params_ok?", ossl_dh_check_params, 0);
510
+ rb_define_method(cDH, "generate_key!", ossl_dh_generate_key, 0);
511
+ rb_define_method(cDH, "compute_key", ossl_dh_compute_key, 1);
512
+ DEF_OSSL_PKEY_BN(cDH, dh, p);
513
+ DEF_OSSL_PKEY_BN(cDH, dh, g);
514
+ DEF_OSSL_PKEY_BN(cDH, dh, pub_key);
515
+ DEF_OSSL_PKEY_BN(cDH, dh, priv_key);
516
+ rb_define_method(cDH, "params", ossl_dh_get_params, 0);
517
+
518
+ OSSL_DEFAULT_DH_512 = ossl_create_dh(
519
+ DEFAULT_DH_512_PRIM, sizeof(DEFAULT_DH_512_PRIM),
520
+ DEFAULT_DH_512_GEN, sizeof(DEFAULT_DH_512_GEN));
521
+ OSSL_DEFAULT_DH_1024 = ossl_create_dh(
522
+ DEFAULT_DH_1024_PRIM, sizeof(DEFAULT_DH_1024_PRIM),
523
+ DEFAULT_DH_1024_GEN, sizeof(DEFAULT_DH_1024_GEN));
524
+ }
525
+
526
+ #else /* defined NO_DH */
527
+ void
528
+ Init_ossl_dh()
529
+ {
530
+ }
531
+ #endif /* NO_DH */
532
+