1 package club.wpia.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.junit.Test;
17 import club.wpia.gigi.crypto.SPKAC;
18 import club.wpia.gigi.testUtils.IOUtils;
19 import sun.security.x509.X509Key;
21 public class TestSPKAC {
24 public void testParse() throws GeneralSecurityException, IOException {
25 String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
26 SPKAC parsed = new SPKAC(Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", "")));
27 assertEquals("i am in the testcase", parsed.getChallenge());
28 RSAKey k = ((RSAKey) parsed.getPubkey());
29 assertEquals("a4004c2addf204fb26ce98f5963cc76def609ec0c50905e091fb84e986e3cb" + //
30 "0d5e14edb9cb8e10524350bd2351589284a4f631ddf9b87f04ea0e58f7d8d816b58" + //
31 "d052ce08b6576c04a7d45daf25b0ac9306f9cbb1f626e4ac301b7a4a3a062252b9a" + //
32 "472b2cde5ec803407b18879a59ccba7716016b1de4537a005b2bd0fd6071", k.getModulus().toString(16));
36 public void testAddData() throws GeneralSecurityException, IOException {
37 String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
38 byte[] data = Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", ""));
39 byte[] tampered = new byte[data.length + 1];
40 System.arraycopy(data, 0, tampered, 0, data.length);
43 fail("Expected illegal arg exception.");
44 } catch (IllegalArgumentException e) {
47 // change the last byte of the signature.
48 data[data.length - 1]--;
51 fail("Expected SignatureException.");
52 } catch (SignatureException e) {
58 public void testGen() throws GeneralSecurityException, IOException {
59 KeyPairGenerator pkg = KeyPairGenerator.getInstance("RSA");
61 KeyPair kp = pkg.generateKeyPair();
63 SPKAC s = new SPKAC((X509Key) kp.getPublic(), "this is a even bigger challenge");
64 Signature sign = Signature.getInstance("SHA512withRSA");
65 sign.initSign(kp.getPrivate());
67 byte[] res = s.getEncoded(sign);
68 SPKAC parsed = new SPKAC(res);
69 assertEquals(s.getChallenge(), parsed.getChallenge());
70 assertEquals(s.getPubkey(), parsed.getPubkey());