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.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/.travis.yml +7 -0
- data/README.md +2 -2
- data/Rakefile +0 -1
- data/ext/rubysl/openssl/extconf.h +50 -0
- data/ext/rubysl/openssl/extconf.rb +144 -0
- data/ext/rubysl/openssl/openssl_missing.c +343 -0
- data/ext/rubysl/openssl/openssl_missing.h +191 -0
- data/ext/rubysl/openssl/ossl.c +552 -0
- data/ext/rubysl/openssl/ossl.h +233 -0
- data/ext/rubysl/openssl/ossl_asn1.c +1160 -0
- data/ext/rubysl/openssl/ossl_asn1.h +59 -0
- data/ext/rubysl/openssl/ossl_bio.c +86 -0
- data/ext/rubysl/openssl/ossl_bio.h +21 -0
- data/ext/rubysl/openssl/ossl_bn.c +852 -0
- data/ext/rubysl/openssl/ossl_bn.h +25 -0
- data/ext/rubysl/openssl/ossl_cipher.c +569 -0
- data/ext/rubysl/openssl/ossl_cipher.h +22 -0
- data/ext/rubysl/openssl/ossl_config.c +75 -0
- data/ext/rubysl/openssl/ossl_config.h +22 -0
- data/ext/rubysl/openssl/ossl_digest.c +259 -0
- data/ext/rubysl/openssl/ossl_digest.h +22 -0
- data/ext/rubysl/openssl/ossl_engine.c +411 -0
- data/ext/rubysl/openssl/ossl_engine.h +20 -0
- data/ext/rubysl/openssl/ossl_hmac.c +268 -0
- data/ext/rubysl/openssl/ossl_hmac.h +19 -0
- data/ext/rubysl/openssl/ossl_ns_spki.c +257 -0
- data/ext/rubysl/openssl/ossl_ns_spki.h +21 -0
- data/ext/rubysl/openssl/ossl_ocsp.c +769 -0
- data/ext/rubysl/openssl/ossl_ocsp.h +24 -0
- data/ext/rubysl/openssl/ossl_pkcs12.c +210 -0
- data/ext/rubysl/openssl/ossl_pkcs12.h +15 -0
- data/ext/rubysl/openssl/ossl_pkcs5.c +99 -0
- data/ext/rubysl/openssl/ossl_pkcs5.h +6 -0
- data/ext/rubysl/openssl/ossl_pkcs7.c +1039 -0
- data/ext/rubysl/openssl/ossl_pkcs7.h +22 -0
- data/ext/rubysl/openssl/ossl_pkey.c +240 -0
- data/ext/rubysl/openssl/ossl_pkey.h +141 -0
- data/ext/rubysl/openssl/ossl_pkey_dh.c +532 -0
- data/ext/rubysl/openssl/ossl_pkey_dsa.c +484 -0
- data/ext/rubysl/openssl/ossl_pkey_ec.c +1593 -0
- data/ext/rubysl/openssl/ossl_pkey_rsa.c +593 -0
- data/ext/rubysl/openssl/ossl_rand.c +202 -0
- data/ext/rubysl/openssl/ossl_rand.h +20 -0
- data/ext/rubysl/openssl/ossl_ssl.c +1484 -0
- data/ext/rubysl/openssl/ossl_ssl.h +36 -0
- data/ext/rubysl/openssl/ossl_ssl_session.c +307 -0
- data/ext/rubysl/openssl/ossl_version.h +16 -0
- data/ext/rubysl/openssl/ossl_x509.c +104 -0
- data/ext/rubysl/openssl/ossl_x509.h +114 -0
- data/ext/rubysl/openssl/ossl_x509attr.c +274 -0
- data/ext/rubysl/openssl/ossl_x509cert.c +764 -0
- data/ext/rubysl/openssl/ossl_x509crl.c +535 -0
- data/ext/rubysl/openssl/ossl_x509ext.c +458 -0
- data/ext/rubysl/openssl/ossl_x509name.c +399 -0
- data/ext/rubysl/openssl/ossl_x509req.c +466 -0
- data/ext/rubysl/openssl/ossl_x509revoked.c +229 -0
- data/ext/rubysl/openssl/ossl_x509store.c +625 -0
- data/ext/rubysl/openssl/ruby_missing.h +41 -0
- data/lib/openssl.rb +1 -0
- data/lib/openssl/bn.rb +35 -0
- data/lib/openssl/buffering.rb +241 -0
- data/lib/openssl/cipher.rb +65 -0
- data/lib/openssl/config.rb +316 -0
- data/lib/openssl/digest.rb +61 -0
- data/lib/openssl/net/ftptls.rb +53 -0
- data/lib/openssl/net/telnets.rb +251 -0
- data/lib/openssl/pkcs7.rb +25 -0
- data/lib/openssl/ssl-internal.rb +187 -0
- data/lib/openssl/ssl.rb +1 -0
- data/lib/openssl/x509-internal.rb +153 -0
- data/lib/openssl/x509.rb +1 -0
- data/lib/rubysl/openssl.rb +28 -0
- data/lib/rubysl/openssl/version.rb +5 -0
- data/rubysl-openssl.gemspec +19 -18
- data/spec/cipher_spec.rb +16 -0
- data/spec/config/freeze_spec.rb +17 -0
- data/spec/hmac/digest_spec.rb +15 -0
- data/spec/hmac/hexdigest_spec.rb +15 -0
- data/spec/random/pseudo_bytes_spec.rb +5 -0
- data/spec/random/random_bytes_spec.rb +5 -0
- data/spec/random/shared/random_bytes.rb +28 -0
- data/spec/shared/constants.rb +11 -0
- data/spec/x509/name/parse_spec.rb +47 -0
- metadata +153 -89
- data/lib/rubysl-openssl.rb +0 -7
- data/lib/rubysl-openssl/version.rb +0 -5
@@ -0,0 +1,229 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: ossl_x509revoked.c 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
|
+
#include "ossl.h"
|
12
|
+
|
13
|
+
#define WrapX509Rev(klass, obj, rev) do { \
|
14
|
+
if (!rev) { \
|
15
|
+
ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
|
16
|
+
} \
|
17
|
+
obj = Data_Wrap_Struct(klass, 0, X509_REVOKED_free, rev); \
|
18
|
+
} while (0)
|
19
|
+
#define GetX509Rev(obj, rev) do { \
|
20
|
+
Data_Get_Struct(obj, X509_REVOKED, rev); \
|
21
|
+
if (!rev) { \
|
22
|
+
ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
|
23
|
+
} \
|
24
|
+
} while (0)
|
25
|
+
#define SafeGetX509Rev(obj, rev) do { \
|
26
|
+
OSSL_Check_Kind(obj, cX509Rev); \
|
27
|
+
GetX509Rev(obj, rev); \
|
28
|
+
} while (0)
|
29
|
+
|
30
|
+
/*
|
31
|
+
* Classes
|
32
|
+
*/
|
33
|
+
VALUE cX509Rev;
|
34
|
+
VALUE eX509RevError;
|
35
|
+
|
36
|
+
/*
|
37
|
+
* PUBLIC
|
38
|
+
*/
|
39
|
+
VALUE
|
40
|
+
ossl_x509revoked_new(X509_REVOKED *rev)
|
41
|
+
{
|
42
|
+
X509_REVOKED *new;
|
43
|
+
VALUE obj;
|
44
|
+
|
45
|
+
if (!rev) {
|
46
|
+
new = X509_REVOKED_new();
|
47
|
+
} else {
|
48
|
+
new = X509_REVOKED_dup(rev);
|
49
|
+
}
|
50
|
+
if (!new) {
|
51
|
+
ossl_raise(eX509RevError, NULL);
|
52
|
+
}
|
53
|
+
WrapX509Rev(cX509Rev, obj, new);
|
54
|
+
|
55
|
+
return obj;
|
56
|
+
}
|
57
|
+
|
58
|
+
X509_REVOKED *
|
59
|
+
DupX509RevokedPtr(VALUE obj)
|
60
|
+
{
|
61
|
+
X509_REVOKED *rev, *new;
|
62
|
+
|
63
|
+
SafeGetX509Rev(obj, rev);
|
64
|
+
if (!(new = X509_REVOKED_dup(rev))) {
|
65
|
+
ossl_raise(eX509RevError, NULL);
|
66
|
+
}
|
67
|
+
|
68
|
+
return new;
|
69
|
+
}
|
70
|
+
|
71
|
+
/*
|
72
|
+
* PRIVATE
|
73
|
+
*/
|
74
|
+
static VALUE
|
75
|
+
ossl_x509revoked_alloc(VALUE klass)
|
76
|
+
{
|
77
|
+
X509_REVOKED *rev;
|
78
|
+
VALUE obj;
|
79
|
+
|
80
|
+
if (!(rev = X509_REVOKED_new())) {
|
81
|
+
ossl_raise(eX509RevError, NULL);
|
82
|
+
}
|
83
|
+
WrapX509Rev(klass, obj, rev);
|
84
|
+
|
85
|
+
return obj;
|
86
|
+
}
|
87
|
+
|
88
|
+
static VALUE
|
89
|
+
ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self)
|
90
|
+
{
|
91
|
+
/* EMPTY */
|
92
|
+
return self;
|
93
|
+
}
|
94
|
+
|
95
|
+
static VALUE
|
96
|
+
ossl_x509revoked_get_serial(VALUE self)
|
97
|
+
{
|
98
|
+
X509_REVOKED *rev;
|
99
|
+
|
100
|
+
GetX509Rev(self, rev);
|
101
|
+
|
102
|
+
return asn1integer_to_num(rev->serialNumber);
|
103
|
+
}
|
104
|
+
|
105
|
+
static VALUE
|
106
|
+
ossl_x509revoked_set_serial(VALUE self, VALUE num)
|
107
|
+
{
|
108
|
+
X509_REVOKED *rev;
|
109
|
+
|
110
|
+
GetX509Rev(self, rev);
|
111
|
+
rev->serialNumber = num_to_asn1integer(num, rev->serialNumber);
|
112
|
+
|
113
|
+
return num;
|
114
|
+
}
|
115
|
+
|
116
|
+
static VALUE
|
117
|
+
ossl_x509revoked_get_time(VALUE self)
|
118
|
+
{
|
119
|
+
X509_REVOKED *rev;
|
120
|
+
|
121
|
+
GetX509Rev(self, rev);
|
122
|
+
|
123
|
+
return asn1time_to_time(rev->revocationDate);
|
124
|
+
}
|
125
|
+
|
126
|
+
static VALUE
|
127
|
+
ossl_x509revoked_set_time(VALUE self, VALUE time)
|
128
|
+
{
|
129
|
+
X509_REVOKED *rev;
|
130
|
+
time_t sec;
|
131
|
+
|
132
|
+
sec = time_to_time_t(time);
|
133
|
+
GetX509Rev(self, rev);
|
134
|
+
if (!X509_time_adj(rev->revocationDate, 0, &sec)) {
|
135
|
+
ossl_raise(eX509RevError, NULL);
|
136
|
+
}
|
137
|
+
|
138
|
+
return time;
|
139
|
+
}
|
140
|
+
/*
|
141
|
+
* Gets X509v3 extensions as array of X509Ext objects
|
142
|
+
*/
|
143
|
+
static VALUE
|
144
|
+
ossl_x509revoked_get_extensions(VALUE self)
|
145
|
+
{
|
146
|
+
X509_REVOKED *rev;
|
147
|
+
int count, i;
|
148
|
+
X509_EXTENSION *ext;
|
149
|
+
VALUE ary;
|
150
|
+
|
151
|
+
GetX509Rev(self, rev);
|
152
|
+
count = X509_REVOKED_get_ext_count(rev);
|
153
|
+
if (count < 0) {
|
154
|
+
OSSL_Debug("count < 0???");
|
155
|
+
return rb_ary_new();
|
156
|
+
}
|
157
|
+
ary = rb_ary_new2(count);
|
158
|
+
for (i=0; i<count; i++) {
|
159
|
+
ext = X509_REVOKED_get_ext(rev, i);
|
160
|
+
rb_ary_push(ary, ossl_x509ext_new(ext));
|
161
|
+
}
|
162
|
+
|
163
|
+
return ary;
|
164
|
+
}
|
165
|
+
|
166
|
+
/*
|
167
|
+
* Sets X509_EXTENSIONs
|
168
|
+
*/
|
169
|
+
static VALUE
|
170
|
+
ossl_x509revoked_set_extensions(VALUE self, VALUE ary)
|
171
|
+
{
|
172
|
+
X509_REVOKED *rev;
|
173
|
+
X509_EXTENSION *ext;
|
174
|
+
int i;
|
175
|
+
VALUE item;
|
176
|
+
|
177
|
+
Check_Type(ary, T_ARRAY);
|
178
|
+
for (i=0; i<RARRAY_LEN(ary); i++) {
|
179
|
+
OSSL_Check_Kind(rb_ary_entry(ary, i), cX509Ext);
|
180
|
+
}
|
181
|
+
GetX509Rev(self, rev);
|
182
|
+
sk_X509_EXTENSION_pop_free(rev->extensions, X509_EXTENSION_free);
|
183
|
+
rev->extensions = NULL;
|
184
|
+
for (i=0; i<RARRAY_LEN(ary); i++) {
|
185
|
+
item = rb_ary_entry(ary, i);
|
186
|
+
ext = DupX509ExtPtr(item);
|
187
|
+
if(!X509_REVOKED_add_ext(rev, ext, -1)) {
|
188
|
+
ossl_raise(eX509RevError, NULL);
|
189
|
+
}
|
190
|
+
}
|
191
|
+
|
192
|
+
return ary;
|
193
|
+
}
|
194
|
+
|
195
|
+
static VALUE
|
196
|
+
ossl_x509revoked_add_extension(VALUE self, VALUE ext)
|
197
|
+
{
|
198
|
+
X509_REVOKED *rev;
|
199
|
+
|
200
|
+
GetX509Rev(self, rev);
|
201
|
+
if(!X509_REVOKED_add_ext(rev, DupX509ExtPtr(ext), -1)) {
|
202
|
+
ossl_raise(eX509RevError, NULL);
|
203
|
+
}
|
204
|
+
|
205
|
+
return ext;
|
206
|
+
}
|
207
|
+
|
208
|
+
/*
|
209
|
+
* INIT
|
210
|
+
*/
|
211
|
+
void
|
212
|
+
Init_ossl_x509revoked()
|
213
|
+
{
|
214
|
+
eX509RevError = rb_define_class_under(mX509, "RevokedError", eOSSLError);
|
215
|
+
|
216
|
+
cX509Rev = rb_define_class_under(mX509, "Revoked", rb_cObject);
|
217
|
+
|
218
|
+
rb_define_alloc_func(cX509Rev, ossl_x509revoked_alloc);
|
219
|
+
rb_define_method(cX509Rev, "initialize", ossl_x509revoked_initialize, -1);
|
220
|
+
|
221
|
+
rb_define_method(cX509Rev, "serial", ossl_x509revoked_get_serial, 0);
|
222
|
+
rb_define_method(cX509Rev, "serial=", ossl_x509revoked_set_serial, 1);
|
223
|
+
rb_define_method(cX509Rev, "time", ossl_x509revoked_get_time, 0);
|
224
|
+
rb_define_method(cX509Rev, "time=", ossl_x509revoked_set_time, 1);
|
225
|
+
rb_define_method(cX509Rev, "extensions", ossl_x509revoked_get_extensions, 0);
|
226
|
+
rb_define_method(cX509Rev, "extensions=", ossl_x509revoked_set_extensions, 1);
|
227
|
+
rb_define_method(cX509Rev, "add_extension", ossl_x509revoked_add_extension, 1);
|
228
|
+
}
|
229
|
+
|
@@ -0,0 +1,625 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: ossl_x509store.c 16691 2008-05-29 17:45:47Z knu $
|
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
|
+
#include <rubysig.h>
|
13
|
+
|
14
|
+
#define WrapX509Store(klass, obj, st) do { \
|
15
|
+
if (!st) { \
|
16
|
+
ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \
|
17
|
+
} \
|
18
|
+
obj = Data_Wrap_Struct(klass, 0, X509_STORE_free, st); \
|
19
|
+
} while (0)
|
20
|
+
#define GetX509Store(obj, st) do { \
|
21
|
+
Data_Get_Struct(obj, X509_STORE, st); \
|
22
|
+
if (!st) { \
|
23
|
+
ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \
|
24
|
+
} \
|
25
|
+
} while (0)
|
26
|
+
#define SafeGetX509Store(obj, st) do { \
|
27
|
+
OSSL_Check_Kind(obj, cX509Store); \
|
28
|
+
GetX509Store(obj, st); \
|
29
|
+
} while (0)
|
30
|
+
|
31
|
+
#define WrapX509StCtx(klass, obj, ctx) do { \
|
32
|
+
if (!ctx) { \
|
33
|
+
ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \
|
34
|
+
} \
|
35
|
+
obj = Data_Wrap_Struct(klass, 0, ossl_x509stctx_free, ctx); \
|
36
|
+
} while (0)
|
37
|
+
#define GetX509StCtx(obj, ctx) do { \
|
38
|
+
Data_Get_Struct(obj, X509_STORE_CTX, ctx); \
|
39
|
+
if (!ctx) { \
|
40
|
+
ossl_raise(rb_eRuntimeError, "STORE_CTX is out of scope!"); \
|
41
|
+
} \
|
42
|
+
} while (0)
|
43
|
+
#define SafeGetX509StCtx(obj, storep) do { \
|
44
|
+
OSSL_Check_Kind(obj, cX509StoreContext); \
|
45
|
+
GetX509Store(obj, ctx); \
|
46
|
+
} while (0)
|
47
|
+
|
48
|
+
/*
|
49
|
+
* Classes
|
50
|
+
*/
|
51
|
+
VALUE cX509Store;
|
52
|
+
VALUE cX509StoreContext;
|
53
|
+
VALUE eX509StoreError;
|
54
|
+
|
55
|
+
/*
|
56
|
+
* Public functions
|
57
|
+
*/
|
58
|
+
VALUE
|
59
|
+
ossl_x509store_new(X509_STORE *store)
|
60
|
+
{
|
61
|
+
VALUE obj;
|
62
|
+
|
63
|
+
WrapX509Store(cX509Store, obj, store);
|
64
|
+
|
65
|
+
return obj;
|
66
|
+
}
|
67
|
+
|
68
|
+
X509_STORE *
|
69
|
+
GetX509StorePtr(VALUE obj)
|
70
|
+
{
|
71
|
+
X509_STORE *store;
|
72
|
+
|
73
|
+
SafeGetX509Store(obj, store);
|
74
|
+
|
75
|
+
return store;
|
76
|
+
}
|
77
|
+
|
78
|
+
X509_STORE *
|
79
|
+
DupX509StorePtr(VALUE obj)
|
80
|
+
{
|
81
|
+
X509_STORE *store;
|
82
|
+
|
83
|
+
SafeGetX509Store(obj, store);
|
84
|
+
CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
|
85
|
+
|
86
|
+
return store;
|
87
|
+
}
|
88
|
+
|
89
|
+
/*
|
90
|
+
* Private functions
|
91
|
+
*/
|
92
|
+
static VALUE
|
93
|
+
ossl_x509store_alloc(VALUE klass)
|
94
|
+
{
|
95
|
+
X509_STORE *store;
|
96
|
+
VALUE obj;
|
97
|
+
|
98
|
+
if((store = X509_STORE_new()) == NULL){
|
99
|
+
ossl_raise(eX509StoreError, NULL);
|
100
|
+
}
|
101
|
+
WrapX509Store(klass, obj, store);
|
102
|
+
|
103
|
+
return obj;
|
104
|
+
}
|
105
|
+
|
106
|
+
/*
|
107
|
+
* General callback for OpenSSL verify
|
108
|
+
*/
|
109
|
+
static VALUE
|
110
|
+
ossl_x509store_set_vfy_cb(VALUE self, VALUE cb)
|
111
|
+
{
|
112
|
+
X509_STORE *store;
|
113
|
+
|
114
|
+
GetX509Store(self, store);
|
115
|
+
X509_STORE_set_ex_data(store, ossl_verify_cb_idx, (void*)cb);
|
116
|
+
rb_iv_set(self, "@verify_callback", cb);
|
117
|
+
|
118
|
+
return cb;
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
/*
|
123
|
+
* call-seq:
|
124
|
+
* X509::Store.new => store
|
125
|
+
*
|
126
|
+
*/
|
127
|
+
static VALUE
|
128
|
+
ossl_x509store_initialize(int argc, VALUE *argv, VALUE self)
|
129
|
+
{
|
130
|
+
X509_STORE *store;
|
131
|
+
|
132
|
+
/* BUG: This method takes any number of arguments but appears to ignore them. */
|
133
|
+
GetX509Store(self, store);
|
134
|
+
store->ex_data.sk = NULL;
|
135
|
+
X509_STORE_set_verify_cb_func(store, ossl_verify_cb);
|
136
|
+
ossl_x509store_set_vfy_cb(self, Qnil);
|
137
|
+
|
138
|
+
#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
|
139
|
+
rb_iv_set(self, "@flags", INT2NUM(0));
|
140
|
+
rb_iv_set(self, "@purpose", INT2NUM(0));
|
141
|
+
rb_iv_set(self, "@trust", INT2NUM(0));
|
142
|
+
#endif
|
143
|
+
|
144
|
+
/* last verification status */
|
145
|
+
rb_iv_set(self, "@error", Qnil);
|
146
|
+
rb_iv_set(self, "@error_string", Qnil);
|
147
|
+
rb_iv_set(self, "@chain", Qnil);
|
148
|
+
rb_iv_set(self, "@time", Qnil);
|
149
|
+
|
150
|
+
return self;
|
151
|
+
}
|
152
|
+
|
153
|
+
static VALUE
|
154
|
+
ossl_x509store_set_flags(VALUE self, VALUE flags)
|
155
|
+
{
|
156
|
+
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
|
157
|
+
X509_STORE *store;
|
158
|
+
long f = NUM2LONG(flags);
|
159
|
+
|
160
|
+
GetX509Store(self, store);
|
161
|
+
X509_STORE_set_flags(store, f);
|
162
|
+
#else
|
163
|
+
rb_iv_set(self, "@flags", flags);
|
164
|
+
#endif
|
165
|
+
|
166
|
+
return flags;
|
167
|
+
}
|
168
|
+
|
169
|
+
static VALUE
|
170
|
+
ossl_x509store_set_purpose(VALUE self, VALUE purpose)
|
171
|
+
{
|
172
|
+
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
|
173
|
+
X509_STORE *store;
|
174
|
+
long p = NUM2LONG(purpose);
|
175
|
+
|
176
|
+
GetX509Store(self, store);
|
177
|
+
X509_STORE_set_purpose(store, p);
|
178
|
+
#else
|
179
|
+
rb_iv_set(self, "@purpose", purpose);
|
180
|
+
#endif
|
181
|
+
|
182
|
+
return purpose;
|
183
|
+
}
|
184
|
+
|
185
|
+
static VALUE
|
186
|
+
ossl_x509store_set_trust(VALUE self, VALUE trust)
|
187
|
+
{
|
188
|
+
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
|
189
|
+
X509_STORE *store;
|
190
|
+
long t = NUM2LONG(trust);
|
191
|
+
|
192
|
+
GetX509Store(self, store);
|
193
|
+
X509_STORE_set_trust(store, t);
|
194
|
+
#else
|
195
|
+
rb_iv_set(self, "@trust", trust);
|
196
|
+
#endif
|
197
|
+
|
198
|
+
return trust;
|
199
|
+
}
|
200
|
+
|
201
|
+
static VALUE
|
202
|
+
ossl_x509store_set_time(VALUE self, VALUE time)
|
203
|
+
{
|
204
|
+
rb_iv_set(self, "@time", time);
|
205
|
+
return time;
|
206
|
+
}
|
207
|
+
|
208
|
+
static VALUE
|
209
|
+
ossl_x509store_add_file(VALUE self, VALUE file)
|
210
|
+
{
|
211
|
+
X509_STORE *store;
|
212
|
+
X509_LOOKUP *lookup;
|
213
|
+
char *path = NULL;
|
214
|
+
|
215
|
+
if(file != Qnil){
|
216
|
+
Check_SafeStr(file);
|
217
|
+
path = RSTRING_PTR(file);
|
218
|
+
}
|
219
|
+
GetX509Store(self, store);
|
220
|
+
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
|
221
|
+
if(lookup == NULL) ossl_raise(eX509StoreError, NULL);
|
222
|
+
if(X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM) != 1){
|
223
|
+
ossl_raise(eX509StoreError, NULL);
|
224
|
+
}
|
225
|
+
|
226
|
+
return self;
|
227
|
+
}
|
228
|
+
|
229
|
+
static VALUE
|
230
|
+
ossl_x509store_add_path(VALUE self, VALUE dir)
|
231
|
+
{
|
232
|
+
X509_STORE *store;
|
233
|
+
X509_LOOKUP *lookup;
|
234
|
+
char *path = NULL;
|
235
|
+
|
236
|
+
if(dir != Qnil){
|
237
|
+
Check_SafeStr(dir);
|
238
|
+
path = RSTRING_PTR(dir);
|
239
|
+
}
|
240
|
+
GetX509Store(self, store);
|
241
|
+
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
|
242
|
+
if(lookup == NULL) ossl_raise(eX509StoreError, NULL);
|
243
|
+
if(X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1){
|
244
|
+
ossl_raise(eX509StoreError, NULL);
|
245
|
+
}
|
246
|
+
|
247
|
+
return self;
|
248
|
+
}
|
249
|
+
|
250
|
+
static VALUE
|
251
|
+
ossl_x509store_set_default_paths(VALUE self)
|
252
|
+
{
|
253
|
+
X509_STORE *store;
|
254
|
+
|
255
|
+
GetX509Store(self, store);
|
256
|
+
if (X509_STORE_set_default_paths(store) != 1){
|
257
|
+
ossl_raise(eX509StoreError, NULL);
|
258
|
+
}
|
259
|
+
|
260
|
+
return Qnil;
|
261
|
+
}
|
262
|
+
|
263
|
+
static VALUE
|
264
|
+
ossl_x509store_add_cert(VALUE self, VALUE arg)
|
265
|
+
{
|
266
|
+
X509_STORE *store;
|
267
|
+
X509 *cert;
|
268
|
+
|
269
|
+
cert = GetX509CertPtr(arg); /* NO NEED TO DUP */
|
270
|
+
GetX509Store(self, store);
|
271
|
+
if (X509_STORE_add_cert(store, cert) != 1){
|
272
|
+
ossl_raise(eX509StoreError, NULL);
|
273
|
+
}
|
274
|
+
|
275
|
+
return self;
|
276
|
+
}
|
277
|
+
|
278
|
+
static VALUE
|
279
|
+
ossl_x509store_add_crl(VALUE self, VALUE arg)
|
280
|
+
{
|
281
|
+
X509_STORE *store;
|
282
|
+
X509_CRL *crl;
|
283
|
+
|
284
|
+
crl = GetX509CRLPtr(arg); /* NO NEED TO DUP */
|
285
|
+
GetX509Store(self, store);
|
286
|
+
if (X509_STORE_add_crl(store, crl) != 1){
|
287
|
+
ossl_raise(eX509StoreError, NULL);
|
288
|
+
}
|
289
|
+
|
290
|
+
return self;
|
291
|
+
}
|
292
|
+
|
293
|
+
static VALUE ossl_x509stctx_get_err(VALUE);
|
294
|
+
static VALUE ossl_x509stctx_get_err_string(VALUE);
|
295
|
+
static VALUE ossl_x509stctx_get_chain(VALUE);
|
296
|
+
|
297
|
+
static VALUE
|
298
|
+
ossl_x509store_verify(int argc, VALUE *argv, VALUE self)
|
299
|
+
{
|
300
|
+
VALUE cert, chain;
|
301
|
+
VALUE ctx, proc, result;
|
302
|
+
|
303
|
+
rb_scan_args(argc, argv, "11", &cert, &chain);
|
304
|
+
ctx = rb_funcall(cX509StoreContext, rb_intern("new"), 3, self, cert, chain);
|
305
|
+
proc = rb_block_given_p() ? rb_block_proc() :
|
306
|
+
rb_iv_get(self, "@verify_callback");
|
307
|
+
rb_iv_set(ctx, "@verify_callback", proc);
|
308
|
+
result = rb_funcall(ctx, rb_intern("verify"), 0);
|
309
|
+
|
310
|
+
rb_iv_set(self, "@error", ossl_x509stctx_get_err(ctx));
|
311
|
+
rb_iv_set(self, "@error_string", ossl_x509stctx_get_err_string(ctx));
|
312
|
+
rb_iv_set(self, "@chain", ossl_x509stctx_get_chain(ctx));
|
313
|
+
|
314
|
+
return result;
|
315
|
+
}
|
316
|
+
|
317
|
+
/*
|
318
|
+
* Public Functions
|
319
|
+
*/
|
320
|
+
static void ossl_x509stctx_free(X509_STORE_CTX*);
|
321
|
+
|
322
|
+
VALUE
|
323
|
+
ossl_x509stctx_new(X509_STORE_CTX *ctx)
|
324
|
+
{
|
325
|
+
VALUE obj;
|
326
|
+
|
327
|
+
WrapX509StCtx(cX509StoreContext, obj, ctx);
|
328
|
+
|
329
|
+
return obj;
|
330
|
+
}
|
331
|
+
|
332
|
+
VALUE
|
333
|
+
ossl_x509stctx_clear_ptr(VALUE obj)
|
334
|
+
{
|
335
|
+
OSSL_Check_Kind(obj, cX509StoreContext);
|
336
|
+
RDATA(obj)->data = NULL;
|
337
|
+
|
338
|
+
return obj;
|
339
|
+
}
|
340
|
+
|
341
|
+
/*
|
342
|
+
* Private functions
|
343
|
+
*/
|
344
|
+
static void
|
345
|
+
ossl_x509stctx_free(X509_STORE_CTX *ctx)
|
346
|
+
{
|
347
|
+
if(ctx->untrusted)
|
348
|
+
sk_X509_pop_free(ctx->untrusted, X509_free);
|
349
|
+
if(ctx->cert)
|
350
|
+
X509_free(ctx->cert);
|
351
|
+
X509_STORE_CTX_free(ctx);
|
352
|
+
}
|
353
|
+
|
354
|
+
static VALUE
|
355
|
+
ossl_x509stctx_alloc(VALUE klass)
|
356
|
+
{
|
357
|
+
X509_STORE_CTX *ctx;
|
358
|
+
VALUE obj;
|
359
|
+
|
360
|
+
if((ctx = X509_STORE_CTX_new()) == NULL){
|
361
|
+
ossl_raise(eX509StoreError, NULL);
|
362
|
+
}
|
363
|
+
WrapX509StCtx(klass, obj, ctx);
|
364
|
+
|
365
|
+
return obj;
|
366
|
+
}
|
367
|
+
|
368
|
+
static VALUE ossl_x509stctx_set_flags(VALUE, VALUE);
|
369
|
+
static VALUE ossl_x509stctx_set_purpose(VALUE, VALUE);
|
370
|
+
static VALUE ossl_x509stctx_set_trust(VALUE, VALUE);
|
371
|
+
static VALUE ossl_x509stctx_set_time(VALUE, VALUE);
|
372
|
+
|
373
|
+
static VALUE
|
374
|
+
ossl_x509stctx_initialize(int argc, VALUE *argv, VALUE self)
|
375
|
+
{
|
376
|
+
VALUE store, cert, chain, t;
|
377
|
+
X509_STORE_CTX *ctx;
|
378
|
+
X509_STORE *x509st;
|
379
|
+
X509 *x509 = NULL;
|
380
|
+
STACK_OF(X509) *x509s = NULL;
|
381
|
+
|
382
|
+
rb_scan_args(argc, argv, "12", &store, &cert, &chain);
|
383
|
+
GetX509StCtx(self, ctx);
|
384
|
+
SafeGetX509Store(store, x509st);
|
385
|
+
if(!NIL_P(cert)) x509 = DupX509CertPtr(cert); /* NEED TO DUP */
|
386
|
+
if(!NIL_P(chain)) x509s = ossl_x509_ary2sk(chain);
|
387
|
+
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
|
388
|
+
if(X509_STORE_CTX_init(ctx, x509st, x509, x509s) != 1){
|
389
|
+
sk_X509_pop_free(x509s, X509_free);
|
390
|
+
ossl_raise(eX509StoreError, NULL);
|
391
|
+
}
|
392
|
+
#else
|
393
|
+
X509_STORE_CTX_init(ctx, x509st, x509, x509s);
|
394
|
+
ossl_x509stctx_set_flags(self, rb_iv_get(store, "@flags"));
|
395
|
+
ossl_x509stctx_set_purpose(self, rb_iv_get(store, "@purpose"));
|
396
|
+
ossl_x509stctx_set_trust(self, rb_iv_get(store, "@trust"));
|
397
|
+
#endif
|
398
|
+
if (!NIL_P(t = rb_iv_get(store, "@time")))
|
399
|
+
ossl_x509stctx_set_time(self, t);
|
400
|
+
rb_iv_set(self, "@verify_callback", rb_iv_get(store, "@verify_callback"));
|
401
|
+
rb_iv_set(self, "@cert", cert);
|
402
|
+
|
403
|
+
return self;
|
404
|
+
}
|
405
|
+
|
406
|
+
static VALUE
|
407
|
+
ossl_x509stctx_verify(VALUE self)
|
408
|
+
{
|
409
|
+
X509_STORE_CTX *ctx;
|
410
|
+
int result;
|
411
|
+
|
412
|
+
GetX509StCtx(self, ctx);
|
413
|
+
X509_STORE_CTX_set_ex_data(ctx, ossl_verify_cb_idx,
|
414
|
+
(void*)rb_iv_get(self, "@verify_callback"));
|
415
|
+
result = X509_verify_cert(ctx);
|
416
|
+
|
417
|
+
return result ? Qtrue : Qfalse;
|
418
|
+
}
|
419
|
+
|
420
|
+
static VALUE
|
421
|
+
ossl_x509stctx_get_chain(VALUE self)
|
422
|
+
{
|
423
|
+
X509_STORE_CTX *ctx;
|
424
|
+
STACK_OF(X509) *chain;
|
425
|
+
X509 *x509;
|
426
|
+
int i, num;
|
427
|
+
VALUE ary;
|
428
|
+
|
429
|
+
GetX509StCtx(self, ctx);
|
430
|
+
if((chain = X509_STORE_CTX_get_chain(ctx)) == NULL){
|
431
|
+
return Qnil;
|
432
|
+
}
|
433
|
+
if((num = sk_X509_num(chain)) < 0){
|
434
|
+
OSSL_Debug("certs in chain < 0???");
|
435
|
+
return rb_ary_new();
|
436
|
+
}
|
437
|
+
ary = rb_ary_new2(num);
|
438
|
+
for(i = 0; i < num; i++) {
|
439
|
+
x509 = sk_X509_value(chain, i);
|
440
|
+
rb_ary_push(ary, ossl_x509_new(x509));
|
441
|
+
}
|
442
|
+
|
443
|
+
return ary;
|
444
|
+
}
|
445
|
+
|
446
|
+
static VALUE
|
447
|
+
ossl_x509stctx_get_err(VALUE self)
|
448
|
+
{
|
449
|
+
X509_STORE_CTX *ctx;
|
450
|
+
|
451
|
+
GetX509StCtx(self, ctx);
|
452
|
+
|
453
|
+
return INT2FIX(X509_STORE_CTX_get_error(ctx));
|
454
|
+
}
|
455
|
+
|
456
|
+
static VALUE
|
457
|
+
ossl_x509stctx_set_error(VALUE self, VALUE err)
|
458
|
+
{
|
459
|
+
X509_STORE_CTX *ctx;
|
460
|
+
|
461
|
+
GetX509StCtx(self, ctx);
|
462
|
+
X509_STORE_CTX_set_error(ctx, NUM2INT(err));
|
463
|
+
|
464
|
+
return err;
|
465
|
+
}
|
466
|
+
|
467
|
+
static VALUE
|
468
|
+
ossl_x509stctx_get_err_string(VALUE self)
|
469
|
+
{
|
470
|
+
X509_STORE_CTX *ctx;
|
471
|
+
long err;
|
472
|
+
|
473
|
+
GetX509StCtx(self, ctx);
|
474
|
+
err = X509_STORE_CTX_get_error(ctx);
|
475
|
+
|
476
|
+
return rb_str_new2(X509_verify_cert_error_string(err));
|
477
|
+
}
|
478
|
+
|
479
|
+
static VALUE
|
480
|
+
ossl_x509stctx_get_err_depth(VALUE self)
|
481
|
+
{
|
482
|
+
X509_STORE_CTX *ctx;
|
483
|
+
|
484
|
+
GetX509StCtx(self, ctx);
|
485
|
+
|
486
|
+
return INT2FIX(X509_STORE_CTX_get_error_depth(ctx));
|
487
|
+
}
|
488
|
+
|
489
|
+
static VALUE
|
490
|
+
ossl_x509stctx_get_curr_cert(VALUE self)
|
491
|
+
{
|
492
|
+
X509_STORE_CTX *ctx;
|
493
|
+
|
494
|
+
GetX509StCtx(self, ctx);
|
495
|
+
|
496
|
+
return ossl_x509_new(X509_STORE_CTX_get_current_cert(ctx));
|
497
|
+
}
|
498
|
+
|
499
|
+
static VALUE
|
500
|
+
ossl_x509stctx_get_curr_crl(VALUE self)
|
501
|
+
{
|
502
|
+
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
|
503
|
+
X509_STORE_CTX *ctx;
|
504
|
+
|
505
|
+
GetX509StCtx(self, ctx);
|
506
|
+
if(!ctx->current_crl) return Qnil;
|
507
|
+
|
508
|
+
return ossl_x509crl_new(ctx->current_crl);
|
509
|
+
#else
|
510
|
+
return Qnil;
|
511
|
+
#endif
|
512
|
+
}
|
513
|
+
|
514
|
+
static VALUE
|
515
|
+
ossl_x509stctx_cleanup(VALUE self)
|
516
|
+
{
|
517
|
+
X509_STORE_CTX *ctx;
|
518
|
+
|
519
|
+
GetX509StCtx(self, ctx);
|
520
|
+
X509_STORE_CTX_cleanup(ctx);
|
521
|
+
|
522
|
+
return self;
|
523
|
+
}
|
524
|
+
|
525
|
+
static VALUE
|
526
|
+
ossl_x509stctx_set_flags(VALUE self, VALUE flags)
|
527
|
+
{
|
528
|
+
X509_STORE_CTX *store;
|
529
|
+
long f = NUM2LONG(flags);
|
530
|
+
|
531
|
+
GetX509StCtx(self, store);
|
532
|
+
X509_STORE_CTX_set_flags(store, f);
|
533
|
+
|
534
|
+
return flags;
|
535
|
+
}
|
536
|
+
|
537
|
+
static VALUE
|
538
|
+
ossl_x509stctx_set_purpose(VALUE self, VALUE purpose)
|
539
|
+
{
|
540
|
+
X509_STORE_CTX *store;
|
541
|
+
long p = NUM2LONG(purpose);
|
542
|
+
|
543
|
+
GetX509StCtx(self, store);
|
544
|
+
X509_STORE_CTX_set_purpose(store, p);
|
545
|
+
|
546
|
+
return purpose;
|
547
|
+
}
|
548
|
+
|
549
|
+
static VALUE
|
550
|
+
ossl_x509stctx_set_trust(VALUE self, VALUE trust)
|
551
|
+
{
|
552
|
+
X509_STORE_CTX *store;
|
553
|
+
long t = NUM2LONG(trust);
|
554
|
+
|
555
|
+
GetX509StCtx(self, store);
|
556
|
+
X509_STORE_CTX_set_trust(store, t);
|
557
|
+
|
558
|
+
return trust;
|
559
|
+
}
|
560
|
+
|
561
|
+
/*
|
562
|
+
* call-seq:
|
563
|
+
* storectx.time = time => time
|
564
|
+
*/
|
565
|
+
static VALUE
|
566
|
+
ossl_x509stctx_set_time(VALUE self, VALUE time)
|
567
|
+
{
|
568
|
+
X509_STORE_CTX *store;
|
569
|
+
long t;
|
570
|
+
|
571
|
+
t = NUM2LONG(rb_Integer(time));
|
572
|
+
GetX509StCtx(self, store);
|
573
|
+
X509_STORE_CTX_set_time(store, 0, t);
|
574
|
+
|
575
|
+
return time;
|
576
|
+
}
|
577
|
+
|
578
|
+
/*
|
579
|
+
* INIT
|
580
|
+
*/
|
581
|
+
void
|
582
|
+
Init_ossl_x509store()
|
583
|
+
{
|
584
|
+
VALUE x509stctx;
|
585
|
+
|
586
|
+
eX509StoreError = rb_define_class_under(mX509, "StoreError", eOSSLError);
|
587
|
+
|
588
|
+
cX509Store = rb_define_class_under(mX509, "Store", rb_cObject);
|
589
|
+
rb_attr(cX509Store, rb_intern("verify_callback"), 1, 0, Qfalse);
|
590
|
+
rb_attr(cX509Store, rb_intern("error"), 1, 0, Qfalse);
|
591
|
+
rb_attr(cX509Store, rb_intern("error_string"), 1, 0, Qfalse);
|
592
|
+
rb_attr(cX509Store, rb_intern("chain"), 1, 0, Qfalse);
|
593
|
+
rb_define_alloc_func(cX509Store, ossl_x509store_alloc);
|
594
|
+
rb_define_method(cX509Store, "initialize", ossl_x509store_initialize, -1);
|
595
|
+
rb_define_method(cX509Store, "verify_callback=", ossl_x509store_set_vfy_cb, 1);
|
596
|
+
rb_define_method(cX509Store, "flags=", ossl_x509store_set_flags, 1);
|
597
|
+
rb_define_method(cX509Store, "purpose=", ossl_x509store_set_purpose, 1);
|
598
|
+
rb_define_method(cX509Store, "trust=", ossl_x509store_set_trust, 1);
|
599
|
+
rb_define_method(cX509Store, "time=", ossl_x509store_set_time, 1);
|
600
|
+
rb_define_method(cX509Store, "add_path", ossl_x509store_add_path, 1);
|
601
|
+
rb_define_method(cX509Store, "add_file", ossl_x509store_add_file, 1);
|
602
|
+
rb_define_method(cX509Store, "set_default_paths", ossl_x509store_set_default_paths, 0);
|
603
|
+
rb_define_method(cX509Store, "add_cert", ossl_x509store_add_cert, 1);
|
604
|
+
rb_define_method(cX509Store, "add_crl", ossl_x509store_add_crl, 1);
|
605
|
+
rb_define_method(cX509Store, "verify", ossl_x509store_verify, -1);
|
606
|
+
|
607
|
+
cX509StoreContext = rb_define_class_under(mX509,"StoreContext",rb_cObject);
|
608
|
+
x509stctx = cX509StoreContext;
|
609
|
+
rb_define_alloc_func(cX509StoreContext, ossl_x509stctx_alloc);
|
610
|
+
rb_define_method(x509stctx,"initialize", ossl_x509stctx_initialize, -1);
|
611
|
+
rb_define_method(x509stctx,"verify", ossl_x509stctx_verify, 0);
|
612
|
+
rb_define_method(x509stctx,"chain", ossl_x509stctx_get_chain,0);
|
613
|
+
rb_define_method(x509stctx,"error", ossl_x509stctx_get_err, 0);
|
614
|
+
rb_define_method(x509stctx,"error=", ossl_x509stctx_set_error, 1);
|
615
|
+
rb_define_method(x509stctx,"error_string",ossl_x509stctx_get_err_string,0);
|
616
|
+
rb_define_method(x509stctx,"error_depth", ossl_x509stctx_get_err_depth, 0);
|
617
|
+
rb_define_method(x509stctx,"current_cert",ossl_x509stctx_get_curr_cert, 0);
|
618
|
+
rb_define_method(x509stctx,"current_crl", ossl_x509stctx_get_curr_crl, 0);
|
619
|
+
rb_define_method(x509stctx,"cleanup", ossl_x509stctx_cleanup, 0);
|
620
|
+
rb_define_method(x509stctx,"flags=", ossl_x509stctx_set_flags, 1);
|
621
|
+
rb_define_method(x509stctx,"purpose=", ossl_x509stctx_set_purpose, 1);
|
622
|
+
rb_define_method(x509stctx,"trust=", ossl_x509stctx_set_trust, 1);
|
623
|
+
rb_define_method(x509stctx,"time=", ossl_x509stctx_set_time, 1);
|
624
|
+
|
625
|
+
}
|