1 13 14 package org.ejbca.core.protocol.xkms; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.math.BigInteger ; 19 import java.security.KeyFactory ; 20 import java.security.KeyPair ; 21 import java.security.cert.X509Certificate ; 22 import java.security.interfaces.RSAPrivateCrtKey ; 23 import java.security.interfaces.RSAPrivateKey ; 24 import java.security.interfaces.RSAPublicKey ; 25 import java.security.spec.RSAPublicKeySpec ; 26 27 import javax.crypto.SecretKey; 28 import javax.xml.bind.JAXBContext; 29 import javax.xml.bind.JAXBElement; 30 import javax.xml.bind.JAXBException; 31 import javax.xml.bind.Marshaller; 32 import javax.xml.bind.PropertyException; 33 import javax.xml.bind.Unmarshaller; 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 37 import junit.framework.TestCase; 38 39 import org.apache.log4j.Logger; 40 import org.apache.xml.security.utils.XMLUtils; 41 import org.bouncycastle.util.encoders.Hex; 42 import org.ejbca.core.protocol.xkms.common.XKMSConstants; 43 import org.ejbca.core.protocol.xkms.common.XKMSNamespacePrefixMapper; 44 import org.ejbca.core.protocol.xkms.common.XKMSUtil; 45 import org.ejbca.util.Base64; 46 import org.ejbca.util.CertTools; 47 import org.ejbca.util.KeyTools; 48 import org.w3._2000._09.xmldsig_.KeyInfoType; 49 import org.w3._2000._09.xmldsig_.RSAKeyValueType; 50 import org.w3._2002._03.xkms_.ObjectFactory; 51 import org.w3._2002._03.xkms_.PrivateKeyType; 52 import org.w3._2002._03.xkms_.PrototypeKeyBindingType; 53 import org.w3._2002._03.xkms_.RegisterRequestType; 54 import org.w3._2002._03.xkms_.RegisterResultType; 55 import org.w3._2002._03.xkms_.UseKeyWithType; 56 import org.w3c.dom.Document ; 57 58 66 67 public class TestXKMSEnc extends TestCase { 68 69 private static Logger log = Logger.getLogger(TestXKMSEnc.class); 70 71 private ObjectFactory xKMSObjectFactory = new ObjectFactory(); 72 private org.w3._2000._09.xmldsig_.ObjectFactory sigFactory = new org.w3._2000._09.xmldsig_.ObjectFactory(); 73 74 private static JAXBContext jAXBContext = null; 75 private static Marshaller marshaller = null; 76 private static Unmarshaller unmarshaller = null; 77 private static DocumentBuilderFactory dbf = null; 78 79 80 static{ 81 try { 82 CertTools.installBCProvider(); 83 org.apache.xml.security.Init.init(); 84 85 jAXBContext = JAXBContext.newInstance("org.w3._2002._03.xkms_:org.w3._2001._04.xmlenc_:org.w3._2000._09.xmldsig_"); 86 marshaller = jAXBContext.createMarshaller(); 87 try { 88 marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",new XKMSNamespacePrefixMapper()); 89 } catch( PropertyException e ) { 90 log.error("Error registering namespace mapper property",e); 91 } 92 dbf = DocumentBuilderFactory.newInstance(); 93 dbf.setNamespaceAware(true); 94 unmarshaller = jAXBContext.createUnmarshaller(); 95 96 } catch (JAXBException e) { 97 log.error("Error initializing RequestAbstractTypeResponseGenerator",e); 98 } 99 100 } 101 102 protected void setUp() throws Exception { 103 log.debug(">setUp()"); 104 105 106 107 log.debug("<setUp()"); 108 } 109 110 protected void tearDown() throws Exception { 111 } 112 113 public void test01KeyEncryption() throws Exception { 114 DocumentBuilder db = dbf.newDocumentBuilder(); 115 KeyPair keys = KeyTools.genKeys("1024", "RSA"); 116 RegisterResultType registerResultType = xKMSObjectFactory.createRegisterResultType(); 117 JAXBElement<RegisterResultType> registerResult = xKMSObjectFactory.createRegisterResult(registerResultType); 118 119 PrivateKeyType privateKeyType1 = XKMSUtil.getEncryptedXMLFromPrivateKey( (RSAPrivateCrtKey ) keys.getPrivate(), "This is total crap"); 120 registerResultType.setPrivateKey(privateKeyType1); 121 122 System.out.print("\n\n"); 123 124 Document registerResultDoc = db.newDocument(); 125 marshaller.marshal( registerResult, registerResultDoc ); 126 127 XMLUtils.outputDOM(registerResultDoc, System.out); 128 129 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 130 XMLUtils.outputDOM(registerResultDoc, baos); 131 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 132 133 System.out.print("\n\n"); 134 135 JAXBElement<RegisterResultType> registerResult2 = (JAXBElement<RegisterResultType>) unmarshaller.unmarshal(bais); 136 registerResultType = registerResult2.getValue(); 137 138 PrivateKeyType privateKeyType2 = registerResultType.getPrivateKey(); 139 140 RSAPrivateKey privkey2 = XKMSUtil.getPrivateKeyFromEncryptedXML(privateKeyType2, "This is total crap"); 141 142 X509Certificate cert = CertTools.genSelfCert("CN=test", 10, null,privkey2, keys.getPublic(), "SHA1WithRSA", true); 143 144 cert.verify(keys.getPublic()); 145 } 146 147 public void test02TestAliceRegistrationAuthenticationKey() throws Exception { 148 String authenticationData= "024837"; 149 150 SecretKey retval = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 20, XKMSUtil.KEY_AUTHENTICATION); 151 152 assertTrue(retval.getEncoded().length == 20); 153 154 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 155 Hex.encode(retval.getEncoded(), baos); 156 157 String resultString = new String (baos.toByteArray()); 158 assertTrue(resultString.equalsIgnoreCase("d6cc34cb83fae2993a393aa8e7de9a06c7fa2c92")); 159 } 160 161 162 163 public void test03TestBOBRegistrationPrivateKeyEncryption() throws Exception { 164 165 String authenticationData= "3N9CJ-K4JKS-04JWF-0934J-SR09JW-IK4"; 166 167 SecretKey retval = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 24, XKMSUtil.KEY_PRIVATEKEYDATA); 168 169 assertTrue(retval.getEncoded().length == 24); 170 171 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 172 Hex.encode(retval.getEncoded(), baos); 173 174 String resultString = new String (baos.toByteArray()); 175 System.out.println(resultString); 176 assertTrue(resultString.equalsIgnoreCase("78e8bbf532d01dece38aa9d2a4a409dbff1a265cdbae1b95")); 177 178 } 179 180 public void test04TestRevocationCodeIdentifyerGeneration() throws Exception { 181 System.out.println("\n"); 182 String authenticationData= "Help I Have Revealed My Key"; 183 184 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 185 Hex.encode(authenticationData.getBytes(), baos); 186 187 String resultString = new String (baos.toByteArray()); 188 System.out.println(resultString); 189 assertTrue(resultString.equalsIgnoreCase("48656c70204920486176652052657665616c6564204d79204b6579")); 190 191 SecretKey key1 = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 20, XKMSUtil.KEY_REVOCATIONCODEIDENTIFIER_PASS1); 192 193 assertTrue(key1.getEncoded().length == 20); 194 195 baos = new ByteArrayOutputStream (); 196 Hex.encode(key1.getEncoded(), baos); 197 198 resultString = new String (baos.toByteArray()); 199 System.out.println(resultString); 200 assertTrue(resultString.equalsIgnoreCase("1c0857c95458c26f44327efd0ef055b08cad5c78")); 201 202 SecretKey key2 = XKMSUtil.getSecretKeyFromPassphrase(new String (key1.getEncoded(),"ISO8859-1"), false,20, XKMSUtil.KEY_REVOCATIONCODEIDENTIFIER_PASS2); 203 204 assertTrue(key2.getEncoded().length == 20); 205 206 baos = new ByteArrayOutputStream (); 207 Hex.encode(key2.getEncoded(), baos); 208 209 resultString = new String (baos.toByteArray()); 210 System.out.println(resultString); 211 assertTrue(resultString.equalsIgnoreCase("e6b44dd9c39988c95c889c41a9a7a5ad90c2cd21")); 212 213 String byte64String = new String (Base64.encode(key2.getEncoded(), false)); 214 System.out.println(byte64String); 215 assertTrue(byte64String.equals("5rRN2cOZiMlciJxBqaelrZDCzSE=")); 216 } 217 218 public void test04TestPublicKeyExtraction() throws Exception { 219 DocumentBuilder db = dbf.newDocumentBuilder(); 220 KeyPair keys = KeyTools.genKeys("1024", "RSA"); 221 222 RegisterRequestType registerRequestType = xKMSObjectFactory.createRegisterRequestType(); 223 registerRequestType.setId("523"); 224 225 UseKeyWithType useKeyWithType = xKMSObjectFactory.createUseKeyWithType(); 226 useKeyWithType.setApplication(XKMSConstants.USEKEYWITH_PKIX); 227 useKeyWithType.setIdentifier("CN=Test Testarsson"); 228 229 registerRequestType.getRespondWith().add(XKMSConstants.RESPONDWITH_X509CHAIN); 230 231 KeyInfoType keyInfoType = sigFactory.createKeyInfoType(); 232 RSAKeyValueType rsaKeyValueType = sigFactory.createRSAKeyValueType(); 233 rsaKeyValueType.setExponent(((RSAPublicKey ) keys.getPublic()).getPublicExponent().toByteArray()); 234 rsaKeyValueType.setModulus(((RSAPublicKey ) keys.getPublic()).getModulus().toByteArray()); 235 JAXBElement<RSAKeyValueType> rsaKeyValue = sigFactory.createRSAKeyValue(rsaKeyValueType); 236 keyInfoType.getContent().add(rsaKeyValue); 237 PrototypeKeyBindingType prototypeKeyBindingType = xKMSObjectFactory.createPrototypeKeyBindingType(); 238 prototypeKeyBindingType.getUseKeyWith().add(useKeyWithType); 239 prototypeKeyBindingType.setKeyInfo(keyInfoType); 240 prototypeKeyBindingType.setId("100231"); 241 registerRequestType.setPrototypeKeyBinding(prototypeKeyBindingType); 242 JAXBElement<RegisterRequestType> registerRequest = xKMSObjectFactory.createRegisterRequest(registerRequestType); 243 244 System.out.print("\n\n"); 245 246 Document registerRequestDoc = db.newDocument(); 247 marshaller.marshal( registerRequest, registerRequestDoc ); 248 249 XMLUtils.outputDOM(registerRequestDoc, System.out); 250 251 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 252 XMLUtils.outputDOM(registerRequestDoc, baos); 253 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 254 255 System.out.print("\n\n"); 256 257 JAXBElement<RegisterRequestType> registerRequest2 = (JAXBElement<RegisterRequestType>) unmarshaller.unmarshal(bais); 258 registerRequestType = registerRequest2.getValue(); 259 260 RSAKeyValueType rSAKeyValueType = (RSAKeyValueType) ((JAXBElement) registerRequestType.getPrototypeKeyBinding().getKeyInfo().getContent().get(0)).getValue(); 261 RSAPublicKeySpec rSAPublicKeySpec = new RSAPublicKeySpec (new BigInteger (rSAKeyValueType.getModulus()), new BigInteger (rSAKeyValueType.getExponent())); 262 RSAPublicKey rSAPublicKey = (RSAPublicKey ) KeyFactory.getInstance("RSA").generatePublic(rSAPublicKeySpec); 263 264 X509Certificate cert = CertTools.genSelfCert("CN=test", 10, null,keys.getPrivate(), rSAPublicKey, "SHA1WithRSA", true); 265 266 cert.verify(rSAPublicKey); 267 268 } 269 270 271 272 } 273 | Popular Tags |