Created
November 12, 2012 18:33
-
-
Save vbatts/4061036 to your computer and use it in GitHub Desktop.
SSL ASN1 decoding variant from MRI to Jruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'find' | |
require 'openssl' | |
ENTRUST_VERSION_OID = '1.2.840.113533.7.65.0' | |
def find_pem(d = '/etc') | |
Find.find(d) do |path| | |
next unless FileTest.file? path | |
if path.end_with? '.pem' | |
if block_given? | |
yield path | |
else | |
return path | |
end | |
end | |
end | |
end | |
def value_from_oid(cert, oid) | |
ext = cert.extensions.detect {|ext| ext.oid == oid } | |
return unless ext | |
return OpenSSL::ASN1.decode(OpenSSL::ASN1.decode(ext.to_der).value[1].value).value | |
end | |
def main(args) | |
find_pem do |pem| | |
cert = OpenSSL::X509::Certificate.new(File.read(pem)) | |
next unless ext = cert.extensions.detect {|e| e.oid == ENTRUST_VERSION_OID} | |
puts value_from_oid(cert, ext.oid) | |
end | |
end | |
main(ARGV) if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ruby -v ssl_test.rb
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
ssl_test.rb:21: warning: shadowing outer local variable - ext
OpenSSL::ASN1::GeneralString:0x0000000167d620
OpenSSL::ASN1::BitString:0x0000000167d5d0
OpenSSL::ASN1::GeneralString:0x0000000167cb80
OpenSSL::ASN1::BitString:0x0000000167cb30
OpenSSL::ASN1::GeneralString:0x0000000167ba28
OpenSSL::ASN1::BitString:0x0000000167b9d8
OpenSSL::ASN1::GeneralString:0x0000000167afb0
OpenSSL::ASN1::BitString:0x0000000167af38
OpenSSL::ASN1::GeneralString:0x0000000167a650
OpenSSL::ASN1::BitString:0x0000000167a600
OpenSSL::ASN1::GeneralString:0x00000001681388
OpenSSL::ASN1::BitString:0x00000001681338
OpenSSL::ASN1::GeneralString:0x00000001680280
OpenSSL::ASN1::BitString:0x00000001680230
OpenSSL::ASN1::GeneralString:0x00000001694438
OpenSSL::ASN1::BitString:0x000000016943c0
OpenSSL::ASN1::GeneralString:0x00000001693740
OpenSSL::ASN1::BitString:0x000000016936f0
$ jruby -v ssl_test.rb
jruby 1.6.7.2 (ruby-1.9.2-p312) (2012-05-01 26e08ba) (OpenJDK 64-Bit Server VM 1.7.0_07) [linux-amd64-java]
ssl_test.rb:21 warning: shadowing outer local variable - ext
ArgumentError: (TypeError) can't convert false into String
decode at org/jruby/ext/openssl/ASN1.java:585
value_from_oid at ssl_test.rb:24
main at ssl_test.rb:31
find_pem at ssl_test.rb:12
find at /opt/jruby/lib/ruby/1.9/find.rb:41
catch at org/jruby/RubyKernel.java:1197
find at /opt/jruby/lib/ruby/1.9/find.rb:40
find_pem at ssl_test.rb:8
main at ssl_test.rb:28
(root) at ssl_test.rb:35
$ java -jar /home/vbatts/src/jruby/lib/jruby-complete.jar -v ssl_test.rb
jruby 1.7.1.dev (1.9.3p286) 2012-11-12 f0cdc2b on OpenJDK 64-Bit Server VM 1.7.0_07-b30 [linux-amd64]
ssl_test.rb:21 warning: shadowing outer local variable - ext
ArgumentError: (TypeError) can't convert false into String
decode at org/jruby/ext/openssl/ASN1.java:588
value_from_oid at ssl_test.rb:24
main at ssl_test.rb:31
find_pem at ssl_test.rb:12
find at jar:file:/home/vbatts/src/jruby/lib/jruby-complete.jar!/META-INF/jruby.home/lib/ruby/1.9/find.rb:41
catch at org/jruby/RubyKernel.java:1175
find at jar:file:/home/vbatts/src/jruby/lib/jruby-complete.jar!/META-INF/jruby.home/lib/ruby/1.9/find.rb:40
find_pem at ssl_test.rb:8
main at ssl_test.rb:28
(root) at ssl_test.rb:35