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