11package com .cybersource .ws .client ;
22
3- import org .apache .ws . security . WSConstants ;
4- import org .apache .ws . security . WSEncryptionPart ;
5- import org .apache .ws . security . WSSecurityException ;
6- import org .apache .ws . security . components . crypto . CredentialException ;
7- import org .apache .ws . security .message .WSSecEncrypt ;
8- import org .apache .ws . security .message .WSSecHeader ;
9- import org .apache .ws . security .message .WSSecSignature ;
3+ import org .apache .wss4j . common . WSEncryptionPart ;
4+ import org .apache .wss4j . common . ext . WSSecurityException ;
5+ import org .apache .wss4j . dom . WSConstants ;
6+ import org .apache .wss4j . dom . WSDocInfo ;
7+ import org .apache .wss4j . dom .message .WSSecEncrypt ;
8+ import org .apache .wss4j . dom .message .WSSecHeader ;
9+ import org .apache .wss4j . dom .message .WSSecSignature ;
1010import org .bouncycastle .jce .provider .BouncyCastleProvider ;
1111import org .w3c .dom .Document ;
1212
13+ import javax .crypto .KeyGenerator ;
1314import java .io .FileInputStream ;
1415import java .io .FileNotFoundException ;
1516import java .io .IOException ;
1617import java .security .*;
17- import java .security .cert .Certificate ;
1818import java .security .cert .CertificateException ;
1919import java .security .cert .X509Certificate ;
2020import java .util .Collections ;
@@ -44,7 +44,9 @@ public class SecurityUtil {
4444 private static final String SIGNATURE_ALGORITHM = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" ;
4545 // By default digest algorithm is set to "http://www.w3.org/2000/09/xmldsig#sha1"
4646 private static final String DIGEST_ALGORITHM = "http://www.w3.org/2001/04/xmlenc#sha256" ;
47-
47+ // SymmetricKey Generator Algorithm to handle message encryption
48+ private static final String SYM_KEY_ALGO = "AES" ;
49+
4850 private static BouncyCastleProvider bcProvider = new BouncyCastleProvider ();
4951
5052 // This is loaded by WSS4J but since we use it lets make sure its here
@@ -57,7 +59,7 @@ public class SecurityUtil {
5759 }
5860 }
5961
60- private static void initKeystore () throws KeyStoreException , CredentialException , IOException , NoSuchAlgorithmException , CertificateException {
62+ private static void initKeystore () throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException {
6163 KeyStore keyStore = KeyStore .getInstance ("jks" );
6264 keyStore .load (null , null );
6365 localKeyStoreHandler = new MessageHandlerKeyStore ();
@@ -190,91 +192,92 @@ private static void readAndStoreCertificateAndPrivateKey(MerchantConfig merchant
190192 * @throws SignException
191193 */
192194 public static Document handleMessageCreation (Document signedDoc , String merchantId , Logger logger ) throws SignEncryptException , SignException {
193-
195+
194196 logger .log (Logger .LT_INFO , "Encrypting Signed doc ..." );
195-
196- WSSecHeader secHeader = new WSSecHeader ();
197+
198+ org .apache .xml .security .Init .init ();
199+ WSSecHeader secHeader = new WSSecHeader (signedDoc );
197200 try {
198- secHeader .insertSecurityHeader (signedDoc );
201+ secHeader .insertSecurityHeader ();
199202 } catch (WSSecurityException e ) {
200203 logger .log (Logger .LT_EXCEPTION , "Exception while adding document in soap securiy header for MLE" );
201204 throw new SignException (e );
202205 }
203-
204- WSSecEncrypt encrBuilder = new WSSecEncrypt ();
206+ WSSecEncrypt encrBuilder = new WSSecEncrypt (secHeader );
205207 //Set the user name to get the encryption certificate.
206208 //The public key of this certificate is used, thus no password necessary. The user name is a keystore alias usually.
207209 encrBuilder .setUserInfo (identities .get (SERVER_ALIAS ).getKeyAlias ());
208-
210+
209211 /*This is to reference a public key or certificate when signing or encrypting a SOAP message.
210212 *The following valid values for these configuration items are:
211213 *IssuerSerial (default),DirectReference[BST],X509KeyIdentifier,Thumbprint,SKIKeyIdentifier,KeyValue (signature only),EncryptedKeySHA1 (encryption only)
212214 */
213215 encrBuilder .setKeyIdentifierType (WSConstants .X509_KEY_IDENTIFIER );
214-
216+
215217 //This encryption algorithm is used to encrypt the data.
216218 encrBuilder .setSymmetricEncAlgorithm (WSConstants .AES_256 );
217-
219+
218220 //Sets the algorithm to encode the symmetric key. Default is the WSConstants.KEYTRANSPORT_RSAOEP algorithm.
219221 //encrBuilder.setKeyEnc(WSConstants.KEYTRANSPORT_RSAOEP);
220-
221-
222+
223+
222224 //Create signed document
223225 //Document signedDoc = createSignedDoc(workingDocument,senderAlias,password,secHeader);
224-
226+
225227 Document signedEncryptedDoc ;
226228 try {
227229 //Builds the SOAP envelope with encrypted Body and adds encrypted key.
228230 // If no external key (symmetricalKey) was set ,generate an encryption
229231 // key (session key) for this Encrypt element. This key will be
230232 // encrypted using the public key of the receiver
231- signedEncryptedDoc = encrBuilder .build (signedDoc , localKeyStoreHandler , secHeader );
232- } catch (WSSecurityException e ) {
233+ signedEncryptedDoc = encrBuilder .build (localKeyStoreHandler , KeyGenerator . getInstance ( SYM_KEY_ALGO ). generateKey () );
234+ } catch (WSSecurityException | NoSuchAlgorithmException e ) {
233235 logger .log (Logger .LT_EXCEPTION , "Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + SERVER_ALIAS );
234236 throw new SignEncryptException ("Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + SERVER_ALIAS , e );
235237 }
236- encrBuilder .prependToHeader (secHeader );
238+ encrBuilder .prependToHeader ();
237239 return signedEncryptedDoc ;
238240 }
239241
240242 /**
241243 * Create signed document
242- * @param workingDocument
244+ * @param signedDoc
243245 * @param keyAlias
244246 * @param password
245247 * @param logger
246248 * @return Document
247249 * @throws SignException
248250 */
249- public static Document createSignedDoc (Document workingDocument ,String keyAlias , String password ,Logger logger ) throws SignException {
250-
251+ public static Document createSignedDoc (Document signedDoc ,String keyAlias , String password ,Logger logger ) throws SignException {
252+
251253 logger .log (Logger .LT_INFO , "Signing request..." );
252- // long startTime = System.nanoTime();
253- WSSecHeader secHeader = new WSSecHeader ();
254+ long startTime = System .nanoTime ();
255+ WSSecHeader secHeader = new WSSecHeader (signedDoc );
254256 try {
255- secHeader .insertSecurityHeader (workingDocument );
257+ secHeader .insertSecurityHeader ();
256258 } catch (WSSecurityException e ) {
257259 logger .log (Logger .LT_EXCEPTION ,
258- "Exception while signing XML document" );
260+ "Exception while signing XML document" );
259261 throw new SignException (e );
260262 }
261-
262- WSSecSignature sign = new WSSecSignature ();
263-
263+
264+ WSSecSignature sign = new WSSecSignature (secHeader );
264265 sign .setUserInfo (keyAlias , password );
265-
266- //sign.setUserInfo(mc.getKeyAlias(), mc.getPassword());
266+
267267 sign .setDigestAlgo (DIGEST_ALGORITHM );
268268 sign .setSignatureAlgorithm (SIGNATURE_ALGORITHM );
269269 sign .setKeyIdentifierType (WSConstants .BST_DIRECT_REFERENCE );
270270 sign .setUseSingleCertificate (true );
271-
271+ //
272+ sign .setWsDocInfo (new WSDocInfo (signedDoc ));
273+
272274 //Set which parts of the message to encrypt/sign.
273275 WSEncryptionPart msgBodyPart = new WSEncryptionPart (WSConstants .ELEM_BODY , WSConstants .URI_SOAP11_ENV , "" );
274- sign . setParts ( Collections . singletonList ( msgBodyPart ));
276+
275277 try {
276- Document document = sign .build (workingDocument , localKeyStoreHandler , secHeader );
277- //System.out.println("SecurityUtil.createSignedDoc time taken to sign the request is " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
278+ sign .addReferencesToSign (Collections .singletonList (msgBodyPart ));
279+ Document document = sign .build (localKeyStoreHandler );
280+ System .out .println ("SecurityUtil.createSignedDoc time taken to sign the request is " + TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - startTime ) + " ms" );
278281 return document ;
279282 } catch (WSSecurityException e ) {
280283 logger .log (Logger .LT_EXCEPTION , "Failed while signing request for , '" + keyAlias + "'" );
0 commit comments