openssl 2.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of openssl might be problematic. Click here for more details.

Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/BSDL +22 -0
  3. data/CONTRIBUTING.md +130 -0
  4. data/History.md +118 -0
  5. data/LICENSE.txt +56 -0
  6. data/README.md +70 -0
  7. data/ext/openssl/deprecation.rb +26 -0
  8. data/ext/openssl/extconf.rb +158 -0
  9. data/ext/openssl/openssl_missing.c +173 -0
  10. data/ext/openssl/openssl_missing.h +244 -0
  11. data/ext/openssl/ossl.c +1201 -0
  12. data/ext/openssl/ossl.h +222 -0
  13. data/ext/openssl/ossl_asn1.c +1992 -0
  14. data/ext/openssl/ossl_asn1.h +66 -0
  15. data/ext/openssl/ossl_bio.c +87 -0
  16. data/ext/openssl/ossl_bio.h +19 -0
  17. data/ext/openssl/ossl_bn.c +1153 -0
  18. data/ext/openssl/ossl_bn.h +23 -0
  19. data/ext/openssl/ossl_cipher.c +1085 -0
  20. data/ext/openssl/ossl_cipher.h +20 -0
  21. data/ext/openssl/ossl_config.c +89 -0
  22. data/ext/openssl/ossl_config.h +19 -0
  23. data/ext/openssl/ossl_digest.c +453 -0
  24. data/ext/openssl/ossl_digest.h +20 -0
  25. data/ext/openssl/ossl_engine.c +580 -0
  26. data/ext/openssl/ossl_engine.h +19 -0
  27. data/ext/openssl/ossl_hmac.c +398 -0
  28. data/ext/openssl/ossl_hmac.h +18 -0
  29. data/ext/openssl/ossl_ns_spki.c +406 -0
  30. data/ext/openssl/ossl_ns_spki.h +19 -0
  31. data/ext/openssl/ossl_ocsp.c +2013 -0
  32. data/ext/openssl/ossl_ocsp.h +23 -0
  33. data/ext/openssl/ossl_pkcs12.c +259 -0
  34. data/ext/openssl/ossl_pkcs12.h +13 -0
  35. data/ext/openssl/ossl_pkcs5.c +180 -0
  36. data/ext/openssl/ossl_pkcs5.h +6 -0
  37. data/ext/openssl/ossl_pkcs7.c +1125 -0
  38. data/ext/openssl/ossl_pkcs7.h +20 -0
  39. data/ext/openssl/ossl_pkey.c +435 -0
  40. data/ext/openssl/ossl_pkey.h +245 -0
  41. data/ext/openssl/ossl_pkey_dh.c +650 -0
  42. data/ext/openssl/ossl_pkey_dsa.c +672 -0
  43. data/ext/openssl/ossl_pkey_ec.c +1899 -0
  44. data/ext/openssl/ossl_pkey_rsa.c +768 -0
  45. data/ext/openssl/ossl_rand.c +238 -0
  46. data/ext/openssl/ossl_rand.h +18 -0
  47. data/ext/openssl/ossl_ssl.c +2679 -0
  48. data/ext/openssl/ossl_ssl.h +41 -0
  49. data/ext/openssl/ossl_ssl_session.c +352 -0
  50. data/ext/openssl/ossl_version.h +15 -0
  51. data/ext/openssl/ossl_x509.c +186 -0
  52. data/ext/openssl/ossl_x509.h +119 -0
  53. data/ext/openssl/ossl_x509attr.c +328 -0
  54. data/ext/openssl/ossl_x509cert.c +860 -0
  55. data/ext/openssl/ossl_x509crl.c +565 -0
  56. data/ext/openssl/ossl_x509ext.c +480 -0
  57. data/ext/openssl/ossl_x509name.c +547 -0
  58. data/ext/openssl/ossl_x509req.c +492 -0
  59. data/ext/openssl/ossl_x509revoked.c +279 -0
  60. data/ext/openssl/ossl_x509store.c +846 -0
  61. data/ext/openssl/ruby_missing.h +32 -0
  62. data/lib/openssl.rb +21 -0
  63. data/lib/openssl/bn.rb +39 -0
  64. data/lib/openssl/buffering.rb +451 -0
  65. data/lib/openssl/cipher.rb +67 -0
  66. data/lib/openssl/config.rb +473 -0
  67. data/lib/openssl/digest.rb +78 -0
  68. data/lib/openssl/pkey.rb +44 -0
  69. data/lib/openssl/ssl.rb +416 -0
  70. data/lib/openssl/x509.rb +176 -0
  71. metadata +178 -0
