jruby-openssl 0.9.7-java → 0.9.8-java

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