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_cipher.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_CIPHER_H_)
12
+ #define _OSSL_CIPHER_H_
13
+
14
+ extern VALUE cCipher;
15
+ extern VALUE eCipherError;
16
+
17
+ const EVP_CIPHER *GetCipherPtr(VALUE);
18
+ VALUE ossl_cipher_new(const EVP_CIPHER *);
19
+ void Init_ossl_cipher(void);
20
+
21
+ #endif /* _OSSL_CIPHER_H_ */
22
+
@@ -0,0 +1,75 @@
1
+ /*
2
+ * $Id: ossl_config.c 29856 2010-11-22 07:21:45Z 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
+ /*
15
+ * Classes
16
+ */
17
+ VALUE cConfig;
18
+ VALUE eConfigError;
19
+
20
+ /*
21
+ * Public
22
+ */
23
+
24
+ static CONF *parse_config(VALUE, CONF*);
25
+
26
+ /*
27
+ * GetConfigPtr is a public C-level function for getting OpenSSL CONF struct
28
+ * from an OpenSSL::Config(eConfig) instance. We decided to implement
29
+ * OpenSSL::Config in Ruby level but we need to pass native CONF struct for
30
+ * some OpenSSL features such as X509V3_EXT_*.
31
+ */
32
+ CONF *
33
+ GetConfigPtr(VALUE obj)
34
+ {
35
+ CONF *conf;
36
+ VALUE str;
37
+ BIO *bio;
38
+ long eline = -1;
39
+
40
+ OSSL_Check_Kind(obj, cConfig);
41
+ str = rb_funcall(obj, rb_intern("to_s"), 0);
42
+ bio = ossl_obj2bio(str);
43
+ conf = NCONF_new(NULL);
44
+ if(!conf){
45
+ BIO_free(bio);
46
+ ossl_raise(eConfigError, NULL);
47
+ }
48
+ if(!NCONF_load_bio(conf, bio, &eline)){
49
+ BIO_free(bio);
50
+ NCONF_free(conf);
51
+ if (eline <= 0) ossl_raise(eConfigError, "wrong config format");
52
+ else ossl_raise(eConfigError, "error in line %d", eline);
53
+ ossl_raise(eConfigError, NULL);
54
+ }
55
+ BIO_free(bio);
56
+
57
+ return conf;
58
+ }
59
+
60
+ /*
61
+ * INIT
62
+ */
63
+ void
64
+ Init_ossl_config()
65
+ {
66
+ char *default_config_file;
67
+ eConfigError = rb_define_class_under(mOSSL, "ConfigError", eOSSLError);
68
+ cConfig = rb_define_class_under(mOSSL, "Config", rb_cObject);
69
+
70
+ default_config_file = CONF_get1_default_config_file();
71
+ rb_define_const(cConfig, "DEFAULT_CONFIG_FILE",
72
+ rb_str_new2(default_config_file));
73
+ OPENSSL_free(default_config_file);
74
+ /* methods are defined by openssl/config.rb */
75
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * $Id: ossl_config.h 11708 2007-02-12 23:01:19Z 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(_OSSL_CONFIG_H_)
12
+ #define _OSSL_CONFIG_H_
13
+
14
+ extern VALUE cConfig;
15
+ extern VALUE eConfigError;
16
+
17
+ CONF* GetConfigPtr(VALUE obj);
18
+ CONF* DupConfigPtr(VALUE obj);
19
+ void Init_ossl_config(void);
20
+
21
+ #endif /* _OSSL_CONFIG_H_ */
22
+
@@ -0,0 +1,259 @@
1
+ /*
2
+ * $Id: ossl_digest.c 15600 2008-02-25 08:48:57Z 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
+ #include "ossl.h"
12
+
13
+ #define GetDigest(obj, ctx) do { \
14
+ Data_Get_Struct(obj, EVP_MD_CTX, ctx); \
15
+ if (!ctx) { \
16
+ ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
17
+ } \
18
+ } while (0)
19
+ #define SafeGetDigest(obj, ctx) do { \
20
+ OSSL_Check_Kind(obj, cDigest); \
21
+ GetDigest(obj, ctx); \
22
+ } while (0)
23
+
24
+ /*
25
+ * Classes
26
+ */
27
+ VALUE cDigest;
28
+ VALUE eDigestError;
29
+
30
+ static VALUE ossl_digest_alloc(VALUE klass);
31
+
32
+ /*
33
+ * Public
34
+ */
35
+ const EVP_MD *
36
+ GetDigestPtr(VALUE obj)
37
+ {
38
+ const EVP_MD *md;
39
+
40
+ if (TYPE(obj) == T_STRING) {
41
+ const char *name = STR2CSTR(obj);
42
+
43
+ md = EVP_get_digestbyname(name);
44
+ if (!md)
45
+ ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
46
+ } else {
47
+ EVP_MD_CTX *ctx;
48
+
49
+ SafeGetDigest(obj, ctx);
50
+
51
+ md = EVP_MD_CTX_md(ctx);
52
+ }
53
+
54
+ return md;
55
+ }
56
+
57
+ VALUE
58
+ ossl_digest_new(const EVP_MD *md)
59
+ {
60
+ VALUE ret;
61
+ EVP_MD_CTX *ctx;
62
+
63
+ ret = ossl_digest_alloc(cDigest);
64
+ GetDigest(ret, ctx);
65
+ EVP_DigestInit_ex(ctx, md, NULL);
66
+
67
+ return ret;
68
+ }
69
+
70
+ /*
71
+ * Private
72
+ */
73
+ static VALUE
74
+ ossl_digest_alloc(VALUE klass)
75
+ {
76
+ EVP_MD_CTX *ctx;
77
+ VALUE obj;
78
+
79
+ ctx = EVP_MD_CTX_create();
80
+ if (ctx == NULL)
81
+ ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed");
82
+ obj = Data_Wrap_Struct(klass, 0, EVP_MD_CTX_destroy, ctx);
83
+
84
+ return obj;
85
+ }
86
+
87
+ VALUE ossl_digest_update(VALUE, VALUE);
88
+
89
+ /*
90
+ * call-seq:
91
+ * Digest.new(string) -> digest
92
+ *
93
+ */
94
+ static VALUE
95
+ ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
96
+ {
97
+ EVP_MD_CTX *ctx;
98
+ const EVP_MD *md;
99
+ char *name;
100
+ VALUE type, data;
101
+
102
+ rb_scan_args(argc, argv, "11", &type, &data);
103
+ md = GetDigestPtr(type);
104
+ if (!NIL_P(data)) StringValue(data);
105
+
106
+ GetDigest(self, ctx);
107
+ EVP_DigestInit_ex(ctx, md, NULL);
108
+
109
+ if (!NIL_P(data)) return ossl_digest_update(self, data);
110
+ return self;
111
+ }
112
+
113
+ static VALUE
114
+ ossl_digest_copy(VALUE self, VALUE other)
115
+ {
116
+ EVP_MD_CTX *ctx1, *ctx2;
117
+
118
+ rb_check_frozen(self);
119
+ if (self == other) return self;
120
+
121
+ GetDigest(self, ctx1);
122
+ SafeGetDigest(other, ctx2);
123
+
124
+ if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
125
+ ossl_raise(eDigestError, NULL);
126
+ }
127
+ return self;
128
+ }
129
+
130
+ /*
131
+ * call-seq:
132
+ * digest.reset -> self
133
+ *
134
+ */
135
+ static VALUE
136
+ ossl_digest_reset(VALUE self)
137
+ {
138
+ EVP_MD_CTX *ctx;
139
+
140
+ GetDigest(self, ctx);
141
+ EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL);
142
+
143
+ return self;
144
+ }
145
+
146
+ /*
147
+ * call-seq:
148
+ * digest.update(string) -> aString
149
+ *
150
+ */
151
+ VALUE
152
+ ossl_digest_update(VALUE self, VALUE data)
153
+ {
154
+ EVP_MD_CTX *ctx;
155
+
156
+ StringValue(data);
157
+ GetDigest(self, ctx);
158
+ EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data));
159
+
160
+ return self;
161
+ }
162
+
163
+ /*
164
+ * call-seq:
165
+ * digest.finish -> aString
166
+ *
167
+ */
168
+ static VALUE
169
+ ossl_digest_finish(int argc, VALUE *argv, VALUE self)
170
+ {
171
+ EVP_MD_CTX *ctx;
172
+ VALUE str;
173
+
174
+ rb_scan_args(argc, argv, "01", &str);
175
+
176
+ GetDigest(self, ctx);
177
+
178
+ if (NIL_P(str)) {
179
+ str = rb_str_new(NULL, EVP_MD_CTX_size(ctx));
180
+ } else {
181
+ StringValue(str);
182
+ rb_str_resize(str, EVP_MD_CTX_size(ctx));
183
+ }
184
+
185
+ EVP_DigestFinal_ex(ctx, RSTRING_PTR(str), NULL);
186
+
187
+ return str;
188
+ }
189
+
190
+ /*
191
+ * call-seq:
192
+ * digest.name -> string
193
+ *
194
+ */
195
+ static VALUE
196
+ ossl_digest_name(VALUE self)
197
+ {
198
+ EVP_MD_CTX *ctx;
199
+
200
+ GetDigest(self, ctx);
201
+
202
+ return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
203
+ }
204
+
205
+ /*
206
+ * call-seq:
207
+ * digest.digest_size -> integer
208
+ *
209
+ * Returns the output size of the digest.
210
+ */
211
+ static VALUE
212
+ ossl_digest_size(VALUE self)
213
+ {
214
+ EVP_MD_CTX *ctx;
215
+
216
+ GetDigest(self, ctx);
217
+
218
+ return INT2NUM(EVP_MD_CTX_size(ctx));
219
+ }
220
+
221
+ static VALUE
222
+ ossl_digest_block_length(VALUE self)
223
+ {
224
+ EVP_MD_CTX *ctx;
225
+
226
+ GetDigest(self, ctx);
227
+
228
+ return INT2NUM(EVP_MD_CTX_block_size(ctx));
229
+ }
230
+
231
+ /*
232
+ * INIT
233
+ */
234
+ void
235
+ Init_ossl_digest()
236
+ {
237
+ rb_require("openssl");
238
+ rb_require("digest");
239
+
240
+ #if 0 /* let rdoc know about mOSSL */
241
+ mOSSL = rb_define_module("OpenSSL");
242
+ #endif
243
+
244
+ cDigest = rb_define_class_under(mOSSL, "Digest", rb_path2class("Digest::Class"));
245
+ eDigestError = rb_define_class_under(cDigest, "DigestError", eOSSLError);
246
+
247
+ rb_define_alloc_func(cDigest, ossl_digest_alloc);
248
+
249
+ rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
250
+ rb_define_copy_func(cDigest, ossl_digest_copy);
251
+ rb_define_method(cDigest, "reset", ossl_digest_reset, 0);
252
+ rb_define_method(cDigest, "update", ossl_digest_update, 1);
253
+ rb_define_alias(cDigest, "<<", "update");
254
+ rb_define_private_method(cDigest, "finish", ossl_digest_finish, -1);
255
+ rb_define_method(cDigest, "digest_length", ossl_digest_size, 0);
256
+ rb_define_method(cDigest, "block_length", ossl_digest_block_length, 0);
257
+
258
+ rb_define_method(cDigest, "name", ossl_digest_name, 0);
259
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * $Id: ossl_digest.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_DIGEST_H_)
12
+ #define _OSSL_DIGEST_H_
13
+
14
+ extern VALUE cDigest;
15
+ extern VALUE eDigestError;
16
+
17
+ const EVP_MD *GetDigestPtr(VALUE);
18
+ VALUE ossl_digest_new(const EVP_MD *);
19
+ void Init_ossl_digest(void);
20
+
21
+ #endif /* _OSSL_DIGEST_H_ */
22
+
@@ -0,0 +1,411 @@
1
+ /*
2
+ * $Id$
3
+ * 'OpenSSL for Ruby' project
4
+ * Copyright (C) 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
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
+ #if defined(OSSL_ENGINE_ENABLED)
14
+
15
+ #define WrapEngine(klass, obj, engine) do { \
16
+ if (!engine) { \
17
+ ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
18
+ } \
19
+ obj = Data_Wrap_Struct(klass, 0, ENGINE_free, engine); \
20
+ } while(0)
21
+ #define GetEngine(obj, engine) do { \
22
+ Data_Get_Struct(obj, ENGINE, engine); \
23
+ if (!engine) { \
24
+ ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
25
+ } \
26
+ } while (0)
27
+ #define SafeGetEngine(obj, engine) do { \
28
+ OSSL_Check_Kind(obj, cEngine); \
29
+ GetPKCS7(obj, engine); \
30
+ } while (0)
31
+
32
+ /*
33
+ * Classes
34
+ */
35
+ VALUE cEngine;
36
+ VALUE eEngineError;
37
+
38
+ /*
39
+ * Private
40
+ */
41
+ #define OSSL_ENGINE_LOAD_IF_MATCH(x) \
42
+ do{\
43
+ if(!strcmp(#x, RSTRING_PTR(name))){\
44
+ ENGINE_load_##x();\
45
+ return Qtrue;\
46
+ }\
47
+ }while(0)
48
+
49
+ static VALUE
50
+ ossl_engine_s_load(int argc, VALUE *argv, VALUE klass)
51
+ {
52
+ #if !defined(HAVE_ENGINE_LOAD_BUILTIN_ENGINES)
53
+ return Qnil;
54
+ #else
55
+ VALUE name;
56
+
57
+ rb_scan_args(argc, argv, "01", &name);
58
+ if(NIL_P(name)){
59
+ ENGINE_load_builtin_engines();
60
+ return Qtrue;
61
+ }
62
+ StringValue(name);
63
+ #ifndef OPENSSL_NO_STATIC_ENGINE
64
+ #if HAVE_ENGINE_LOAD_DYNAMIC
65
+ OSSL_ENGINE_LOAD_IF_MATCH(dynamic);
66
+ #endif
67
+ #if HAVE_ENGINE_LOAD_CSWIFT
68
+ OSSL_ENGINE_LOAD_IF_MATCH(cswift);
69
+ #endif
70
+ #if HAVE_ENGINE_LOAD_CHIL
71
+ OSSL_ENGINE_LOAD_IF_MATCH(chil);
72
+ #endif
73
+ #if HAVE_ENGINE_LOAD_ATALLA
74
+ OSSL_ENGINE_LOAD_IF_MATCH(atalla);
75
+ #endif
76
+ #if HAVE_ENGINE_LOAD_NURON
77
+ OSSL_ENGINE_LOAD_IF_MATCH(nuron);
78
+ #endif
79
+ #if HAVE_ENGINE_LOAD_UBSEC
80
+ OSSL_ENGINE_LOAD_IF_MATCH(ubsec);
81
+ #endif
82
+ #if HAVE_ENGINE_LOAD_AEP
83
+ OSSL_ENGINE_LOAD_IF_MATCH(aep);
84
+ #endif
85
+ #if HAVE_ENGINE_LOAD_SUREWARE
86
+ OSSL_ENGINE_LOAD_IF_MATCH(sureware);
87
+ #endif
88
+ #if HAVE_ENGINE_LOAD_4758CCA
89
+ OSSL_ENGINE_LOAD_IF_MATCH(4758cca);
90
+ #endif
91
+ #endif
92
+ #ifdef HAVE_ENGINE_LOAD_OPENBSD_DEV_CRYPTO
93
+ OSSL_ENGINE_LOAD_IF_MATCH(openbsd_dev_crypto);
94
+ #endif
95
+ OSSL_ENGINE_LOAD_IF_MATCH(openssl);
96
+ rb_warning("no such builtin loader for `%s'", RSTRING_PTR(name));
97
+ return Qnil;
98
+ #endif /* HAVE_ENGINE_LOAD_BUILTIN_ENGINES */
99
+ }
100
+
101
+ static VALUE
102
+ ossl_engine_s_cleanup(VALUE self)
103
+ {
104
+ #if defined(HAVE_ENGINE_CLEANUP)
105
+ ENGINE_cleanup();
106
+ #endif
107
+ return Qnil;
108
+ }
109
+
110
+ static VALUE
111
+ ossl_engine_s_engines(VALUE klass)
112
+ {
113
+ ENGINE *e;
114
+ VALUE ary, obj;
115
+
116
+ ary = rb_ary_new();
117
+ for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){
118
+ WrapEngine(klass, obj, e);
119
+ rb_ary_push(ary, obj);
120
+ }
121
+
122
+ return ary;
123
+ }
124
+
125
+ static VALUE
126
+ ossl_engine_s_by_id(VALUE klass, VALUE id)
127
+ {
128
+ ENGINE *e;
129
+ VALUE obj;
130
+
131
+ StringValue(id);
132
+ ossl_engine_s_load(1, &id, klass);
133
+ if(!(e = ENGINE_by_id(RSTRING_PTR(id))))
134
+ ossl_raise(eEngineError, NULL);
135
+ WrapEngine(klass, obj, e);
136
+ if(rb_block_given_p()) rb_yield(obj);
137
+ if(!ENGINE_init(e))
138
+ ossl_raise(eEngineError, NULL);
139
+ ENGINE_ctrl(e, ENGINE_CTRL_SET_PASSWORD_CALLBACK,
140
+ 0, NULL, (void(*)(void))ossl_pem_passwd_cb);
141
+ ERR_clear_error();
142
+
143
+ return obj;
144
+ }
145
+
146
+ static VALUE
147
+ ossl_engine_s_alloc(VALUE klass)
148
+ {
149
+ ENGINE *e;
150
+ VALUE obj;
151
+
152
+ if (!(e = ENGINE_new())) {
153
+ ossl_raise(eEngineError, NULL);
154
+ }
155
+ WrapEngine(klass, obj, e);
156
+
157
+ return obj;
158
+ }
159
+
160
+ static VALUE
161
+ ossl_engine_get_id(VALUE self)
162
+ {
163
+ ENGINE *e;
164
+ GetEngine(self, e);
165
+ return rb_str_new2(ENGINE_get_id(e));
166
+ }
167
+
168
+ static VALUE
169
+ ossl_engine_get_name(VALUE self)
170
+ {
171
+ ENGINE *e;
172
+ GetEngine(self, e);
173
+ return rb_str_new2(ENGINE_get_name(e));
174
+ }
175
+
176
+ static VALUE
177
+ ossl_engine_finish(VALUE self)
178
+ {
179
+ ENGINE *e;
180
+
181
+ GetEngine(self, e);
182
+ if(!ENGINE_finish(e)) ossl_raise(eEngineError, NULL);
183
+
184
+ return Qnil;
185
+ }
186
+
187
+ static VALUE
188
+ ossl_engine_get_cipher(VALUE self, VALUE name)
189
+ {
190
+ #if defined(HAVE_ENGINE_GET_CIPHER)
191
+ ENGINE *e;
192
+ const EVP_CIPHER *ciph, *tmp;
193
+ char *s;
194
+ int nid;
195
+
196
+ s = StringValuePtr(name);
197
+ tmp = EVP_get_cipherbyname(s);
198
+ if(!tmp) ossl_raise(eEngineError, "no such cipher `%s'", s);
199
+ nid = EVP_CIPHER_nid(tmp);
200
+ GetEngine(self, e);
201
+ ciph = ENGINE_get_cipher(e, nid);
202
+ if(!ciph) ossl_raise(eEngineError, NULL);
203
+
204
+ return ossl_cipher_new(ciph);
205
+ #else
206
+ rb_notimplement();
207
+ #endif
208
+ }
209
+
210
+ static VALUE
211
+ ossl_engine_get_digest(VALUE self, VALUE name)
212
+ {
213
+ #if defined(HAVE_ENGINE_GET_DIGEST)
214
+ ENGINE *e;
215
+ const EVP_MD *md, *tmp;
216
+ char *s;
217
+ int nid;
218
+
219
+ s = StringValuePtr(name);
220
+ tmp = EVP_get_digestbyname(s);
221
+ if(!tmp) ossl_raise(eEngineError, "no such digest `%s'", s);
222
+ nid = EVP_MD_nid(tmp);
223
+ GetEngine(self, e);
224
+ md = ENGINE_get_digest(e, nid);
225
+ if(!md) ossl_raise(eEngineError, NULL);
226
+
227
+ return ossl_digest_new(md);
228
+ #else
229
+ rb_notimplement();
230
+ #endif
231
+ }
232
+
233
+ static VALUE
234
+ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
235
+ {
236
+ ENGINE *e;
237
+ EVP_PKEY *pkey;
238
+ VALUE id, data, obj;
239
+ char *sid, *sdata;
240
+
241
+ rb_scan_args(argc, argv, "02", &id, &data);
242
+ sid = NIL_P(id) ? NULL : StringValuePtr(id);
243
+ sdata = NIL_P(data) ? NULL : StringValuePtr(data);
244
+ GetEngine(self, e);
245
+ #if OPENSSL_VERSION_NUMBER < 0x00907000L
246
+ pkey = ENGINE_load_private_key(e, sid, sdata);
247
+ #else
248
+ pkey = ENGINE_load_private_key(e, sid, NULL, sdata);
249
+ #endif
250
+ if (!pkey) ossl_raise(eEngineError, NULL);
251
+ obj = ossl_pkey_new(pkey);
252
+ OSSL_PKEY_SET_PRIVATE(obj);
253
+
254
+ return obj;
255
+ }
256
+
257
+ static VALUE
258
+ ossl_engine_load_pubkey(int argc, VALUE *argv, VALUE self)
259
+ {
260
+ ENGINE *e;
261
+ EVP_PKEY *pkey;
262
+ VALUE id, data;
263
+ char *sid, *sdata;
264
+
265
+ rb_scan_args(argc, argv, "02", &id, &data);
266
+ sid = NIL_P(id) ? NULL : StringValuePtr(id);
267
+ sdata = NIL_P(data) ? NULL : StringValuePtr(data);
268
+ GetEngine(self, e);
269
+ #if OPENSSL_VERSION_NUMBER < 0x00907000L
270
+ pkey = ENGINE_load_public_key(e, sid, sdata);
271
+ #else
272
+ pkey = ENGINE_load_public_key(e, sid, NULL, sdata);
273
+ #endif
274
+ if (!pkey) ossl_raise(eEngineError, NULL);
275
+
276
+ return ossl_pkey_new(pkey);
277
+ }
278
+
279
+ static VALUE
280
+ ossl_engine_set_default(VALUE self, VALUE flag)
281
+ {
282
+ ENGINE *e;
283
+ int f = NUM2INT(flag);
284
+
285
+ GetEngine(self, e);
286
+ ENGINE_set_default(e, f);
287
+
288
+ return Qtrue;
289
+ }
290
+
291
+ static VALUE
292
+ ossl_engine_ctrl_cmd(int argc, VALUE *argv, VALUE self)
293
+ {
294
+ ENGINE *e;
295
+ VALUE cmd, val;
296
+ int ret;
297
+
298
+ GetEngine(self, e);
299
+ rb_scan_args(argc, argv, "11", &cmd, &val);
300
+ StringValue(cmd);
301
+ if (!NIL_P(val)) StringValue(val);
302
+ ret = ENGINE_ctrl_cmd_string(e, RSTRING_PTR(cmd),
303
+ NIL_P(val) ? NULL : RSTRING_PTR(val), 0);
304
+ if (!ret) ossl_raise(eEngineError, NULL);
305
+
306
+ return self;
307
+ }
308
+
309
+ static VALUE
310
+ ossl_engine_cmd_flag_to_name(int flag)
311
+ {
312
+ switch(flag){
313
+ case ENGINE_CMD_FLAG_NUMERIC: return rb_str_new2("NUMERIC");
314
+ case ENGINE_CMD_FLAG_STRING: return rb_str_new2("STRING");
315
+ case ENGINE_CMD_FLAG_NO_INPUT: return rb_str_new2("NO_INPUT");
316
+ case ENGINE_CMD_FLAG_INTERNAL: return rb_str_new2("INTERNAL");
317
+ default: return rb_str_new2("UNKNOWN");
318
+ }
319
+ }
320
+
321
+ static VALUE
322
+ ossl_engine_get_cmds(VALUE self)
323
+ {
324
+ ENGINE *e;
325
+ const ENGINE_CMD_DEFN *defn, *p;
326
+ VALUE ary, tmp;
327
+
328
+ GetEngine(self, e);
329
+ ary = rb_ary_new();
330
+ if ((defn = ENGINE_get_cmd_defns(e)) != NULL){
331
+ for (p = defn; p->cmd_num > 0; p++){
332
+ tmp = rb_ary_new();
333
+ rb_ary_push(tmp, rb_str_new2(p->cmd_name));
334
+ rb_ary_push(tmp, rb_str_new2(p->cmd_desc));
335
+ rb_ary_push(tmp, ossl_engine_cmd_flag_to_name(p->cmd_flags));
336
+ rb_ary_push(ary, tmp);
337
+ }
338
+ }
339
+
340
+ return ary;
341
+ }
342
+
343
+ static VALUE
344
+ ossl_engine_inspect(VALUE self)
345
+ {
346
+ VALUE str;
347
+ const char *cname = rb_class2name(rb_obj_class(self));
348
+
349
+ str = rb_str_new2("#<");
350
+ rb_str_cat2(str, cname);
351
+ rb_str_cat2(str, " id=\"");
352
+ rb_str_append(str, ossl_engine_get_id(self));
353
+ rb_str_cat2(str, "\" name=\"");
354
+ rb_str_append(str, ossl_engine_get_name(self));
355
+ rb_str_cat2(str, "\">");
356
+
357
+ return str;
358
+ }
359
+
360
+ #define DefEngineConst(x) rb_define_const(cEngine, #x, INT2NUM(ENGINE_##x))
361
+
362
+ void
363
+ Init_ossl_engine()
364
+ {
365
+ cEngine = rb_define_class_under(mOSSL, "Engine", rb_cObject);
366
+ eEngineError = rb_define_class_under(cEngine, "EngineError", eOSSLError);
367
+
368
+ rb_define_alloc_func(cEngine, ossl_engine_s_alloc);
369
+ rb_define_singleton_method(cEngine, "load", ossl_engine_s_load, -1);
370
+ rb_define_singleton_method(cEngine, "cleanup", ossl_engine_s_cleanup, 0);
371
+ rb_define_singleton_method(cEngine, "engines", ossl_engine_s_engines, 0);
372
+ rb_define_singleton_method(cEngine, "by_id", ossl_engine_s_by_id, 1);
373
+ rb_undef_method(CLASS_OF(cEngine), "new");
374
+
375
+ rb_define_method(cEngine, "id", ossl_engine_get_id, 0);
376
+ rb_define_method(cEngine, "name", ossl_engine_get_name, 0);
377
+ rb_define_method(cEngine, "finish", ossl_engine_finish, 0);
378
+ rb_define_method(cEngine, "cipher", ossl_engine_get_cipher, 1);
379
+ rb_define_method(cEngine, "digest", ossl_engine_get_digest, 1);
380
+ rb_define_method(cEngine, "load_private_key", ossl_engine_load_privkey, -1);
381
+ rb_define_method(cEngine, "load_public_key", ossl_engine_load_pubkey, -1);
382
+ rb_define_method(cEngine, "set_default", ossl_engine_set_default, 1);
383
+ rb_define_method(cEngine, "ctrl_cmd", ossl_engine_ctrl_cmd, -1);
384
+ rb_define_method(cEngine, "cmds", ossl_engine_get_cmds, 0);
385
+ rb_define_method(cEngine, "inspect", ossl_engine_inspect, 0);
386
+
387
+ DefEngineConst(METHOD_RSA);
388
+ DefEngineConst(METHOD_DSA);
389
+ DefEngineConst(METHOD_DH);
390
+ DefEngineConst(METHOD_RAND);
391
+ #ifdef ENGINE_METHOD_BN_MOD_EXP
392
+ DefEngineConst(METHOD_BN_MOD_EXP);
393
+ #endif
394
+ #ifdef ENGINE_METHOD_BN_MOD_EXP_CRT
395
+ DefEngineConst(METHOD_BN_MOD_EXP_CRT);
396
+ #endif
397
+ #ifdef ENGINE_METHOD_CIPHERS
398
+ DefEngineConst(METHOD_CIPHERS);
399
+ #endif
400
+ #ifdef ENGINE_METHOD_DIGESTS
401
+ DefEngineConst(METHOD_DIGESTS);
402
+ #endif
403
+ DefEngineConst(METHOD_ALL);
404
+ DefEngineConst(METHOD_NONE);
405
+ }
406
+ #else
407
+ void
408
+ Init_ossl_engine()
409
+ {
410
+ }
411
+ #endif