001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.model.dataformat;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.model.DataFormatDefinition;
029    import org.apache.camel.spi.DataFormat;
030    import org.apache.camel.spi.NamespaceAware;
031    import org.apache.camel.util.jsse.KeyStoreParameters;
032    
033    /**
034     * Represents as XML Security Encrypter/Decrypter {@link DataFormat}
035     */
036    @XmlRootElement(name = "secureXML")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class XMLSecurityDataFormat extends DataFormatDefinition implements NamespaceAware {
039    
040        private static final transient String TRIPLEDES = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
041    
042        @XmlAttribute
043        private String xmlCipherAlgorithm;
044        @XmlAttribute
045        private String passPhrase;
046        @XmlAttribute
047        private String secureTag;
048        @XmlAttribute
049        private Boolean secureTagContents;
050        @XmlAttribute
051        private String keyCipherAlgorithm;
052        @XmlAttribute
053        private String recipientKeyAlias;
054        @XmlAttribute
055        private String keyOrTrustStoreParametersId;
056        @XmlAttribute
057        private String keyPassword;
058        
059        @XmlTransient
060        private KeyStoreParameters keyOrTrustStoreParameters;
061        
062        @XmlTransient
063        private Map<String, String> namespaces;
064        
065        
066        public XMLSecurityDataFormat() {
067            super("secureXML");
068        }
069    
070        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents) {
071            this();
072            this.setSecureTag(secureTag);
073            this.setSecureTagContents(secureTagContents);
074        }
075        
076        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents) {
077            this();
078            this.setSecureTag(secureTag);
079            this.setSecureTagContents(secureTagContents);
080            this.setNamespaces(namespaces);
081        }
082    
083        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase) {
084            this(secureTag, secureTagContents);
085            this.setPassPhrase(passPhrase);
086        }
087        
088        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, 
089                                     String passPhrase) {
090            this(secureTag, secureTagContents);
091            this.setPassPhrase(passPhrase);
092            this.setNamespaces(namespaces);
093        }
094        
095        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase,
096                                     String xmlCipherAlgorithm) {
097            this(secureTag, secureTagContents, passPhrase);
098            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
099        }
100        
101        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase,
102                                     String xmlCipherAlgorithm) {
103            this(secureTag, secureTagContents, passPhrase);
104            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
105            this.setNamespaces(namespaces);
106        }
107        
108        /**
109         * @deprecated  use {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, String)} or 
110         *                  {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, KeyStoreParameters)} instead
111         */
112        @Deprecated
113        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
114                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
115            this(secureTag, secureTagContents);
116            this.setRecipientKeyAlias(recipientKeyAlias);
117            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
118            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
119        }
120    
121        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
122                                     String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
123            this(secureTag, secureTagContents);
124            this.setRecipientKeyAlias(recipientKeyAlias);
125            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
126            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
127            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
128        }
129        
130        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
131                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
132            this(secureTag, secureTagContents);
133            this.setRecipientKeyAlias(recipientKeyAlias);
134            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
135            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
136            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
137        }
138    
139        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
140                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
141            this(secureTag, secureTagContents);
142            this.setRecipientKeyAlias(recipientKeyAlias);
143            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
144            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
145            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
146            this.setKeyPassword(keyPassword);
147        }
148    
149        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
150            String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
151            this(secureTag, secureTagContents);
152            this.setRecipientKeyAlias(recipientKeyAlias);
153            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
154            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
155            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
156            this.setKeyPassword(keyPassword);
157        }
158        
159        /**
160         * @deprecated  use {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, String)} or 
161         *                  {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, KeyStoreParameters)} instead
162         */
163        @Deprecated
164        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
165                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
166            this(secureTag, secureTagContents);
167            this.setRecipientKeyAlias(recipientKeyAlias);
168            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
169            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
170            this.setNamespaces(namespaces);
171        }
172        
173        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
174                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
175            this(secureTag, secureTagContents);
176            this.setRecipientKeyAlias(recipientKeyAlias);
177            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
178            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
179            this.setNamespaces(namespaces);
180            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
181        }
182    
183        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
184                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
185            this(secureTag, secureTagContents);
186            this.setRecipientKeyAlias(recipientKeyAlias);
187            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
188            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
189            this.setNamespaces(namespaces);
190            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
191        }
192        
193        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
194                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
195            this(secureTag, secureTagContents);
196            this.setRecipientKeyAlias(recipientKeyAlias);
197            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
198            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
199            this.setNamespaces(namespaces);
200            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
201            this.setKeyPassword(keyPassword);
202        }
203    
204        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
205                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
206            this(secureTag, secureTagContents);
207            this.setRecipientKeyAlias(recipientKeyAlias);
208            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
209            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
210            this.setNamespaces(namespaces);
211            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
212            this.setKeyPassword(keyPassword);
213        }
214            
215    
216        @Override
217        protected void configureDataFormat(DataFormat dataFormat) {
218            if (getSecureTag() != null) {
219                setProperty(dataFormat, "secureTag", getSecureTag());
220            } else {
221                setProperty(dataFormat, "secureTag", "");
222            }
223    
224            setProperty(dataFormat, "secureTagContents", isSecureTagContents());
225    
226            if (passPhrase != null) {
227                setProperty(dataFormat, "passPhrase", getPassPhrase().getBytes());
228            } else {
229                setProperty(dataFormat, "passPhrase", "Just another 24 Byte key".getBytes());
230            }
231            if (getXmlCipherAlgorithm() != null) {
232                setProperty(dataFormat, "xmlCipherAlgorithm", getXmlCipherAlgorithm());
233            } else {
234                setProperty(dataFormat, "xmlCipherAlgorithm", TRIPLEDES);
235            }
236            if (getKeyCipherAlgorithm() != null) {
237                setProperty(dataFormat, "keyCipherAlgorithm", getKeyCipherAlgorithm());
238            }
239            if (getRecipientKeyAlias() != null) {
240                setProperty(dataFormat, "recipientKeyAlias", getRecipientKeyAlias());
241            }
242            if (getKeyOrTrustStoreParametersId() != null) {
243                setProperty(dataFormat, "keyOrTrustStoreParametersId", getKeyOrTrustStoreParametersId());
244            }
245            if (keyOrTrustStoreParameters != null) {
246                setProperty(dataFormat, "keyOrTrustStoreParameters", this.keyOrTrustStoreParameters);
247            }
248            if (namespaces != null) {
249                setProperty(dataFormat, "namespaces", this.namespaces);
250            }
251            if (keyPassword != null) {
252                setProperty(dataFormat, "keyPassword", this.getKeyPassword());
253            }
254        }
255    
256        public String getXmlCipherAlgorithm() {
257            return xmlCipherAlgorithm;
258        }
259    
260        public void setXmlCipherAlgorithm(String xmlCipherAlgorithm) {
261            this.xmlCipherAlgorithm = xmlCipherAlgorithm;
262        }
263    
264        public String getPassPhrase() {
265            return passPhrase;
266        }
267    
268        public void setPassPhrase(String passPhrase) {
269            this.passPhrase = passPhrase;
270        }
271    
272        public String getSecureTag() {
273            return secureTag;
274        }
275    
276        public void setSecureTag(String secureTag) {
277            this.secureTag = secureTag;
278        }
279    
280        public Boolean getSecureTagContents() {
281            return secureTagContents;
282        }
283    
284        public void setSecureTagContents(Boolean secureTagContents) {
285            this.secureTagContents = secureTagContents;
286        }
287    
288        public boolean isSecureTagContents() {
289            return secureTagContents != null && secureTagContents;
290        }
291    
292        public void setKeyCipherAlgorithm(String keyCipherAlgorithm) {
293            this.keyCipherAlgorithm = keyCipherAlgorithm;
294        }
295    
296        public String getKeyCipherAlgorithm() {
297            return keyCipherAlgorithm;
298        }
299    
300        public void setRecipientKeyAlias(String recipientKeyAlias) {
301            this.recipientKeyAlias = recipientKeyAlias;
302        }
303    
304        public String getRecipientKeyAlias() {
305            return recipientKeyAlias;
306        }
307        
308        public void setKeyOrTrustStoreParametersId(String id) {
309            this.keyOrTrustStoreParametersId = id;
310        }
311        
312        public String getKeyOrTrustStoreParametersId() {
313            return this.keyOrTrustStoreParametersId;
314        }
315        
316        private void setKeyOrTrustStoreParameters(KeyStoreParameters keyOrTrustStoreParameters) {
317            this.keyOrTrustStoreParameters = keyOrTrustStoreParameters;
318        }
319        
320        public String getKeyPassword() {
321            return this.keyPassword;
322        }
323        
324        public void setKeyPassword(String keyPassword) {
325            this.keyPassword = keyPassword;
326        }
327    
328        @Override
329        public void setNamespaces(Map<String, String> nspaces) {
330            if (this.namespaces == null) {
331                this.namespaces = new HashMap<String, String>();
332            }
333            this.namespaces.putAll(nspaces);
334        }
335        
336    }