jruby-openssl 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jruby-openssl might be problematic. Click here for more details.

Binary file
Binary file
data/lib/jopenssl.jar ADDED
Binary file
data/lib/openssl.rb ADDED
@@ -0,0 +1,24 @@
1
+ =begin
2
+ = $RCSfile: openssl.rb,v $ -- 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: openssl.rb,v 1.1 2003/07/23 16:11:29 gotoyuzo Exp $
15
+ =end
16
+
17
+ require 'openssl.so'
18
+
19
+ require 'openssl/bn'
20
+ require 'openssl/cipher'
21
+ require 'openssl/digest'
22
+ require 'openssl/ssl'
23
+ require 'openssl/x509'
24
+
data/lib/openssl/bn.rb ADDED
@@ -0,0 +1,35 @@
1
+ =begin
2
+ = $RCSfile: bn.rb,v $ -- Ruby-space definitions that completes C-space funcs for BN
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: bn.rb,v 1.1 2003/07/23 16:11:30 gotoyuzo Exp $
15
+ =end
16
+
17
+ ##
18
+ # Should we care what if somebody require this file directly?
19
+ #require 'openssl'
20
+
21
+ module OpenSSL
22
+ class BN
23
+ include Comparable
24
+ end # BN
25
+ end # OpenSSL
26
+
27
+ ##
28
+ # Add double dispatch to Integer
29
+ #
30
+ class Integer
31
+ def to_bn
32
+ OpenSSL::BN::new(self)
33
+ end
34
+ end # Integer
35
+
@@ -0,0 +1,239 @@
1
+ =begin
2
+ = $RCSfile: buffering.rb,v $ -- 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,v 1.5.2.4 2005/09/04 22:03:24 gotoyuzo Exp $
15
+ =end
16
+
17
+ module Buffering
18
+ include Enumerable
19
+ attr_accessor :sync
20
+ BLOCK_SIZE = 1024*16
21
+
22
+ def initialize(*args)
23
+ @eof = false
24
+ @rbuffer = ""
25
+ @sync = @io.sync
26
+ end
27
+
28
+ #
29
+ # for reading.
30
+ #
31
+ private
32
+
33
+ def fill_rbuff
34
+ begin
35
+ @rbuffer << self.sysread(BLOCK_SIZE)
36
+ rescue Errno::EAGAIN
37
+ retry
38
+ rescue EOFError
39
+ @eof = true
40
+ end
41
+ end
42
+
43
+ def consume_rbuff(size=nil)
44
+ if @rbuffer.empty?
45
+ nil
46
+ else
47
+ size = @rbuffer.size unless size
48
+ ret = @rbuffer[0, size]
49
+ @rbuffer[0, size] = ""
50
+ ret
51
+ end
52
+ end
53
+
54
+ public
55
+
56
+ def read(size=nil, buf=nil)
57
+ if size == 0
58
+ if buf
59
+ buf.clear
60
+ else
61
+ buf = ""
62
+ end
63
+ return @eof ? nil : buf
64
+ end
65
+ until @eof
66
+ break if size && size <= @rbuffer.size
67
+ fill_rbuff
68
+ end
69
+ ret = consume_rbuff(size) || ""
70
+ if buf
71
+ buf.replace(ret)
72
+ ret = buf
73
+ end
74
+ (size && ret.empty?) ? nil : ret
75
+ end
76
+
77
+ def readpartial(maxlen, buf=nil)
78
+ if maxlen == 0
79
+ if buf
80
+ buf.clear
81
+ else
82
+ buf = ""
83
+ end
84
+ return @eof ? nil : buf
85
+ end
86
+ if @rbuffer.empty?
87
+ begin
88
+ return sysread(maxlen, buf)
89
+ rescue Errno::EAGAIN
90
+ retry
91
+ end
92
+ end
93
+ ret = consume_rbuff(maxlen)
94
+ if buf
95
+ buf.replace(ret)
96
+ ret = buf
97
+ end
98
+ raise EOFError if ret.empty?
99
+ ret
100
+ end
101
+
102
+ def gets(eol=$/)
103
+ idx = @rbuffer.index(eol)
104
+ until @eof
105
+ break if idx
106
+ fill_rbuff
107
+ idx = @rbuffer.index(eol)
108
+ end
109
+ if eol.is_a?(Regexp)
110
+ size = idx ? idx+$&.size : nil
111
+ else
112
+ size = idx ? idx+eol.size : nil
113
+ end
114
+ consume_rbuff(size)
115
+ end
116
+
117
+ def each(eol=$/)
118
+ while line = self.gets(eol)
119
+ yield line
120
+ end
121
+ end
122
+ alias each_line each
123
+
124
+ def readlines(eol=$/)
125
+ ary = []
126
+ while line = self.gets(eol)
127
+ ary << line
128
+ end
129
+ ary
130
+ end
131
+
132
+ def readline(eol=$/)
133
+ raise EOFError if eof?
134
+ gets(eol)
135
+ end
136
+
137
+ def getc
138
+ c = read(1)
139
+ c ? c[0] : nil
140
+ end
141
+
142
+ def each_byte
143
+ while c = getc
144
+ yield(c)
145
+ end
146
+ end
147
+
148
+ def readchar
149
+ raise EOFError if eof?
150
+ getc
151
+ end
152
+
153
+ def ungetc(c)
154
+ @rbuffer[0,0] = c.chr
155
+ end
156
+
157
+ def eof?
158
+ fill_rbuff if !@eof && @rbuffer.empty?
159
+ @eof && @rbuffer.empty?
160
+ end
161
+ alias eof eof?
162
+
163
+ #
164
+ # for writing.
165
+ #
166
+ private
167
+
168
+ def do_write(s)
169
+ @wbuffer = "" unless defined? @wbuffer
170
+ @wbuffer << s
171
+ @sync ||= false
172
+ if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
173
+ remain = idx ? idx + $/.size : @wbuffer.length
174
+ nwritten = 0
175
+ while remain > 0
176
+ str = @wbuffer[nwritten,remain]
177
+ begin
178
+ nwrote = syswrite(str)
179
+ rescue Errno::EAGAIN
180
+ retry
181
+ end
182
+ remain -= nwrote
183
+ nwritten += nwrote
184
+ end
185
+ @wbuffer[0,nwritten] = ""
186
+ end
187
+ end
188
+
189
+ public
190
+
191
+ def write(s)
192
+ do_write(s)
193
+ s.length
194
+ end
195
+
196
+ def << (s)
197
+ do_write(s)
198
+ self
199
+ end
200
+
201
+ def puts(*args)
202
+ s = ""
203
+ if args.empty?
204
+ s << "\n"
205
+ end
206
+ args.each{|arg|
207
+ s << arg.to_s
208
+ if $/ && /\n\z/ !~ s
209
+ s << "\n"
210
+ end
211
+ }
212
+ do_write(s)
213
+ nil
214
+ end
215
+
216
+ def print(*args)
217
+ s = ""
218
+ args.each{ |arg| s << arg.to_s }
219
+ do_write(s)
220
+ nil
221
+ end
222
+
223
+ def printf(s, *args)
224
+ do_write(s % args)
225
+ nil
226
+ end
227
+
228
+ def flush
229
+ osync = @sync
230
+ @sync = true
231
+ do_write ""
232
+ @sync = osync
233
+ end
234
+
235
+ def close
236
+ flush rescue nil
237
+ sysclose
238
+ end
239
+ end
@@ -0,0 +1,58 @@
1
+ =begin
2
+ = $RCSfile: cipher.rb,v $ -- Ruby-space predefined Cipher subclasses
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: cipher.rb,v 1.1.2.2 2006/06/20 11:18:15 gotoyuzo Exp $
15
+ =end
16
+
17
+ ##
18
+ # Should we care what if somebody require this file directly?
19
+ #require 'openssl'
20
+
21
+ module OpenSSL
22
+ module 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
+ class Cipher
45
+ def random_key
46
+ str = OpenSSL::Random.random_bytes(self.key_len)
47
+ self.key = str
48
+ return str
49
+ end
50
+
51
+ def random_iv
52
+ str = OpenSSL::Random.random_bytes(self.iv_len)
53
+ self.iv = str
54
+ return str
55
+ end
56
+ end
57
+ end # Cipher
58
+ end # OpenSSL
@@ -0,0 +1,48 @@
1
+ =begin
2
+ = $RCSfile: digest.rb,v $ -- Ruby-space predefined Digest subclasses
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: digest.rb,v 1.1.2.2 2006/06/20 11:18:15 gotoyuzo Exp $
15
+ =end
16
+
17
+ ##
18
+ # Should we care what if somebody require this file directly?
19
+ #require 'openssl'
20
+
21
+ module OpenSSL
22
+ module Digest
23
+
24
+ alg = %w(DSS DSS1 MD2 MD4 MD5 MDC2 RIPEMD160 SHA SHA1)
25
+ if OPENSSL_VERSION_NUMBER > 0x00908000
26
+ alg += %w(SHA224 SHA256 SHA384 SHA512)
27
+ end
28
+ alg.each{|name|
29
+ klass = Class.new(Digest){
30
+ define_method(:initialize){|*data|
31
+ if data.length > 1
32
+ raise ArgumentError,
33
+ "wrong number of arguments (#{data.length} for 1)"
34
+ end
35
+ super(name, data.first)
36
+ }
37
+ }
38
+ singleton = (class <<klass; self; end)
39
+ singleton.class_eval{
40
+ define_method(:digest){|data| Digest.digest(name, data) }
41
+ define_method(:hexdigest){|data| Digest.hexdigest(name, data) }
42
+ }
43
+ const_set(name, klass)
44
+ }
45
+
46
+ end # Digest
47
+ end # OpenSSL
48
+
@@ -0,0 +1,135 @@
1
+ =begin
2
+ = $RCSfile: ssl.rb,v $ -- Ruby-space definitions that completes C-space funcs for SSL
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: ssl.rb,v 1.5.2.6 2006/05/23 18:14:05 gotoyuzo Exp $
15
+ =end
16
+
17
+ require "openssl"
18
+ require "openssl/buffering"
19
+ require "fcntl"
20
+
21
+ module OpenSSL
22
+ module SSL
23
+ module SocketForwarder
24
+ def addr
25
+ to_io.addr
26
+ end
27
+
28
+ def peeraddr
29
+ to_io.peeraddr
30
+ end
31
+
32
+ def setsockopt(level, optname, optval)
33
+ to_io.setsockopt(level, optname, optval)
34
+ end
35
+
36
+ def getsockopt(level, optname)
37
+ to_io.getsockopt(level, optname)
38
+ end
39
+
40
+ def fcntl(*args)
41
+ to_io.fcntl(*args)
42
+ end
43
+
44
+ def closed?
45
+ to_io.closed?
46
+ end
47
+
48
+ def do_not_reverse_lookup=(flag)
49
+ to_io.do_not_reverse_lookup = flag
50
+ end
51
+ end
52
+
53
+ module Nonblock
54
+ def initialize(*args)
55
+ flag = File::NONBLOCK
56
+ flag |= @io.fcntl(Fcntl::F_GETFL) if defined?(Fcntl::F_GETFL)
57
+ @io.fcntl(Fcntl::F_SETFL, flag)
58
+ super
59
+ end
60
+ end
61
+
62
+ class SSLSocket
63
+ include Buffering
64
+ include SocketForwarder
65
+ include Nonblock
66
+
67
+ def post_connection_check(hostname)
68
+ check_common_name = true
69
+ cert = peer_cert
70
+ cert.extensions.each{|ext|
71
+ next if ext.oid != "subjectAltName"
72
+ ext.value.split(/,\s+/).each{|general_name|
73
+ if /\ADNS:(.*)/ =~ general_name
74
+ check_common_name = false
75
+ reg = Regexp.escape($1).gsub(/\\\*/, "[^.]+")
76
+ return true if /\A#{reg}\z/i =~ hostname
77
+ elsif /\AIP Address:(.*)/ =~ general_name
78
+ check_common_name = false
79
+ return true if $1 == hostname
80
+ end
81
+ }
82
+ }
83
+ if check_common_name
84
+ cert.subject.to_a.each{|oid, value|
85
+ if oid == "CN"
86
+ reg = Regexp.escape(value).gsub(/\\\*/, "[^.]+")
87
+ return true if /\A#{reg}\z/i =~ hostname
88
+ end
89
+ }
90
+ end
91
+ raise SSLError, "hostname not match"
92
+ end
93
+ end
94
+
95
+ class SSLServer
96
+ include SocketForwarder
97
+ attr_accessor :start_immediately
98
+
99
+ def initialize(svr, ctx)
100
+ @svr = svr
101
+ @ctx = ctx
102
+ unless ctx.session_id_context
103
+ session_id = OpenSSL::Digest::MD5.hexdigest($0)
104
+ @ctx.session_id_context = session_id
105
+ end
106
+ @start_immediately = true
107
+ end
108
+
109
+ def to_io
110
+ @svr
111
+ end
112
+
113
+ def listen(backlog=5)
114
+ @svr.listen(backlog)
115
+ end
116
+
117
+ def accept
118
+ sock = @svr.accept
119
+ begin
120
+ ssl = OpenSSL::SSL::SSLSocket.new(sock, @ctx)
121
+ ssl.sync_close = true
122
+ ssl.accept if @start_immediately
123
+ ssl
124
+ rescue SSLError => ex
125
+ sock.close
126
+ raise ex
127
+ end
128
+ end
129
+
130
+ def close
131
+ @svr.close
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,154 @@
1
+ =begin
2
+ = $RCSfile: x509.rb,v $ -- Ruby-space definitions that completes C-space funcs for X509 and subclasses
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: x509.rb,v 1.4.2.2 2004/12/19 08:28:33 gotoyuzo Exp $
15
+ =end
16
+
17
+ require "openssl"
18
+
19
+ module OpenSSL
20
+ module X509
21
+ class ExtensionFactory
22
+ def create_extension(*arg)
23
+ if arg.size > 1
24
+ create_ext(*arg)
25
+ else
26
+ send("create_ext_from_"+arg[0].class.name.downcase, arg[0])
27
+ end
28
+ end
29
+
30
+ def create_ext_from_array(ary)
31
+ raise ExtensionError, "unexpected array form" if ary.size > 3
32
+ create_ext(ary[0], ary[1], ary[2])
33
+ end
34
+
35
+ def create_ext_from_string(str) # "oid = critical, value"
36
+ oid, value = str.split(/=/, 2)
37
+ oid.strip!
38
+ value.strip!
39
+ create_ext(oid, value)
40
+ end
41
+
42
+ def create_ext_from_hash(hash)
43
+ create_ext(hash["oid"], hash["value"], hash["critical"])
44
+ end
45
+ end
46
+
47
+ class Extension
48
+ def to_s # "oid = critical, value"
49
+ str = self.oid
50
+ str << " = "
51
+ str << "critical, " if self.critical?
52
+ str << self.value.gsub(/\n/, ", ")
53
+ end
54
+
55
+ def to_h # {"oid"=>sn|ln, "value"=>value, "critical"=>true|false}
56
+ {"oid"=>self.oid,"value"=>self.value,"critical"=>self.critical?}
57
+ end
58
+
59
+ def to_a
60
+ [ self.oid, self.value, self.critical? ]
61
+ end
62
+ end
63
+
64
+ class Name
65
+ module RFC2253DN
66
+ Special = ',=+<>#;'
67
+ HexChar = /[0-9a-fA-F]/
68
+ HexPair = /#{HexChar}#{HexChar}/
69
+ HexString = /#{HexPair}+/
70
+ Pair = /\\(?:[#{Special}]|\\|"|#{HexPair})/
71
+ StringChar = /[^#{Special}\\"]/
72
+ QuoteChar = /[^\\"]/
73
+ AttributeType = /[a-zA-Z][0-9a-zA-Z]*|[0-9]+(?:\.[0-9]+)*/
74
+ AttributeValue = /
75
+ (?!["#])((?:#{StringChar}|#{Pair})*)|
76
+ \#(#{HexString})|
77
+ "((?:#{QuoteChar}|#{Pair})*)"
78
+ /x
79
+ TypeAndValue = /\A(#{AttributeType})=#{AttributeValue}/
80
+
81
+ module_function
82
+
83
+ def expand_pair(str)
84
+ return nil unless str
85
+ return str.gsub(Pair){|pair|
86
+ case pair.size
87
+ when 2 then pair[1,1]
88
+ when 3 then Integer("0x#{pair[1,2]}").chr
89
+ else raise OpenSSL::X509::NameError, "invalid pair: #{str}"
90
+ end
91
+ }
92
+ end
93
+
94
+ def expand_hexstring(str)
95
+ return nil unless str
96
+ der = str.gsub(HexPair){|hex| Integer("0x#{hex}").chr }
97
+ a1 = OpenSSL::ASN1.decode(der)
98
+ return a1.value, a1.tag
99
+ end
100
+
101
+ def expand_value(str1, str2, str3)
102
+ value = expand_pair(str1)
103
+ value, tag = expand_hexstring(str2) unless value
104
+ value = expand_pair(str3) unless value
105
+ return value, tag
106
+ end
107
+
108
+ def scan(dn)
109
+ str = dn
110
+ ary = []
111
+ while true
112
+ if md = TypeAndValue.match(str)
113
+ matched = md.to_s
114
+ remain = md.post_match
115
+ type = md[1]
116
+ value, tag = expand_value(md[2], md[3], md[4]) rescue nil
117
+ if value
118
+ type_and_value = [type, value]
119
+ type_and_value.push(tag) if tag
120
+ ary.unshift(type_and_value)
121
+ if remain.length > 2 && remain[0] == ?,
122
+ str = remain[1..-1]
123
+ next
124
+ elsif remain.length > 2 && remain[0] == ?+
125
+ raise OpenSSL::X509::NameError,
126
+ "multi-valued RDN is not supported: #{dn}"
127
+ elsif remain.empty?
128
+ break
129
+ end
130
+ end
131
+ end
132
+ msg_dn = dn[0, dn.length - str.length] + " =>" + str
133
+ raise OpenSSL::X509::NameError, "malformed RDN: #{msg_dn}"
134
+ end
135
+ return ary
136
+ end
137
+ end
138
+
139
+ class <<self
140
+ def parse_rfc2253(str, template=OBJECT_TYPE_TEMPLATE)
141
+ ary = OpenSSL::X509::Name::RFC2253DN.scan(str)
142
+ self.new(ary, template)
143
+ end
144
+
145
+ def parse_openssl(str, template=OBJECT_TYPE_TEMPLATE)
146
+ ary = str.scan(/\s*([^\/,]+)\s*/).collect{|i| i[0].split("=", 2) }
147
+ self.new(ary, template)
148
+ end
149
+
150
+ alias parse parse_openssl
151
+ end
152
+ end
153
+ end
154
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubyforge_project:
3
+ has_rdoc: false
4
+ extra_rdoc_files: []
5
+ bindir: bin
6
+ specification_version: 1
7
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
8
+ requirements:
9
+ - - '>'
10
+ - !ruby/object:Gem::Version
11
+ version: 0.0.0
12
+ version:
13
+ extensions: []
14
+ email: ola.bini@ki.se
15
+ authors:
16
+ - Ola Bini
17
+ version: !ruby/object:Gem::Version
18
+ version: 0.0.1
19
+ post_install_message:
20
+ platform: ruby
21
+ rubygems_version: 0.9.1
22
+ description:
23
+ signing_key:
24
+ date: 2007-03-01 23:00:00 +00:00
25
+ dependencies: []
26
+ default_executable:
27
+ homepage: http://jruby-extras.rubyforge.org/
28
+ test_files: []
29
+ require_paths:
30
+ - lib
31
+ cert_chain:
32
+ requirements:
33
+ - Java 1.5
34
+ executables: []
35
+ summary: JRuby Openssl
36
+ files:
37
+ - lib/bcmail-jdk14-135.jar
38
+ - lib/bcprov-jdk14-135.jar
39
+ - lib/jopenssl.jar
40
+ - lib/openssl.rb
41
+ - lib/openssl/bn.rb
42
+ - lib/openssl/buffering.rb
43
+ - lib/openssl/cipher.rb
44
+ - lib/openssl/digest.rb
45
+ - lib/openssl/ssl.rb
46
+ - lib/openssl/x509.rb
47
+ autorequire:
48
+ rdoc_options: []
49
+ name: jruby-openssl