@@ -0,0 +1,492 @@
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
+ #define NewX509Req(klass) \
13
+ TypedData_Wrap_Struct((klass), &ossl_x509req_type, 0)
14
+ #define SetX509Req(obj, req) do { \
15
+ if (!(req)) { \
16
+ ossl_raise(rb_eRuntimeError, "Req wasn't initialized!"); \
17
+ } \
18
+ RTYPEDDATA_DATA(obj) = (req); \
19
+ } while (0)
20
+ #define GetX509Req(obj, req) do { \
21
+ TypedData_Get_Struct((obj), X509_REQ, &ossl_x509req_type, (req)); \
22
+ if (!(req)) { \
23
+ ossl_raise(rb_eRuntimeError, "Req wasn't initialized!"); \
24
+ } \
25
+ } while (0)
26
+ #define SafeGetX509Req(obj, req) do { \
27
+ OSSL_Check_Kind((obj), cX509Req); \
28
+ GetX509Req((obj), (req)); \
29
+ } while (0)
30
+
31
+ /*
32
+ * Classes
33
+ */
34
+ VALUE cX509Req;
35
+ VALUE eX509ReqError;
36
+
37
+ static void
38
+ ossl_x509req_free(void *ptr)
39
+ {
40
+ X509_REQ_free(ptr);
41
+ }
42
+
43
+ static const rb_data_type_t ossl_x509req_type = {
44
+ "OpenSSL/X509/REQ",
45
+ {
46
+ 0, ossl_x509req_free,
47
+ },
48
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
49
+ };
50
+
51
+ /*
52
+ * Public functions
53
+ */
54
+ VALUE
55
+ ossl_x509req_new(X509_REQ *req)
56
+ {
57
+ X509_REQ *new;
58
+ VALUE obj;
59
+
60
+ obj = NewX509Req(cX509Req);
61
+ if (!req) {
62
+ new = X509_REQ_new();
63
+ } else {
64
+ new = X509_REQ_dup(req);
65
+ }
66
+ if (!new) {
67
+ ossl_raise(eX509ReqError, NULL);
68
+ }
69
+ SetX509Req(obj, new);
70
+
71
+ return obj;
72
+ }
73
+
74
+ X509_REQ *
75
+ GetX509ReqPtr(VALUE obj)
76
+ {
77
+ X509_REQ *req;
78
+
79
+ SafeGetX509Req(obj, req);
80
+
81
+ return req;
82
+ }
83
+
84
+ X509_REQ *
85
+ DupX509ReqPtr(VALUE obj)
86
+ {
87
+ X509_REQ *req, *new;
88
+
89
+ SafeGetX509Req(obj, req);
90
+ if (!(new = X509_REQ_dup(req))) {
91
+ ossl_raise(eX509ReqError, NULL);
92
+ }
93
+
94
+ return new;
95
+ }
96
+
97
+ /*
98
+ * Private functions
99
+ */
100
+ static VALUE
101
+ ossl_x509req_alloc(VALUE klass)
102
+ {
103
+ X509_REQ *req;
104
+ VALUE obj;
105
+
106
+ obj = NewX509Req(klass);
107
+ if (!(req = X509_REQ_new())) {
108
+ ossl_raise(eX509ReqError, NULL);
109
+ }
110
+ SetX509Req(obj, req);
111
+
112
+ return obj;
113
+ }
114
+
115
+ static VALUE
116
+ ossl_x509req_initialize(int argc, VALUE *argv, VALUE self)
117
+ {
118
+ BIO *in;
119
+ X509_REQ *req, *x = DATA_PTR(self);
120
+ VALUE arg;
121
+
122
+ if (rb_scan_args(argc, argv, "01", &arg) == 0) {
123
+ return self;
124
+ }
125
+ arg = ossl_to_der_if_possible(arg);
126
+ in = ossl_obj2bio(arg);
127
+ req = PEM_read_bio_X509_REQ(in, &x, NULL, NULL);
128
+ DATA_PTR(self) = x;
129
+ if (!req) {
130
+ OSSL_BIO_reset(in);
131
+ req = d2i_X509_REQ_bio(in, &x);
132
+ DATA_PTR(self) = x;
133
+ }
134
+ BIO_free(in);
135
+ if (!req) ossl_raise(eX509ReqError, NULL);
136
+
137
+ return self;
138
+ }
139
+
140
+ static VALUE
141
+ ossl_x509req_copy(VALUE self, VALUE other)
142
+ {
143
+ X509_REQ *a, *b, *req;
144
+
145
+ rb_check_frozen(self);
146
+ if (self == other) return self;
147
+ GetX509Req(self, a);
148
+ SafeGetX509Req(other, b);
149
+ if (!(req = X509_REQ_dup(b))) {
150
+ ossl_raise(eX509ReqError, NULL);
151
+ }
152
+ X509_REQ_free(a);
153
+ DATA_PTR(self) = req;
154
+
155
+ return self;
156
+ }
157
+
158
+ static VALUE
159
+ ossl_x509req_to_pem(VALUE self)
160
+ {
161
+ X509_REQ *req;
162
+ BIO *out;
163
+ BUF_MEM *buf;
164
+ VALUE str;
165
+
166
+ GetX509Req(self, req);
167
+ if (!(out = BIO_new(BIO_s_mem()))) {
168
+ ossl_raise(eX509ReqError, NULL);
169
+ }
170
+ if (!PEM_write_bio_X509_REQ(out, req)) {
171
+ BIO_free(out);
172
+ ossl_raise(eX509ReqError, NULL);
173
+ }
174
+ BIO_get_mem_ptr(out, &buf);
175
+ str = rb_str_new(buf->data, buf->length);
176
+ BIO_free(out);
177
+
178
+ return str;
179
+ }
180
+
181
+ static VALUE
182
+ ossl_x509req_to_der(VALUE self)
183
+ {
184
+ X509_REQ *req;
185
+ VALUE str;
186
+ long len;
187
+ unsigned char *p;
188
+
189
+ GetX509Req(self, req);
190
+ if ((len = i2d_X509_REQ(req, NULL)) <= 0)
191
+ ossl_raise(eX509ReqError, NULL);
192
+ str = rb_str_new(0, len);
193
+ p = (unsigned char *)RSTRING_PTR(str);
194
+ if (i2d_X509_REQ(req, &p) <= 0)
195
+ ossl_raise(eX509ReqError, NULL);
196
+ ossl_str_adjust(str, p);
197
+
198
+ return str;
199
+ }
200
+
201
+ static VALUE
202
+ ossl_x509req_to_text(VALUE self)
203
+ {
204
+ X509_REQ *req;
205
+ BIO *out;
206
+ BUF_MEM *buf;
207
+ VALUE str;
208
+
209
+ GetX509Req(self, req);
210
+ if (!(out = BIO_new(BIO_s_mem()))) {
211
+ ossl_raise(eX509ReqError, NULL);
212
+ }
213
+ if (!X509_REQ_print(out, req)) {
214
+ BIO_free(out);
215
+ ossl_raise(eX509ReqError, NULL);
216
+ }
217
+ BIO_get_mem_ptr(out, &buf);
218
+ str = rb_str_new(buf->data, buf->length);
219
+ BIO_free(out);
220
+
221
+ return str;
222
+ }
223
+
224
+ #if 0
225
+ /*
226
+ * Makes X509 from X509_REQuest
227
+ */
228
+ static VALUE
229
+ ossl_x509req_to_x509(VALUE self, VALUE days, VALUE key)
230
+ {
231
+ X509_REQ *req;
232
+ X509 *x509;
233
+
234
+ GetX509Req(self, req);
235
+ ...
236
+ if (!(x509 = X509_REQ_to_X509(req, d, pkey))) {
237
+ ossl_raise(eX509ReqError, NULL);
238
+ }
239
+
240
+ return ossl_x509_new(x509);
241
+ }
242
+ #endif
243
+
244
+ static VALUE
245
+ ossl_x509req_get_version(VALUE self)
246
+ {
247
+ X509_REQ *req;
248
+ long version;
249
+
250
+ GetX509Req(self, req);
251
+ version = X509_REQ_get_version(req);
252
+
253
+ return LONG2NUM(version);
254
+ }
255
+
256
+ static VALUE
257
+ ossl_x509req_set_version(VALUE self, VALUE version)
258
+ {
259
+ X509_REQ *req;
260
+ long ver;
261
+
262
+ if ((ver = NUM2LONG(version)) < 0) {
263
+ ossl_raise(eX509ReqError, "version must be >= 0!");
264
+ }
265
+ GetX509Req(self, req);
266
+ if (!X509_REQ_set_version(req, ver)) {
267
+ ossl_raise(eX509ReqError, "X509_REQ_set_version");
268
+ }
269
+
270
+ return version;
271
+ }
272
+
273
+ static VALUE
274
+ ossl_x509req_get_subject(VALUE self)
275
+ {
276
+ X509_REQ *req;
277
+ X509_NAME *name;
278
+
279
+ GetX509Req(self, req);
280
+ if (!(name = X509_REQ_get_subject_name(req))) { /* NO DUP - don't free */
281
+ ossl_raise(eX509ReqError, NULL);
282
+ }
283
+
284
+ return ossl_x509name_new(name);
285
+ }
286
+
287
+ static VALUE
288
+ ossl_x509req_set_subject(VALUE self, VALUE subject)
289
+ {
290
+ X509_REQ *req;
291
+
292
+ GetX509Req(self, req);
293
+ /* DUPs name */
294
+ if (!X509_REQ_set_subject_name(req, GetX509NamePtr(subject))) {
295
+ ossl_raise(eX509ReqError, NULL);
296
+ }
297
+
298
+ return subject;
299
+ }
300
+
301
+ static VALUE
302
+ ossl_x509req_get_signature_algorithm(VALUE self)
303
+ {
304
+ X509_REQ *req;
305
+ const X509_ALGOR *alg;
306
+ BIO *out;
307
+ BUF_MEM *buf;
308
+ VALUE str;
309
+
310
+ GetX509Req(self, req);
311
+
312
+ if (!(out = BIO_new(BIO_s_mem()))) {
313
+ ossl_raise(eX509ReqError, NULL);
314
+ }
315
+ X509_REQ_get0_signature(req, NULL, &alg);
316
+ if (!i2a_ASN1_OBJECT(out, alg->algorithm)) {
317
+ BIO_free(out);
318
+ ossl_raise(eX509ReqError, NULL);
319
+ }
320
+ BIO_get_mem_ptr(out, &buf);
321
+ str = rb_str_new(buf->data, buf->length);
322
+ BIO_free(out);
323
+ return str;
324
+ }
325
+
326
+ static VALUE
327
+ ossl_x509req_get_public_key(VALUE self)
328
+ {
329
+ X509_REQ *req;
330
+ EVP_PKEY *pkey;
331
+
332
+ GetX509Req(self, req);
333
+ if (!(pkey = X509_REQ_get_pubkey(req))) { /* adds reference */
334
+ ossl_raise(eX509ReqError, NULL);
335
+ }
336
+
337
+ return ossl_pkey_new(pkey); /* NO DUP - OK */
338
+ }
339
+
340
+ static VALUE
341
+ ossl_x509req_set_public_key(VALUE self, VALUE key)
342
+ {
343
+ X509_REQ *req;
344
+ EVP_PKEY *pkey;
345
+
346
+ GetX509Req(self, req);
347
+ pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
348
+ if (!X509_REQ_set_pubkey(req, pkey)) {
349
+ ossl_raise(eX509ReqError, NULL);
350
+ }
351
+
352
+ return key;
353
+ }
354
+
355
+ static VALUE
356
+ ossl_x509req_sign(VALUE self, VALUE key, VALUE digest)
357
+ {
358
+ X509_REQ *req;
359
+ EVP_PKEY *pkey;
360
+ const EVP_MD *md;
361
+
362
+ GetX509Req(self, req);
363
+ pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
364
+ md = GetDigestPtr(digest);
365
+ if (!X509_REQ_sign(req, pkey, md)) {
366
+ ossl_raise(eX509ReqError, NULL);
367
+ }
368
+
369
+ return self;
370
+ }
371
+
372
+ /*
373
+ * Checks that cert signature is made with PRIVversion of this PUBLIC 'key'
374
+ */
375
+ static VALUE
376
+ ossl_x509req_verify(VALUE self, VALUE key)
377
+ {
378
+ X509_REQ *req;
379
+ EVP_PKEY *pkey;
380
+
381
+ GetX509Req(self, req);
382
+ pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
383
+ switch (X509_REQ_verify(req, pkey)) {
384
+ case 1:
385
+ return Qtrue;
386
+ case 0:
387
+ ossl_clear_error();
388
+ return Qfalse;
389
+ default:
390
+ ossl_raise(eX509ReqError, NULL);
391
+ }
392
+ }
393
+
394
+ static VALUE
395
+ ossl_x509req_get_attributes(VALUE self)
396
+ {
397
+ X509_REQ *req;
398
+ int count, i;
399
+ X509_ATTRIBUTE *attr;
400
+ VALUE ary;
401
+
402
+ GetX509Req(self, req);
403
+
404
+ count = X509_REQ_get_attr_count(req);
405
+ if (count < 0) {
406
+ OSSL_Debug("count < 0???");
407
+ return rb_ary_new();
408
+ }
409
+ ary = rb_ary_new2(count);
410
+ for (i=0; i<count; i++) {
411
+ attr = X509_REQ_get_attr(req, i);
412
+ rb_ary_push(ary, ossl_x509attr_new(attr));
413
+ }
414
+
415
+ return ary;
416
+ }
417
+
418
+ static VALUE
419
+ ossl_x509req_set_attributes(VALUE self, VALUE ary)
420
+ {
421
+ X509_REQ *req;
422
+ X509_ATTRIBUTE *attr;
423
+ long i;
424
+ VALUE item;
425
+
426
+ Check_Type(ary, T_ARRAY);
427
+ for (i=0;i<RARRAY_LEN(ary); i++) {
428
+ OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Attr);
429
+ }
430
+ GetX509Req(self, req);
431
+ while ((attr = X509_REQ_delete_attr(req, 0)))
432
+ X509_ATTRIBUTE_free(attr);
433
+ for (i=0;i<RARRAY_LEN(ary); i++) {
434
+ item = RARRAY_AREF(ary, i);
435
+ attr = GetX509AttrPtr(item);
436
+ if (!X509_REQ_add1_attr(req, attr)) {
437
+ ossl_raise(eX509ReqError, NULL);
438
+ }
439
+ }
440
+ return ary;
441
+ }
442
+
443
+ static VALUE
444
+ ossl_x509req_add_attribute(VALUE self, VALUE attr)
445
+ {
446
+ X509_REQ *req;
447
+
448
+ GetX509Req(self, req);
449
+ if (!X509_REQ_add1_attr(req, GetX509AttrPtr(attr))) {
450
+ ossl_raise(eX509ReqError, NULL);
451
+ }
452
+
453
+ return attr;
454
+ }
455
+
456
+ /*
457
+ * X509_REQUEST init
458
+ */
459
+ void
460
+ Init_ossl_x509req(void)
461
+ {
462
+ #if 0
463
+ mOSSL = rb_define_module("OpenSSL");
464
+ eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
465
+ mX509 = rb_define_module_under(mOSSL, "X509");
466
+ #endif
467
+
468
+ eX509ReqError = rb_define_class_under(mX509, "RequestError", eOSSLError);
469
+
470
+ cX509Req = rb_define_class_under(mX509, "Request", rb_cObject);
471
+
472
+ rb_define_alloc_func(cX509Req, ossl_x509req_alloc);
473
+ rb_define_method(cX509Req, "initialize", ossl_x509req_initialize, -1);
474
+ rb_define_copy_func(cX509Req, ossl_x509req_copy);
475
+
476
+ rb_define_method(cX509Req, "to_pem", ossl_x509req_to_pem, 0);
477
+ rb_define_method(cX509Req, "to_der", ossl_x509req_to_der, 0);
478
+ rb_define_alias(cX509Req, "to_s", "to_pem");
479
+ rb_define_method(cX509Req, "to_text", ossl_x509req_to_text, 0);
480
+ rb_define_method(cX509Req, "version", ossl_x509req_get_version, 0);
481
+ rb_define_method(cX509Req, "version=", ossl_x509req_set_version, 1);
482
+ rb_define_method(cX509Req, "subject", ossl_x509req_get_subject, 0);
483
+ rb_define_method(cX509Req, "subject=", ossl_x509req_set_subject, 1);
484
+ rb_define_method(cX509Req, "signature_algorithm", ossl_x509req_get_signature_algorithm, 0);
485
+ rb_define_method(cX509Req, "public_key", ossl_x509req_get_public_key, 0);
486
+ rb_define_method(cX509Req, "public_key=", ossl_x509req_set_public_key, 1);
487
+ rb_define_method(cX509Req, "sign", ossl_x509req_sign, 2);
488
+ rb_define_method(cX509Req, "verify", ossl_x509req_verify, 1);
489
+ rb_define_method(cX509Req, "attributes", ossl_x509req_get_attributes, 0);
490
+ rb_define_method(cX509Req, "attributes=", ossl_x509req_set_attributes, 1);
491
+ rb_define_method(cX509Req, "add_attribute", ossl_x509req_add_attribute, 1);
492
+ }