summaryrefslogtreecommitdiffstats
path: root/bcpkix/src/main/java/org/bouncycastle/mozilla/test/SPKACTest.java
blob: 192bfd86622296e90be1332636e8775c1e0b61b5 (plain)
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
package org.bouncycastle.mozilla.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.PublicKey;
import java.security.Security;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.DEROutputStream;
import org.bouncycastle.asn1.mozilla.PublicKeyAndChallenge;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.mozilla.SignedPublicKeyAndChallenge;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.test.SimpleTest;

public class SPKACTest
    extends SimpleTest
{
  byte[] spkac = Base64.decode(
         "MIIBOjCBpDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEApne7ti0ibPhV8Iht"+
         "7Pws5iRckM7x4mtZYxEpeX5/IO8tDsBFdY86ewuY2f2KCca0oMWr43kdkZbPyzf4"+
         "CSV+0fZm9MJyNMywygZjoOCC+rS8kr0Ef31iHChhYsyejJnjw116Jnn96syhdHY6"+
         "lVD1rK0nn5ZkHjxU74gjoZu6BJMCAwEAARYAMA0GCSqGSIb3DQEBBAUAA4GBAKFL"+
         "g/luv0C7gMTI8ZKfFoSyi7Q7kiSQcmSj1WJgT56ouIRJO5NdvB/1n4GNik8VOAU0"+
         "NRztvGy3ZGqgbSav7lrxcNEvXH+dLbtS97s7yiaozpsOcEHqsBribpLOTRzYa8ci"+
         "CwkPmIiYqcby11diKLpd+W9RFYNme2v0rrbM2CyV");
 

  public String getName()
  {
      return "SignedPubicKeyAndChallenge";
  }

  public void spkacTest(String testName, byte[] req)
      throws Exception
  {
      SignedPublicKeyAndChallenge spkac;

      spkac = new SignedPublicKeyAndChallenge(req);

      PublicKeyAndChallenge pkac = spkac.getPublicKeyAndChallenge();
      PublicKey pubKey = spkac.getPublicKey("BC");
      ASN1Primitive obj = pkac.toASN1Primitive();
      if (obj == null)
      {
          fail("Error - " + testName + " PKAC ASN1Primitive was null.");
      }
      
      obj = spkac.toASN1Primitive();
      if (obj == null)
      {
          fail("Error - "+testName+ " SPKAC ASN1Primitive was null.");
      }

      SubjectPublicKeyInfo spki = pkac.getSubjectPublicKeyInfo();
      if (spki == null)
      {
          fail("Error - "+testName + " SubjectPublicKeyInfo was null.");
      }
      
      DERIA5String challenge = pkac.getChallenge();
      // Most cases this will be a string of length zero.
      if (challenge == null)
      {
          fail(":Error - "+testName+ " challenge was null.");
      }
    
      ByteArrayInputStream    bIn = new ByteArrayInputStream(req);
      ASN1InputStream         dIn = new ASN1InputStream(bIn);


      ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
      DEROutputStream         dOut = new DEROutputStream(bOut);

      dOut.writeObject(spkac.toASN1Primitive());

      byte[]                  bytes = bOut.toByteArray();

      if (bytes.length != req.length)
      {
          fail(testName + " failed length test");
      }

      for (int i = 0; i != req.length; i++)
      {
          if (bytes[i] != req[i])
          {
              fail(testName + " failed comparison test");
          }
      }

      if (!spkac.verify("BC"))
      {
          fail(testName + " verification failed");
      }
  }

  public void performTest()
      throws Exception
  {
      spkacTest("spkac", spkac);
  }

  public static void main(String[] args)
  {
      Security.addProvider(new BouncyCastleProvider());
      
      runTest(new SPKACTest());
  }
}