zig_example 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/ext/mkmf.rb +2734 -0
  3. data/ext/openssl/openssl_missing.c +40 -0
  4. data/ext/openssl/openssl_missing.h +238 -0
  5. data/ext/openssl/ossl.c +1295 -0
  6. data/ext/openssl/ossl.h +201 -0
  7. data/ext/openssl/ossl_asn1.c +1891 -0
  8. data/ext/openssl/ossl_asn1.h +62 -0
  9. data/ext/openssl/ossl_bio.c +42 -0
  10. data/ext/openssl/ossl_bio.h +16 -0
  11. data/ext/openssl/ossl_bn.c +1344 -0
  12. data/ext/openssl/ossl_bn.h +26 -0
  13. data/ext/openssl/ossl_cipher.c +1074 -0
  14. data/ext/openssl/ossl_cipher.h +20 -0
  15. data/ext/openssl/ossl_config.c +460 -0
  16. data/ext/openssl/ossl_config.h +16 -0
  17. data/ext/openssl/ossl_digest.c +425 -0
  18. data/ext/openssl/ossl_digest.h +20 -0
  19. data/ext/openssl/ossl_engine.c +568 -0
  20. data/ext/openssl/ossl_engine.h +19 -0
  21. data/ext/openssl/ossl_hmac.c +310 -0
  22. data/ext/openssl/ossl_hmac.h +18 -0
  23. data/ext/openssl/ossl_kdf.c +311 -0
  24. data/ext/openssl/ossl_kdf.h +6 -0
  25. data/ext/openssl/ossl_ns_spki.c +405 -0
  26. data/ext/openssl/ossl_ns_spki.h +19 -0
  27. data/ext/openssl/ossl_ocsp.c +1965 -0
  28. data/ext/openssl/ossl_ocsp.h +23 -0
  29. data/ext/openssl/ossl_pkcs12.c +275 -0
  30. data/ext/openssl/ossl_pkcs12.h +13 -0
  31. data/ext/openssl/ossl_pkcs7.c +1081 -0
  32. data/ext/openssl/ossl_pkcs7.h +36 -0
  33. data/ext/openssl/ossl_pkey.c +1624 -0
  34. data/ext/openssl/ossl_pkey.h +204 -0
  35. data/ext/openssl/ossl_pkey_dh.c +440 -0
  36. data/ext/openssl/ossl_pkey_dsa.c +359 -0
  37. data/ext/openssl/ossl_pkey_ec.c +1655 -0
  38. data/ext/openssl/ossl_pkey_rsa.c +579 -0
  39. data/ext/openssl/ossl_rand.c +200 -0
  40. data/ext/openssl/ossl_rand.h +18 -0
  41. data/ext/openssl/ossl_ssl.c +3142 -0
  42. data/ext/openssl/ossl_ssl.h +36 -0
  43. data/ext/openssl/ossl_ssl_session.c +331 -0
  44. data/ext/openssl/ossl_ts.c +1539 -0
  45. data/ext/openssl/ossl_ts.h +16 -0
  46. data/ext/openssl/ossl_x509.c +256 -0
  47. data/ext/openssl/ossl_x509.h +115 -0
  48. data/ext/openssl/ossl_x509attr.c +324 -0
  49. data/ext/openssl/ossl_x509cert.c +1002 -0
  50. data/ext/openssl/ossl_x509crl.c +545 -0
  51. data/ext/openssl/ossl_x509ext.c +490 -0
  52. data/ext/openssl/ossl_x509name.c +597 -0
  53. data/ext/openssl/ossl_x509req.c +444 -0
  54. data/ext/openssl/ossl_x509revoked.c +300 -0
  55. data/ext/openssl/ossl_x509store.c +986 -0
  56. data/ext/zigrb_100doors/build.zig +0 -12
  57. data/ext/zigrb_100doors/extconf.rb +2 -19
  58. data/ext/zigrb_ackermann/build.zig +0 -12
  59. data/ext/zigrb_ackermann/extconf.rb +2 -19
  60. data/ext/zigrb_lucas_lehmer/build.zig +0 -12
  61. data/ext/zigrb_lucas_lehmer/extconf.rb +2 -19
  62. data/lib/zig_example/version.rb +1 -1
  63. metadata +56 -2
