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'.)
19
class ExtensionFactory
20
def create_extension(*arg)
24
send("create_ext_from_"+arg[0].class.name.downcase, arg[0])
28
def create_ext_from_array(ary)
29
raise ExtensionError, "unexpected array form" if ary.size > 3
30
create_ext(ary[0], ary[1], ary[2])
33
def create_ext_from_string(str) # "oid = critical, value"
34
oid, value = str.split(/=/, 2)
37
create_ext(oid, value)
40
def create_ext_from_hash(hash)
41
create_ext(hash["oid"], hash["value"], hash["critical"])
46
def to_s # "oid = critical, value"
49
str << "critical, " if self.critical?
50
str << self.value.gsub(/\n/, ", ")
53
def to_h # {"oid"=>sn|ln, "value"=>value, "critical"=>true|false}
54
{"oid"=>self.oid,"value"=>self.value,"critical"=>self.critical?}
58
[ self.oid, self.value, self.critical? ]
65
HexChar = /[0-9a-fA-F]/
66
HexPair = /#{HexChar}#{HexChar}/
67
HexString = /#{HexPair}+/
68
Pair = /\\(?:[#{Special}]|\\|"|#{HexPair})/
69
StringChar = /[^#{Special}\\"]/
71
AttributeType = /[a-zA-Z][0-9a-zA-Z]*|[0-9]+(?:\.[0-9]+)*/
73
(?!["#])((?:#{StringChar}|#{Pair})*)|
75
"((?:#{QuoteChar}|#{Pair})*)"
77
TypeAndValue = /\A(#{AttributeType})=#{AttributeValue}/
83
return str.gsub(Pair){
87
when 3 then Integer("0x#{pair[1,2]}").chr
88
else raise OpenSSL::X509::NameError, "invalid pair: #{str}"
93
def expand_hexstring(str)
95
der = str.gsub(HexPair){$&.to_i(16).chr }
96
a1 = OpenSSL::ASN1.decode(der)
97
return a1.value, a1.tag
100
def expand_value(str1, str2, str3)
101
value = expand_pair(str1)
102
value, tag = expand_hexstring(str2) unless value
103
value = expand_pair(str3) unless value
111
if md = TypeAndValue.match(str)
113
remain = md.post_match
115
value, tag = expand_value(md[2], md[3], md[4]) rescue nil
117
type_and_value = [type, value]
118
type_and_value.push(tag) if tag
119
ary.unshift(type_and_value)
120
if remain.length > 2 && remain[0] == ?,
123
elsif remain.length > 2 && remain[0] == ?+
124
raise OpenSSL::X509::NameError,
125
"multi-valued RDN is not supported: #{dn}"
131
msg_dn = dn[0, dn.length - str.length] + " =>" + str
132
raise OpenSSL::X509::NameError, "malformed RDN: #{msg_dn}"
139
def parse_rfc2253(str, template=OBJECT_TYPE_TEMPLATE)
140
ary = OpenSSL::X509::Name::RFC2253DN.scan(str)
141
self.new(ary, template)
144
def parse_openssl(str, template=OBJECT_TYPE_TEMPLATE)
145
ary = str.scan(/\s*([^\/,]+)\s*/).collect{|i| i[0].split("=", 2) }
146
self.new(ary, template)
149
alias parse parse_openssl