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.CamelContext;
029    import org.apache.camel.model.DataFormatDefinition;
030    import org.apache.camel.spi.DataFormat;
031    import org.apache.camel.spi.NamespaceAware;
032    import org.apache.camel.util.jsse.KeyStoreParameters;
033    
034    /**
035     * Represents as XML Security Encrypter/Decrypter {@link DataFormat}
036     */
037    @XmlRootElement(name = "secureXML")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class XMLSecurityDataFormat extends DataFormatDefinition implements NamespaceAware {
040    
041        private static final String TRIPLEDES = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
042    
043        @XmlAttribute
044        private String xmlCipherAlgorithm;
045        @XmlAttribute
046        private String passPhrase;
047        @XmlAttribute
048        private String secureTag;
049        @XmlAttribute
050        private Boolean secureTagContents;
051        @XmlAttribute
052        private String keyCipherAlgorithm;
053        @XmlAttribute
054        private String recipientKeyAlias;
055        @XmlAttribute
056        private String keyOrTrustStoreParametersId;
057        @XmlAttribute
058        private String keyPassword;
059        @XmlAttribute
060        private String digestAlgorithm;
061        @XmlAttribute
062        private String mgfAlgorithm;
063    
064        @XmlTransient
065        private KeyStoreParameters keyOrTrustStoreParameters;
066        
067        @XmlTransient
068        private Map<String, String> namespaces;
069        
070        
071        public XMLSecurityDataFormat() {
072            super("secureXML");
073        }
074    
075        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents) {
076            this();
077            this.setSecureTag(secureTag);
078            this.setSecureTagContents(secureTagContents);
079        }
080        
081        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents) {
082            this();
083            this.setSecureTag(secureTag);
084            this.setSecureTagContents(secureTagContents);
085            this.setNamespaces(namespaces);
086        }
087    
088        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase) {
089            this(secureTag, secureTagContents);
090            this.setPassPhrase(passPhrase);
091        }
092        
093        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, 
094                                     String passPhrase) {
095            this(secureTag, secureTagContents);
096            this.setPassPhrase(passPhrase);
097            this.setNamespaces(namespaces);
098        }
099        
100        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase,
101                                     String xmlCipherAlgorithm) {
102            this(secureTag, secureTagContents, passPhrase);
103            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
104        }
105        
106        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase,
107                                     String xmlCipherAlgorithm) {
108            this(secureTag, secureTagContents, passPhrase);
109            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
110            this.setNamespaces(namespaces);
111        }
112        
113        /**
114         * @deprecated  use {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, String)} or 
115         *                  {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, KeyStoreParameters)} instead
116         */
117        @Deprecated
118        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
119                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
120            this(secureTag, secureTagContents);
121            this.setRecipientKeyAlias(recipientKeyAlias);
122            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
123            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
124        }
125    
126        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
127                                     String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
128            this(secureTag, secureTagContents);
129            this.setRecipientKeyAlias(recipientKeyAlias);
130            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
131            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
132            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
133        }
134        
135        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
136                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
137            this(secureTag, secureTagContents);
138            this.setRecipientKeyAlias(recipientKeyAlias);
139            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
140            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
141            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
142        }
143    
144        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
145                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
146            this(secureTag, secureTagContents);
147            this.setRecipientKeyAlias(recipientKeyAlias);
148            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
149            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
150            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
151            this.setKeyPassword(keyPassword);
152        }
153    
154        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
155            String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
156            this(secureTag, secureTagContents);
157            this.setRecipientKeyAlias(recipientKeyAlias);
158            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
159            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
160            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
161            this.setKeyPassword(keyPassword);
162        }
163        
164        /**
165         * @deprecated  use {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, String)} or 
166         *                  {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, KeyStoreParameters)} instead
167         */
168        @Deprecated
169        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
170                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
171            this(secureTag, secureTagContents);
172            this.setRecipientKeyAlias(recipientKeyAlias);
173            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
174            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
175            this.setNamespaces(namespaces);
176        }
177        
178        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
179                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
180            this(secureTag, secureTagContents);
181            this.setRecipientKeyAlias(recipientKeyAlias);
182            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
183            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
184            this.setNamespaces(namespaces);
185            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
186        }
187    
188        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
189                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
190            this(secureTag, secureTagContents);
191            this.setRecipientKeyAlias(recipientKeyAlias);
192            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
193            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
194            this.setNamespaces(namespaces);
195            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
196        }
197        
198        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
199                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
200            this(secureTag, secureTagContents);
201            this.setRecipientKeyAlias(recipientKeyAlias);
202            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
203            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
204            this.setNamespaces(namespaces);
205            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
206            this.setKeyPassword(keyPassword);
207        }
208    
209        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
210                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
211            this(secureTag, secureTagContents);
212            this.setRecipientKeyAlias(recipientKeyAlias);
213            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
214            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
215            this.setNamespaces(namespaces);
216            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
217            this.setKeyPassword(keyPassword);
218        }
219        
220        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
221                 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword,
222                 String digestAlgorithm) {
223            this(secureTag, secureTagContents);
224            this.setRecipientKeyAlias(recipientKeyAlias);
225            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
226            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
227            this.setNamespaces(namespaces);
228            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
229            this.setKeyPassword(keyPassword);
230            this.setDigestAlgorithm(digestAlgorithm);
231        }
232    
233        @Override
234        protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
235            if (getSecureTag() != null) {
236                setProperty(camelContext, dataFormat, "secureTag", getSecureTag());
237            } else {
238                setProperty(camelContext, dataFormat, "secureTag", "");
239            }
240    
241            setProperty(camelContext, dataFormat, "secureTagContents", isSecureTagContents());
242    
243            if (passPhrase != null) {
244                setProperty(camelContext, dataFormat, "passPhrase", getPassPhrase().getBytes());
245            } else {
246                setProperty(camelContext, dataFormat, "passPhrase", "Just another 24 Byte key".getBytes());
247            }
248            if (getXmlCipherAlgorithm() != null) {
249                setProperty(camelContext, dataFormat, "xmlCipherAlgorithm", getXmlCipherAlgorithm());
250            } else {
251                setProperty(camelContext, dataFormat, "xmlCipherAlgorithm", TRIPLEDES);
252            }
253            if (getKeyCipherAlgorithm() != null) {
254                setProperty(camelContext, dataFormat, "keyCipherAlgorithm", getKeyCipherAlgorithm());
255            }
256            if (getRecipientKeyAlias() != null) {
257                setProperty(camelContext, dataFormat, "recipientKeyAlias", getRecipientKeyAlias());
258            }
259            if (getKeyOrTrustStoreParametersId() != null) {
260                setProperty(camelContext, dataFormat, "keyOrTrustStoreParametersId", getKeyOrTrustStoreParametersId());
261            }
262            if (keyOrTrustStoreParameters != null) {
263                setProperty(camelContext, dataFormat, "keyOrTrustStoreParameters", this.keyOrTrustStoreParameters);
264            }
265            if (namespaces != null) {
266                setProperty(camelContext, dataFormat, "namespaces", this.namespaces);
267            }
268            if (keyPassword != null) {
269                setProperty(camelContext, dataFormat, "keyPassword", this.getKeyPassword());
270            }
271            if (digestAlgorithm != null) {
272                setProperty(camelContext, dataFormat, "digestAlgorithm", this.getDigestAlgorithm());
273            }
274            if (mgfAlgorithm != null) {
275                setProperty(camelContext, dataFormat, "mgfAlgorithm", this.getMgfAlgorithm());
276            }
277        }
278    
279        public String getXmlCipherAlgorithm() {
280            return xmlCipherAlgorithm;
281        }
282    
283        public void setXmlCipherAlgorithm(String xmlCipherAlgorithm) {
284            this.xmlCipherAlgorithm = xmlCipherAlgorithm;
285        }
286    
287        public String getPassPhrase() {
288            return passPhrase;
289        }
290    
291        public void setPassPhrase(String passPhrase) {
292            this.passPhrase = passPhrase;
293        }
294    
295        public String getSecureTag() {
296            return secureTag;
297        }
298    
299        public void setSecureTag(String secureTag) {
300            this.secureTag = secureTag;
301        }
302    
303        public Boolean getSecureTagContents() {
304            return secureTagContents;
305        }
306    
307        public void setSecureTagContents(Boolean secureTagContents) {
308            this.secureTagContents = secureTagContents;
309        }
310    
311        public boolean isSecureTagContents() {
312            return secureTagContents != null && secureTagContents;
313        }
314    
315        public void setKeyCipherAlgorithm(String keyCipherAlgorithm) {
316            this.keyCipherAlgorithm = keyCipherAlgorithm;
317        }
318    
319        public String getKeyCipherAlgorithm() {
320            return keyCipherAlgorithm;
321        }
322    
323        public void setRecipientKeyAlias(String recipientKeyAlias) {
324            this.recipientKeyAlias = recipientKeyAlias;
325        }
326    
327        public String getRecipientKeyAlias() {
328            return recipientKeyAlias;
329        }
330        
331        public void setKeyOrTrustStoreParametersId(String id) {
332            this.keyOrTrustStoreParametersId = id;
333        }
334        
335        public String getKeyOrTrustStoreParametersId() {
336            return this.keyOrTrustStoreParametersId;
337        }
338        
339        private void setKeyOrTrustStoreParameters(KeyStoreParameters keyOrTrustStoreParameters) {
340            this.keyOrTrustStoreParameters = keyOrTrustStoreParameters;
341        }
342        
343        public String getKeyPassword() {
344            return this.keyPassword;
345        }
346    
347        public void setKeyPassword(String keyPassword) {
348            this.keyPassword = keyPassword;
349        }
350    
351        public String getDigestAlgorithm() {
352            return digestAlgorithm;
353        }
354    
355        public void setDigestAlgorithm(String digestAlgorithm) {
356            this.digestAlgorithm = digestAlgorithm;
357        }
358    
359        public String getMgfAlgorithm() {
360            return mgfAlgorithm;
361        }
362    
363        public void setMgfAlgorithm(String mgfAlgorithm) {
364            this.mgfAlgorithm = mgfAlgorithm;
365        }
366    
367        @Override
368        public void setNamespaces(Map<String, String> nspaces) {
369            if (this.namespaces == null) {
370                this.namespaces = new HashMap<String, String>();
371            }
372            this.namespaces.putAll(nspaces);
373        }
374        
375    }