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,20 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: ossl_engine.h 11708 2007-02-12 23:01:19Z shyouhei $
|
3
|
+
* 'OpenSSL for Ruby' project
|
4
|
+
* Copyright (C) 2003 Michal Rokos <m.rokos@sh.cvut.cz>
|
5
|
+
* Copyright (C) 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
6
|
+
* All rights reserved.
|
7
|
+
*/
|
8
|
+
/*
|
9
|
+
* This program is licenced under the same licence as Ruby.
|
10
|
+
* (See the file 'LICENCE'.)
|
11
|
+
*/
|
12
|
+
#if !defined(OSSL_ENGINE_H)
|
13
|
+
#define OSSL_ENGINE_H
|
14
|
+
|
15
|
+
extern VALUE cEngine;
|
16
|
+
extern VALUE eEngineError;
|
17
|
+
|
18
|
+
void Init_ossl_engine(void);
|
19
|
+
|
20
|
+
#endif /* OSSL_ENGINE_H */
|
@@ -0,0 +1,268 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: ossl_hmac.c 28004 2010-05-24 23:58:49Z 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(OPENSSL_NO_HMAC)
|
12
|
+
|
13
|
+
#include "ossl.h"
|
14
|
+
|
15
|
+
#define MakeHMAC(obj, klass, ctx) \
|
16
|
+
obj = Data_Make_Struct(klass, HMAC_CTX, 0, ossl_hmac_free, ctx)
|
17
|
+
#define GetHMAC(obj, ctx) do { \
|
18
|
+
Data_Get_Struct(obj, HMAC_CTX, ctx); \
|
19
|
+
if (!ctx) { \
|
20
|
+
ossl_raise(rb_eRuntimeError, "HMAC wasn't initialized"); \
|
21
|
+
} \
|
22
|
+
} while (0)
|
23
|
+
#define SafeGetHMAC(obj, ctx) do { \
|
24
|
+
OSSL_Check_Kind(obj, cHMAC); \
|
25
|
+
GetHMAC(obj, ctx); \
|
26
|
+
} while (0)
|
27
|
+
|
28
|
+
/*
|
29
|
+
* Classes
|
30
|
+
*/
|
31
|
+
VALUE cHMAC;
|
32
|
+
VALUE eHMACError;
|
33
|
+
|
34
|
+
/*
|
35
|
+
* Public
|
36
|
+
*/
|
37
|
+
|
38
|
+
/*
|
39
|
+
* Private
|
40
|
+
*/
|
41
|
+
static void
|
42
|
+
ossl_hmac_free(HMAC_CTX *ctx)
|
43
|
+
{
|
44
|
+
HMAC_CTX_cleanup(ctx);
|
45
|
+
ruby_xfree(ctx);
|
46
|
+
}
|
47
|
+
|
48
|
+
static VALUE
|
49
|
+
ossl_hmac_alloc(VALUE klass)
|
50
|
+
{
|
51
|
+
HMAC_CTX *ctx;
|
52
|
+
VALUE obj;
|
53
|
+
|
54
|
+
MakeHMAC(obj, klass, ctx);
|
55
|
+
HMAC_CTX_init(ctx);
|
56
|
+
|
57
|
+
return obj;
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
/*
|
62
|
+
* call-seq:
|
63
|
+
* HMAC.new(key, digest) -> hmac
|
64
|
+
*
|
65
|
+
*/
|
66
|
+
static VALUE
|
67
|
+
ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
|
68
|
+
{
|
69
|
+
HMAC_CTX *ctx;
|
70
|
+
|
71
|
+
StringValue(key);
|
72
|
+
GetHMAC(self, ctx);
|
73
|
+
HMAC_Init_ex(ctx, RSTRING_PTR(key), RSTRING_LEN(key),
|
74
|
+
GetDigestPtr(digest), NULL);
|
75
|
+
|
76
|
+
return self;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE
|
80
|
+
ossl_hmac_copy(VALUE self, VALUE other)
|
81
|
+
{
|
82
|
+
HMAC_CTX *ctx1, *ctx2;
|
83
|
+
|
84
|
+
rb_check_frozen(self);
|
85
|
+
if (self == other) return self;
|
86
|
+
|
87
|
+
GetHMAC(self, ctx1);
|
88
|
+
SafeGetHMAC(other, ctx2);
|
89
|
+
|
90
|
+
HMAC_CTX_copy(ctx1, ctx2);
|
91
|
+
return self;
|
92
|
+
}
|
93
|
+
|
94
|
+
/*
|
95
|
+
* call-seq:
|
96
|
+
* hmac.update(string) -> self
|
97
|
+
*
|
98
|
+
*/
|
99
|
+
static VALUE
|
100
|
+
ossl_hmac_update(VALUE self, VALUE data)
|
101
|
+
{
|
102
|
+
HMAC_CTX *ctx;
|
103
|
+
|
104
|
+
StringValue(data);
|
105
|
+
GetHMAC(self, ctx);
|
106
|
+
HMAC_Update(ctx, RSTRING_PTR(data), RSTRING_LEN(data));
|
107
|
+
|
108
|
+
return self;
|
109
|
+
}
|
110
|
+
|
111
|
+
static void
|
112
|
+
hmac_final(HMAC_CTX *ctx, char **buf, int *buf_len)
|
113
|
+
{
|
114
|
+
HMAC_CTX final;
|
115
|
+
|
116
|
+
HMAC_CTX_copy(&final, ctx);
|
117
|
+
if (!(*buf = OPENSSL_malloc(HMAC_size(&final)))) {
|
118
|
+
HMAC_CTX_cleanup(&final);
|
119
|
+
OSSL_Debug("Allocating %d mem", HMAC_size(&final));
|
120
|
+
ossl_raise(eHMACError, "Cannot allocate memory for hmac");
|
121
|
+
}
|
122
|
+
HMAC_Final(&final, *buf, buf_len);
|
123
|
+
HMAC_CTX_cleanup(&final);
|
124
|
+
}
|
125
|
+
|
126
|
+
/*
|
127
|
+
* call-seq:
|
128
|
+
* hmac.digest -> aString
|
129
|
+
*
|
130
|
+
*/
|
131
|
+
static VALUE
|
132
|
+
ossl_hmac_digest(VALUE self)
|
133
|
+
{
|
134
|
+
HMAC_CTX *ctx;
|
135
|
+
char *buf;
|
136
|
+
int buf_len;
|
137
|
+
VALUE digest;
|
138
|
+
|
139
|
+
GetHMAC(self, ctx);
|
140
|
+
hmac_final(ctx, &buf, &buf_len);
|
141
|
+
digest = ossl_buf2str(buf, buf_len);
|
142
|
+
|
143
|
+
return digest;
|
144
|
+
}
|
145
|
+
|
146
|
+
/*
|
147
|
+
* call-seq:
|
148
|
+
* hmac.hexdigest -> aString
|
149
|
+
*
|
150
|
+
*/
|
151
|
+
static VALUE
|
152
|
+
ossl_hmac_hexdigest(VALUE self)
|
153
|
+
{
|
154
|
+
HMAC_CTX *ctx;
|
155
|
+
char *buf, *hexbuf;
|
156
|
+
int buf_len;
|
157
|
+
VALUE hexdigest;
|
158
|
+
|
159
|
+
GetHMAC(self, ctx);
|
160
|
+
hmac_final(ctx, &buf, &buf_len);
|
161
|
+
if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) {
|
162
|
+
OPENSSL_free(buf);
|
163
|
+
ossl_raise(eHMACError, "Memory alloc error");
|
164
|
+
}
|
165
|
+
OPENSSL_free(buf);
|
166
|
+
hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
|
167
|
+
|
168
|
+
return hexdigest;
|
169
|
+
}
|
170
|
+
|
171
|
+
/*
|
172
|
+
* call-seq:
|
173
|
+
* hmac.reset -> self
|
174
|
+
*
|
175
|
+
*/
|
176
|
+
static VALUE
|
177
|
+
ossl_hmac_reset(VALUE self)
|
178
|
+
{
|
179
|
+
HMAC_CTX *ctx;
|
180
|
+
|
181
|
+
GetHMAC(self, ctx);
|
182
|
+
HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
|
183
|
+
|
184
|
+
return self;
|
185
|
+
}
|
186
|
+
|
187
|
+
/*
|
188
|
+
* call-seq:
|
189
|
+
* HMAC.digest(digest, key, data) -> aString
|
190
|
+
*
|
191
|
+
*/
|
192
|
+
static VALUE
|
193
|
+
ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
194
|
+
{
|
195
|
+
char *buf;
|
196
|
+
int buf_len;
|
197
|
+
|
198
|
+
StringValue(key);
|
199
|
+
StringValue(data);
|
200
|
+
buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key),
|
201
|
+
RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
|
202
|
+
|
203
|
+
return rb_str_new(buf, buf_len);
|
204
|
+
}
|
205
|
+
|
206
|
+
/*
|
207
|
+
* call-seq:
|
208
|
+
* HMAC.digest(digest, key, data) -> aString
|
209
|
+
*
|
210
|
+
*/
|
211
|
+
static VALUE
|
212
|
+
ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
213
|
+
{
|
214
|
+
char *buf, *hexbuf;
|
215
|
+
int buf_len;
|
216
|
+
VALUE hexdigest;
|
217
|
+
|
218
|
+
StringValue(key);
|
219
|
+
StringValue(data);
|
220
|
+
|
221
|
+
buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key),
|
222
|
+
RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
|
223
|
+
if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) {
|
224
|
+
ossl_raise(eHMACError, "Cannot convert buf to hexbuf");
|
225
|
+
}
|
226
|
+
hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
|
227
|
+
|
228
|
+
return hexdigest;
|
229
|
+
}
|
230
|
+
|
231
|
+
/*
|
232
|
+
* INIT
|
233
|
+
*/
|
234
|
+
void
|
235
|
+
Init_ossl_hmac()
|
236
|
+
{
|
237
|
+
#if 0 /* let rdoc know about mOSSL */
|
238
|
+
mOSSL = rb_define_module("OpenSSL");
|
239
|
+
#endif
|
240
|
+
|
241
|
+
eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError);
|
242
|
+
|
243
|
+
cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject);
|
244
|
+
|
245
|
+
rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
|
246
|
+
rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3);
|
247
|
+
rb_define_singleton_method(cHMAC, "hexdigest", ossl_hmac_s_hexdigest, 3);
|
248
|
+
|
249
|
+
rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
|
250
|
+
rb_define_copy_func(cHMAC, ossl_hmac_copy);
|
251
|
+
|
252
|
+
rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
|
253
|
+
rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
|
254
|
+
rb_define_alias(cHMAC, "<<", "update");
|
255
|
+
rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0);
|
256
|
+
rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0);
|
257
|
+
rb_define_alias(cHMAC, "inspect", "hexdigest");
|
258
|
+
rb_define_alias(cHMAC, "to_s", "hexdigest");
|
259
|
+
}
|
260
|
+
|
261
|
+
#else /* NO_HMAC */
|
262
|
+
# warning >>> OpenSSL is compiled without HMAC support <<<
|
263
|
+
void
|
264
|
+
Init_ossl_hmac()
|
265
|
+
{
|
266
|
+
rb_warning("HMAC will NOT be avaible: OpenSSL is compiled without HMAC.");
|
267
|
+
}
|
268
|
+
#endif /* NO_HMAC */
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: ossl_hmac.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_HMAC_H_)
|
12
|
+
#define _OSSL_HMAC_H_
|
13
|
+
|
14
|
+
extern VALUE cHMAC;
|
15
|
+
extern VALUE eHMACError;
|
16
|
+
|
17
|
+
void Init_ossl_hmac(void);
|
18
|
+
|
19
|
+
#endif /* _OSSL_HMAC_H_ */
|
@@ -0,0 +1,257 @@
|
|
1
|
+
/*
|
2
|
+
* $Id$
|
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 WrapSPKI(klass, obj, spki) do { \
|
14
|
+
if (!spki) { \
|
15
|
+
ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \
|
16
|
+
} \
|
17
|
+
obj = Data_Wrap_Struct(klass, 0, NETSCAPE_SPKI_free, spki); \
|
18
|
+
} while (0)
|
19
|
+
#define GetSPKI(obj, spki) do { \
|
20
|
+
Data_Get_Struct(obj, NETSCAPE_SPKI, spki); \
|
21
|
+
if (!spki) { \
|
22
|
+
ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \
|
23
|
+
} \
|
24
|
+
} while (0)
|
25
|
+
|
26
|
+
/*
|
27
|
+
* Classes
|
28
|
+
*/
|
29
|
+
VALUE mNetscape;
|
30
|
+
VALUE cSPKI;
|
31
|
+
VALUE eSPKIError;
|
32
|
+
|
33
|
+
/*
|
34
|
+
* Public functions
|
35
|
+
*/
|
36
|
+
|
37
|
+
/*
|
38
|
+
* Private functions
|
39
|
+
*/
|
40
|
+
static VALUE
|
41
|
+
ossl_spki_alloc(VALUE klass)
|
42
|
+
{
|
43
|
+
NETSCAPE_SPKI *spki;
|
44
|
+
VALUE obj;
|
45
|
+
|
46
|
+
if (!(spki = NETSCAPE_SPKI_new())) {
|
47
|
+
ossl_raise(eSPKIError, NULL);
|
48
|
+
}
|
49
|
+
WrapSPKI(klass, obj, spki);
|
50
|
+
|
51
|
+
return obj;
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE
|
55
|
+
ossl_spki_initialize(int argc, VALUE *argv, VALUE self)
|
56
|
+
{
|
57
|
+
NETSCAPE_SPKI *spki;
|
58
|
+
VALUE buffer;
|
59
|
+
const unsigned char *p;
|
60
|
+
|
61
|
+
if (rb_scan_args(argc, argv, "01", &buffer) == 0) {
|
62
|
+
return self;
|
63
|
+
}
|
64
|
+
StringValue(buffer);
|
65
|
+
if (!(spki = NETSCAPE_SPKI_b64_decode(RSTRING_PTR(buffer), -1))) {
|
66
|
+
p = (const unsigned char *)RSTRING_PTR(buffer);
|
67
|
+
if (!(spki = d2i_NETSCAPE_SPKI(NULL, &p, RSTRING_LEN(buffer)))) {
|
68
|
+
ossl_raise(eSPKIError, NULL);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
NETSCAPE_SPKI_free(DATA_PTR(self));
|
72
|
+
DATA_PTR(self) = spki;
|
73
|
+
ERR_clear_error();
|
74
|
+
|
75
|
+
return self;
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE
|
79
|
+
ossl_spki_to_der(VALUE self)
|
80
|
+
{
|
81
|
+
NETSCAPE_SPKI *spki;
|
82
|
+
VALUE str;
|
83
|
+
long len;
|
84
|
+
unsigned char *p;
|
85
|
+
|
86
|
+
GetSPKI(self, spki);
|
87
|
+
if ((len = i2d_NETSCAPE_SPKI(spki, NULL)) <= 0)
|
88
|
+
ossl_raise(eX509CertError, NULL);
|
89
|
+
str = rb_str_new(0, len);
|
90
|
+
p = RSTRING_PTR(str);
|
91
|
+
if (i2d_NETSCAPE_SPKI(spki, &p) <= 0)
|
92
|
+
ossl_raise(eX509CertError, NULL);
|
93
|
+
ossl_str_adjust(str, p);
|
94
|
+
|
95
|
+
return str;
|
96
|
+
}
|
97
|
+
|
98
|
+
static VALUE
|
99
|
+
ossl_spki_to_pem(VALUE self)
|
100
|
+
{
|
101
|
+
NETSCAPE_SPKI *spki;
|
102
|
+
char *data;
|
103
|
+
VALUE str;
|
104
|
+
|
105
|
+
GetSPKI(self, spki);
|
106
|
+
if (!(data = NETSCAPE_SPKI_b64_encode(spki))) {
|
107
|
+
ossl_raise(eSPKIError, NULL);
|
108
|
+
}
|
109
|
+
str = ossl_buf2str(data, strlen(data));
|
110
|
+
|
111
|
+
return str;
|
112
|
+
}
|
113
|
+
|
114
|
+
static VALUE
|
115
|
+
ossl_spki_print(VALUE self)
|
116
|
+
{
|
117
|
+
NETSCAPE_SPKI *spki;
|
118
|
+
BIO *out;
|
119
|
+
BUF_MEM *buf;
|
120
|
+
VALUE str;
|
121
|
+
|
122
|
+
GetSPKI(self, spki);
|
123
|
+
if (!(out = BIO_new(BIO_s_mem()))) {
|
124
|
+
ossl_raise(eSPKIError, NULL);
|
125
|
+
}
|
126
|
+
if (!NETSCAPE_SPKI_print(out, spki)) {
|
127
|
+
BIO_free(out);
|
128
|
+
ossl_raise(eSPKIError, NULL);
|
129
|
+
}
|
130
|
+
BIO_get_mem_ptr(out, &buf);
|
131
|
+
str = rb_str_new(buf->data, buf->length);
|
132
|
+
BIO_free(out);
|
133
|
+
|
134
|
+
return str;
|
135
|
+
}
|
136
|
+
|
137
|
+
static VALUE
|
138
|
+
ossl_spki_get_public_key(VALUE self)
|
139
|
+
{
|
140
|
+
NETSCAPE_SPKI *spki;
|
141
|
+
EVP_PKEY *pkey;
|
142
|
+
|
143
|
+
GetSPKI(self, spki);
|
144
|
+
if (!(pkey = NETSCAPE_SPKI_get_pubkey(spki))) { /* adds an reference */
|
145
|
+
ossl_raise(eSPKIError, NULL);
|
146
|
+
}
|
147
|
+
|
148
|
+
return ossl_pkey_new(pkey); /* NO DUP - OK */
|
149
|
+
}
|
150
|
+
|
151
|
+
static VALUE
|
152
|
+
ossl_spki_set_public_key(VALUE self, VALUE key)
|
153
|
+
{
|
154
|
+
NETSCAPE_SPKI *spki;
|
155
|
+
|
156
|
+
GetSPKI(self, spki);
|
157
|
+
if (!NETSCAPE_SPKI_set_pubkey(spki, GetPKeyPtr(key))) { /* NO NEED TO DUP */
|
158
|
+
ossl_raise(eSPKIError, NULL);
|
159
|
+
}
|
160
|
+
|
161
|
+
return key;
|
162
|
+
}
|
163
|
+
|
164
|
+
static VALUE
|
165
|
+
ossl_spki_get_challenge(VALUE self)
|
166
|
+
{
|
167
|
+
NETSCAPE_SPKI *spki;
|
168
|
+
|
169
|
+
GetSPKI(self, spki);
|
170
|
+
if (spki->spkac->challenge->length <= 0) {
|
171
|
+
OSSL_Debug("Challenge.length <= 0?");
|
172
|
+
return rb_str_new(0, 0);
|
173
|
+
}
|
174
|
+
|
175
|
+
return rb_str_new(spki->spkac->challenge->data,
|
176
|
+
spki->spkac->challenge->length);
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE
|
180
|
+
ossl_spki_set_challenge(VALUE self, VALUE str)
|
181
|
+
{
|
182
|
+
NETSCAPE_SPKI *spki;
|
183
|
+
|
184
|
+
StringValue(str);
|
185
|
+
GetSPKI(self, spki);
|
186
|
+
if (!ASN1_STRING_set(spki->spkac->challenge, RSTRING_PTR(str),
|
187
|
+
RSTRING_LEN(str))) {
|
188
|
+
ossl_raise(eSPKIError, NULL);
|
189
|
+
}
|
190
|
+
|
191
|
+
return str;
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE
|
195
|
+
ossl_spki_sign(VALUE self, VALUE key, VALUE digest)
|
196
|
+
{
|
197
|
+
NETSCAPE_SPKI *spki;
|
198
|
+
EVP_PKEY *pkey;
|
199
|
+
const EVP_MD *md;
|
200
|
+
|
201
|
+
pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
|
202
|
+
md = GetDigestPtr(digest);
|
203
|
+
GetSPKI(self, spki);
|
204
|
+
if (!NETSCAPE_SPKI_sign(spki, pkey, md)) {
|
205
|
+
ossl_raise(eSPKIError, NULL);
|
206
|
+
}
|
207
|
+
|
208
|
+
return self;
|
209
|
+
}
|
210
|
+
|
211
|
+
/*
|
212
|
+
* Checks that cert signature is made with PRIVversion of this PUBLIC 'key'
|
213
|
+
*/
|
214
|
+
static VALUE
|
215
|
+
ossl_spki_verify(VALUE self, VALUE key)
|
216
|
+
{
|
217
|
+
NETSCAPE_SPKI *spki;
|
218
|
+
|
219
|
+
GetSPKI(self, spki);
|
220
|
+
switch (NETSCAPE_SPKI_verify(spki, GetPKeyPtr(key))) { /* NO NEED TO DUP */
|
221
|
+
case 0:
|
222
|
+
return Qfalse;
|
223
|
+
case 1:
|
224
|
+
return Qtrue;
|
225
|
+
default:
|
226
|
+
ossl_raise(eSPKIError, NULL);
|
227
|
+
}
|
228
|
+
return Qnil; /* dummy */
|
229
|
+
}
|
230
|
+
|
231
|
+
/*
|
232
|
+
* NETSCAPE_SPKI init
|
233
|
+
*/
|
234
|
+
void
|
235
|
+
Init_ossl_ns_spki()
|
236
|
+
{
|
237
|
+
mNetscape = rb_define_module_under(mOSSL, "Netscape");
|
238
|
+
|
239
|
+
eSPKIError = rb_define_class_under(mNetscape, "SPKIError", eOSSLError);
|
240
|
+
|
241
|
+
cSPKI = rb_define_class_under(mNetscape, "SPKI", rb_cObject);
|
242
|
+
|
243
|
+
rb_define_alloc_func(cSPKI, ossl_spki_alloc);
|
244
|
+
rb_define_method(cSPKI, "initialize", ossl_spki_initialize, -1);
|
245
|
+
|
246
|
+
rb_define_method(cSPKI, "to_der", ossl_spki_to_der, 0);
|
247
|
+
rb_define_method(cSPKI, "to_pem", ossl_spki_to_pem, 0);
|
248
|
+
rb_define_alias(cSPKI, "to_s", "to_pem");
|
249
|
+
rb_define_method(cSPKI, "to_text", ossl_spki_print, 0);
|
250
|
+
rb_define_method(cSPKI, "public_key", ossl_spki_get_public_key, 0);
|
251
|
+
rb_define_method(cSPKI, "public_key=", ossl_spki_set_public_key, 1);
|
252
|
+
rb_define_method(cSPKI, "sign", ossl_spki_sign, 2);
|
253
|
+
rb_define_method(cSPKI, "verify", ossl_spki_verify, 1);
|
254
|
+
rb_define_method(cSPKI, "challenge", ossl_spki_get_challenge, 0);
|
255
|
+
rb_define_method(cSPKI, "challenge=", ossl_spki_set_challenge, 1);
|
256
|
+
}
|
257
|
+
|