zig_example 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) 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 +53 -0
  61. data/ext/zigrb_lucas_lehmer/extconf.rb +3 -0
  62. data/ext/zigrb_lucas_lehmer/src/lucas_lehmer.c +60 -0
  63. data/ext/zigrb_lucas_lehmer/src/lucas_lehmer.h +1 -0
  64. data/ext/zigrb_lucas_lehmer/src/wrapper.zig +42 -0
  65. data/lib/zig_example/version.rb +1 -1
  66. data/lib/zig_example.rb +1 -0
  67. metadata +63 -3
@@ -0,0 +1,359 @@
1
+ /*
2
+ * 'OpenSSL for Ruby' project
3
+ * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4
+ * All rights reserved.
5
+ */
6
+ /*
7
+ * This program is licensed under the same licence as Ruby.
8
+ * (See the file 'LICENCE'.)
9
+ */
10
+ #include "ossl.h"
11
+
12
+ #if !defined(OPENSSL_NO_DSA)
13
+
14
+ #define GetPKeyDSA(obj, pkey) do { \
15
+ GetPKey((obj), (pkey)); \
16
+ if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DSA) { /* PARANOIA? */ \
17
+ ossl_raise(rb_eRuntimeError, "THIS IS NOT A DSA!"); \
18
+ } \
19
+ } while (0)
20
+ #define GetDSA(obj, dsa) do { \
21
+ EVP_PKEY *_pkey; \
22
+ GetPKeyDSA((obj), _pkey); \
23
+ (dsa) = EVP_PKEY_get0_DSA(_pkey); \
24
+ } while (0)
25
+
26
+ static inline int
27
+ DSA_HAS_PRIVATE(OSSL_3_const DSA *dsa)
28
+ {
29
+ const BIGNUM *bn;
30
+ DSA_get0_key(dsa, NULL, &bn);
31
+ return !!bn;
32
+ }
33
+
34
+ static inline int
35
+ DSA_PRIVATE(VALUE obj, OSSL_3_const DSA *dsa)
36
+ {
37
+ return DSA_HAS_PRIVATE(dsa) || OSSL_PKEY_IS_PRIVATE(obj);
38
+ }
39
+
40
+ /*
41
+ * Classes
42
+ */
43
+ VALUE cDSA;
44
+ VALUE eDSAError;
45
+
46
+ /*
47
+ * Private
48
+ */
49
+ /*
50
+ * call-seq:
51
+ * DSA.new -> dsa
52
+ * DSA.new(string [, pass]) -> dsa
53
+ * DSA.new(size) -> dsa
54
+ *
55
+ * Creates a new DSA instance by reading an existing key from _string_.
56
+ *
57
+ * If called without arguments, creates a new instance with no key components
58
+ * set. They can be set individually by #set_pqg and #set_key.
59
+ *
60
+ * If called with a String, tries to parse as DER or PEM encoding of a \DSA key.
61
+ * See also OpenSSL::PKey.read which can parse keys of any kinds.
62
+ *
63
+ * If called with a number, generates random parameters and a key pair. This
64
+ * form works as an alias of DSA.generate.
65
+ *
66
+ * +string+::
67
+ * A String that contains a DER or PEM encoded key.
68
+ * +pass+::
69
+ * A String that contains an optional password.
70
+ * +size+::
71
+ * See DSA.generate.
72
+ *
73
+ * Examples:
74
+ * p OpenSSL::PKey::DSA.new(1024)
75
+ * #=> #<OpenSSL::PKey::DSA:0x000055a8d6025bf0 oid=DSA>
76
+ *
77
+ * p OpenSSL::PKey::DSA.new(File.read('dsa.pem'))
78
+ * #=> #<OpenSSL::PKey::DSA:0x000055555d6b8110 oid=DSA>
79
+ *
80
+ * p OpenSSL::PKey::DSA.new(File.read('dsa.pem'), 'mypassword')
81
+ * #=> #<OpenSSL::PKey::DSA:0x0000556f973c40b8 oid=DSA>
82
+ */
83
+ static VALUE
84
+ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
85
+ {
86
+ EVP_PKEY *pkey;
87
+ DSA *dsa;
88
+ BIO *in = NULL;
89
+ VALUE arg, pass;
90
+ int type;
91
+
92
+ TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
93
+ if (pkey)
94
+ rb_raise(rb_eTypeError, "pkey already initialized");
95
+
96
+ /* The DSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
97
+ rb_scan_args(argc, argv, "02", &arg, &pass);
98
+ if (argc == 0) {
99
+ dsa = DSA_new();
100
+ if (!dsa)
101
+ ossl_raise(eDSAError, "DSA_new");
102
+ goto legacy;
103
+ }
104
+
105
+ pass = ossl_pem_passwd_value(pass);
106
+ arg = ossl_to_der_if_possible(arg);
107
+ in = ossl_obj2bio(&arg);
108
+
109
+ /* DER-encoded DSAPublicKey format isn't supported by the generic routine */
110
+ dsa = (DSA *)PEM_ASN1_read_bio((d2i_of_void *)d2i_DSAPublicKey,
111
+ PEM_STRING_DSA_PUBLIC,
112
+ in, NULL, NULL, NULL);
113
+ if (dsa)
114
+ goto legacy;
115
+ OSSL_BIO_reset(in);
116
+
117
+ pkey = ossl_pkey_read_generic(in, pass);
118
+ BIO_free(in);
119
+ if (!pkey)
120
+ ossl_raise(eDSAError, "Neither PUB key nor PRIV key");
121
+
122
+ type = EVP_PKEY_base_id(pkey);
123
+ if (type != EVP_PKEY_DSA) {
124
+ EVP_PKEY_free(pkey);
125
+ rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
126
+ }
127
+ RTYPEDDATA_DATA(self) = pkey;
128
+ return self;
129
+
130
+ legacy:
131
+ BIO_free(in);
132
+ pkey = EVP_PKEY_new();
133
+ if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa) != 1) {
134
+ EVP_PKEY_free(pkey);
135
+ DSA_free(dsa);
136
+ ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
137
+ }
138
+ RTYPEDDATA_DATA(self) = pkey;
139
+ return self;
140
+ }
141
+
142
+ #ifndef HAVE_EVP_PKEY_DUP
143
+ static VALUE
144
+ ossl_dsa_initialize_copy(VALUE self, VALUE other)
145
+ {
146
+ EVP_PKEY *pkey;
147
+ DSA *dsa, *dsa_new;
148
+
149
+ TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
150
+ if (pkey)
151
+ rb_raise(rb_eTypeError, "pkey already initialized");
152
+ GetDSA(other, dsa);
153
+
154
+ dsa_new = (DSA *)ASN1_dup((i2d_of_void *)i2d_DSAPrivateKey,
155
+ (d2i_of_void *)d2i_DSAPrivateKey,
156
+ (char *)dsa);
157
+ if (!dsa_new)
158
+ ossl_raise(eDSAError, "ASN1_dup");
159
+
160
+ pkey = EVP_PKEY_new();
161
+ if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa_new) != 1) {
162
+ EVP_PKEY_free(pkey);
163
+ DSA_free(dsa_new);
164
+ ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
165
+ }
166
+ RTYPEDDATA_DATA(self) = pkey;
167
+
168
+ return self;
169
+ }
170
+ #endif
171
+
172
+ /*
173
+ * call-seq:
174
+ * dsa.public? -> true | false
175
+ *
176
+ * Indicates whether this DSA instance has a public key associated with it or
177
+ * not. The public key may be retrieved with DSA#public_key.
178
+ */
179
+ static VALUE
180
+ ossl_dsa_is_public(VALUE self)
181
+ {
182
+ const DSA *dsa;
183
+ const BIGNUM *bn;
184
+
185
+ GetDSA(self, dsa);
186
+ DSA_get0_key(dsa, &bn, NULL);
187
+
188
+ return bn ? Qtrue : Qfalse;
189
+ }
190
+
191
+ /*
192
+ * call-seq:
193
+ * dsa.private? -> true | false
194
+ *
195
+ * Indicates whether this DSA instance has a private key associated with it or
196
+ * not. The private key may be retrieved with DSA#private_key.
197
+ */
198
+ static VALUE
199
+ ossl_dsa_is_private(VALUE self)
200
+ {
201
+ OSSL_3_const DSA *dsa;
202
+
203
+ GetDSA(self, dsa);
204
+
205
+ return DSA_PRIVATE(self, dsa) ? Qtrue : Qfalse;
206
+ }
207
+
208
+ /*
209
+ * call-seq:
210
+ * dsa.export([cipher, password]) -> aString
211
+ * dsa.to_pem([cipher, password]) -> aString
212
+ * dsa.to_s([cipher, password]) -> aString
213
+ *
214
+ * Encodes this DSA to its PEM encoding.
215
+ *
216
+ * === Parameters
217
+ * * _cipher_ is an OpenSSL::Cipher.
218
+ * * _password_ is a string containing your password.
219
+ *
220
+ * === Examples
221
+ * DSA.to_pem -> aString
222
+ * DSA.to_pem(cipher, 'mypassword') -> aString
223
+ *
224
+ */
225
+ static VALUE
226
+ ossl_dsa_export(int argc, VALUE *argv, VALUE self)
227
+ {
228
+ OSSL_3_const DSA *dsa;
229
+
230
+ GetDSA(self, dsa);
231
+ if (DSA_HAS_PRIVATE(dsa))
232
+ return ossl_pkey_export_traditional(argc, argv, self, 0);
233
+ else
234
+ return ossl_pkey_export_spki(self, 0);
235
+ }
236
+
237
+ /*
238
+ * call-seq:
239
+ * dsa.to_der -> aString
240
+ *
241
+ * Encodes this DSA to its DER encoding.
242
+ *
243
+ */
244
+ static VALUE
245
+ ossl_dsa_to_der(VALUE self)
246
+ {
247
+ OSSL_3_const DSA *dsa;
248
+
249
+ GetDSA(self, dsa);
250
+ if (DSA_HAS_PRIVATE(dsa))
251
+ return ossl_pkey_export_traditional(0, NULL, self, 1);
252
+ else
253
+ return ossl_pkey_export_spki(self, 1);
254
+ }
255
+
256
+
257
+ /*
258
+ * call-seq:
259
+ * dsa.params -> hash
260
+ *
261
+ * Stores all parameters of key to the hash
262
+ * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
263
+ * Don't use :-)) (I's up to you)
264
+ */
265
+ static VALUE
266
+ ossl_dsa_get_params(VALUE self)
267
+ {
268
+ OSSL_3_const DSA *dsa;
269
+ VALUE hash;
270
+ const BIGNUM *p, *q, *g, *pub_key, *priv_key;
271
+
272
+ GetDSA(self, dsa);
273
+ DSA_get0_pqg(dsa, &p, &q, &g);
274
+ DSA_get0_key(dsa, &pub_key, &priv_key);
275
+
276
+ hash = rb_hash_new();
277
+ rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
278
+ rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
279
+ rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(g));
280
+ rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pub_key));
281
+ rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(priv_key));
282
+
283
+ return hash;
284
+ }
285
+
286
+ /*
287
+ * Document-method: OpenSSL::PKey::DSA#set_pqg
288
+ * call-seq:
289
+ * dsa.set_pqg(p, q, g) -> self
290
+ *
291
+ * Sets _p_, _q_, _g_ to the DSA instance.
292
+ */
293
+ OSSL_PKEY_BN_DEF3(dsa, DSA, pqg, p, q, g)
294
+ /*
295
+ * Document-method: OpenSSL::PKey::DSA#set_key
296
+ * call-seq:
297
+ * dsa.set_key(pub_key, priv_key) -> self
298
+ *
299
+ * Sets _pub_key_ and _priv_key_ for the DSA instance. _priv_key_ may be +nil+.
300
+ */
301
+ OSSL_PKEY_BN_DEF2(dsa, DSA, key, pub_key, priv_key)
302
+
303
+ /*
304
+ * INIT
305
+ */
306
+ void
307
+ Init_ossl_dsa(void)
308
+ {
309
+ #if 0
310
+ mPKey = rb_define_module_under(mOSSL, "PKey");
311
+ cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
312
+ ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
313
+ #endif
314
+
315
+ /* Document-class: OpenSSL::PKey::DSAError
316
+ *
317
+ * Generic exception that is raised if an operation on a DSA PKey
318
+ * fails unexpectedly or in case an instantiation of an instance of DSA
319
+ * fails due to non-conformant input data.
320
+ */
321
+ eDSAError = rb_define_class_under(mPKey, "DSAError", ePKeyError);
322
+
323
+ /* Document-class: OpenSSL::PKey::DSA
324
+ *
325
+ * DSA, the Digital Signature Algorithm, is specified in NIST's
326
+ * FIPS 186-3. It is an asymmetric public key algorithm that may be used
327
+ * similar to e.g. RSA.
328
+ */
329
+ cDSA = rb_define_class_under(mPKey, "DSA", cPKey);
330
+
331
+ rb_define_method(cDSA, "initialize", ossl_dsa_initialize, -1);
332
+ #ifndef HAVE_EVP_PKEY_DUP
333
+ rb_define_method(cDSA, "initialize_copy", ossl_dsa_initialize_copy, 1);
334
+ #endif
335
+
336
+ rb_define_method(cDSA, "public?", ossl_dsa_is_public, 0);
337
+ rb_define_method(cDSA, "private?", ossl_dsa_is_private, 0);
338
+ rb_define_method(cDSA, "export", ossl_dsa_export, -1);
339
+ rb_define_alias(cDSA, "to_pem", "export");
340
+ rb_define_alias(cDSA, "to_s", "export");
341
+ rb_define_method(cDSA, "to_der", ossl_dsa_to_der, 0);
342
+
343
+ DEF_OSSL_PKEY_BN(cDSA, dsa, p);
344
+ DEF_OSSL_PKEY_BN(cDSA, dsa, q);
345
+ DEF_OSSL_PKEY_BN(cDSA, dsa, g);
346
+ DEF_OSSL_PKEY_BN(cDSA, dsa, pub_key);
347
+ DEF_OSSL_PKEY_BN(cDSA, dsa, priv_key);
348
+ rb_define_method(cDSA, "set_pqg", ossl_dsa_set_pqg, 3);
349
+ rb_define_method(cDSA, "set_key", ossl_dsa_set_key, 2);
350
+
351
+ rb_define_method(cDSA, "params", ossl_dsa_get_params, 0);
352
+ }
353
+
354
+ #else /* defined NO_DSA */
355
+ void
356
+ Init_ossl_dsa(void)
357
+ {
358
+ }
359
+ #endif /* NO_DSA */