View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.common.crypto;
21  
22  import java.io.IOException;
23  
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.CallbackHandler;
26  import javax.security.auth.callback.UnsupportedCallbackException;
27  
28  import org.apache.wss4j.common.ext.WSPasswordCallback;
29  import org.apache.wss4j.common.util.FIPSUtils;
30  import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
31  import org.jasypt.iv.RandomIvGenerator;
32  import org.jasypt.salt.RandomSaltGenerator;
33  
34  
35  /**
36   * An implementation of PasswordEncryptor that relies on Jasypt's StandardPBEStringEncryptor to
37   * encrypt and decrypt passwords. The default algorithm that is used is "PBEWithMD5AndTripleDES".
38   */
39  public class JasyptPasswordEncryptor implements PasswordEncryptor {
40  
41      public static final String DEFAULT_ALGORITHM = 
42          FIPSUtils.isFIPSEnabled() 
43              ? "PBEWithHmacSHA512AndAES_256" : "PBEWithMD5AndTripleDES";
44  
45      private static final org.slf4j.Logger LOG =
46          org.slf4j.LoggerFactory.getLogger(JasyptPasswordEncryptor.class);
47  
48      private final StandardPBEStringEncryptor passwordEncryptor;
49      private CallbackHandler callbackHandler;
50  
51      public JasyptPasswordEncryptor(String password) {
52          this(password, DEFAULT_ALGORITHM);
53      }
54  
55      public JasyptPasswordEncryptor(String password, String algorithm) {
56          passwordEncryptor = new StandardPBEStringEncryptor();
57          passwordEncryptor.setPassword(password);
58          passwordEncryptor.setAlgorithm(algorithm);
59          if (FIPSUtils.isFIPSEnabled()) {
60              passwordEncryptor.setSaltGenerator(new RandomSaltGenerator("PKCS11"));
61              passwordEncryptor.setIvGenerator(new RandomIvGenerator("PKCS11"));
62          }
63      }
64  
65      public JasyptPasswordEncryptor(CallbackHandler callbackHandler) {
66          this(callbackHandler, DEFAULT_ALGORITHM);
67      }
68  
69      public JasyptPasswordEncryptor(CallbackHandler callbackHandler, String algorithm) {
70          passwordEncryptor = new StandardPBEStringEncryptor();
71          passwordEncryptor.setAlgorithm(algorithm);
72          if (FIPSUtils.isFIPSEnabled()) {
73              passwordEncryptor.setSaltGenerator(new RandomSaltGenerator("PKCS11"));
74              passwordEncryptor.setIvGenerator(new RandomIvGenerator("PKCS11"));
75          }
76          this.callbackHandler = callbackHandler;
77      }
78  
79      /**
80       * Encrypt the given password
81       * @param password the password to be encrypted
82       * @return the encrypted password
83       */
84      public String encrypt(String password) {
85          if (callbackHandler != null) {
86              WSPasswordCallback pwCb =
87                  new WSPasswordCallback("", WSPasswordCallback.PASSWORD_ENCRYPTOR_PASSWORD);
88              try {
89                  callbackHandler.handle(new Callback[]{pwCb});
90              } catch (IOException | UnsupportedCallbackException e) {
91                  LOG.debug("Error in getting password: ", e);
92              }
93              if (pwCb.getPassword() != null) {
94                  passwordEncryptor.setPassword(pwCb.getPassword());
95              }
96          }
97          return passwordEncryptor.encrypt(password);
98      }
99  
100     /**
101      * Decrypt the given encrypted password
102      * @param encryptedPassword the encrypted password to decrypt
103      * @return the decrypted password
104      */
105     public String decrypt(String encryptedPassword) {
106         if (callbackHandler != null) {
107             WSPasswordCallback pwCb =
108                 new WSPasswordCallback("", WSPasswordCallback.PASSWORD_ENCRYPTOR_PASSWORD);
109             try {
110                 callbackHandler.handle(new Callback[]{pwCb});
111             } catch (IOException | UnsupportedCallbackException e) {
112                 LOG.debug("Error in getting password: ", e);
113             }
114             if (pwCb.getPassword() != null) {
115                 passwordEncryptor.setPassword(pwCb.getPassword());
116             }
117         }
118         return passwordEncryptor.decrypt(encryptedPassword);
119     }
120 
121 }