2
= $RCSfile$ -- Ruby-space definitions that completes C-space funcs for X509 and subclasses
5
'OpenSSL for Ruby 2' project
6
Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz>
10
This program is licenced under the same licence as Ruby.
11
(See the file 'LICENCE'.)
14
$Id: x509.rb 11708 2007-02-12 23:01:19Z shyouhei $
21
class ExtensionFactory
22
def create_extension(*arg)
26
send("create_ext_from_"+arg[0].class.name.downcase, arg[0])
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])
35
def create_ext_from_string(str) # "oid = critical, value"
36
oid, value = str.split(/=/, 2)
39
create_ext(oid, value)
42
def create_ext_from_hash(hash)
43
create_ext(hash["oid"], hash["value"], hash["critical"])
48
def to_s # "oid = critical, value"
51
str << "critical, " if self.critical?
52
str << self.value.gsub(/\n/, ", ")
55
def to_h # {"oid"=>sn|ln, "value"=>value, "critical"=>true|false}
56
{"oid"=>self.oid,"value"=>self.value,"critical"=>self.critical?}
60
[ self.oid, self.value, self.critical? ]
67
HexChar = /[0-9a-fA-F]/
68
HexPair = /#{HexChar}#{HexChar}/
69
HexString = /#{HexPair}+/
70
Pair = /\\(?:[#{Special}]|\\|"|#{HexPair})/
71
StringChar = /[^#{Special}\\"]/
73
AttributeType = /[a-zA-Z][0-9a-zA-Z]*|[0-9]+(?:\.[0-9]+)*/
75
(?!["#])((?:#{StringChar}|#{Pair})*)|
77
"((?:#{QuoteChar}|#{Pair})*)"
79
TypeAndValue = /\A(#{AttributeType})=#{AttributeValue}/
85
return str.gsub(Pair){|pair|
88
when 3 then Integer("0x#{pair[1,2]}").chr
89
else raise OpenSSL::X509::NameError, "invalid pair: #{str}"
94
def expand_hexstring(str)
96
der = str.gsub(HexPair){|hex| Integer("0x#{hex}").chr }
97
a1 = OpenSSL::ASN1.decode(der)
98
return a1.value, a1.tag
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
112
if md = TypeAndValue.match(str)
114
remain = md.post_match
116
value, tag = expand_value(md[2], md[3], md[4]) rescue nil
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] == ?,
124
elsif remain.length > 2 && remain[0] == ?+
125
raise OpenSSL::X509::NameError,
126
"multi-valued RDN is not supported: #{dn}"
132
msg_dn = dn[0, dn.length - str.length] + " =>" + str
133
raise OpenSSL::X509::NameError, "malformed RDN: #{msg_dn}"
140
def parse_rfc2253(str, template=OBJECT_TYPE_TEMPLATE)
141
ary = OpenSSL::X509::Name::RFC2253DN.scan(str)
142
self.new(ary, template)
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)
150
alias parse parse_openssl