1 package com.atlassian.security.auth.trustedapps;
2
3 import java.security.KeyPair;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.NoSuchProviderException;
6 import java.security.PrivateKey;
7 import java.security.PublicKey;
8 import java.security.spec.InvalidKeySpecException;
9
10 import junit.framework.TestCase;
11
12
13
14
15 public class TestBouncyCastleEncryptionProviderKeyPairGeneration extends TestCase
16 {
17 private final EncryptionProvider encryptionProvider = new BouncyCastleEncryptionProvider();
18 private final KeyPair keyPair;
19
20 public TestBouncyCastleEncryptionProviderKeyPairGeneration()
21 {
22 try
23 {
24 keyPair = encryptionProvider.generateNewKeyPair();
25 }
26 catch (NoSuchAlgorithmException e)
27 {
28 throw new RuntimeException(e);
29 }
30 catch (NoSuchProviderException e)
31 {
32 throw new RuntimeException(e);
33 }
34 }
35
36 public void testPrivateKey() throws Exception
37 {
38 assertEquals("RSA", keyPair.getPrivate().getAlgorithm());
39 byte[] data = keyPair.getPrivate().getEncoded();
40
41 PrivateKey privateKey = encryptionProvider.toPrivateKey(data);
42 assertEquals(keyPair.getPrivate(), privateKey);
43
44 try
45 {
46 data[7] += 7;
47 encryptionProvider.toPrivateKey(data);
48 fail("wrong data");
49 }
50 catch (InvalidKeySpecException e)
51 {
52
53 }
54 }
55
56 public void testPublicKey() throws Exception
57 {
58 assertEquals("RSA", keyPair.getPublic().getAlgorithm());
59 byte[] data = keyPair.getPublic().getEncoded();
60
61 PublicKey publicKey = encryptionProvider.toPublicKey(data);
62 assertEquals(keyPair.getPublic(), publicKey);
63
64 try
65 {
66 data[5] += 7;
67 encryptionProvider.toPublicKey(data);
68 fail("wrong data");
69 }
70 catch (InvalidKeySpecException e)
71 {
72
73 }
74 }
75
76 public void testCertificateLifecycle() throws Exception
77 {
78 EncryptedCertificate encrypted = encryptionProvider.createEncryptedCertificate("TestBouncyCastleEncryptionProvider", keyPair.getPrivate(), "myAppId");
79 assertNotNull(encrypted);
80 assertEquals("myAppId", encrypted.getID());
81 assertNotNull(encrypted.getCertificate());
82 assertNotNull(encrypted.getSecretKey());
83
84 ApplicationCertificate decrypted = encryptionProvider.decodeEncryptedCertificate(encrypted, keyPair.getPublic(), "myAppId");
85 assertNotNull(decrypted);
86 assertEquals("TestBouncyCastleEncryptionProvider", decrypted.getUserName());
87 assertEquals("myAppId", decrypted.getApplicationID());
88 }
89
90 public void testFunnyUserName() throws Exception
91 {
92 String user = "\u8FCE\u6B61\u5149\u81E8\u5178";
93 EncryptedCertificate encrypted = encryptionProvider.createEncryptedCertificate(user, keyPair.getPrivate(), "myAppId");
94 assertNotNull(encrypted);
95 assertEquals("myAppId", encrypted.getID());
96 assertNotNull(encrypted.getCertificate());
97 assertNotNull(encrypted.getSecretKey());
98
99 ApplicationCertificate decrypted = encryptionProvider.decodeEncryptedCertificate(encrypted, keyPair.getPublic(), "myAppId");
100 assertNotNull(decrypted);
101 assertEquals(user, decrypted.getUserName());
102 assertEquals("myAppId", decrypted.getApplicationID());
103 }
104 }