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.component.crypto.processor;
018
019 import java.io.InputStream;
020 import java.lang.reflect.Field;
021 import java.security.KeyStore;
022 import java.security.NoSuchAlgorithmException;
023 import java.security.NoSuchProviderException;
024 import java.security.Signature;
025 import java.util.Map;
026
027 import org.apache.camel.Exchange;
028 import org.apache.camel.Message;
029 import org.apache.camel.Processor;
030 import org.apache.camel.component.crypto.DigitalSignatureConfiguration;
031 import org.apache.camel.component.crypto.DigitalSignatureConstants;
032 import org.apache.camel.util.ExchangeHelper;
033
034 import static org.apache.camel.component.crypto.DigitalSignatureConstants.KEYSTORE_ALIAS;
035
036 public abstract class DigitalSignatureProcessor implements Processor {
037
038 protected DigitalSignatureConfiguration config;
039
040 public DigitalSignatureProcessor(DigitalSignatureConfiguration configuration) {
041 this.config = configuration;
042 }
043
044 public void calculateSignature(Exchange exchange, Signature signer) throws Exception {
045 Object payload = exchange.getIn().getBody();
046 if (payload != null) {
047 InputStream payloadStream = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, payload);
048 byte[] buffer = new byte[config.getBufferSize()];
049 int read;
050 while ((read = payloadStream.read(buffer)) > 0) {
051 signer.update(buffer, 0, read);
052 }
053 }
054 }
055
056 protected String getAlias(Exchange exchange) throws Exception {
057 KeyStore keystore = config.getKeystore();
058 if (keystore != null) {
059 String alias = exchange.getIn().getHeader(KEYSTORE_ALIAS, String.class);
060 if (alias == null) {
061 alias = config.getAlias();
062 }
063
064 // if there is only one entry then use it. Document this well
065 if (alias == null && keystore.size() == 1) {
066 alias = keystore.aliases().nextElement();
067 }
068 return alias;
069 }
070 return null;
071 }
072
073 public void setConfig(DigitalSignatureConfiguration config) {
074 this.config = config;
075 }
076
077 protected Signature createSignatureService() throws NoSuchAlgorithmException, NoSuchProviderException {
078 String algorithm = config.getAlgorithm();
079 String provider = config.getProvider();
080 Signature signer = provider == null ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, provider);
081 return signer;
082 }
083
084 protected void clearMessageHeaders(Message in) {
085 if (config.getClearHeaders()) {
086 Map<String, Object> headers = in.getHeaders();
087 for (Field f : DigitalSignatureConstants.class.getFields()) {
088 headers.remove(f.getName());
089 }
090 }
091 }
092 }