jruby-openssl 0.7.4 → 0.7.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/History.txt +51 -14
- data/Manifest.txt +136 -79
- data/README.txt +1 -1
- data/Rakefile +17 -8
- data/lib/{openssl → 1.8/openssl}/bn.rb +2 -2
- data/lib/{openssl → 1.8/openssl}/buffering.rb +3 -1
- data/lib/{openssl → 1.8/openssl}/cipher.rb +0 -0
- data/lib/{openssl → 1.8/openssl}/config.rb +1 -1
- data/lib/{openssl → 1.8/openssl}/digest.rb +2 -2
- data/lib/{openssl → 1.8/openssl}/pkcs7.rb +0 -0
- data/lib/{openssl/ssl.rb → 1.8/openssl/ssl-internal.rb} +2 -2
- data/lib/1.8/openssl/ssl.rb +1 -0
- data/lib/{openssl/x509.rb → 1.8/openssl/x509-internal.rb} +8 -9
- data/lib/1.8/openssl/x509.rb +1 -0
- data/lib/{openssl.rb → 1.8/openssl.rb} +2 -11
- data/lib/1.9/openssl/bn.rb +35 -0
- data/lib/1.9/openssl/buffering.rb +448 -0
- data/lib/1.9/openssl/cipher.rb +65 -0
- data/lib/1.9/openssl/config.rb +313 -0
- data/lib/1.9/openssl/digest.rb +72 -0
- data/lib/1.9/openssl/ssl-internal.rb +177 -0
- data/lib/1.9/openssl/ssl.rb +2 -0
- data/lib/1.9/openssl/x509-internal.rb +158 -0
- data/lib/1.9/openssl/x509.rb +2 -0
- data/lib/1.9/openssl.rb +22 -0
- data/lib/{jopenssl → shared/jopenssl}/version.rb +1 -1
- data/lib/shared/jopenssl.jar +0 -0
- data/lib/{openssl → shared/openssl}/dummy.rb +0 -0
- data/lib/{openssl → shared/openssl}/dummyssl.rb +0 -0
- data/lib/shared/openssl/pkcs12.rb +50 -0
- data/lib/shared/openssl/ssl.rb +1 -0
- data/lib/shared/openssl/x509.rb +1 -0
- data/lib/shared/openssl.rb +20 -0
- data/test/{openssl → 1.8}/ssl_server.rb +0 -0
- data/test/{openssl → 1.8}/test_asn1.rb +15 -0
- data/test/{openssl → 1.8}/test_cipher.rb +0 -0
- data/test/{openssl → 1.8}/test_config.rb +0 -0
- data/test/{openssl → 1.8}/test_digest.rb +0 -0
- data/test/{openssl → 1.8}/test_ec.rb +0 -0
- data/test/{openssl → 1.8}/test_hmac.rb +0 -0
- data/test/{openssl → 1.8}/test_ns_spki.rb +0 -0
- data/test/{openssl → 1.8}/test_pair.rb +10 -2
- data/test/{openssl → 1.8}/test_pkcs7.rb +0 -0
- data/test/{openssl → 1.8}/test_pkey_rsa.rb +0 -0
- data/test/{openssl → 1.8}/test_ssl.rb +17 -20
- data/test/{openssl → 1.8}/test_x509cert.rb +0 -0
- data/test/{openssl → 1.8}/test_x509crl.rb +0 -0
- data/test/{openssl → 1.8}/test_x509ext.rb +0 -0
- data/test/{openssl → 1.8}/test_x509name.rb +0 -0
- data/test/{openssl → 1.8}/test_x509req.rb +0 -0
- data/test/{openssl → 1.8}/test_x509store.rb +0 -0
- data/test/{openssl → 1.8}/utils.rb +0 -0
- data/test/1.9/ssl_server.rb +81 -0
- data/test/1.9/test_asn1.rb +589 -0
- data/test/1.9/test_bn.rb +23 -0
- data/test/1.9/test_buffering.rb +88 -0
- data/test/1.9/test_cipher.rb +107 -0
- data/test/1.9/test_config.rb +288 -0
- data/test/1.9/test_digest.rb +118 -0
- data/test/1.9/test_engine.rb +15 -0
- data/test/1.9/test_hmac.rb +32 -0
- data/test/1.9/test_ns_spki.rb +50 -0
- data/test/1.9/test_ocsp.rb +47 -0
- data/test/1.9/test_pair.rb +257 -0
- data/test/1.9/test_pkcs12.rb +209 -0
- data/test/1.9/test_pkcs7.rb +151 -0
- data/test/1.9/test_pkey_dh.rb +72 -0
- data/test/1.9/test_pkey_dsa.rb +224 -0
- data/test/1.9/test_pkey_ec.rb +182 -0
- data/test/1.9/test_pkey_rsa.rb +244 -0
- data/test/1.9/test_ssl.rb +455 -0
- data/test/1.9/test_ssl_session.rb +327 -0
- data/test/1.9/test_x509cert.rb +217 -0
- data/test/1.9/test_x509crl.rb +221 -0
- data/test/1.9/test_x509ext.rb +69 -0
- data/test/1.9/test_x509name.rb +296 -0
- data/test/1.9/test_x509req.rb +150 -0
- data/test/1.9/test_x509store.rb +229 -0
- data/test/1.9/utils.rb +304 -0
- data/test/fixture/ids_in_subject_rdn_set.pem +31 -0
- data/test/fixture/purpose/ca/ca_config.rb +1 -1
- data/test/fixture/purpose/ca/gen_cert.rb +128 -0
- data/test/fixture/purpose/ca/newcerts/4_cert.pem +19 -0
- data/test/fixture/purpose/ca/serial +1 -1
- data/test/fixture/purpose/sslserver_no_dsig_in_keyUsage.pem +19 -0
- data/test/ruby/envutil.rb +208 -0
- data/test/ruby/ut_eof.rb +128 -0
- data/test/test_certificate.rb +9 -0
- data/test/test_java.rb +1 -1
- data/test/test_openssl.rb +1 -1
- data/test/test_pkcs7.rb +16 -0
- data/test/test_pkey_dsa.rb +180 -0
- data/test/test_pkey_rsa.rb +298 -0
- data/test/test_ssl.rb +1 -1
- data/test/test_x509store.rb +8 -0
- metadata +133 -73
- data/lib/jopenssl.jar +0 -0
- data/test/test_pkey.rb +0 -204
@@ -0,0 +1,448 @@
|
|
1
|
+
=begin
|
2
|
+
= $RCSfile$ -- Buffering mix-in module.
|
3
|
+
|
4
|
+
= Info
|
5
|
+
'OpenSSL for Ruby 2' project
|
6
|
+
Copyright (C) 2001 GOTOU YUUZOU <gotoyuzo@notwork.org>
|
7
|
+
All rights reserved.
|
8
|
+
|
9
|
+
= Licence
|
10
|
+
This program is licenced under the same licence as Ruby.
|
11
|
+
(See the file 'LICENCE'.)
|
12
|
+
|
13
|
+
= Version
|
14
|
+
$Id: buffering.rb 32012 2011-06-11 14:07:42Z nahi $
|
15
|
+
=end
|
16
|
+
|
17
|
+
##
|
18
|
+
# OpenSSL IO buffering mix-in module.
|
19
|
+
#
|
20
|
+
# This module allows an OpenSSL::SSL::SSLSocket to behave like an IO.
|
21
|
+
|
22
|
+
module OpenSSL::Buffering
|
23
|
+
include Enumerable
|
24
|
+
|
25
|
+
##
|
26
|
+
# The "sync mode" of the SSLSocket.
|
27
|
+
#
|
28
|
+
# See IO#sync for full details.
|
29
|
+
|
30
|
+
attr_accessor :sync
|
31
|
+
|
32
|
+
##
|
33
|
+
# Default size to read from or write to the SSLSocket for buffer operations.
|
34
|
+
|
35
|
+
BLOCK_SIZE = 1024*16
|
36
|
+
|
37
|
+
def initialize(*args)
|
38
|
+
@eof = false
|
39
|
+
@rbuffer = ""
|
40
|
+
@sync = @io.sync
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# for reading.
|
45
|
+
#
|
46
|
+
private
|
47
|
+
|
48
|
+
##
|
49
|
+
# Fills the buffer from the underlying SSLSocket
|
50
|
+
|
51
|
+
def fill_rbuff
|
52
|
+
begin
|
53
|
+
@rbuffer << self.sysread(BLOCK_SIZE)
|
54
|
+
rescue Errno::EAGAIN
|
55
|
+
retry
|
56
|
+
rescue EOFError
|
57
|
+
@eof = true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# Consumes +size+ bytes from the buffer
|
63
|
+
|
64
|
+
def consume_rbuff(size=nil)
|
65
|
+
if @rbuffer.empty?
|
66
|
+
nil
|
67
|
+
else
|
68
|
+
size = @rbuffer.size unless size
|
69
|
+
ret = @rbuffer[0, size]
|
70
|
+
@rbuffer[0, size] = ""
|
71
|
+
ret
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
public
|
76
|
+
|
77
|
+
##
|
78
|
+
# Reads +size+ bytes from the stream. If +buf+ is provided it must
|
79
|
+
# reference a string which will receive the data.
|
80
|
+
#
|
81
|
+
# See IO#read for full details.
|
82
|
+
|
83
|
+
def read(size=nil, buf=nil)
|
84
|
+
if size == 0
|
85
|
+
if buf
|
86
|
+
buf.clear
|
87
|
+
return buf
|
88
|
+
else
|
89
|
+
return ""
|
90
|
+
end
|
91
|
+
end
|
92
|
+
until @eof
|
93
|
+
break if size && size <= @rbuffer.size
|
94
|
+
fill_rbuff
|
95
|
+
end
|
96
|
+
ret = consume_rbuff(size) || ""
|
97
|
+
if buf
|
98
|
+
buf.replace(ret)
|
99
|
+
ret = buf
|
100
|
+
end
|
101
|
+
(size && ret.empty?) ? nil : ret
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# Reads at most +maxlen+ bytes from the stream. If +buf+ is provided it
|
106
|
+
# must reference a string which will receive the data.
|
107
|
+
#
|
108
|
+
# See IO#readpartial for full details.
|
109
|
+
|
110
|
+
def readpartial(maxlen, buf=nil)
|
111
|
+
if maxlen == 0
|
112
|
+
if buf
|
113
|
+
buf.clear
|
114
|
+
return buf
|
115
|
+
else
|
116
|
+
return ""
|
117
|
+
end
|
118
|
+
end
|
119
|
+
if @rbuffer.empty?
|
120
|
+
begin
|
121
|
+
return sysread(maxlen, buf)
|
122
|
+
rescue Errno::EAGAIN
|
123
|
+
retry
|
124
|
+
end
|
125
|
+
end
|
126
|
+
ret = consume_rbuff(maxlen)
|
127
|
+
if buf
|
128
|
+
buf.replace(ret)
|
129
|
+
ret = buf
|
130
|
+
end
|
131
|
+
raise EOFError if ret.empty?
|
132
|
+
ret
|
133
|
+
end
|
134
|
+
|
135
|
+
##
|
136
|
+
# Reads at most +maxlen+ bytes in the non-blocking manner.
|
137
|
+
#
|
138
|
+
# When no data can be read without blocking it raises
|
139
|
+
# OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.
|
140
|
+
#
|
141
|
+
# IO::WaitReadable means SSL needs to read internally so read_nonblock
|
142
|
+
# should be called again when the underlying IO is readable.
|
143
|
+
#
|
144
|
+
# IO::WaitWritable means SSL needs to write internally so read_nonblock
|
145
|
+
# should be called again after the underlying IO is writable.
|
146
|
+
#
|
147
|
+
# OpenSSL::Buffering#read_nonblock needs two rescue clause as follows:
|
148
|
+
#
|
149
|
+
# # emulates blocking read (readpartial).
|
150
|
+
# begin
|
151
|
+
# result = ssl.read_nonblock(maxlen)
|
152
|
+
# rescue IO::WaitReadable
|
153
|
+
# IO.select([io])
|
154
|
+
# retry
|
155
|
+
# rescue IO::WaitWritable
|
156
|
+
# IO.select(nil, [io])
|
157
|
+
# retry
|
158
|
+
# end
|
159
|
+
#
|
160
|
+
# Note that one reason that read_nonblock writes to the underlying IO is
|
161
|
+
# when the peer requests a new TLS/SSL handshake. See openssl the FAQ for
|
162
|
+
# more details. http://www.openssl.org/support/faq.html
|
163
|
+
|
164
|
+
def read_nonblock(maxlen, buf=nil)
|
165
|
+
if maxlen == 0
|
166
|
+
if buf
|
167
|
+
buf.clear
|
168
|
+
return buf
|
169
|
+
else
|
170
|
+
return ""
|
171
|
+
end
|
172
|
+
end
|
173
|
+
if @rbuffer.empty?
|
174
|
+
return sysread_nonblock(maxlen, buf)
|
175
|
+
end
|
176
|
+
ret = consume_rbuff(maxlen)
|
177
|
+
if buf
|
178
|
+
buf.replace(ret)
|
179
|
+
ret = buf
|
180
|
+
end
|
181
|
+
raise EOFError if ret.empty?
|
182
|
+
ret
|
183
|
+
end
|
184
|
+
|
185
|
+
##
|
186
|
+
# Reads the next "line+ from the stream. Lines are separated by +eol+. If
|
187
|
+
# +limit+ is provided the result will not be longer than the given number of
|
188
|
+
# bytes.
|
189
|
+
#
|
190
|
+
# +eol+ may be a String or Regexp.
|
191
|
+
#
|
192
|
+
# Unlike IO#gets the line read will not be assigned to +$_+.
|
193
|
+
#
|
194
|
+
# Unlike IO#gets the separator must be provided if a limit is provided.
|
195
|
+
|
196
|
+
def gets(eol=$/, limit=nil)
|
197
|
+
idx = @rbuffer.index(eol)
|
198
|
+
until @eof
|
199
|
+
break if idx
|
200
|
+
fill_rbuff
|
201
|
+
idx = @rbuffer.index(eol)
|
202
|
+
end
|
203
|
+
if eol.is_a?(Regexp)
|
204
|
+
size = idx ? idx+$&.size : nil
|
205
|
+
else
|
206
|
+
size = idx ? idx+eol.size : nil
|
207
|
+
end
|
208
|
+
if limit and limit >= 0
|
209
|
+
size = [size, limit].min
|
210
|
+
end
|
211
|
+
consume_rbuff(size)
|
212
|
+
end
|
213
|
+
|
214
|
+
##
|
215
|
+
# Executes the block for every line in the stream where lines are separated
|
216
|
+
# by +eol+.
|
217
|
+
#
|
218
|
+
# See also #gets
|
219
|
+
|
220
|
+
def each(eol=$/)
|
221
|
+
while line = self.gets(eol)
|
222
|
+
yield line
|
223
|
+
end
|
224
|
+
end
|
225
|
+
alias each_line each
|
226
|
+
|
227
|
+
##
|
228
|
+
# Reads lines from the stream which are separated by +eol+.
|
229
|
+
#
|
230
|
+
# See also #gets
|
231
|
+
|
232
|
+
def readlines(eol=$/)
|
233
|
+
ary = []
|
234
|
+
while line = self.gets(eol)
|
235
|
+
ary << line
|
236
|
+
end
|
237
|
+
ary
|
238
|
+
end
|
239
|
+
|
240
|
+
##
|
241
|
+
# Reads a line from the stream which is separated by +eol+.
|
242
|
+
#
|
243
|
+
# Raises EOFError if at end of file.
|
244
|
+
|
245
|
+
def readline(eol=$/)
|
246
|
+
raise EOFError if eof?
|
247
|
+
gets(eol)
|
248
|
+
end
|
249
|
+
|
250
|
+
##
|
251
|
+
# Reads one character from the stream. Returns nil if called at end of
|
252
|
+
# file.
|
253
|
+
|
254
|
+
def getc
|
255
|
+
read(1)
|
256
|
+
end
|
257
|
+
|
258
|
+
##
|
259
|
+
# Calls the given block once for each byte in the stream.
|
260
|
+
|
261
|
+
def each_byte # :yields: byte
|
262
|
+
while c = getc
|
263
|
+
yield(c.ord)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
##
|
268
|
+
# Reads a one-character string from the stream. Raises an EOFError at end
|
269
|
+
# of file.
|
270
|
+
|
271
|
+
def readchar
|
272
|
+
raise EOFError if eof?
|
273
|
+
getc
|
274
|
+
end
|
275
|
+
|
276
|
+
##
|
277
|
+
# Pushes character +c+ back onto the stream such that a subsequent buffered
|
278
|
+
# character read will return it.
|
279
|
+
#
|
280
|
+
# Unlike IO#getc multiple bytes may be pushed back onto the stream.
|
281
|
+
#
|
282
|
+
# Has no effect on unbuffered reads (such as #sysread).
|
283
|
+
|
284
|
+
def ungetc(c)
|
285
|
+
@rbuffer[0,0] = c.chr
|
286
|
+
end
|
287
|
+
|
288
|
+
##
|
289
|
+
# Returns true if the stream is at file which means there is no more data to
|
290
|
+
# be read.
|
291
|
+
|
292
|
+
def eof?
|
293
|
+
fill_rbuff if !@eof && @rbuffer.empty?
|
294
|
+
@eof && @rbuffer.empty?
|
295
|
+
end
|
296
|
+
alias eof eof?
|
297
|
+
|
298
|
+
#
|
299
|
+
# for writing.
|
300
|
+
#
|
301
|
+
private
|
302
|
+
|
303
|
+
##
|
304
|
+
# Writes +s+ to the buffer. When the buffer is full or #sync is true the
|
305
|
+
# buffer is flushed to the underlying socket.
|
306
|
+
|
307
|
+
def do_write(s)
|
308
|
+
@wbuffer = "" unless defined? @wbuffer
|
309
|
+
@wbuffer << s
|
310
|
+
@sync ||= false
|
311
|
+
if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
|
312
|
+
remain = idx ? idx + $/.size : @wbuffer.length
|
313
|
+
nwritten = 0
|
314
|
+
while remain > 0
|
315
|
+
str = @wbuffer[nwritten,remain]
|
316
|
+
begin
|
317
|
+
nwrote = syswrite(str)
|
318
|
+
rescue Errno::EAGAIN
|
319
|
+
retry
|
320
|
+
end
|
321
|
+
remain -= nwrote
|
322
|
+
nwritten += nwrote
|
323
|
+
end
|
324
|
+
@wbuffer[0,nwritten] = ""
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
public
|
329
|
+
|
330
|
+
##
|
331
|
+
# Writes +s+ to the stream. If the argument is not a string it will be
|
332
|
+
# converted using String#to_s. Returns the number of bytes written.
|
333
|
+
|
334
|
+
def write(s)
|
335
|
+
do_write(s)
|
336
|
+
s.length
|
337
|
+
end
|
338
|
+
|
339
|
+
##
|
340
|
+
# Writes +str+ in the non-blocking manner.
|
341
|
+
#
|
342
|
+
# If there is buffered data, it is flushed first. This may block.
|
343
|
+
#
|
344
|
+
# write_nonblock returns number of bytes written to the SSL connection.
|
345
|
+
#
|
346
|
+
# When no data can be written without blocking it raises
|
347
|
+
# OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.
|
348
|
+
#
|
349
|
+
# IO::WaitReadable means SSL needs to read internally so write_nonblock
|
350
|
+
# should be called again after the underlying IO is readable.
|
351
|
+
#
|
352
|
+
# IO::WaitWritable means SSL needs to write internally so write_nonblock
|
353
|
+
# should be called again after underlying IO is writable.
|
354
|
+
#
|
355
|
+
# So OpenSSL::Buffering#write_nonblock needs two rescue clause as follows.
|
356
|
+
#
|
357
|
+
# # emulates blocking write.
|
358
|
+
# begin
|
359
|
+
# result = ssl.write_nonblock(str)
|
360
|
+
# rescue IO::WaitReadable
|
361
|
+
# IO.select([io])
|
362
|
+
# retry
|
363
|
+
# rescue IO::WaitWritable
|
364
|
+
# IO.select(nil, [io])
|
365
|
+
# retry
|
366
|
+
# end
|
367
|
+
#
|
368
|
+
# Note that one reason that write_nonblock reads from the underlying IO
|
369
|
+
# is when the peer requests a new TLS/SSL handshake. See the openssl FAQ
|
370
|
+
# for more details. http://www.openssl.org/support/faq.html
|
371
|
+
|
372
|
+
def write_nonblock(s)
|
373
|
+
flush
|
374
|
+
syswrite_nonblock(s)
|
375
|
+
end
|
376
|
+
|
377
|
+
##
|
378
|
+
# Writes +s+ to the stream. +s+ will be converted to a String using
|
379
|
+
# String#to_s.
|
380
|
+
|
381
|
+
def << (s)
|
382
|
+
do_write(s)
|
383
|
+
self
|
384
|
+
end
|
385
|
+
|
386
|
+
##
|
387
|
+
# Writes +args+ to the stream along with a record separator.
|
388
|
+
#
|
389
|
+
# See IO#puts for full details.
|
390
|
+
|
391
|
+
def puts(*args)
|
392
|
+
s = ""
|
393
|
+
if args.empty?
|
394
|
+
s << "\n"
|
395
|
+
end
|
396
|
+
args.each{|arg|
|
397
|
+
s << arg.to_s
|
398
|
+
if $/ && /\n\z/ !~ s
|
399
|
+
s << "\n"
|
400
|
+
end
|
401
|
+
}
|
402
|
+
do_write(s)
|
403
|
+
nil
|
404
|
+
end
|
405
|
+
|
406
|
+
##
|
407
|
+
# Writes +args+ to the stream.
|
408
|
+
#
|
409
|
+
# See IO#print for full details.
|
410
|
+
|
411
|
+
def print(*args)
|
412
|
+
s = ""
|
413
|
+
args.each{ |arg| s << arg.to_s }
|
414
|
+
do_write(s)
|
415
|
+
nil
|
416
|
+
end
|
417
|
+
|
418
|
+
##
|
419
|
+
# Formats and writes to the stream converting parameters under control of
|
420
|
+
# the format string.
|
421
|
+
#
|
422
|
+
# See Kernel#sprintf for format string details.
|
423
|
+
|
424
|
+
def printf(s, *args)
|
425
|
+
do_write(s % args)
|
426
|
+
nil
|
427
|
+
end
|
428
|
+
|
429
|
+
##
|
430
|
+
# Flushes buffered data to the SSLSocket.
|
431
|
+
|
432
|
+
def flush
|
433
|
+
osync = @sync
|
434
|
+
@sync = true
|
435
|
+
do_write ""
|
436
|
+
return self
|
437
|
+
ensure
|
438
|
+
@sync = osync
|
439
|
+
end
|
440
|
+
|
441
|
+
##
|
442
|
+
# Closes the SSLSocket and flushes any unwritten data.
|
443
|
+
|
444
|
+
def close
|
445
|
+
flush rescue nil
|
446
|
+
sysclose
|
447
|
+
end
|
448
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# $RCSfile$
|
4
|
+
#
|
5
|
+
# = Ruby-space predefined Cipher subclasses
|
6
|
+
#
|
7
|
+
# = Info
|
8
|
+
# 'OpenSSL for Ruby 2' project
|
9
|
+
# Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
10
|
+
# All rights reserved.
|
11
|
+
#
|
12
|
+
# = Licence
|
13
|
+
# This program is licenced under the same licence as Ruby.
|
14
|
+
# (See the file 'LICENCE'.)
|
15
|
+
#
|
16
|
+
# = Version
|
17
|
+
# $Id: cipher.rb 33067 2011-08-25 00:52:10Z drbrain $
|
18
|
+
#
|
19
|
+
#++
|
20
|
+
|
21
|
+
module OpenSSL
|
22
|
+
class Cipher
|
23
|
+
%w(AES CAST5 BF DES IDEA RC2 RC4 RC5).each{|name|
|
24
|
+
klass = Class.new(Cipher){
|
25
|
+
define_method(:initialize){|*args|
|
26
|
+
cipher_name = args.inject(name){|n, arg| "#{n}-#{arg}" }
|
27
|
+
super(cipher_name)
|
28
|
+
}
|
29
|
+
}
|
30
|
+
const_set(name, klass)
|
31
|
+
}
|
32
|
+
|
33
|
+
%w(128 192 256).each{|keylen|
|
34
|
+
klass = Class.new(Cipher){
|
35
|
+
define_method(:initialize){|mode|
|
36
|
+
mode ||= "CBC"
|
37
|
+
cipher_name = "AES-#{keylen}-#{mode}"
|
38
|
+
super(cipher_name)
|
39
|
+
}
|
40
|
+
}
|
41
|
+
const_set("AES#{keylen}", klass)
|
42
|
+
}
|
43
|
+
|
44
|
+
# Generate, set, and return a random key.
|
45
|
+
# You must call cipher.encrypt or cipher.decrypt before calling this method.
|
46
|
+
def random_key
|
47
|
+
str = OpenSSL::Random.random_bytes(self.key_len)
|
48
|
+
self.key = str
|
49
|
+
return str
|
50
|
+
end
|
51
|
+
|
52
|
+
# Generate, set, and return a random iv.
|
53
|
+
# You must call cipher.encrypt or cipher.decrypt before calling this method.
|
54
|
+
def random_iv
|
55
|
+
str = OpenSSL::Random.random_bytes(self.iv_len)
|
56
|
+
self.iv = str
|
57
|
+
return str
|
58
|
+
end
|
59
|
+
|
60
|
+
# This class is only provided for backwards compatibility. Use OpenSSL::Cipher in the future.
|
61
|
+
class Cipher < Cipher
|
62
|
+
# add warning
|
63
|
+
end
|
64
|
+
end # Cipher
|
65
|
+
end # OpenSSL
|