2017-09-03 08:35:27 -04:00
# coding: ASCII-8BIT
2020-02-16 01:21:29 -05:00
# frozen_string_literal: true
2010-12-15 14:50:00 -05:00
require_relative 'utils'
2004-05-17 16:23:28 -04:00
2017-09-03 08:35:27 -04:00
if defined? ( OpenSSL )
2004-05-19 14:30:52 -04:00
2016-05-18 00:07:47 -04:00
class OpenSSL :: TestX509Name < OpenSSL :: TestCase
2004-05-17 16:23:28 -04:00
def setup
2016-12-10 03:12:02 -05:00
super
2004-05-17 16:23:28 -04:00
@obj_type_tmpl = Hash . new ( OpenSSL :: ASN1 :: PRINTABLESTRING )
@obj_type_tmpl . update ( OpenSSL :: X509 :: Name :: OBJECT_TYPE_TEMPLATE )
end
def test_s_new
dn = [ [ " C " , " JP " ] , [ " O " , " example " ] , [ " CN " , " www.example.jp " ] ]
name = OpenSSL :: X509 :: Name . new ( dn )
ary = name . to_a
assert_equal ( " /C=JP/O=example/CN=www.example.jp " , name . to_s )
assert_equal ( " C " , ary [ 0 ] [ 0 ] )
assert_equal ( " O " , ary [ 1 ] [ 0 ] )
assert_equal ( " CN " , ary [ 2 ] [ 0 ] )
assert_equal ( " JP " , ary [ 0 ] [ 1 ] )
assert_equal ( " example " , ary [ 1 ] [ 1 ] )
assert_equal ( " www.example.jp " , ary [ 2 ] [ 1 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 2 ] [ 2 ] )
dn = [
[ " countryName " , " JP " ] ,
[ " organizationName " , " example " ] ,
[ " commonName " , " www.example.jp " ]
]
name = OpenSSL :: X509 :: Name . new ( dn )
ary = name . to_a
assert_equal ( " /C=JP/O=example/CN=www.example.jp " , name . to_s )
assert_equal ( " C " , ary [ 0 ] [ 0 ] )
assert_equal ( " O " , ary [ 1 ] [ 0 ] )
assert_equal ( " CN " , ary [ 2 ] [ 0 ] )
assert_equal ( " JP " , ary [ 0 ] [ 1 ] )
assert_equal ( " example " , ary [ 1 ] [ 1 ] )
assert_equal ( " www.example.jp " , ary [ 2 ] [ 1 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 2 ] [ 2 ] )
name = OpenSSL :: X509 :: Name . new ( dn , @obj_type_tmpl )
ary = name . to_a
assert_equal ( " /C=JP/O=example/CN=www.example.jp " , name . to_s )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 2 ] [ 2 ] )
dn = [
[ " countryName " , " JP " , OpenSSL :: ASN1 :: PRINTABLESTRING ] ,
[ " organizationName " , " example " , OpenSSL :: ASN1 :: PRINTABLESTRING ] ,
[ " commonName " , " www.example.jp " , OpenSSL :: ASN1 :: PRINTABLESTRING ]
]
name = OpenSSL :: X509 :: Name . new ( dn )
ary = name . to_a
assert_equal ( " /C=JP/O=example/CN=www.example.jp " , name . to_s )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 2 ] [ 2 ] )
dn = [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " GOTOU Yuuzou " ] ,
[ " emailAddress " , " gotoyuzo@ruby-lang.org " ] ,
[ " serialNumber " , " 123 " ] ,
]
name = OpenSSL :: X509 :: Name . new ( dn )
ary = name . to_a
2004-11-17 03:04:19 -05:00
assert_equal ( " /DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123 " , name . to_s )
2004-05-17 16:23:28 -04:00
assert_equal ( " DC " , ary [ 0 ] [ 0 ] )
assert_equal ( " DC " , ary [ 1 ] [ 0 ] )
assert_equal ( " CN " , ary [ 2 ] [ 0 ] )
2004-11-17 03:04:19 -05:00
assert_equal ( " emailAddress " , ary [ 3 ] [ 0 ] )
assert_equal ( " serialNumber " , ary [ 4 ] [ 0 ] )
2004-05-17 16:23:28 -04:00
assert_equal ( " org " , ary [ 0 ] [ 1 ] )
assert_equal ( " ruby-lang " , ary [ 1 ] [ 1 ] )
assert_equal ( " GOTOU Yuuzou " , ary [ 2 ] [ 1 ] )
assert_equal ( " gotoyuzo@ruby-lang.org " , ary [ 3 ] [ 1 ] )
assert_equal ( " 123 " , ary [ 4 ] [ 1 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 2 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 3 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 4 ] [ 2 ] )
name_from_der = OpenSSL :: X509 :: Name . new ( name . to_der )
assert_equal ( name_from_der . to_s , name . to_s )
assert_equal ( name_from_der . to_a , name . to_a )
assert_equal ( name_from_der . to_der , name . to_der )
end
2012-02-07 23:19:33 -05:00
def test_unrecognized_oid
dn = [ [ " 1.2.3.4.5.6.7.8.9.7.5.3.1 " , " Unknown OID 1 " ] ,
[ " 1.1.2.3.5.8.13.21.34 " , " Unknown OID 2 " ] ,
[ " C " , " US " ] ,
[ " postalCode " , " 60602 " ] ,
[ " ST " , " Illinois " ] ,
[ " L " , " Chicago " ] ,
2012-02-08 04:49:36 -05:00
#["street", "123 Fake St"],
2012-02-07 23:19:33 -05:00
[ " O " , " Some Company LLC " ] ,
[ " CN " , " mydomain.com " ] ]
name = OpenSSL :: X509 :: Name . new ( dn )
ary = name . to_a
2012-02-08 04:49:36 -05:00
#assert_equal("/1.2.3.4.5.6.7.8.9.7.5.3.1=Unknown OID 1/1.1.2.3.5.8.13.21.34=Unknown OID 2/C=US/postalCode=60602/ST=Illinois/L=Chicago/street=123 Fake St/O=Some Company LLC/CN=mydomain.com", name.to_s)
assert_equal ( " /1.2.3.4.5.6.7.8.9.7.5.3.1=Unknown OID 1/1.1.2.3.5.8.13.21.34=Unknown OID 2/C=US/postalCode=60602/ST=Illinois/L=Chicago/O=Some Company LLC/CN=mydomain.com " , name . to_s )
2012-02-07 23:19:33 -05:00
assert_equal ( " 1.2.3.4.5.6.7.8.9.7.5.3.1 " , ary [ 0 ] [ 0 ] )
assert_equal ( " 1.1.2.3.5.8.13.21.34 " , ary [ 1 ] [ 0 ] )
assert_equal ( " C " , ary [ 2 ] [ 0 ] )
assert_equal ( " postalCode " , ary [ 3 ] [ 0 ] )
assert_equal ( " ST " , ary [ 4 ] [ 0 ] )
assert_equal ( " L " , ary [ 5 ] [ 0 ] )
2012-02-08 04:49:36 -05:00
#assert_equal("street", ary[6][0])
assert_equal ( " O " , ary [ 6 ] [ 0 ] )
assert_equal ( " CN " , ary [ 7 ] [ 0 ] )
2012-02-07 23:19:33 -05:00
assert_equal ( " Unknown OID 1 " , ary [ 0 ] [ 1 ] )
assert_equal ( " Unknown OID 2 " , ary [ 1 ] [ 1 ] )
assert_equal ( " US " , ary [ 2 ] [ 1 ] )
assert_equal ( " 60602 " , ary [ 3 ] [ 1 ] )
assert_equal ( " Illinois " , ary [ 4 ] [ 1 ] )
assert_equal ( " Chicago " , ary [ 5 ] [ 1 ] )
2012-02-08 04:49:36 -05:00
#assert_equal("123 Fake St", ary[6][1])
assert_equal ( " Some Company LLC " , ary [ 6 ] [ 1 ] )
assert_equal ( " mydomain.com " , ary [ 7 ] [ 1 ] )
2012-02-07 23:19:33 -05:00
end
def test_unrecognized_oid_parse_encode_equality
dn = [ [ " 1.2.3.4.5.6.7.8.9.7.5.3.2 " , " Unknown OID1 " ] ,
[ " 1.1.2.3.5.8.13.21.35 " , " Unknown OID2 " ] ,
[ " C " , " US " ] ,
[ " postalCode " , " 60602 " ] ,
[ " ST " , " Illinois " ] ,
[ " L " , " Chicago " ] ,
2012-02-08 04:49:36 -05:00
#["street", "123 Fake St"],
2012-02-07 23:19:33 -05:00
[ " O " , " Some Company LLC " ] ,
[ " CN " , " mydomain.com " ] ]
name1 = OpenSSL :: X509 :: Name . new ( dn )
name2 = OpenSSL :: X509 :: Name . parse ( name1 . to_s )
assert_equal ( name1 . to_s , name2 . to_s )
assert_equal ( name1 . to_a , name2 . to_a )
end
2004-05-17 16:23:28 -04:00
def test_s_parse
2017-09-03 08:35:27 -04:00
dn = " /DC=org/DC=ruby-lang/CN=www.ruby-lang.org/1.2.3.4.5.6=A=BCD "
2004-05-17 16:23:28 -04:00
name = OpenSSL :: X509 :: Name . parse ( dn )
assert_equal ( dn , name . to_s )
ary = name . to_a
2017-09-03 08:35:27 -04:00
assert_equal [
[ " DC " , " org " , OpenSSL :: ASN1 :: IA5STRING ] ,
[ " DC " , " ruby-lang " , OpenSSL :: ASN1 :: IA5STRING ] ,
[ " CN " , " www.ruby-lang.org " , OpenSSL :: ASN1 :: UTF8STRING ] ,
[ " 1.2.3.4.5.6 " , " A=BCD " , OpenSSL :: ASN1 :: UTF8STRING ] ,
] , ary
2004-05-17 16:23:28 -04:00
2017-09-03 08:35:27 -04:00
dn2 = " DC=org, DC=ruby-lang, CN=www.ruby-lang.org, 1.2.3.4.5.6=A=BCD "
2010-09-10 04:54:50 -04:00
name = OpenSSL :: X509 :: Name . parse ( dn2 )
2004-05-17 16:23:28 -04:00
assert_equal ( dn , name . to_s )
2017-09-03 08:35:27 -04:00
assert_equal ary , name . to_a
2004-05-17 16:23:28 -04:00
2010-09-10 04:54:50 -04:00
name = OpenSSL :: X509 :: Name . parse ( dn2 , @obj_type_tmpl )
2004-05-17 16:23:28 -04:00
ary = name . to_a
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 2 ] [ 2 ] )
2017-09-03 08:35:27 -04:00
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 3 ] [ 2 ] )
2004-05-17 16:23:28 -04:00
end
2004-09-13 08:35:25 -04:00
def test_s_parse_rfc2253
scanner = OpenSSL :: X509 :: Name :: RFC2253DN . method ( :scan )
assert_equal ( [ [ " C " , " JP " ] ] , scanner . call ( " C=JP " ) )
assert_equal ( [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " GOTOU Yuuzou " ] ,
[ " emailAddress " , " gotoyuzo@ruby-lang.org " ] ,
] ,
scanner . call (
" emailAddress=gotoyuzo@ruby-lang.org,CN=GOTOU Yuuzou, " +
" DC=ruby-lang,DC=org " )
)
u8 = OpenSSL :: ASN1 :: UTF8STRING
assert_equal ( [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " O " , " ,=+<> # ; " ] ,
[ " O " , " ,=+<> # ; " ] ,
[ " OU " , " " ] ,
[ " OU " , " " ] ,
[ " L " , " aaa= \" bbb, ccc \" " ] ,
[ " L " , " aaa= \" bbb, ccc \" " ] ,
[ " CN " , " \345 \276 \214 \350 \227 \244 \350 \243 \225 \350 \224 \265 " ] ,
[ " CN " , " \345 \276 \214 \350 \227 \244 \350 \243 \225 \350 \224 \265 " ] ,
[ " CN " , " \345 \276 \214 \350 \227 \244 \350 \243 \225 \350 \224 \265 " ] ,
[ " CN " , " \345 \276 \214 \350 \227 \244 \350 \243 \225 \350 \224 \265 " , u8 ] ,
[ " 2.5.4.3 " , " GOTOU, Yuuzou " ] ,
[ " 2.5.4.3 " , " GOTOU, Yuuzou " ] ,
[ " 2.5.4.3 " , " GOTOU, Yuuzou " ] ,
[ " 2.5.4.3 " , " GOTOU, Yuuzou " ] ,
[ " CN " , " GOTOU \" gotoyuzo \" Yuuzou " ] ,
[ " CN " , " GOTOU \" gotoyuzo \" Yuuzou " ] ,
[ " 1.2.840.113549.1.9.1 " , " gotoyuzo@ruby-lang.org " ] ,
[ " emailAddress " , " gotoyuzo@ruby-lang.org " ] ,
] ,
scanner . call (
" emailAddress=gotoyuzo@ruby-lang.org, " +
" 1.2.840.113549.1.9.1=gotoyuzo@ruby-lang.org, " +
'CN=GOTOU \"gotoyuzo\" Yuuzou,' +
'CN="GOTOU \"gotoyuzo\" Yuuzou",' +
'2.5.4.3=GOTOU\,\20Yuuzou,' +
'2.5.4.3=GOTOU\, Yuuzou,' +
'2.5.4.3="GOTOU, Yuuzou",' +
'2.5.4.3="GOTOU\, Yuuzou",' +
" CN= # 0C0CE5BE8CE897A4E8A395E894B5, " +
'CN=\E5\BE\8C\E8\97\A4\E8\A3\95\E8\94\B5,' +
" CN= \" \xE5 \xBE \x8C \xE8 \x97 \xA4 \xE8 \xA3 \x95 \xE8 \x94 \xB5 \" , " +
" CN= \xE5 \xBE \x8C \xE8 \x97 \xA4 \xE8 \xA3 \x95 \xE8 \x94 \xB5 , " +
'L=aaa\=\"bbb\, ccc\",' +
'L="aaa=\"bbb, ccc\"",' +
'OU=,' +
'OU="",' +
'O=\,\=\+\<\>\#\;,' +
'O=",=+<>#;",' +
" DC=ruby-lang, " +
" DC=org " )
)
[
" DC=org+DC=jp " ,
" DC=org,DC=ruby-lang+DC=rubyist,DC=www "
] . each { | dn |
ex = scanner . call ( dn ) rescue $!
dn_r = Regexp . escape ( dn )
assert_match ( / ^multi-valued RDN is not supported: #{ dn_r } / , ex . message )
}
[
2020-02-16 01:21:29 -05:00
[ " DC=org,DC=exapmle,CN " , " CN " ] ,
2004-09-13 08:35:25 -04:00
[ " DC=org,DC=example, " , " " ] ,
2020-02-16 01:21:29 -05:00
[ " DC=org,DC=exapmle,CN=www.example.org; " , " CN=www.example.org; " ] ,
[ " DC=org,DC=exapmle,CN= # www.example.org " , " CN= # www.example.org " ] ,
[ " DC=org,DC=exapmle,CN= # 777777.example.org " , " CN= # 777777.example.org " ] ,
[ " DC=org,DC=exapmle,CN= \" www.example \" .org " , " CN= \" www.example \" .org " ] ,
[ " DC=org,DC=exapmle,CN=www. \" example.org \" " , " CN=www. \" example.org \" " ] ,
[ " DC=org,DC=exapmle,CN=www. \" example \" .org " , " CN=www. \" example \" .org " ] ,
2004-09-13 08:35:25 -04:00
] . each { | dn , msg |
ex = scanner . call ( dn ) rescue $!
assert_match ( / ^malformed RDN: .*=> #{ Regexp . escape ( msg ) } / , ex . message )
}
dn = " CN=www.ruby-lang.org,DC=ruby-lang,DC=org "
name = OpenSSL :: X509 :: Name . parse_rfc2253 ( dn )
assert_equal ( dn , name . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
ary = name . to_a
assert_equal ( " DC " , ary [ 0 ] [ 0 ] )
assert_equal ( " DC " , ary [ 1 ] [ 0 ] )
assert_equal ( " CN " , ary [ 2 ] [ 0 ] )
assert_equal ( " org " , ary [ 0 ] [ 1 ] )
assert_equal ( " ruby-lang " , ary [ 1 ] [ 1 ] )
assert_equal ( " www.ruby-lang.org " , ary [ 2 ] [ 1 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 2 ] [ 2 ] )
end
2004-05-17 16:23:28 -04:00
def test_add_entry
dn = [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " GOTOU Yuuzou " ] ,
[ " emailAddress " , " gotoyuzo@ruby-lang.org " ] ,
[ " serialNumber " , " 123 " ] ,
]
name = OpenSSL :: X509 :: Name . new
dn . each { | attr | name . add_entry ( * attr ) }
ary = name . to_a
2004-11-17 03:04:19 -05:00
assert_equal ( " /DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123 " , name . to_s )
2004-05-17 16:23:28 -04:00
assert_equal ( " DC " , ary [ 0 ] [ 0 ] )
assert_equal ( " DC " , ary [ 1 ] [ 0 ] )
assert_equal ( " CN " , ary [ 2 ] [ 0 ] )
2004-11-17 03:04:19 -05:00
assert_equal ( " emailAddress " , ary [ 3 ] [ 0 ] )
assert_equal ( " serialNumber " , ary [ 4 ] [ 0 ] )
2004-05-17 16:23:28 -04:00
assert_equal ( " org " , ary [ 0 ] [ 1 ] )
assert_equal ( " ruby-lang " , ary [ 1 ] [ 1 ] )
assert_equal ( " GOTOU Yuuzou " , ary [ 2 ] [ 1 ] )
assert_equal ( " gotoyuzo@ruby-lang.org " , ary [ 3 ] [ 1 ] )
assert_equal ( " 123 " , ary [ 4 ] [ 1 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 0 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 1 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: UTF8STRING , ary [ 2 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: IA5STRING , ary [ 3 ] [ 2 ] )
assert_equal ( OpenSSL :: ASN1 :: PRINTABLESTRING , ary [ 4 ] [ 2 ] )
end
2010-12-10 16:26:23 -05:00
2012-02-08 04:49:36 -05:00
def test_add_entry_street
# openssl/crypto/objects/obj_mac.h 1.83
dn = [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " GOTOU Yuuzou " ] ,
[ " emailAddress " , " gotoyuzo@ruby-lang.org " ] ,
[ " serialNumber " , " 123 " ] ,
[ " street " , " Namiki " ] ,
]
name = OpenSSL :: X509 :: Name . new
dn . each { | attr | name . add_entry ( * attr ) }
ary = name . to_a
assert_equal ( " /DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123/street=Namiki " , name . to_s )
assert_equal ( " Namiki " , ary [ 5 ] [ 1 ] )
end
2017-09-03 08:35:27 -04:00
def test_add_entry_placing
der = %w{ 30 2A
31 12
30 10 06 03 55 04 0 A 0 C 09 72 75 62 79 2 D 6 C 61 6 E 67
31 14
30 08 06 03 55 04 0 B 0 C 01 61
30 08 06 03 55 04 0 B 0 C 01 62 }
orig = OpenSSL :: X509 :: Name . new ( [ der . join ] . pack ( " H* " ) )
assert_equal ( " OU=b+OU=a,O=ruby-lang " , orig . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
# Skip for now; they do not work
#
# dn = orig.dup
# dn.add_entry("CN", "unya", loc: 0, set: 0)
# assert_equal("OU=b+OU=a,O=ruby-lang,CN=unya", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
# dn = orig.dup
# dn.add_entry("CN", "unya", loc: 0, set: 1)
# assert_equal("OU=b+OU=a,O=ruby-lang+CN=unya", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
dn = orig . dup
dn . add_entry ( " CN " , " unya " , loc : 1 , set : - 1 )
assert_equal ( " OU=b+OU=a,O=ruby-lang+CN=unya " , dn . dup . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
# dn = orig.dup
# dn.add_entry("CN", "unya", loc: 1, set: 0)
# assert_equal("OU=b+OU=a,CN=unya,O=ruby-lang", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
dn = orig . dup
dn . add_entry ( " CN " , " unya " , loc : 1 , set : 1 )
assert_equal ( " CN=unya+OU=b+OU=a,O=ruby-lang " , dn . dup . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
dn = orig . dup
dn . add_entry ( " CN " , " unya " , loc : - 1 , set : - 1 )
assert_equal ( " CN=unya+OU=b+OU=a,O=ruby-lang " , dn . dup . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
dn = orig . dup
dn . add_entry ( " CN " , " unya " , loc : - 1 , set : 0 )
assert_equal ( " CN=unya,OU=b+OU=a,O=ruby-lang " , dn . dup . to_s ( OpenSSL :: X509 :: Name :: RFC2253 ) )
end
def test_to_s
dn = [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " フー, バー " ] ,
]
name = OpenSSL :: X509 :: Name . new
dn . each { | x | name . add_entry ( * x ) }
assert_equal " /DC=org/DC=ruby-lang/ " \
" CN= \\ xE3 \\ x83 \\ x95 \\ xE3 \\ x83 \\ xBC, \\ xE3 \\ x83 \\ x90 \\ xE3 \\ x83 \\ xBC " ,
name . to_s
# OpenSSL escapes characters with MSB by default
assert_equal \
" CN= \\ E3 \\ 83 \\ 95 \\ E3 \\ 83 \\ BC \\ , \\ E3 \\ 83 \\ 90 \\ E3 \\ 83 \\ BC, " \
" DC=ruby-lang,DC=org " ,
name . to_s ( OpenSSL :: X509 :: Name :: RFC2253 )
assert_equal " DC = org, DC = ruby-lang, " \
" CN = \" \\ E3 \\ 83 \\ 95 \\ E3 \\ 83 \\ BC, \\ E3 \\ 83 \\ 90 \\ E3 \\ 83 \\ BC \" " ,
name . to_s ( OpenSSL :: X509 :: Name :: ONELINE )
2018-08-08 10:13:53 -04:00
empty = OpenSSL :: X509 :: Name . new
assert_equal " " , empty . to_s
assert_equal " " , empty . to_s ( OpenSSL :: X509 :: Name :: COMPAT )
assert_equal " " , empty . to_s ( OpenSSL :: X509 :: Name :: RFC2253 )
assert_equal " " , empty . to_s ( OpenSSL :: X509 :: Name :: ONELINE )
2017-09-03 08:35:27 -04:00
end
def test_to_utf8
dn = [
[ " DC " , " org " ] ,
[ " DC " , " ruby-lang " ] ,
[ " CN " , " フー, バー " ] ,
]
name = OpenSSL :: X509 :: Name . new
dn . each { | x | name . add_entry ( * x ) }
str = name . to_utf8
2020-02-16 01:21:29 -05:00
expected = String . new ( " CN=フー \\ , バー,DC=ruby-lang,DC=org " ) . force_encoding ( " UTF-8 " )
2017-09-03 08:35:27 -04:00
assert_equal expected , str
assert_equal Encoding . find ( " UTF-8 " ) , str . encoding
2018-08-08 10:13:53 -04:00
empty = OpenSSL :: X509 :: Name . new
assert_equal " " , empty . to_utf8
2017-09-03 08:35:27 -04:00
end
2010-12-10 16:26:23 -05:00
def test_equals2
2017-09-03 08:35:27 -04:00
n1 = OpenSSL :: X509 :: Name . parse_rfc2253 'CN=a'
n2 = OpenSSL :: X509 :: Name . parse_rfc2253 'CN=a'
2010-12-10 16:26:23 -05:00
assert_equal n1 , n2
2020-02-16 01:21:29 -05:00
assert_equal ( false , n1 == 'abc' )
assert_equal ( false , n2 == nil )
2010-12-10 16:26:23 -05:00
end
def test_spaceship
2018-10-17 20:24:49 -04:00
n1 = OpenSSL :: X509 :: Name . new ( [ [ " CN " , " a " ] ] )
n2 = OpenSSL :: X509 :: Name . new ( [ [ " CN " , " a " ] ] )
n3 = OpenSSL :: X509 :: Name . new ( [ [ " CN " , " ab " ] ] )
2019-06-01 08:07:35 -04:00
assert_equal ( 0 , n1 < = > n2 )
assert_equal ( - 1 , n1 < = > n3 )
assert_equal ( 0 , n2 < = > n1 )
assert_equal ( - 1 , n2 < = > n3 )
assert_equal ( 1 , n3 < = > n1 )
assert_equal ( 1 , n3 < = > n2 )
2020-02-16 01:21:29 -05:00
assert_equal ( nil , n1 < = > 'abc' )
assert_equal ( nil , n2 < = > 123 )
assert_equal ( nil , n3 < = > nil )
2010-12-10 16:26:23 -05:00
end
2011-06-23 09:51:55 -04:00
def name_hash ( name )
# OpenSSL 1.0.0 uses SHA1 for canonical encoding (not just a der) of
# X509Name for X509_NAME_hash.
name . respond_to? ( :hash_old ) ? name . hash_old : name . hash
end
def test_hash
dn = " /DC=org/DC=ruby-lang/CN=www.ruby-lang.org "
name = OpenSSL :: X509 :: Name . parse ( dn )
2020-04-19 11:14:36 -04:00
d = OpenSSL :: Digest . digest ( 'MD5' , name . to_der )
2011-06-23 09:51:55 -04:00
expected = ( d [ 0 ] . ord & 0xff ) | ( d [ 1 ] . ord & 0xff ) << 8 | ( d [ 2 ] . ord & 0xff ) << 16 | ( d [ 3 ] . ord & 0xff ) << 24
assert_equal ( expected , name_hash ( name ) )
#
dn = " /DC=org/DC=ruby-lang/CN=baz.ruby-lang.org "
name = OpenSSL :: X509 :: Name . parse ( dn )
2020-04-19 11:14:36 -04:00
d = OpenSSL :: Digest . digest ( 'MD5' , name . to_der )
2011-06-23 09:51:55 -04:00
expected = ( d [ 0 ] . ord & 0xff ) | ( d [ 1 ] . ord & 0xff ) << 8 | ( d [ 2 ] . ord & 0xff ) << 16 | ( d [ 3 ] . ord & 0xff ) << 24
assert_equal ( expected , name_hash ( name ) )
end
2016-08-29 01:47:09 -04:00
2017-01-31 05:08:22 -05:00
def test_equality
name0 = OpenSSL :: X509 :: Name . new ( [ [ " DC " , " org " ] , [ " DC " , " ruby-lang " ] , [ " CN " , " bar.ruby-lang.org " ] ] )
name1 = OpenSSL :: X509 :: Name . new ( [ [ " DC " , " org " ] , [ " DC " , " ruby-lang " ] , [ " CN " , " bar.ruby-lang.org " ] ] )
name2 = OpenSSL :: X509 :: Name . new ( [ [ " DC " , " org " ] , [ " DC " , " ruby-lang " ] , [ " CN " , " baz.ruby-lang.org " ] ] )
assert_equal true , name0 == name1
assert_equal true , name0 . eql? ( name1 )
assert_equal false , name0 == name2
assert_equal false , name0 . eql? ( name2 )
end
2020-02-16 01:21:29 -05:00
def test_marshal
name = OpenSSL :: X509 :: Name . new ( [ [ " DC " , " org " ] , [ " DC " , " ruby-lang " ] , [ " CN " , " bar.ruby-lang.org " ] ] )
deserialized = Marshal . load ( Marshal . dump ( name ) )
assert_equal name . to_der , deserialized . to_der
end
2016-08-29 01:47:09 -04:00
def test_dup
name = OpenSSL :: X509 :: Name . parse ( " /CN=ruby-lang.org " )
assert_equal ( name . to_der , name . dup . to_der )
end
2004-05-17 16:23:28 -04:00
end
2004-05-19 14:30:52 -04:00
end