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