]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/crypto/TestSPKAC.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / crypto / TestSPKAC.java
1 package org.cacert.gigi.crypto;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.security.GeneralSecurityException;
8 import java.security.KeyPair;
9 import java.security.KeyPairGenerator;
10 import java.security.Signature;
11 import java.security.SignatureException;
12 import java.security.interfaces.RSAKey;
13 import java.util.Base64;
14
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.junit.Test;
17
18 import sun.security.x509.X509Key;
19
20 public class TestSPKAC {
21
22     @Test
23     public void testParse() throws GeneralSecurityException, IOException {
24         String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
25         SPKAC parsed = new SPKAC(Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", "")));
26         assertEquals("i am in the testcase", parsed.getChallenge());
27         RSAKey k = ((RSAKey) parsed.getPubkey());
28         assertEquals("a4004c2addf204fb26ce98f5963cc76def609ec0c50905e091fb84e986e3cb" + //
29                 "0d5e14edb9cb8e10524350bd2351589284a4f631ddf9b87f04ea0e58f7d8d816b58" + //
30                 "d052ce08b6576c04a7d45daf25b0ac9306f9cbb1f626e4ac301b7a4a3a062252b9a" + //
31                 "472b2cde5ec803407b18879a59ccba7716016b1de4537a005b2bd0fd6071", k.getModulus().toString(16));
32     }
33
34     @Test
35     public void testAddData() throws GeneralSecurityException, IOException {
36         String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
37         byte[] data = Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", ""));
38         byte[] tampered = new byte[data.length + 1];
39         System.arraycopy(data, 0, tampered, 0, data.length);
40         try {
41             new SPKAC(tampered);
42             fail("Expected illegal arg exception.");
43         } catch (IllegalArgumentException e) {
44             // expected
45         }
46         // change the last byte of the signature.
47         data[data.length - 1]--;
48         try {
49             new SPKAC(data);
50             fail("Expected SignatureException.");
51         } catch (SignatureException e) {
52             // expected
53         }
54     }
55
56     @Test
57     public void testGen() throws GeneralSecurityException, IOException {
58         KeyPairGenerator pkg = KeyPairGenerator.getInstance("RSA");
59         pkg.initialize(1024);
60         KeyPair kp = pkg.generateKeyPair();
61
62         SPKAC s = new SPKAC((X509Key) kp.getPublic(), "this is a even bigger challange");
63         Signature sign = Signature.getInstance("SHA512withRSA");
64         sign.initSign(kp.getPrivate());
65
66         byte[] res = s.getEncoded(sign);
67         SPKAC parsed = new SPKAC(res);
68         assertEquals(s.getChallenge(), parsed.getChallenge());
69         assertEquals(s.getPubkey(), parsed.getPubkey());
70
71     }
72 }