@@ -0,0 +1,986 @@
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 NewX509Store(klass) \
13
+ TypedData_Wrap_Struct((klass), &ossl_x509store_type, 0)
14
+ #define SetX509Store(obj, st) do { \
15
+ if (!(st)) { \
16
+ ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \
17
+ } \
18
+ RTYPEDDATA_DATA(obj) = (st); \
19
+ } while (0)
20
+ #define GetX509Store(obj, st) do { \
21
+ TypedData_Get_Struct((obj), X509_STORE, &ossl_x509store_type, (st)); \
22
+ if (!(st)) { \
23
+ ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \
24
+ } \
25
+ } while (0)
26
+
27
+ #define NewX509StCtx(klass) \
28
+ TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, 0)
29
+ #define SetX509StCtx(obj, ctx) do { \
30
+ if (!(ctx)) { \
31
+ ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \
32
+ } \
33
+ RTYPEDDATA_DATA(obj) = (ctx); \
34
+ } while (0)
35
+ #define GetX509StCtx(obj, ctx) do { \
36
+ TypedData_Get_Struct((obj), X509_STORE_CTX, &ossl_x509stctx_type, (ctx)); \
37
+ if (!(ctx)) { \
38
+ ossl_raise(rb_eRuntimeError, "STORE_CTX is out of scope!"); \
39
+ } \
40
+ } while (0)
41
+
42
+ /*
43
+ * Verify callback stuff
44
+ */
45
+ static int stctx_ex_verify_cb_idx, store_ex_verify_cb_idx;
46
+ static VALUE ossl_x509stctx_new(X509_STORE_CTX *);
47
+
48
+ struct ossl_verify_cb_args {
49
+ VALUE proc;
50
+ VALUE preverify_ok;
51
+ VALUE store_ctx;
52
+ };
53
+
54
+ static VALUE
55
+ ossl_x509stctx_new_i(VALUE arg)
56
+ {
57
+ return ossl_x509stctx_new((X509_STORE_CTX *)arg);
58
+ }
59
+
60
+ static VALUE
61
+ call_verify_cb_proc(VALUE arg)
62
+ {
63
+ struct ossl_verify_cb_args *args = (struct ossl_verify_cb_args *)arg;
64
+ return rb_funcall(args->proc, rb_intern("call"), 2,
65
+ args->preverify_ok, args->store_ctx);
66
+ }
67
+
68
+ int
69
+ ossl_verify_cb_call(VALUE proc, int ok, X509_STORE_CTX *ctx)
70
+ {
71
+ VALUE rctx, ret;
72
+ struct ossl_verify_cb_args args;
73
+ int state;
74
+
75
+ if (NIL_P(proc))
76
+ return ok;
77
+
78
+ ret = Qfalse;
79
+ rctx = rb_protect(ossl_x509stctx_new_i, (VALUE)ctx, &state);
80
+ if (state) {
81
+ rb_set_errinfo(Qnil);
82
+ rb_warn("StoreContext initialization failure");
83
+ }
84
+ else {
85
+ args.proc = proc;
86
+ args.preverify_ok = ok ? Qtrue : Qfalse;
87
+ args.store_ctx = rctx;
88
+ ret = rb_protect(call_verify_cb_proc, (VALUE)&args, &state);
89
+ if (state) {
90
+ rb_set_errinfo(Qnil);
91
+ rb_warn("exception in verify_callback is ignored");
92
+ }
93
+ RTYPEDDATA_DATA(rctx) = NULL;
94
+ }
95
+ if (ret == Qtrue) {
96
+ X509_STORE_CTX_set_error(ctx, X509_V_OK);
97
+ ok = 1;
98
+ }
99
+ else {
100
+ if (X509_STORE_CTX_get_error(ctx) == X509_V_OK)
101
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
102
+ ok = 0;
103
+ }
104
+
105
+ return ok;
106
+ }
107
+
108
+ /*
109
+ * Classes
110
+ */
111
+ VALUE cX509Store;
112
+ VALUE cX509StoreContext;
113
+ VALUE eX509StoreError;
114
+
115
+ static void
116
+ ossl_x509store_mark(void *ptr)
117
+ {
118
+ X509_STORE *store = ptr;
119
+ rb_gc_mark((VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx));
120
+ }
121
+
122
+ static void
123
+ ossl_x509store_free(void *ptr)
124
+ {
125
+ X509_STORE_free(ptr);
126
+ }
127
+
128
+ static const rb_data_type_t ossl_x509store_type = {
129
+ "OpenSSL/X509/STORE",
130
+ {
131
+ ossl_x509store_mark, ossl_x509store_free,
132
+ },
133
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
134
+ };
135
+
136
+ /*
137
+ * Public functions
138
+ */
139
+ X509_STORE *
140
+ GetX509StorePtr(VALUE obj)
141
+ {
142
+ X509_STORE *store;
143
+
144
+ GetX509Store(obj, store);
145
+
146
+ return store;
147
+ }
148
+
149
+ /*
150
+ * Private functions
151
+ */
152
+ static int
153
+ x509store_verify_cb(int ok, X509_STORE_CTX *ctx)
154
+ {
155
+ VALUE proc;
156
+
157
+ proc = (VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx);
158
+ if (!proc)
159
+ proc = (VALUE)X509_STORE_get_ex_data(X509_STORE_CTX_get0_store(ctx),
160
+ store_ex_verify_cb_idx);
161
+ if (!proc)
162
+ return ok;
163
+
164
+ return ossl_verify_cb_call(proc, ok, ctx);
165
+ }
166
+
167
+ static VALUE
168
+ ossl_x509store_alloc(VALUE klass)
169
+ {
170
+ X509_STORE *store;
171
+ VALUE obj;
172
+
173
+ obj = NewX509Store(klass);
174
+ if ((store = X509_STORE_new()) == NULL)
175
+ ossl_raise(eX509StoreError, "X509_STORE_new");
176
+ SetX509Store(obj, store);
177
+
178
+ return obj;
179
+ }
180
+
181
+ /*
182
+ * General callback for OpenSSL verify
183
+ */
184
+ static VALUE
185
+ ossl_x509store_set_vfy_cb(VALUE self, VALUE cb)
186
+ {
187
+ X509_STORE *store;
188
+
189
+ GetX509Store(self, store);
190
+ X509_STORE_set_ex_data(store, store_ex_verify_cb_idx, (void *)cb);
191
+ rb_iv_set(self, "@verify_callback", cb);
192
+
193
+ return cb;
194
+ }
195
+
196
+
197
+ /*
198
+ * call-seq:
199
+ * X509::Store.new => store
200
+ *
201
+ * Creates a new X509::Store.
202
+ */
203
+ static VALUE
204
+ ossl_x509store_initialize(int argc, VALUE *argv, VALUE self)
205
+ {
206
+ X509_STORE *store;
207
+
208
+ GetX509Store(self, store);
209
+ if (argc != 0)
210
+ rb_warn("OpenSSL::X509::Store.new does not take any arguments");
211
+ #if !defined(HAVE_OPAQUE_OPENSSL)
212
+ /* [Bug #405] [Bug #1678] [Bug #3000]; already fixed? */
213
+ store->ex_data.sk = NULL;
214
+ #endif
215
+ X509_STORE_set_verify_cb(store, x509store_verify_cb);
216
+ ossl_x509store_set_vfy_cb(self, Qnil);
217
+
218
+ /* last verification status */
219
+ rb_iv_set(self, "@error", Qnil);
220
+ rb_iv_set(self, "@error_string", Qnil);
221
+ rb_iv_set(self, "@chain", Qnil);
222
+ rb_iv_set(self, "@time", Qnil);
223
+
224
+ return self;
225
+ }
226
+
227
+ /*
228
+ * call-seq:
229
+ * store.flags = flags
230
+ *
231
+ * Sets the default flags used by certificate chain verification performed with
232
+ * the Store.
233
+ *
234
+ * _flags_ consists of zero or more of the constants defined in OpenSSL::X509
235
+ * with name V_FLAG_* or'ed together.
236
+ *
237
+ * OpenSSL::X509::StoreContext#flags= can be used to change the flags for a
238
+ * single verification operation.
239
+ *
240
+ * See also the man page X509_VERIFY_PARAM_set_flags(3).
241
+ */
242
+ static VALUE
243
+ ossl_x509store_set_flags(VALUE self, VALUE flags)
244
+ {
245
+ X509_STORE *store;
246
+ long f = NUM2LONG(flags);
247
+
248
+ GetX509Store(self, store);
249
+ X509_STORE_set_flags(store, f);
250
+
251
+ return flags;
252
+ }
253
+
254
+ /*
255
+ * call-seq:
256
+ * store.purpose = purpose
257
+ *
258
+ * Sets the store's default verification purpose. If specified,
259
+ * the verifications on the store will check every certificate's extensions are
260
+ * consistent with the purpose. The purpose is specified by constants:
261
+ *
262
+ * * X509::PURPOSE_SSL_CLIENT
263
+ * * X509::PURPOSE_SSL_SERVER
264
+ * * X509::PURPOSE_NS_SSL_SERVER
265
+ * * X509::PURPOSE_SMIME_SIGN
266
+ * * X509::PURPOSE_SMIME_ENCRYPT
267
+ * * X509::PURPOSE_CRL_SIGN
268
+ * * X509::PURPOSE_ANY
269
+ * * X509::PURPOSE_OCSP_HELPER
270
+ * * X509::PURPOSE_TIMESTAMP_SIGN
271
+ *
272
+ * OpenSSL::X509::StoreContext#purpose= can be used to change the value for a
273
+ * single verification operation.
274
+ *
275
+ * See also the man page X509_VERIFY_PARAM_set_purpose(3).
276
+ */
277
+ static VALUE
278
+ ossl_x509store_set_purpose(VALUE self, VALUE purpose)
279
+ {
280
+ X509_STORE *store;
281
+ int p = NUM2INT(purpose);
282
+
283
+ GetX509Store(self, store);
284
+ X509_STORE_set_purpose(store, p);
285
+
286
+ return purpose;
287
+ }
288
+
289
+ /*
290
+ * call-seq:
291
+ * store.trust = trust
292
+ *
293
+ * Sets the default trust settings used by the certificate verification with
294
+ * the store.
295
+ *
296
+ * OpenSSL::X509::StoreContext#trust= can be used to change the value for a
297
+ * single verification operation.
298
+ *
299
+ * See also the man page X509_VERIFY_PARAM_set_trust(3).
300
+ */
301
+ static VALUE
302
+ ossl_x509store_set_trust(VALUE self, VALUE trust)
303
+ {
304
+ X509_STORE *store;
305
+ int t = NUM2INT(trust);
306
+
307
+ GetX509Store(self, store);
308
+ X509_STORE_set_trust(store, t);
309
+
310
+ return trust;
311
+ }
312
+
313
+ /*
314
+ * call-seq:
315
+ * store.time = time
316
+ *
317
+ * Sets the time to be used in the certificate verifications with the store.
318
+ * By default, if not specified, the current system time is used.
319
+ *
320
+ * OpenSSL::X509::StoreContext#time= can be used to change the value for a
321
+ * single verification operation.
322
+ *
323
+ * See also the man page X509_VERIFY_PARAM_set_time(3).
324
+ */
325
+ static VALUE
326
+ ossl_x509store_set_time(VALUE self, VALUE time)
327
+ {
328
+ rb_iv_set(self, "@time", time);
329
+ return time;
330
+ }
331
+
332
+ /*
333
+ * call-seq:
334
+ * store.add_file(file) -> self
335
+ *
336
+ * Adds the certificates in _file_ to the certificate store. _file_ is the path
337
+ * to the file, and the file contains one or more certificates in PEM format
338
+ * concatenated together.
339
+ *
340
+ * See also the man page X509_LOOKUP_file(3).
341
+ */
342
+ static VALUE
343
+ ossl_x509store_add_file(VALUE self, VALUE file)
344
+ {
345
+ X509_STORE *store;
346
+ X509_LOOKUP *lookup;
347
+ const char *path;
348
+
349
+ GetX509Store(self, store);
350
+ path = StringValueCStr(file);
351
+ lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
352
+ if (!lookup)
353
+ ossl_raise(eX509StoreError, "X509_STORE_add_lookup");
354
+ if (X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM) != 1)
355
+ ossl_raise(eX509StoreError, "X509_LOOKUP_load_file");
356
+ #if OPENSSL_VERSION_NUMBER < 0x10101000 || defined(LIBRESSL_VERSION_NUMBER)
357
+ /*
358
+ * X509_load_cert_crl_file() which is called from X509_LOOKUP_load_file()
359
+ * did not check the return value of X509_STORE_add_{cert,crl}(), leaking
360
+ * "cert already in hash table" errors on the error queue, if duplicate
361
+ * certificates are found. This will be fixed by OpenSSL 1.1.1.
362
+ */
363
+ ossl_clear_error();
364
+ #endif
365
+
366
+ return self;
367
+ }
368
+
369
+ /*
370
+ * call-seq:
371
+ * store.add_path(path) -> self
372
+ *
373
+ * Adds _path_ as the hash dir to be looked up by the store.
374
+ *
375
+ * See also the man page X509_LOOKUP_hash_dir(3).
376
+ */
377
+ static VALUE
378
+ ossl_x509store_add_path(VALUE self, VALUE dir)
379
+ {
380
+ X509_STORE *store;
381
+ X509_LOOKUP *lookup;
382
+ const char *path;
383
+
384
+ GetX509Store(self, store);
385
+ path = StringValueCStr(dir);
386
+ lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
387
+ if (!lookup)
388
+ ossl_raise(eX509StoreError, "X509_STORE_add_lookup");
389
+ if (X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1)
390
+ ossl_raise(eX509StoreError, "X509_LOOKUP_add_dir");
391
+
392
+ return self;
393
+ }
394
+
395
+ /*
396
+ * call-seq:
397
+ * store.set_default_paths
398
+ *
399
+ * Configures _store_ to look up CA certificates from the system default
400
+ * certificate store as needed basis. The location of the store can usually be
401
+ * determined by:
402
+ *
403
+ * * OpenSSL::X509::DEFAULT_CERT_FILE
404
+ * * OpenSSL::X509::DEFAULT_CERT_DIR
405
+ *
406
+ * See also the man page X509_STORE_set_default_paths(3).
407
+ */
408
+ static VALUE
409
+ ossl_x509store_set_default_paths(VALUE self)
410
+ {
411
+ X509_STORE *store;
412
+
413
+ GetX509Store(self, store);
414
+ if (X509_STORE_set_default_paths(store) != 1)
415
+ ossl_raise(eX509StoreError, "X509_STORE_set_default_paths");
416
+
417
+ return Qnil;
418
+ }
419
+
420
+ /*
421
+ * call-seq:
422
+ * store.add_cert(cert) -> self
423
+ *
424
+ * Adds the OpenSSL::X509::Certificate _cert_ to the certificate store.
425
+ *
426
+ * See also the man page X509_STORE_add_cert(3).
427
+ */
428
+ static VALUE
429
+ ossl_x509store_add_cert(VALUE self, VALUE arg)
430
+ {
431
+ X509_STORE *store;
432
+ X509 *cert;
433
+
434
+ cert = GetX509CertPtr(arg); /* NO NEED TO DUP */
435
+ GetX509Store(self, store);
436
+ if (X509_STORE_add_cert(store, cert) != 1)
437
+ ossl_raise(eX509StoreError, "X509_STORE_add_cert");
438
+
439
+ return self;
440
+ }
441
+
442
+ /*
443
+ * call-seq:
444
+ * store.add_crl(crl) -> self
445
+ *
446
+ * Adds the OpenSSL::X509::CRL _crl_ to the store.
447
+ *
448
+ * See also the man page X509_STORE_add_crl(3).
449
+ */
450
+ static VALUE
451
+ ossl_x509store_add_crl(VALUE self, VALUE arg)
452
+ {
453
+ X509_STORE *store;
454
+ X509_CRL *crl;
455
+
456
+ crl = GetX509CRLPtr(arg); /* NO NEED TO DUP */
457
+ GetX509Store(self, store);
458
+ if (X509_STORE_add_crl(store, crl) != 1)
459
+ ossl_raise(eX509StoreError, "X509_STORE_add_crl");
460
+
461
+ return self;
462
+ }
463
+
464
+ static VALUE ossl_x509stctx_get_err(VALUE);
465
+ static VALUE ossl_x509stctx_get_err_string(VALUE);
466
+ static VALUE ossl_x509stctx_get_chain(VALUE);
467
+
468
+ /*
469
+ * call-seq:
470
+ * store.verify(cert, chain = nil) -> true | false
471
+ *
472
+ * Performs a certificate verification on the OpenSSL::X509::Certificate _cert_.
473
+ *
474
+ * _chain_ can be an array of OpenSSL::X509::Certificate that is used to
475
+ * construct the certificate chain.
476
+ *
477
+ * If a block is given, it overrides the callback set by #verify_callback=.
478
+ *
479
+ * After finishing the verification, the error information can be retrieved by
480
+ * #error, #error_string, and the resulting complete certificate chain can be
481
+ * retrieved by #chain.
482
+ */
483
+ static VALUE
484
+ ossl_x509store_verify(int argc, VALUE *argv, VALUE self)
485
+ {
486
+ VALUE cert, chain;
487
+ VALUE ctx, proc, result;
488
+
489
+ rb_scan_args(argc, argv, "11", &cert, &chain);
490
+ ctx = rb_funcall(cX509StoreContext, rb_intern("new"), 3, self, cert, chain);
491
+ proc = rb_block_given_p() ? rb_block_proc() :
492
+ rb_iv_get(self, "@verify_callback");
493
+ rb_iv_set(ctx, "@verify_callback", proc);
494
+ result = rb_funcall(ctx, rb_intern("verify"), 0);
495
+
496
+ rb_iv_set(self, "@error", ossl_x509stctx_get_err(ctx));
497
+ rb_iv_set(self, "@error_string", ossl_x509stctx_get_err_string(ctx));
498
+ rb_iv_set(self, "@chain", ossl_x509stctx_get_chain(ctx));
499
+
500
+ return result;
501
+ }
502
+
503
+ /*
504
+ * Private functions
505
+ */
506
+ static void
507
+ ossl_x509stctx_mark(void *ptr)
508
+ {
509
+ X509_STORE_CTX *ctx = ptr;
510
+ rb_gc_mark((VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx));
511
+ }
512
+
513
+ static void
514
+ ossl_x509stctx_free(void *ptr)
515
+ {
516
+ X509_STORE_CTX *ctx = ptr;
517
+ if (X509_STORE_CTX_get0_untrusted(ctx))
518
+ sk_X509_pop_free(X509_STORE_CTX_get0_untrusted(ctx), X509_free);
519
+ if (X509_STORE_CTX_get0_cert(ctx))
520
+ X509_free(X509_STORE_CTX_get0_cert(ctx));
521
+ X509_STORE_CTX_free(ctx);
522
+ }
523
+
524
+ static const rb_data_type_t ossl_x509stctx_type = {
525
+ "OpenSSL/X509/STORE_CTX",
526
+ {
527
+ ossl_x509stctx_mark, ossl_x509stctx_free,
528
+ },
529
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
530
+ };
531
+
532
+ static VALUE
533
+ ossl_x509stctx_alloc(VALUE klass)
534
+ {
535
+ X509_STORE_CTX *ctx;
536
+ VALUE obj;
537
+
538
+ obj = NewX509StCtx(klass);
539
+ if ((ctx = X509_STORE_CTX_new()) == NULL)
540
+ ossl_raise(eX509StoreError, "X509_STORE_CTX_new");
541
+ SetX509StCtx(obj, ctx);
542
+
543
+ return obj;
544
+ }
545
+
546
+ static VALUE
547
+ ossl_x509stctx_new(X509_STORE_CTX *ctx)
548
+ {
549
+ VALUE obj;
550
+
551
+ obj = NewX509StCtx(cX509StoreContext);
552
+ SetX509StCtx(obj, ctx);
553
+
554
+ return obj;
555
+ }
556
+
557
+ static VALUE ossl_x509stctx_set_flags(VALUE, VALUE);
558
+ static VALUE ossl_x509stctx_set_purpose(VALUE, VALUE);
559
+ static VALUE ossl_x509stctx_set_trust(VALUE, VALUE);
560
+ static VALUE ossl_x509stctx_set_time(VALUE, VALUE);
561
+
562
+ /*
563
+ * call-seq:
564
+ * StoreContext.new(store, cert = nil, untrusted = nil)
565
+ *
566
+ * Sets up a StoreContext for a verification of the X.509 certificate _cert_.
567
+ */
568
+ static VALUE
569
+ ossl_x509stctx_initialize(int argc, VALUE *argv, VALUE self)
570
+ {
571
+ VALUE store, cert, chain, t;
572
+ X509_STORE_CTX *ctx;
573
+ X509_STORE *x509st;
574
+ X509 *x509 = NULL;
575
+ STACK_OF(X509) *x509s = NULL;
576
+ int state;
577
+
578
+ rb_scan_args(argc, argv, "12", &store, &cert, &chain);
579
+ GetX509StCtx(self, ctx);
580
+ GetX509Store(store, x509st);
581
+ if (!NIL_P(cert))
582
+ x509 = DupX509CertPtr(cert); /* NEED TO DUP */
583
+ if (!NIL_P(chain)) {
584
+ x509s = ossl_protect_x509_ary2sk(chain, &state);
585
+ if (state) {
586
+ X509_free(x509);
587
+ rb_jump_tag(state);
588
+ }
589
+ }
590
+ if (X509_STORE_CTX_init(ctx, x509st, x509, x509s) != 1){
591
+ X509_free(x509);
592
+ sk_X509_pop_free(x509s, X509_free);
593
+ ossl_raise(eX509StoreError, "X509_STORE_CTX_init");
594
+ }
595
+ if (!NIL_P(t = rb_iv_get(store, "@time")))
596
+ ossl_x509stctx_set_time(self, t);
597
+ rb_iv_set(self, "@verify_callback", rb_iv_get(store, "@verify_callback"));
598
+ rb_iv_set(self, "@cert", cert);
599
+
600
+ return self;
601
+ }
602
+
603
+ /*
604
+ * call-seq:
605
+ * stctx.verify -> true | false
606
+ *
607
+ * Performs the certificate verification using the parameters set to _stctx_.
608
+ *
609
+ * See also the man page X509_verify_cert(3).
610
+ */
611
+ static VALUE
612
+ ossl_x509stctx_verify(VALUE self)
613
+ {
614
+ X509_STORE_CTX *ctx;
615
+
616
+ GetX509StCtx(self, ctx);
617
+ X509_STORE_CTX_set_ex_data(ctx, stctx_ex_verify_cb_idx,
618
+ (void *)rb_iv_get(self, "@verify_callback"));
619
+
620
+ switch (X509_verify_cert(ctx)) {
621
+ case 1:
622
+ return Qtrue;
623
+ case 0:
624
+ ossl_clear_error();
625
+ return Qfalse;
626
+ default:
627
+ ossl_raise(eX509CertError, "X509_verify_cert");
628
+ }
629
+ }
630
+
631
+ /*
632
+ * call-seq:
633
+ * stctx.chain -> nil | Array of X509::Certificate
634
+ *
635
+ * Returns the verified chain.
636
+ *
637
+ * See also the man page X509_STORE_CTX_set0_verified_chain(3).
638
+ */
639
+ static VALUE
640
+ ossl_x509stctx_get_chain(VALUE self)
641
+ {
642
+ X509_STORE_CTX *ctx;
643
+ const STACK_OF(X509) *chain;
644
+
645
+ GetX509StCtx(self, ctx);
646
+ chain = X509_STORE_CTX_get0_chain(ctx);
647
+ if (!chain)
648
+ return Qnil; /* Could be an empty array instead? */
649
+ return ossl_x509_sk2ary(chain);
650
+ }
651
+
652
+ /*
653
+ * call-seq:
654
+ * stctx.error -> Integer
655
+ *
656
+ * Returns the error code of _stctx_. This is typically called after #verify
657
+ * is done, or from the verification callback set to
658
+ * OpenSSL::X509::Store#verify_callback=.
659
+ *
660
+ * See also the man page X509_STORE_CTX_get_error(3).
661
+ */
662
+ static VALUE
663
+ ossl_x509stctx_get_err(VALUE self)
664
+ {
665
+ X509_STORE_CTX *ctx;
666
+
667
+ GetX509StCtx(self, ctx);
668
+
669
+ return INT2NUM(X509_STORE_CTX_get_error(ctx));
670
+ }
671
+
672
+ /*
673
+ * call-seq:
674
+ * stctx.error = error_code
675
+ *
676
+ * Sets the error code of _stctx_. This is used by the verification callback
677
+ * set to OpenSSL::X509::Store#verify_callback=.
678
+ *
679
+ * See also the man page X509_STORE_CTX_set_error(3).
680
+ */
681
+ static VALUE
682
+ ossl_x509stctx_set_error(VALUE self, VALUE err)
683
+ {
684
+ X509_STORE_CTX *ctx;
685
+
686
+ GetX509StCtx(self, ctx);
687
+ X509_STORE_CTX_set_error(ctx, NUM2INT(err));
688
+
689
+ return err;
690
+ }
691
+
692
+ /*
693
+ * call-seq:
694
+ * stctx.error_string -> String
695
+ *
696
+ * Returns the human readable error string corresponding to the error code
697
+ * retrieved by #error.
698
+ *
699
+ * See also the man page X509_verify_cert_error_string(3).
700
+ */
701
+ static VALUE
702
+ ossl_x509stctx_get_err_string(VALUE self)
703
+ {
704
+ X509_STORE_CTX *ctx;
705
+ long err;
706
+
707
+ GetX509StCtx(self, ctx);
708
+ err = X509_STORE_CTX_get_error(ctx);
709
+
710
+ return rb_str_new2(X509_verify_cert_error_string(err));
711
+ }
712
+
713
+ /*
714
+ * call-seq:
715
+ * stctx.error_depth -> Integer
716
+ *
717
+ * Returns the depth of the chain. This is used in combination with #error.
718
+ *
719
+ * See also the man page X509_STORE_CTX_get_error_depth(3).
720
+ */
721
+ static VALUE
722
+ ossl_x509stctx_get_err_depth(VALUE self)
723
+ {
724
+ X509_STORE_CTX *ctx;
725
+
726
+ GetX509StCtx(self, ctx);
727
+
728
+ return INT2NUM(X509_STORE_CTX_get_error_depth(ctx));
729
+ }
730
+
731
+ /*
732
+ * call-seq:
733
+ * stctx.current_cert -> X509::Certificate
734
+ *
735
+ * Returns the certificate which caused the error.
736
+ *
737
+ * See also the man page X509_STORE_CTX_get_current_cert(3).
738
+ */
739
+ static VALUE
740
+ ossl_x509stctx_get_curr_cert(VALUE self)
741
+ {
742
+ X509_STORE_CTX *ctx;
743
+
744
+ GetX509StCtx(self, ctx);
745
+
746
+ return ossl_x509_new(X509_STORE_CTX_get_current_cert(ctx));
747
+ }
748
+
749
+ /*
750
+ * call-seq:
751
+ * stctx.current_crl -> X509::CRL
752
+ *
753
+ * Returns the CRL which caused the error.
754
+ *
755
+ * See also the man page X509_STORE_CTX_get_current_crl(3).
756
+ */
757
+ static VALUE
758
+ ossl_x509stctx_get_curr_crl(VALUE self)
759
+ {
760
+ X509_STORE_CTX *ctx;
761
+ X509_CRL *crl;
762
+
763
+ GetX509StCtx(self, ctx);
764
+ crl = X509_STORE_CTX_get0_current_crl(ctx);
765
+ if (!crl)
766
+ return Qnil;
767
+
768
+ return ossl_x509crl_new(crl);
769
+ }
770
+
771
+ /*
772
+ * call-seq:
773
+ * stctx.flags = flags
774
+ *
775
+ * Sets the verification flags to the context. This overrides the default value
776
+ * set by Store#flags=.
777
+ *
778
+ * See also the man page X509_VERIFY_PARAM_set_flags(3).
779
+ */
780
+ static VALUE
781
+ ossl_x509stctx_set_flags(VALUE self, VALUE flags)
782
+ {
783
+ X509_STORE_CTX *store;
784
+ long f = NUM2LONG(flags);
785
+
786
+ GetX509StCtx(self, store);
787
+ X509_STORE_CTX_set_flags(store, f);
788
+
789
+ return flags;
790
+ }
791
+
792
+ /*
793
+ * call-seq:
794
+ * stctx.purpose = purpose
795
+ *
796
+ * Sets the purpose of the context. This overrides the default value set by
797
+ * Store#purpose=.
798
+ *
799
+ * See also the man page X509_VERIFY_PARAM_set_purpose(3).
800
+ */
801
+ static VALUE
802
+ ossl_x509stctx_set_purpose(VALUE self, VALUE purpose)
803
+ {
804
+ X509_STORE_CTX *store;
805
+ int p = NUM2INT(purpose);
806
+
807
+ GetX509StCtx(self, store);
808
+ X509_STORE_CTX_set_purpose(store, p);
809
+
810
+ return purpose;
811
+ }
812
+
813
+ /*
814
+ * call-seq:
815
+ * stctx.trust = trust
816
+ *
817
+ * Sets the trust settings of the context. This overrides the default value set
818
+ * by Store#trust=.
819
+ *
820
+ * See also the man page X509_VERIFY_PARAM_set_trust(3).
821
+ */
822
+ static VALUE
823
+ ossl_x509stctx_set_trust(VALUE self, VALUE trust)
824
+ {
825
+ X509_STORE_CTX *store;
826
+ int t = NUM2INT(trust);
827
+
828
+ GetX509StCtx(self, store);
829
+ X509_STORE_CTX_set_trust(store, t);
830
+
831
+ return trust;
832
+ }
833
+
834
+ /*
835
+ * call-seq:
836
+ * stctx.time = time
837
+ *
838
+ * Sets the time used in the verification. If not set, the current time is used.
839
+ *
840
+ * See also the man page X509_VERIFY_PARAM_set_time(3).
841
+ */
842
+ static VALUE
843
+ ossl_x509stctx_set_time(VALUE self, VALUE time)
844
+ {
845
+ X509_STORE_CTX *store;
846
+ long t;
847
+
848
+ t = NUM2LONG(rb_Integer(time));
849
+ GetX509StCtx(self, store);
850
+ X509_STORE_CTX_set_time(store, 0, t);
851
+
852
+ return time;
853
+ }
854
+
855
+ /*
856
+ * INIT
857
+ */
858
+ void
859
+ Init_ossl_x509store(void)
860
+ {
861
+ #undef rb_intern
862
+ #if 0
863
+ mOSSL = rb_define_module("OpenSSL");
864
+ eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
865
+ mX509 = rb_define_module_under(mOSSL, "X509");
866
+ #endif
867
+
868
+ /* Register ext_data slot for verify callback Proc */
869
+ stctx_ex_verify_cb_idx = X509_STORE_CTX_get_ex_new_index(0, (void *)"stctx_ex_verify_cb_idx", 0, 0, 0);
870
+ if (stctx_ex_verify_cb_idx < 0)
871
+ ossl_raise(eOSSLError, "X509_STORE_CTX_get_ex_new_index");
872
+ store_ex_verify_cb_idx = X509_STORE_get_ex_new_index(0, (void *)"store_ex_verify_cb_idx", 0, 0, 0);
873
+ if (store_ex_verify_cb_idx < 0)
874
+ ossl_raise(eOSSLError, "X509_STORE_get_ex_new_index");
875
+
876
+ eX509StoreError = rb_define_class_under(mX509, "StoreError", eOSSLError);
877
+
878
+ /* Document-class: OpenSSL::X509::Store
879
+ *
880
+ * The X509 certificate store holds trusted CA certificates used to verify
881
+ * peer certificates.
882
+ *
883
+ * The easiest way to create a useful certificate store is:
884
+ *
885
+ * cert_store = OpenSSL::X509::Store.new
886
+ * cert_store.set_default_paths
887
+ *
888
+ * This will use your system's built-in certificates.
889
+ *
890
+ * If your system does not have a default set of certificates you can obtain
891
+ * a set extracted from Mozilla CA certificate store by cURL maintainers
892
+ * here: https://curl.haxx.se/docs/caextract.html (You may wish to use the
893
+ * firefox-db2pem.sh script to extract the certificates from a local install
894
+ * to avoid man-in-the-middle attacks.)
895
+ *
896
+ * After downloading or generating a cacert.pem from the above link you
897
+ * can create a certificate store from the pem file like this:
898
+ *
899
+ * cert_store = OpenSSL::X509::Store.new
900
+ * cert_store.add_file 'cacert.pem'
901
+ *
902
+ * The certificate store can be used with an SSLSocket like this:
903
+ *
904
+ * ssl_context = OpenSSL::SSL::SSLContext.new
905
+ * ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
906
+ * ssl_context.cert_store = cert_store
907
+ *
908
+ * tcp_socket = TCPSocket.open 'example.com', 443
909
+ *
910
+ * ssl_socket = OpenSSL::SSL::SSLSocket.new tcp_socket, ssl_context
911
+ */
912
+
913
+ cX509Store = rb_define_class_under(mX509, "Store", rb_cObject);
914
+ /*
915
+ * The callback for additional certificate verification. It is invoked for
916
+ * each certificate in the chain and can be used to implement custom
917
+ * certificate verification conditions.
918
+ *
919
+ * The callback is invoked with two values, a boolean that indicates if the
920
+ * pre-verification by OpenSSL has succeeded or not, and the StoreContext in
921
+ * use.
922
+ *
923
+ * The callback can use StoreContext#error= to change the error code as
924
+ * needed. The callback must return either true or false.
925
+ *
926
+ * NOTE: any exception raised within the callback will be ignored.
927
+ *
928
+ * See also the man page X509_STORE_CTX_set_verify_cb(3).
929
+ */
930
+ rb_attr(cX509Store, rb_intern("verify_callback"), 1, 0, Qfalse);
931
+ /*
932
+ * The error code set by the last call of #verify.
933
+ *
934
+ * See also StoreContext#error.
935
+ */
936
+ rb_attr(cX509Store, rb_intern("error"), 1, 0, Qfalse);
937
+ /*
938
+ * The description for the error code set by the last call of #verify.
939
+ *
940
+ * See also StoreContext#error_string.
941
+ */
942
+ rb_attr(cX509Store, rb_intern("error_string"), 1, 0, Qfalse);
943
+ /*
944
+ * The certificate chain constructed by the last call of #verify.
945
+ *
946
+ * See also StoreContext#chain.
947
+ */
948
+ rb_attr(cX509Store, rb_intern("chain"), 1, 0, Qfalse);
949
+ rb_define_alloc_func(cX509Store, ossl_x509store_alloc);
950
+ rb_define_method(cX509Store, "initialize", ossl_x509store_initialize, -1);
951
+ rb_undef_method(cX509Store, "initialize_copy");
952
+ rb_define_method(cX509Store, "verify_callback=", ossl_x509store_set_vfy_cb, 1);
953
+ rb_define_method(cX509Store, "flags=", ossl_x509store_set_flags, 1);
954
+ rb_define_method(cX509Store, "purpose=", ossl_x509store_set_purpose, 1);
955
+ rb_define_method(cX509Store, "trust=", ossl_x509store_set_trust, 1);
956
+ rb_define_method(cX509Store, "time=", ossl_x509store_set_time, 1);
957
+ rb_define_method(cX509Store, "add_path", ossl_x509store_add_path, 1);
958
+ rb_define_method(cX509Store, "add_file", ossl_x509store_add_file, 1);
959
+ rb_define_method(cX509Store, "set_default_paths", ossl_x509store_set_default_paths, 0);
960
+ rb_define_method(cX509Store, "add_cert", ossl_x509store_add_cert, 1);
961
+ rb_define_method(cX509Store, "add_crl", ossl_x509store_add_crl, 1);
962
+ rb_define_method(cX509Store, "verify", ossl_x509store_verify, -1);
963
+
964
+ /*
965
+ * Document-class: OpenSSL::X509::StoreContext
966
+ *
967
+ * A StoreContext is used while validating a single certificate and holds
968
+ * the status involved.
969
+ */
970
+ cX509StoreContext = rb_define_class_under(mX509,"StoreContext", rb_cObject);
971
+ rb_define_alloc_func(cX509StoreContext, ossl_x509stctx_alloc);
972
+ rb_define_method(cX509StoreContext, "initialize", ossl_x509stctx_initialize, -1);
973
+ rb_undef_method(cX509StoreContext, "initialize_copy");
974
+ rb_define_method(cX509StoreContext, "verify", ossl_x509stctx_verify, 0);
975
+ rb_define_method(cX509StoreContext, "chain", ossl_x509stctx_get_chain,0);
976
+ rb_define_method(cX509StoreContext, "error", ossl_x509stctx_get_err, 0);
977
+ rb_define_method(cX509StoreContext, "error=", ossl_x509stctx_set_error, 1);
978
+ rb_define_method(cX509StoreContext, "error_string", ossl_x509stctx_get_err_string,0);
979
+ rb_define_method(cX509StoreContext, "error_depth", ossl_x509stctx_get_err_depth, 0);
980
+ rb_define_method(cX509StoreContext, "current_cert", ossl_x509stctx_get_curr_cert, 0);
981
+ rb_define_method(cX509StoreContext, "current_crl", ossl_x509stctx_get_curr_crl, 0);
982
+ rb_define_method(cX509StoreContext, "flags=", ossl_x509stctx_set_flags, 1);
983
+ rb_define_method(cX509StoreContext, "purpose=", ossl_x509stctx_set_purpose, 1);
984
+ rb_define_method(cX509StoreContext, "trust=", ossl_x509stctx_set_trust, 1);
985
+ rb_define_method(cX509StoreContext, "time=", ossl_x509stctx_set_time, 1);
986
+ }