KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > xkms > TestXKMSEnc


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.protocol.xkms;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.math.BigInteger JavaDoc;
19 import java.security.KeyFactory JavaDoc;
20 import java.security.KeyPair JavaDoc;
21 import java.security.cert.X509Certificate JavaDoc;
22 import java.security.interfaces.RSAPrivateCrtKey JavaDoc;
23 import java.security.interfaces.RSAPrivateKey JavaDoc;
24 import java.security.interfaces.RSAPublicKey JavaDoc;
25 import java.security.spec.RSAPublicKeySpec JavaDoc;
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 JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
57
58 /**
59  *
60  *
61  *
62  * @author Philip Vendil 2006 sep 27
63  *
64  * @version $Id: TestXKMSEnc.java,v 1.2.2.1 2007/02/02 09:34:02 anatom Exp $
65  */

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 JavaDoc 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 JavaDoc {
103         log.debug(">setUp()");
104         
105
106
107         log.debug("<setUp()");
108     }
109
110     protected void tearDown() throws Exception JavaDoc {
111     }
112   
113     public void test01KeyEncryption() throws Exception JavaDoc {
114         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
115         KeyPair JavaDoc keys = KeyTools.genKeys("1024", "RSA");
116         RegisterResultType registerResultType = xKMSObjectFactory.createRegisterResultType();
117         JAXBElement<RegisterResultType> registerResult = xKMSObjectFactory.createRegisterResult(registerResultType);
118                 
119         PrivateKeyType privateKeyType1 = XKMSUtil.getEncryptedXMLFromPrivateKey( (RSAPrivateCrtKey JavaDoc) keys.getPrivate(), "This is total crap");
120         registerResultType.setPrivateKey(privateKeyType1);
121         
122         System.out.print("\n\n");
123                 
124         Document JavaDoc registerResultDoc = db.newDocument();
125         marshaller.marshal( registerResult, registerResultDoc );
126         
127         XMLUtils.outputDOM(registerResultDoc, System.out);
128         
129         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
130         XMLUtils.outputDOM(registerResultDoc, baos);
131         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(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 JavaDoc privkey2 = XKMSUtil.getPrivateKeyFromEncryptedXML(privateKeyType2, "This is total crap");
141         
142         X509Certificate JavaDoc 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 JavaDoc{
148         String JavaDoc authenticationData= "024837";
149         
150         SecretKey retval = XKMSUtil.getSecretKeyFromPassphrase(authenticationData, true, 20, XKMSUtil.KEY_AUTHENTICATION);
151                 
152         assertTrue(retval.getEncoded().length == 20);
153         
154         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
155         Hex.encode(retval.getEncoded(), baos);
156         
157         String JavaDoc resultString = new String JavaDoc(baos.toByteArray());
158         assertTrue(resultString.equalsIgnoreCase("d6cc34cb83fae2993a393aa8e7de9a06c7fa2c92"));
159     }
160     
161
162     
163     public void test03TestBOBRegistrationPrivateKeyEncryption() throws Exception JavaDoc{
164         
165         String JavaDoc 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 JavaDoc baos = new ByteArrayOutputStream JavaDoc();
172         Hex.encode(retval.getEncoded(), baos);
173                 
174         String JavaDoc resultString = new String JavaDoc(baos.toByteArray());
175         System.out.println(resultString);
176         assertTrue(resultString.equalsIgnoreCase("78e8bbf532d01dece38aa9d2a4a409dbff1a265cdbae1b95"));
177
178     }
179     
180     public void test04TestRevocationCodeIdentifyerGeneration() throws Exception JavaDoc{
181         System.out.println("\n");
182         String JavaDoc authenticationData= "Help I Have Revealed My Key";
183         
184         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
185         Hex.encode(authenticationData.getBytes(), baos);
186                 
187         String JavaDoc resultString = new String JavaDoc(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 JavaDoc();
196         Hex.encode(key1.getEncoded(), baos);
197                 
198         resultString = new String JavaDoc(baos.toByteArray());
199         System.out.println(resultString);
200         assertTrue(resultString.equalsIgnoreCase("1c0857c95458c26f44327efd0ef055b08cad5c78"));
201         
202         SecretKey key2 = XKMSUtil.getSecretKeyFromPassphrase(new String JavaDoc(key1.getEncoded(),"ISO8859-1"), false,20, XKMSUtil.KEY_REVOCATIONCODEIDENTIFIER_PASS2);
203             
204         assertTrue(key2.getEncoded().length == 20);
205             
206         baos = new ByteArrayOutputStream JavaDoc();
207         Hex.encode(key2.getEncoded(), baos);
208
209         resultString = new String JavaDoc(baos.toByteArray());
210         System.out.println(resultString);
211         assertTrue(resultString.equalsIgnoreCase("e6b44dd9c39988c95c889c41a9a7a5ad90c2cd21"));
212         
213         String JavaDoc byte64String = new String JavaDoc(Base64.encode(key2.getEncoded(), false));
214         System.out.println(byte64String);
215         assertTrue(byte64String.equals("5rRN2cOZiMlciJxBqaelrZDCzSE="));
216     }
217     
218     public void test04TestPublicKeyExtraction() throws Exception JavaDoc{
219         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
220         KeyPair JavaDoc 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 JavaDoc) keys.getPublic()).getPublicExponent().toByteArray());
234         rsaKeyValueType.setModulus(((RSAPublicKey JavaDoc) 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 JavaDoc registerRequestDoc = db.newDocument();
247         marshaller.marshal( registerRequest, registerRequestDoc );
248         
249         XMLUtils.outputDOM(registerRequestDoc, System.out);
250         
251         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
252         XMLUtils.outputDOM(registerRequestDoc, baos);
253         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(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 JavaDoc rSAPublicKeySpec = new RSAPublicKeySpec JavaDoc(new BigInteger JavaDoc(rSAKeyValueType.getModulus()), new BigInteger JavaDoc(rSAKeyValueType.getExponent()));
262         RSAPublicKey JavaDoc rSAPublicKey = (RSAPublicKey JavaDoc) KeyFactory.getInstance("RSA").generatePublic(rSAPublicKeySpec);
263         
264         X509Certificate JavaDoc 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