1require_relative 'utils'
2
3if defined?(OpenSSL)
4
5class OpenSSL::TestNSSPI < Test::Unit::TestCase
6  def setup
7    # This request data is adopt from the specification of
8    # "Netscape Extensions for User Key Generation".
9    # -- http://wp.netscape.com/eng/security/comm4-keygen.html
10    @b64  = "MIHFMHEwXDANBgkqhkiG9w0BAQEFAANLADBIAkEAnX0TILJrOMUue+PtwBRE6XfV"
11    @b64 << "WtKQbsshxk5ZhcUwcwyvcnIq9b82QhJdoACdD34rqfCAIND46fXKQUnb0mvKzQID"
12    @b64 << "AQABFhFNb3ppbGxhSXNNeUZyaWVuZDANBgkqhkiG9w0BAQQFAANBAAKv2Eex2n/S"
13    @b64 << "r/7iJNroWlSzSMtTiQTEB+ADWHGj9u1xrUrOilq/o2cuQxIfZcNZkYAkWP4DubqW"
14    @b64 << "i0//rgBvmco="
15  end
16
17  def test_build_data
18    key1 = OpenSSL::TestUtils::TEST_KEY_RSA1024
19    key2 = OpenSSL::TestUtils::TEST_KEY_RSA2048
20    spki = OpenSSL::Netscape::SPKI.new
21    spki.challenge = "RandomString"
22    spki.public_key = key1.public_key
23    spki.sign(key1, OpenSSL::Digest::SHA1.new)
24    assert(spki.verify(spki.public_key))
25    assert(spki.verify(key1.public_key))
26    assert(!spki.verify(key2.public_key))
27
28    der = spki.to_der
29    spki = OpenSSL::Netscape::SPKI.new(der)
30    assert_equal("RandomString", spki.challenge)
31    assert_equal(key1.public_key.to_der, spki.public_key.to_der)
32    assert(spki.verify(spki.public_key))
33    assert_not_nil(spki.to_text)
34  end
35
36  def test_decode_data
37    spki = OpenSSL::Netscape::SPKI.new(@b64)
38    assert_equal(@b64, spki.to_pem)
39    assert_equal(@b64.unpack("m").first, spki.to_der)
40    assert_equal("MozillaIsMyFriend", spki.challenge)
41    assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
42
43    spki = OpenSSL::Netscape::SPKI.new(@b64.unpack("m").first)
44    assert_equal(@b64, spki.to_pem)
45    assert_equal(@b64.unpack("m").first, spki.to_der)
46    assert_equal("MozillaIsMyFriend", spki.challenge)
47    assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
48  end
49end
50
51end
52