1 package org.cacert.gigi.crypto;
3 import static org.junit.Assert.*;
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;
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.junit.Test;
18 import sun.security.x509.X509Key;
20 public class TestSPKAC {
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));
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);
42 fail("Expected illegal arg exception.");
43 } catch (IllegalArgumentException e) {
46 // change the last byte of the signature.
47 data[data.length - 1]--;
50 fail("Expected SignatureException.");
51 } catch (SignatureException e) {
57 public void testGen() throws GeneralSecurityException, IOException {
58 KeyPairGenerator pkg = KeyPairGenerator.getInstance("RSA");
60 KeyPair kp = pkg.generateKeyPair();
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());
66 byte[] res = s.getEncoded(sign);
67 SPKAC parsed = new SPKAC(res);
68 assertEquals(s.getChallenge(), parsed.getChallenge());
69 assertEquals(s.getPubkey(), parsed.getPubkey());