1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
require_relative "utils"
if defined?(OpenSSL)
module OpenSSL
class TestPKCS12 < Test::Unit::TestCase
include OpenSSL::TestUtils
def setup
@mycert = cert
end
def test_create
pkcs12 = OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert
)
assert_equal @mycert, pkcs12.certificate
assert_equal TEST_KEY_RSA2048, pkcs12.key
assert_nil pkcs12.ca_certs
end
def test_create_no_pass
pkcs12 = OpenSSL::PKCS12.create(
nil,
"hello",
TEST_KEY_RSA2048,
@mycert
)
assert_equal @mycert, pkcs12.certificate
assert_equal TEST_KEY_RSA2048, pkcs12.key
assert_nil pkcs12.ca_certs
decoded = OpenSSL::PKCS12.new(pkcs12.to_der)
assert_cert @mycert, decoded.certificate
end
def test_create_with_chain
chain = [cert, cert]
pkcs12 = OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
chain
)
assert_equal chain, pkcs12.ca_certs
end
def test_create_with_bad_nid
assert_raises(ArgumentError) do
OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
[],
"foo"
)
end
end
def test_create_with_itr
OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
[],
nil,
nil,
2048
)
assert_raises(TypeError) do
OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
[],
nil,
nil,
"omg"
)
end
end
def test_create_with_mac_itr
OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
[],
nil,
nil,
nil,
2048
)
assert_raises(TypeError) do
OpenSSL::PKCS12.create(
"omg",
"hello",
TEST_KEY_RSA2048,
@mycert,
[],
nil,
nil,
nil,
"omg"
)
end
end
private
def assert_cert expected, actual
[
:subject,
:issuer,
:serial,
:not_before,
:not_after,
].each do |attribute|
assert_equal expected.send(attribute), actual.send(attribute)
end
end
def cert
ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
now = Time.now
ca_exts = [
["basicConstraints","CA:TRUE",true],
["keyUsage","keyCertSign, cRLSign",true],
["subjectKeyIdentifier","hash",false],
["authorityKeyIdentifier","keyid:always",false],
]
issue_cert(ca, TEST_KEY_RSA2048, 1, now, now+3600, ca_exts,
nil, nil, OpenSSL::Digest::SHA1.new)
end
end
end
end
|