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 */ 017package org.apache.camel.model.dataformat; 018 019import java.security.Key; 020import java.security.spec.AlgorithmParameterSpec; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlRootElement; 026 027import org.apache.camel.CamelContext; 028import org.apache.camel.model.DataFormatDefinition; 029import org.apache.camel.spi.DataFormat; 030import org.apache.camel.spi.Metadata; 031import org.apache.camel.spi.RouteContext; 032import org.apache.camel.util.CamelContextHelper; 033import org.apache.camel.util.ObjectHelper; 034 035/** 036 * Crypto data format is used for encrypting and decrypting of messages using Java Cryptographic Extension. 037 * 038 * @version 039 */ 040@Metadata(firstVersion = "2.3.0", label = "dataformat,transformation,security", title = "Crypto (Java Cryptographic Extension)") 041@XmlRootElement(name = "crypto") 042@XmlAccessorType(XmlAccessType.FIELD) 043public class CryptoDataFormat extends DataFormatDefinition { 044 @XmlAttribute @Metadata(defaultValue = "DES/CBC/PKCS5Padding") 045 private String algorithm; 046 @XmlAttribute 047 private String cryptoProvider; 048 @XmlAttribute 049 private String keyRef; 050 @XmlAttribute 051 private String initVectorRef; 052 @XmlAttribute 053 private String algorithmParameterRef; 054 @XmlAttribute 055 private Integer buffersize; 056 @XmlAttribute @Metadata(defaultValue = "HmacSHA1") 057 private String macAlgorithm = "HmacSHA1"; 058 @XmlAttribute 059 private Boolean shouldAppendHMAC; 060 @XmlAttribute 061 private Boolean inline; 062 063 public CryptoDataFormat() { 064 super("crypto"); 065 } 066 067 @Override 068 protected DataFormat createDataFormat(RouteContext routeContext) { 069 DataFormat cryptoFormat = super.createDataFormat(routeContext); 070 071 if (ObjectHelper.isNotEmpty(keyRef)) { 072 Key key = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), keyRef, Key.class); 073 setProperty(routeContext.getCamelContext(), cryptoFormat, "key", key); 074 } 075 if (ObjectHelper.isNotEmpty(algorithmParameterRef)) { 076 AlgorithmParameterSpec spec = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), 077 algorithmParameterRef, AlgorithmParameterSpec.class); 078 setProperty(routeContext.getCamelContext(), cryptoFormat, "AlgorithmParameterSpec", spec); 079 } 080 if (ObjectHelper.isNotEmpty(initVectorRef)) { 081 byte[] iv = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), initVectorRef, byte[].class); 082 setProperty(routeContext.getCamelContext(), cryptoFormat, "InitializationVector", iv); 083 } 084 return cryptoFormat; 085 } 086 087 @Override 088 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 089 Boolean answer = ObjectHelper.toBoolean(shouldAppendHMAC); 090 if (answer != null && !answer) { 091 setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.FALSE); 092 } else { 093 setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.TRUE); 094 } 095 answer = ObjectHelper.toBoolean(inline); 096 if (answer != null && answer) { 097 setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.TRUE); 098 } else { 099 setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.FALSE); 100 } 101 if (algorithm != null) { 102 setProperty(camelContext, dataFormat, "algorithm", algorithm); 103 } 104 if (cryptoProvider != null) { 105 setProperty(camelContext, dataFormat, "cryptoProvider", cryptoProvider); 106 } 107 if (macAlgorithm != null) { 108 setProperty(camelContext, dataFormat, "macAlgorithm", macAlgorithm); 109 } 110 if (buffersize != null) { 111 setProperty(camelContext, dataFormat, "buffersize", buffersize); 112 } 113 } 114 115 public String getAlgorithm() { 116 return algorithm; 117 } 118 119 /** 120 * The JCE algorithm name indicating the cryptographic algorithm that will be used. 121 * <p/> 122 * Is by default DES/CBC/PKCS5Padding. 123 */ 124 public void setAlgorithm(String algorithm) { 125 this.algorithm = algorithm; 126 } 127 128 public String getCryptoProvider() { 129 return cryptoProvider; 130 } 131 132 /** 133 * The name of the JCE Security Provider that should be used. 134 */ 135 public void setCryptoProvider(String cryptoProvider) { 136 this.cryptoProvider = cryptoProvider; 137 } 138 139 public String getKeyRef() { 140 return keyRef; 141 } 142 143 /** 144 * Refers to the secret key to lookup from the register to use. 145 */ 146 public void setKeyRef(String keyRef) { 147 this.keyRef = keyRef; 148 } 149 150 public String getInitVectorRef() { 151 return initVectorRef; 152 } 153 154 /** 155 * Refers to a byte array containing the Initialization Vector that will be used to initialize the Cipher. 156 */ 157 public void setInitVectorRef(String initVectorRef) { 158 this.initVectorRef = initVectorRef; 159 } 160 161 public String getAlgorithmParameterRef() { 162 return algorithmParameterRef; 163 } 164 165 /** 166 * A JCE AlgorithmParameterSpec used to initialize the Cipher. 167 * <p/> 168 * Will lookup the type using the given name as a {@link java.security.spec.AlgorithmParameterSpec} type. 169 */ 170 public void setAlgorithmParameterRef(String algorithmParameterRef) { 171 this.algorithmParameterRef = algorithmParameterRef; 172 } 173 174 public Integer getBuffersize() { 175 return buffersize; 176 } 177 178 /** 179 * The size of the buffer used in the signature process. 180 */ 181 public void setBuffersize(Integer buffersize) { 182 this.buffersize = buffersize; 183 } 184 185 public String getMacAlgorithm() { 186 return macAlgorithm; 187 } 188 189 /** 190 * The JCE algorithm name indicating the Message Authentication algorithm. 191 */ 192 public void setMacAlgorithm(String macAlgorithm) { 193 this.macAlgorithm = macAlgorithm; 194 } 195 196 public Boolean getShouldAppendHMAC() { 197 return shouldAppendHMAC; 198 } 199 200 /** 201 * Flag indicating that a Message Authentication Code should be calculated and appended to the encrypted data. 202 */ 203 public void setShouldAppendHMAC(Boolean shouldAppendHMAC) { 204 this.shouldAppendHMAC = shouldAppendHMAC; 205 } 206 207 public Boolean getInline() { 208 return inline; 209 } 210 211 /** 212 * Flag indicating that the configured IV should be inlined into the encrypted data stream. 213 * <p/> 214 * Is by default false. 215 */ 216 public void setInline(Boolean inline) { 217 this.inline = inline; 218 } 219}