| 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 |
package org.globus.gsi.bc; |
| 16 |
|
| 17 |
import org.globus.gsi.CertUtil; |
| 18 |
|
| 19 |
import java.security.Provider; |
| 20 |
|
| 21 |
import java.security.Security; |
| 22 |
|
| 23 |
import org.globus.common.CoGProperties; |
| 24 |
|
| 25 |
import java.security.KeyPairGenerator; |
| 26 |
import java.security.KeyPair; |
| 27 |
import java.io.StringWriter; |
| 28 |
import java.io.ByteArrayInputStream; |
| 29 |
|
| 30 |
import org.globus.gsi.OpenSSLKey; |
| 31 |
import org.globus.gsi.bc.BouncyCastleOpenSSLKey; |
| 32 |
|
| 33 |
import junit.framework.TestCase; |
| 34 |
|
| 35 |
import org.apache.commons.logging.Log; |
| 36 |
import org.apache.commons.logging.LogFactory; |
| 37 |
|
| 38 |
public class BouncyCastleOpenSSLKeyTest extends TestCase { |
| 39 |
|
| 40 |
private static final String pwd = "testpwd"; |
| 41 |
|
| 42 |
private Log logger = LogFactory.getLog(BouncyCastleOpenSSLKeyTest.class); |
| 43 |
|
| 44 |
private KeyPair getKeyPair() throws Exception { |
| 45 |
CertUtil.init(); |
| 46 |
|
| 47 |
int bits = 512; |
| 48 |
|
| 49 |
KeyPairGenerator keyGen = null; |
| 50 |
keyGen = KeyPairGenerator.getInstance("RSA", "BC"); |
| 51 |
keyGen.initialize(bits); |
| 52 |
|
| 53 |
return keyGen.genKeyPair(); |
| 54 |
} |
| 55 |
|
| 56 |
public void testEncrypt() throws Exception { |
| 57 |
KeyPair keyPair = getKeyPair(); |
| 58 |
|
| 59 |
OpenSSLKey key = new BouncyCastleOpenSSLKey(keyPair.getPrivate()); |
| 60 |
|
| 61 |
assertTrue(!key.isEncrypted()); |
| 62 |
|
| 63 |
key.encrypt(pwd); |
| 64 |
|
| 65 |
assertTrue(key.isEncrypted()); |
| 66 |
} |
| 67 |
|
| 68 |
public void testEncryptAES() throws Exception { |
| 69 |
KeyPair keyPair = getKeyPair(); |
| 70 |
|
| 71 |
OpenSSLKey key = new BouncyCastleOpenSSLKey(keyPair.getPrivate()); |
| 72 |
|
| 73 |
assertTrue(!key.isEncrypted()); |
| 74 |
|
| 75 |
key.setEncryptionAlgorithm("AES-128-CBC"); |
| 76 |
|
| 77 |
key.encrypt(pwd); |
| 78 |
|
| 79 |
assertTrue(key.isEncrypted()); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
private String toString(OpenSSLKey key) throws Exception { |
| 84 |
StringWriter writer = new StringWriter(); |
| 85 |
key.writeTo(writer); |
| 86 |
writer.close(); |
| 87 |
String s = writer.toString(); |
| 88 |
logger.debug(s); |
| 89 |
return s; |
| 90 |
} |
| 91 |
|
| 92 |
public void testDecryptedToString() throws Exception { |
| 93 |
KeyPair keyPair = getKeyPair(); |
| 94 |
OpenSSLKey inKey = new BouncyCastleOpenSSLKey(keyPair.getPrivate()); |
| 95 |
assertTrue(!inKey.isEncrypted()); |
| 96 |
|
| 97 |
ByteArrayInputStream in = null; |
| 98 |
in = new ByteArrayInputStream(toString(inKey).getBytes()); |
| 99 |
OpenSSLKey outKey = new BouncyCastleOpenSSLKey(in); |
| 100 |
assertTrue(!outKey.isEncrypted()); |
| 101 |
|
| 102 |
in = new ByteArrayInputStream(toString(outKey).getBytes()); |
| 103 |
OpenSSLKey outKey2 = new BouncyCastleOpenSSLKey(in); |
| 104 |
assertTrue(!outKey2.isEncrypted()); |
| 105 |
} |
| 106 |
|
| 107 |
public void testEcryptedToString() throws Exception { |
| 108 |
KeyPair keyPair = getKeyPair(); |
| 109 |
OpenSSLKey inKey = new BouncyCastleOpenSSLKey(keyPair.getPrivate()); |
| 110 |
assertTrue(!inKey.isEncrypted()); |
| 111 |
inKey.encrypt(pwd); |
| 112 |
assertTrue(inKey.isEncrypted()); |
| 113 |
|
| 114 |
ByteArrayInputStream in = null; |
| 115 |
in = new ByteArrayInputStream(toString(inKey).getBytes()); |
| 116 |
OpenSSLKey outKey = new BouncyCastleOpenSSLKey(in); |
| 117 |
assertTrue(outKey.isEncrypted()); |
| 118 |
|
| 119 |
in = new ByteArrayInputStream(toString(outKey).getBytes()); |
| 120 |
OpenSSLKey outKey2 = new BouncyCastleOpenSSLKey(in); |
| 121 |
assertTrue(outKey2.isEncrypted()); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|