1 # frozen_string_literal: true
2 require_relative "utils"
6 class OpenSSL::TestX509Attribute < OpenSSL::TestCase
8 ef = OpenSSL::X509::ExtensionFactory.new
9 val = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
10 ef.create_extension("keyUsage", "keyCertSign", true)
12 attr = OpenSSL::X509::Attribute.new("extReq", val)
13 assert_equal("extReq", attr.oid)
14 assert_equal(val.to_der, attr.value.to_der)
16 attr = OpenSSL::X509::Attribute.new("1.2.840.113549.1.9.14", val)
17 assert_equal("extReq", attr.oid)
21 # oid: challengePassword, values: Set[UTF8String<"abc123">]
22 test_der = "\x30\x15\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x07\x31\x08" \
23 "\x0c\x06\x61\x62\x63\x31\x32\x33".b
24 attr = OpenSSL::X509::Attribute.new(test_der)
25 assert_equal(test_der, attr.to_der)
26 assert_equal("challengePassword", attr.oid)
27 assert_equal("abc123", attr.value.value[0].value)
31 ef = OpenSSL::X509::ExtensionFactory.new
32 val = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
33 ef.create_extension("keyUsage", "keyCertSign", true)
35 attr = OpenSSL::X509::Attribute.new("extReq", val)
36 expected = OpenSSL::ASN1::Sequence.new([
37 OpenSSL::ASN1::ObjectId.new("extReq"),
40 assert_equal(expected.to_der, attr.to_der)
43 def test_invalid_value
44 # should not change the original value
45 test_der = "\x30\x15\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x07\x31\x08" \
46 "\x0c\x06\x61\x62\x63\x31\x32\x33".b
47 attr = OpenSSL::X509::Attribute.new(test_der)
48 assert_raise(TypeError) {
51 assert_equal(test_der, attr.to_der)
52 assert_raise(OpenSSL::X509::AttributeError) {
55 assert_equal(test_der, attr.to_der)
59 val = OpenSSL::ASN1::Set([
60 OpenSSL::ASN1::UTF8String("abc123")
62 attr = OpenSSL::X509::Attribute.new("challengePassword", val)
63 assert_equal(attr.to_der, attr.dup.to_der)
67 val1 = OpenSSL::ASN1::Set([
68 OpenSSL::ASN1::UTF8String("abc123")
70 attr1 = OpenSSL::X509::Attribute.new("challengePassword", val1)
71 attr2 = OpenSSL::X509::Attribute.new("challengePassword", val1)
72 ef = OpenSSL::X509::ExtensionFactory.new
73 val2 = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
74 ef.create_extension("keyUsage", "keyCertSign", true)
76 attr3 = OpenSSL::X509::Attribute.new("extReq", val2)
78 assert_equal false, attr1 == 12345
79 assert_equal true, attr1 == attr2
80 assert_equal false, attr1 == attr3
84 val = OpenSSL::ASN1::Set([
85 OpenSSL::ASN1::UTF8String("abc123")
87 attr = OpenSSL::X509::Attribute.new("challengePassword", val)
88 deserialized = Marshal.load(Marshal.dump(attr))
90 assert_equal attr.to_der, deserialized.to_der