Debian Codegraph @

ssl-proxies/src/test/java/org/globus/gsi/OpenSSLKeyTest.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15
16 package org.globus.gsi;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.junit.Assert.assertThat;
27 import java.security.GeneralSecurityException;
28 import java.security.PrivateKey;
29 import org.globus.gsi.bc.BouncyCastleOpenSSLKey;
30 import org.globus.gsi.testutils.FileSetupUtil;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36 /**
37 * Created by IntelliJ IDEA. User: turtlebender Date: Dec 31, 2009 Time: 9:54:25
38 * AM To change this template use File | Settings | File Templates.
39 */
40 @Category( { SecurityTest.class })
41 public class OpenSSLKeyTest {
42
43 static FileSetupUtil file;
44
45 @BeforeClass
46 public static void setup() throws Exception {
47 file = new FileSetupUtil("key.pem");
48 file.copyFileToTemp();
49 file.getTempFile();
50 }
51
52 @AfterClass
53 public static void cleanup() throws Exception {
54 file.deleteFile();
55 }
56
57 @Test
58 public void testOpenSSLKeyCreation() throws Exception {
59 OpenSSLKey opensslkey = new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
60 byte[] encoded = opensslkey.getEncoded();
61 OpenSSLKey byteStreamInit = new BouncyCastleOpenSSLKey("RSA", encoded);
62 assertThat(opensslkey.getEncoded(), is(byteStreamInit.getEncoded()));
63 PrivateKey privateKey = opensslkey.getPrivateKey();
64 OpenSSLKey privateKeyInit = new BouncyCastleOpenSSLKey(privateKey);
65 assertThat(opensslkey.getEncoded(), is(privateKeyInit.getEncoded()));
66 opensslkey.encrypt("password");
67 assertThat(opensslkey.getEncoded(), is(not(encoded)));
68 byteStreamInit.encrypt("password");
69 opensslkey = new BouncyCastleOpenSSLKey(opensslkey.getPrivateKey());
70 opensslkey.decrypt("password");
71 byteStreamInit = new BouncyCastleOpenSSLKey(byteStreamInit.getPrivateKey());
72 byteStreamInit.decrypt("password");
73 assertThat(opensslkey.getEncoded(), is(byteStreamInit.getEncoded()));
74 }
75
76 @Test(expected = IllegalArgumentException.class)
77 public void testNullByteStream() throws Exception {
78 new BouncyCastleOpenSSLKey("RSA", null);
79 }
80
81 @Test(expected = GeneralSecurityException.class)
82 public void testEmptyByteStream() throws Exception {
83 new BouncyCastleOpenSSLKey("RSA", new byte[] {});
84 }
85
86 // @Test
87 // public void testNullAlgo() throws Exception{
88 // new BouncyCastleOpenSSLKey(null, new byte[]{});
89 // }
90
91
92 @Test
93 public void testEqualsNull() throws Exception
94 {
95 OpenSSLKey key =
96 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
97 assertThat(key, not(equalTo(null)));
98 }
99
100 @Test
101 public void testEqualsReflexive() throws Exception
102 {
103 OpenSSLKey key =
104 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
105
106 assertThat(key, equalTo(key));
107 }
108
109 @Test
110 public void testEqualsSymmetricForEqualKeys() throws Exception
111 {
112 OpenSSLKey key1 =
113 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
114 OpenSSLKey key2 =
115 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
116
117 assertThat(key2, equalTo(key1));
118 assertThat(key1, equalTo(key2));
119 }
120
121 @Test
122 public void testEqualsForKeysDifferingByEncrypted() throws Exception
123 {
124 OpenSSLKey key1 =
125 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
126 OpenSSLKey key2 =
127 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
128 key2.encrypt("too many secrets");
129
130 assertThat(key2, not(equalTo(key1)));
131 }
132
133
134 @Test
135 public void testSerializableUnencrypted() throws Exception {
136 OpenSSLKey key =
137 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
138 OpenSSLKey copy = serialiseAndDeserialise(key);
139
140 assertThat(copy, equalTo(key));
141 }
142
143
144 @Test
145 public void testSerializableEncrypted() throws Exception {
146 OpenSSLKey key =
147 new BouncyCastleOpenSSLKey(file.getAbsoluteFilename());
148 key.encrypt("too many secrets");
149 OpenSSLKey copy = serialiseAndDeserialise(key);
150
151 assertThat(copy, equalTo(key));
152 }
153
154
155 private OpenSSLKey serialiseAndDeserialise(OpenSSLKey key) throws IOException, ClassNotFoundException {
156 ByteArrayOutputStream storage = new ByteArrayOutputStream();
157 new ObjectOutputStream(storage).writeObject(key);
158 byte[] data = storage.toByteArray();
159
160 ObjectInputStream in =
161 new ObjectInputStream(new ByteArrayInputStream(data));
162 return (OpenSSLKey) in.readObject();
163 }
164 }