openssl-custom 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/BSDL +22 -0
- data/CONTRIBUTING.md +132 -0
- data/History.md +485 -0
- data/LICENSE.txt +56 -0
- data/README.md +66 -0
- data/ext/openssl/extconf.rb +190 -0
- data/ext/openssl/openssl_missing.c +106 -0
- data/ext/openssl/openssl_missing.h +257 -0
- data/ext/openssl/ossl.c +1282 -0
- data/ext/openssl/ossl.h +181 -0
- data/ext/openssl/ossl_asn1.c +1878 -0
- data/ext/openssl/ossl_asn1.h +62 -0
- data/ext/openssl/ossl_bio.c +42 -0
- data/ext/openssl/ossl_bio.h +16 -0
- data/ext/openssl/ossl_bn.c +1270 -0
- data/ext/openssl/ossl_bn.h +26 -0
- data/ext/openssl/ossl_cipher.c +1075 -0
- data/ext/openssl/ossl_cipher.h +20 -0
- data/ext/openssl/ossl_config.c +89 -0
- data/ext/openssl/ossl_config.h +19 -0
- data/ext/openssl/ossl_digest.c +425 -0
- data/ext/openssl/ossl_digest.h +20 -0
- data/ext/openssl/ossl_engine.c +567 -0
- data/ext/openssl/ossl_engine.h +19 -0
- data/ext/openssl/ossl_hmac.c +389 -0
- data/ext/openssl/ossl_hmac.h +18 -0
- data/ext/openssl/ossl_kdf.c +303 -0
- data/ext/openssl/ossl_kdf.h +6 -0
- data/ext/openssl/ossl_ns_spki.c +405 -0
- data/ext/openssl/ossl_ns_spki.h +19 -0
- data/ext/openssl/ossl_ocsp.c +2013 -0
- data/ext/openssl/ossl_ocsp.h +23 -0
- data/ext/openssl/ossl_pkcs12.c +257 -0
- data/ext/openssl/ossl_pkcs12.h +13 -0
- data/ext/openssl/ossl_pkcs7.c +1098 -0
- data/ext/openssl/ossl_pkcs7.h +36 -0
- data/ext/openssl/ossl_pkey.c +673 -0
- data/ext/openssl/ossl_pkey.h +241 -0
- data/ext/openssl/ossl_pkey_dh.c +650 -0
- data/ext/openssl/ossl_pkey_dsa.c +664 -0
- data/ext/openssl/ossl_pkey_ec.c +1827 -0
- data/ext/openssl/ossl_pkey_rsa.c +966 -0
- data/ext/openssl/ossl_rand.c +200 -0
- data/ext/openssl/ossl_rand.h +18 -0
- data/ext/openssl/ossl_ssl.c +3080 -0
- data/ext/openssl/ossl_ssl.h +36 -0
- data/ext/openssl/ossl_ssl_session.c +332 -0
- data/ext/openssl/ossl_ts.c +1524 -0
- data/ext/openssl/ossl_ts.h +16 -0
- data/ext/openssl/ossl_x509.c +262 -0
- data/ext/openssl/ossl_x509.h +115 -0
- data/ext/openssl/ossl_x509attr.c +324 -0
- data/ext/openssl/ossl_x509cert.c +846 -0
- data/ext/openssl/ossl_x509crl.c +542 -0
- data/ext/openssl/ossl_x509ext.c +491 -0
- data/ext/openssl/ossl_x509name.c +590 -0
- data/ext/openssl/ossl_x509req.c +441 -0
- data/ext/openssl/ossl_x509revoked.c +300 -0
- data/ext/openssl/ossl_x509store.c +902 -0
- data/ext/openssl/ruby_missing.h +24 -0
- data/lib/openssl/bn.rb +40 -0
- data/lib/openssl/buffering.rb +478 -0
- data/lib/openssl/cipher.rb +67 -0
- data/lib/openssl/config.rb +501 -0
- data/lib/openssl/digest.rb +73 -0
- data/lib/openssl/hmac.rb +13 -0
- data/lib/openssl/marshal.rb +30 -0
- data/lib/openssl/pkcs5.rb +22 -0
- data/lib/openssl/pkey.rb +42 -0
- data/lib/openssl/ssl.rb +542 -0
- data/lib/openssl/version.rb +5 -0
- data/lib/openssl/x509.rb +369 -0
- data/lib/openssl.rb +38 -0
- metadata +196 -0
@@ -0,0 +1,20 @@
|
|
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
|
+
#if !defined(_OSSL_CIPHER_H_)
|
11
|
+
#define _OSSL_CIPHER_H_
|
12
|
+
|
13
|
+
extern VALUE cCipher;
|
14
|
+
extern VALUE eCipherError;
|
15
|
+
|
16
|
+
const EVP_CIPHER *ossl_evp_get_cipherbyname(VALUE);
|
17
|
+
VALUE ossl_cipher_new(const EVP_CIPHER *);
|
18
|
+
void Init_ossl_cipher(void);
|
19
|
+
|
20
|
+
#endif /* _OSSL_CIPHER_H_ */
|
@@ -0,0 +1,89 @@
|
|
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
|
+
|
13
|
+
/*
|
14
|
+
* Classes
|
15
|
+
*/
|
16
|
+
VALUE cConfig;
|
17
|
+
/* Document-class: OpenSSL::ConfigError
|
18
|
+
*
|
19
|
+
* General error for openssl library configuration files. Including formatting,
|
20
|
+
* parsing errors, etc.
|
21
|
+
*/
|
22
|
+
VALUE eConfigError;
|
23
|
+
|
24
|
+
/*
|
25
|
+
* Public
|
26
|
+
*/
|
27
|
+
|
28
|
+
/*
|
29
|
+
* DupConfigPtr is a public C-level function for getting OpenSSL CONF struct
|
30
|
+
* from an OpenSSL::Config(eConfig) instance. We decided to implement
|
31
|
+
* OpenSSL::Config in Ruby level but we need to pass native CONF struct for
|
32
|
+
* some OpenSSL features such as X509V3_EXT_*.
|
33
|
+
*/
|
34
|
+
CONF *
|
35
|
+
DupConfigPtr(VALUE obj)
|
36
|
+
{
|
37
|
+
CONF *conf;
|
38
|
+
VALUE str;
|
39
|
+
BIO *bio;
|
40
|
+
long eline = -1;
|
41
|
+
|
42
|
+
OSSL_Check_Kind(obj, cConfig);
|
43
|
+
str = rb_funcall(obj, rb_intern("to_s"), 0);
|
44
|
+
bio = ossl_obj2bio(&str);
|
45
|
+
conf = NCONF_new(NULL);
|
46
|
+
if(!conf){
|
47
|
+
BIO_free(bio);
|
48
|
+
ossl_raise(eConfigError, NULL);
|
49
|
+
}
|
50
|
+
if(!NCONF_load_bio(conf, bio, &eline)){
|
51
|
+
BIO_free(bio);
|
52
|
+
NCONF_free(conf);
|
53
|
+
if (eline <= 0)
|
54
|
+
ossl_raise(eConfigError, "wrong config format");
|
55
|
+
else
|
56
|
+
ossl_raise(eConfigError, "error in line %d", eline);
|
57
|
+
}
|
58
|
+
BIO_free(bio);
|
59
|
+
|
60
|
+
return conf;
|
61
|
+
}
|
62
|
+
|
63
|
+
/* Document-const: DEFAULT_CONFIG_FILE
|
64
|
+
*
|
65
|
+
* The default system configuration file for openssl
|
66
|
+
*/
|
67
|
+
|
68
|
+
/*
|
69
|
+
* INIT
|
70
|
+
*/
|
71
|
+
void
|
72
|
+
Init_ossl_config(void)
|
73
|
+
{
|
74
|
+
char *default_config_file;
|
75
|
+
|
76
|
+
#if 0
|
77
|
+
mOSSL = rb_define_module("OpenSSL");
|
78
|
+
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
79
|
+
#endif
|
80
|
+
|
81
|
+
eConfigError = rb_define_class_under(mOSSL, "ConfigError", eOSSLError);
|
82
|
+
cConfig = rb_define_class_under(mOSSL, "Config", rb_cObject);
|
83
|
+
|
84
|
+
default_config_file = CONF_get1_default_config_file();
|
85
|
+
rb_define_const(cConfig, "DEFAULT_CONFIG_FILE",
|
86
|
+
rb_str_new2(default_config_file));
|
87
|
+
OPENSSL_free(default_config_file);
|
88
|
+
/* methods are defined by openssl/config.rb */
|
89
|
+
}
|
@@ -0,0 +1,19 @@
|
|
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
|
+
#if !defined(_OSSL_CONFIG_H_)
|
11
|
+
#define _OSSL_CONFIG_H_
|
12
|
+
|
13
|
+
extern VALUE cConfig;
|
14
|
+
extern VALUE eConfigError;
|
15
|
+
|
16
|
+
CONF* DupConfigPtr(VALUE obj);
|
17
|
+
void Init_ossl_config(void);
|
18
|
+
|
19
|
+
#endif /* _OSSL_CONFIG_H_ */
|
@@ -0,0 +1,425 @@
|
|
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 GetDigest(obj, ctx) do { \
|
13
|
+
TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_digest_type, (ctx)); \
|
14
|
+
if (!(ctx)) { \
|
15
|
+
ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
|
16
|
+
} \
|
17
|
+
} while (0)
|
18
|
+
|
19
|
+
/*
|
20
|
+
* Classes
|
21
|
+
*/
|
22
|
+
VALUE cDigest;
|
23
|
+
VALUE eDigestError;
|
24
|
+
|
25
|
+
static VALUE ossl_digest_alloc(VALUE klass);
|
26
|
+
|
27
|
+
static void
|
28
|
+
ossl_digest_free(void *ctx)
|
29
|
+
{
|
30
|
+
EVP_MD_CTX_destroy(ctx);
|
31
|
+
}
|
32
|
+
|
33
|
+
static const rb_data_type_t ossl_digest_type = {
|
34
|
+
"OpenSSL/Digest",
|
35
|
+
{
|
36
|
+
0, ossl_digest_free,
|
37
|
+
},
|
38
|
+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
39
|
+
};
|
40
|
+
|
41
|
+
/*
|
42
|
+
* Public
|
43
|
+
*/
|
44
|
+
const EVP_MD *
|
45
|
+
ossl_evp_get_digestbyname(VALUE obj)
|
46
|
+
{
|
47
|
+
const EVP_MD *md;
|
48
|
+
ASN1_OBJECT *oid = NULL;
|
49
|
+
|
50
|
+
if (RB_TYPE_P(obj, T_STRING)) {
|
51
|
+
const char *name = StringValueCStr(obj);
|
52
|
+
|
53
|
+
md = EVP_get_digestbyname(name);
|
54
|
+
if (!md) {
|
55
|
+
oid = OBJ_txt2obj(name, 0);
|
56
|
+
md = EVP_get_digestbyobj(oid);
|
57
|
+
ASN1_OBJECT_free(oid);
|
58
|
+
}
|
59
|
+
if(!md)
|
60
|
+
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%"PRIsVALUE").", obj);
|
61
|
+
} else {
|
62
|
+
EVP_MD_CTX *ctx;
|
63
|
+
|
64
|
+
GetDigest(obj, ctx);
|
65
|
+
|
66
|
+
md = EVP_MD_CTX_md(ctx);
|
67
|
+
}
|
68
|
+
|
69
|
+
return md;
|
70
|
+
}
|
71
|
+
|
72
|
+
VALUE
|
73
|
+
ossl_digest_new(const EVP_MD *md)
|
74
|
+
{
|
75
|
+
VALUE ret;
|
76
|
+
EVP_MD_CTX *ctx;
|
77
|
+
|
78
|
+
ret = ossl_digest_alloc(cDigest);
|
79
|
+
ctx = EVP_MD_CTX_new();
|
80
|
+
if (!ctx)
|
81
|
+
ossl_raise(eDigestError, "EVP_MD_CTX_new");
|
82
|
+
RTYPEDDATA_DATA(ret) = ctx;
|
83
|
+
|
84
|
+
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
85
|
+
ossl_raise(eDigestError, "Digest initialization failed");
|
86
|
+
|
87
|
+
return ret;
|
88
|
+
}
|
89
|
+
|
90
|
+
/*
|
91
|
+
* Private
|
92
|
+
*/
|
93
|
+
static VALUE
|
94
|
+
ossl_digest_alloc(VALUE klass)
|
95
|
+
{
|
96
|
+
return TypedData_Wrap_Struct(klass, &ossl_digest_type, 0);
|
97
|
+
}
|
98
|
+
|
99
|
+
VALUE ossl_digest_update(VALUE, VALUE);
|
100
|
+
|
101
|
+
/*
|
102
|
+
* call-seq:
|
103
|
+
* Digest.new(string [, data]) -> Digest
|
104
|
+
*
|
105
|
+
* Creates a Digest instance based on _string_, which is either the ln
|
106
|
+
* (long name) or sn (short name) of a supported digest algorithm.
|
107
|
+
*
|
108
|
+
* If _data_ (a String) is given, it is used as the initial input to the
|
109
|
+
* Digest instance, i.e.
|
110
|
+
*
|
111
|
+
* digest = OpenSSL::Digest.new('sha256', 'digestdata')
|
112
|
+
*
|
113
|
+
* is equivalent to
|
114
|
+
*
|
115
|
+
* digest = OpenSSL::Digest.new('sha256')
|
116
|
+
* digest.update('digestdata')
|
117
|
+
*/
|
118
|
+
static VALUE
|
119
|
+
ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
|
120
|
+
{
|
121
|
+
EVP_MD_CTX *ctx;
|
122
|
+
const EVP_MD *md;
|
123
|
+
VALUE type, data;
|
124
|
+
|
125
|
+
rb_scan_args(argc, argv, "11", &type, &data);
|
126
|
+
md = ossl_evp_get_digestbyname(type);
|
127
|
+
if (!NIL_P(data)) StringValue(data);
|
128
|
+
|
129
|
+
TypedData_Get_Struct(self, EVP_MD_CTX, &ossl_digest_type, ctx);
|
130
|
+
if (!ctx) {
|
131
|
+
RTYPEDDATA_DATA(self) = ctx = EVP_MD_CTX_new();
|
132
|
+
if (!ctx)
|
133
|
+
ossl_raise(eDigestError, "EVP_MD_CTX_new");
|
134
|
+
}
|
135
|
+
|
136
|
+
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
137
|
+
ossl_raise(eDigestError, "Digest initialization failed");
|
138
|
+
|
139
|
+
if (!NIL_P(data)) return ossl_digest_update(self, data);
|
140
|
+
return self;
|
141
|
+
}
|
142
|
+
|
143
|
+
static VALUE
|
144
|
+
ossl_digest_copy(VALUE self, VALUE other)
|
145
|
+
{
|
146
|
+
EVP_MD_CTX *ctx1, *ctx2;
|
147
|
+
|
148
|
+
rb_check_frozen(self);
|
149
|
+
if (self == other) return self;
|
150
|
+
|
151
|
+
TypedData_Get_Struct(self, EVP_MD_CTX, &ossl_digest_type, ctx1);
|
152
|
+
if (!ctx1) {
|
153
|
+
RTYPEDDATA_DATA(self) = ctx1 = EVP_MD_CTX_new();
|
154
|
+
if (!ctx1)
|
155
|
+
ossl_raise(eDigestError, "EVP_MD_CTX_new");
|
156
|
+
}
|
157
|
+
GetDigest(other, ctx2);
|
158
|
+
|
159
|
+
if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
|
160
|
+
ossl_raise(eDigestError, NULL);
|
161
|
+
}
|
162
|
+
return self;
|
163
|
+
}
|
164
|
+
|
165
|
+
/*
|
166
|
+
* call-seq:
|
167
|
+
* digest.reset -> self
|
168
|
+
*
|
169
|
+
* Resets the Digest in the sense that any Digest#update that has been
|
170
|
+
* performed is abandoned and the Digest is set to its initial state again.
|
171
|
+
*
|
172
|
+
*/
|
173
|
+
static VALUE
|
174
|
+
ossl_digest_reset(VALUE self)
|
175
|
+
{
|
176
|
+
EVP_MD_CTX *ctx;
|
177
|
+
|
178
|
+
GetDigest(self, ctx);
|
179
|
+
if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL) != 1) {
|
180
|
+
ossl_raise(eDigestError, "Digest initialization failed.");
|
181
|
+
}
|
182
|
+
|
183
|
+
return self;
|
184
|
+
}
|
185
|
+
|
186
|
+
/*
|
187
|
+
* call-seq:
|
188
|
+
* digest.update(string) -> aString
|
189
|
+
*
|
190
|
+
* Not every message digest can be computed in one single pass. If a message
|
191
|
+
* digest is to be computed from several subsequent sources, then each may
|
192
|
+
* be passed individually to the Digest instance.
|
193
|
+
*
|
194
|
+
* === Example
|
195
|
+
* digest = OpenSSL::Digest.new('SHA256')
|
196
|
+
* digest.update('First input')
|
197
|
+
* digest << 'Second input' # equivalent to digest.update('Second input')
|
198
|
+
* result = digest.digest
|
199
|
+
*
|
200
|
+
*/
|
201
|
+
VALUE
|
202
|
+
ossl_digest_update(VALUE self, VALUE data)
|
203
|
+
{
|
204
|
+
EVP_MD_CTX *ctx;
|
205
|
+
|
206
|
+
StringValue(data);
|
207
|
+
GetDigest(self, ctx);
|
208
|
+
|
209
|
+
if (!EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data)))
|
210
|
+
ossl_raise(eDigestError, "EVP_DigestUpdate");
|
211
|
+
|
212
|
+
return self;
|
213
|
+
}
|
214
|
+
|
215
|
+
/*
|
216
|
+
* call-seq:
|
217
|
+
* digest.finish -> aString
|
218
|
+
*
|
219
|
+
*/
|
220
|
+
static VALUE
|
221
|
+
ossl_digest_finish(int argc, VALUE *argv, VALUE self)
|
222
|
+
{
|
223
|
+
EVP_MD_CTX *ctx;
|
224
|
+
VALUE str;
|
225
|
+
int out_len;
|
226
|
+
|
227
|
+
GetDigest(self, ctx);
|
228
|
+
rb_scan_args(argc, argv, "01", &str);
|
229
|
+
out_len = EVP_MD_CTX_size(ctx);
|
230
|
+
|
231
|
+
if (NIL_P(str)) {
|
232
|
+
str = rb_str_new(NULL, out_len);
|
233
|
+
} else {
|
234
|
+
StringValue(str);
|
235
|
+
rb_str_resize(str, out_len);
|
236
|
+
}
|
237
|
+
|
238
|
+
if (!EVP_DigestFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), NULL))
|
239
|
+
ossl_raise(eDigestError, "EVP_DigestFinal_ex");
|
240
|
+
|
241
|
+
return str;
|
242
|
+
}
|
243
|
+
|
244
|
+
/*
|
245
|
+
* call-seq:
|
246
|
+
* digest.name -> string
|
247
|
+
*
|
248
|
+
* Returns the sn of this Digest algorithm.
|
249
|
+
*
|
250
|
+
* === Example
|
251
|
+
* digest = OpenSSL::Digest.new('SHA512')
|
252
|
+
* puts digest.name # => SHA512
|
253
|
+
*
|
254
|
+
*/
|
255
|
+
static VALUE
|
256
|
+
ossl_digest_name(VALUE self)
|
257
|
+
{
|
258
|
+
EVP_MD_CTX *ctx;
|
259
|
+
|
260
|
+
GetDigest(self, ctx);
|
261
|
+
|
262
|
+
return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
|
263
|
+
}
|
264
|
+
|
265
|
+
/*
|
266
|
+
* call-seq:
|
267
|
+
* digest.digest_length -> integer
|
268
|
+
*
|
269
|
+
* Returns the output size of the digest, i.e. the length in bytes of the
|
270
|
+
* final message digest result.
|
271
|
+
*
|
272
|
+
* === Example
|
273
|
+
* digest = OpenSSL::Digest.new('SHA1')
|
274
|
+
* puts digest.digest_length # => 20
|
275
|
+
*
|
276
|
+
*/
|
277
|
+
static VALUE
|
278
|
+
ossl_digest_size(VALUE self)
|
279
|
+
{
|
280
|
+
EVP_MD_CTX *ctx;
|
281
|
+
|
282
|
+
GetDigest(self, ctx);
|
283
|
+
|
284
|
+
return INT2NUM(EVP_MD_CTX_size(ctx));
|
285
|
+
}
|
286
|
+
|
287
|
+
/*
|
288
|
+
* call-seq:
|
289
|
+
* digest.block_length -> integer
|
290
|
+
*
|
291
|
+
* Returns the block length of the digest algorithm, i.e. the length in bytes
|
292
|
+
* of an individual block. Most modern algorithms partition a message to be
|
293
|
+
* digested into a sequence of fix-sized blocks that are processed
|
294
|
+
* consecutively.
|
295
|
+
*
|
296
|
+
* === Example
|
297
|
+
* digest = OpenSSL::Digest.new('SHA1')
|
298
|
+
* puts digest.block_length # => 64
|
299
|
+
*/
|
300
|
+
static VALUE
|
301
|
+
ossl_digest_block_length(VALUE self)
|
302
|
+
{
|
303
|
+
EVP_MD_CTX *ctx;
|
304
|
+
|
305
|
+
GetDigest(self, ctx);
|
306
|
+
|
307
|
+
return INT2NUM(EVP_MD_CTX_block_size(ctx));
|
308
|
+
}
|
309
|
+
|
310
|
+
/*
|
311
|
+
* INIT
|
312
|
+
*/
|
313
|
+
void
|
314
|
+
Init_ossl_digest(void)
|
315
|
+
{
|
316
|
+
#if 0
|
317
|
+
mOSSL = rb_define_module("OpenSSL");
|
318
|
+
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
319
|
+
#endif
|
320
|
+
|
321
|
+
/* Document-class: OpenSSL::Digest
|
322
|
+
*
|
323
|
+
* OpenSSL::Digest allows you to compute message digests (sometimes
|
324
|
+
* interchangeably called "hashes") of arbitrary data that are
|
325
|
+
* cryptographically secure, i.e. a Digest implements a secure one-way
|
326
|
+
* function.
|
327
|
+
*
|
328
|
+
* One-way functions offer some useful properties. E.g. given two
|
329
|
+
* distinct inputs the probability that both yield the same output
|
330
|
+
* is highly unlikely. Combined with the fact that every message digest
|
331
|
+
* algorithm has a fixed-length output of just a few bytes, digests are
|
332
|
+
* often used to create unique identifiers for arbitrary data. A common
|
333
|
+
* example is the creation of a unique id for binary documents that are
|
334
|
+
* stored in a database.
|
335
|
+
*
|
336
|
+
* Another useful characteristic of one-way functions (and thus the name)
|
337
|
+
* is that given a digest there is no indication about the original
|
338
|
+
* data that produced it, i.e. the only way to identify the original input
|
339
|
+
* is to "brute-force" through every possible combination of inputs.
|
340
|
+
*
|
341
|
+
* These characteristics make one-way functions also ideal companions
|
342
|
+
* for public key signature algorithms: instead of signing an entire
|
343
|
+
* document, first a hash of the document is produced with a considerably
|
344
|
+
* faster message digest algorithm and only the few bytes of its output
|
345
|
+
* need to be signed using the slower public key algorithm. To validate
|
346
|
+
* the integrity of a signed document, it suffices to re-compute the hash
|
347
|
+
* and verify that it is equal to that in the signature.
|
348
|
+
*
|
349
|
+
* You can get a list of all digest algorithms supported on your system by
|
350
|
+
* running this command in your terminal:
|
351
|
+
*
|
352
|
+
* openssl list -digest-algorithms
|
353
|
+
*
|
354
|
+
* Among the OpenSSL 1.1.1 supported message digest algorithms are:
|
355
|
+
* * SHA224, SHA256, SHA384, SHA512, SHA512-224 and SHA512-256
|
356
|
+
* * SHA3-224, SHA3-256, SHA3-384 and SHA3-512
|
357
|
+
* * BLAKE2s256 and BLAKE2b512
|
358
|
+
*
|
359
|
+
* Each of these algorithms can be instantiated using the name:
|
360
|
+
*
|
361
|
+
* digest = OpenSSL::Digest.new('SHA256')
|
362
|
+
*
|
363
|
+
* "Breaking" a message digest algorithm means defying its one-way
|
364
|
+
* function characteristics, i.e. producing a collision or finding a way
|
365
|
+
* to get to the original data by means that are more efficient than
|
366
|
+
* brute-forcing etc. Most of the supported digest algorithms can be
|
367
|
+
* considered broken in this sense, even the very popular MD5 and SHA1
|
368
|
+
* algorithms. Should security be your highest concern, then you should
|
369
|
+
* probably rely on SHA224, SHA256, SHA384 or SHA512.
|
370
|
+
*
|
371
|
+
* === Hashing a file
|
372
|
+
*
|
373
|
+
* data = File.read('document')
|
374
|
+
* sha256 = OpenSSL::Digest.new('SHA256')
|
375
|
+
* digest = sha256.digest(data)
|
376
|
+
*
|
377
|
+
* === Hashing several pieces of data at once
|
378
|
+
*
|
379
|
+
* data1 = File.read('file1')
|
380
|
+
* data2 = File.read('file2')
|
381
|
+
* data3 = File.read('file3')
|
382
|
+
* sha256 = OpenSSL::Digest.new('SHA256')
|
383
|
+
* sha256 << data1
|
384
|
+
* sha256 << data2
|
385
|
+
* sha256 << data3
|
386
|
+
* digest = sha256.digest
|
387
|
+
*
|
388
|
+
* === Reuse a Digest instance
|
389
|
+
*
|
390
|
+
* data1 = File.read('file1')
|
391
|
+
* sha256 = OpenSSL::Digest.new('SHA256')
|
392
|
+
* digest1 = sha256.digest(data1)
|
393
|
+
*
|
394
|
+
* data2 = File.read('file2')
|
395
|
+
* sha256.reset
|
396
|
+
* digest2 = sha256.digest(data2)
|
397
|
+
*
|
398
|
+
*/
|
399
|
+
|
400
|
+
/*
|
401
|
+
* Digest::Class is defined by the digest library. rb_require() cannot be
|
402
|
+
* used here because it bypasses RubyGems.
|
403
|
+
*/
|
404
|
+
rb_funcall(Qnil, rb_intern_const("require"), 1, rb_str_new_cstr("digest"));
|
405
|
+
cDigest = rb_define_class_under(mOSSL, "Digest", rb_path2class("Digest::Class"));
|
406
|
+
/* Document-class: OpenSSL::Digest::DigestError
|
407
|
+
*
|
408
|
+
* Generic Exception class that is raised if an error occurs during a
|
409
|
+
* Digest operation.
|
410
|
+
*/
|
411
|
+
eDigestError = rb_define_class_under(cDigest, "DigestError", eOSSLError);
|
412
|
+
|
413
|
+
rb_define_alloc_func(cDigest, ossl_digest_alloc);
|
414
|
+
|
415
|
+
rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
|
416
|
+
rb_define_method(cDigest, "initialize_copy", ossl_digest_copy, 1);
|
417
|
+
rb_define_method(cDigest, "reset", ossl_digest_reset, 0);
|
418
|
+
rb_define_method(cDigest, "update", ossl_digest_update, 1);
|
419
|
+
rb_define_alias(cDigest, "<<", "update");
|
420
|
+
rb_define_private_method(cDigest, "finish", ossl_digest_finish, -1);
|
421
|
+
rb_define_method(cDigest, "digest_length", ossl_digest_size, 0);
|
422
|
+
rb_define_method(cDigest, "block_length", ossl_digest_block_length, 0);
|
423
|
+
|
424
|
+
rb_define_method(cDigest, "name", ossl_digest_name, 0);
|
425
|
+
}
|
@@ -0,0 +1,20 @@
|
|
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
|
+
#if !defined(_OSSL_DIGEST_H_)
|
11
|
+
#define _OSSL_DIGEST_H_
|
12
|
+
|
13
|
+
extern VALUE cDigest;
|
14
|
+
extern VALUE eDigestError;
|
15
|
+
|
16
|
+
const EVP_MD *ossl_evp_get_digestbyname(VALUE);
|
17
|
+
VALUE ossl_digest_new(const EVP_MD *);
|
18
|
+
void Init_ossl_digest(void);
|
19
|
+
|
20
|
+
#endif /* _OSSL_DIGEST_H_ */
|