Java tutorial
/* * eID Applet Project. * Copyright (C) 2009-2010 FedICT. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version * 3.0 as published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, see * http://www.gnu.org/licenses/. */ package org.sinekartads.core.cms; import java.security.InvalidKeyException; import java.security.InvalidParameterException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.SignatureException; import org.apache.commons.codec.binary.Hex; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.bouncycastle.util.Arrays; //import org.bouncycastle.util.Arrays; /** * A signature proxy implementation for SHA256withRSA signatures. * * @Deprecated use ExternalSigner * */ public class SHA256WithRSAProxySignature extends Signature { private static final Log LOG = LogFactory.getLog(SHA256WithRSAProxySignature.class); private static final ThreadLocal<byte[]> digestValues = new ThreadLocal<byte[]>(); private static final ThreadLocal<byte[]> signatureValues = new ThreadLocal<byte[]>(); private final MessageDigest messageDigest; public SHA256WithRSAProxySignature() throws NoSuchAlgorithmException { super("SHA256withRSA"); LOG.debug("constructor"); this.messageDigest = MessageDigest.getInstance("SHA-256"); } public static void reset() { SHA256WithRSAProxySignature.digestValues.set(null); SHA256WithRSAProxySignature.signatureValues.set(null); } @Override protected Object engineGetParameter(String param) throws InvalidParameterException { throw new UnsupportedOperationException(); } @Override protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { LOG.debug("engineInitSign: " + privateKey.getAlgorithm()); } @Override protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { throw new UnsupportedOperationException(); } @Override protected void engineSetParameter(String param, Object value) throws InvalidParameterException { throw new UnsupportedOperationException(); } @Override protected byte[] engineSign() throws SignatureException { LOG.debug("engineSign"); byte[] signatureValue = SHA256WithRSAProxySignature.signatureValues.get(); if (null != signatureValue) { LOG.debug("injecting signature value: " + Hex.encodeHexString(signatureValue)); reset(); return signatureValue; } LOG.debug("returning a dummy signature value"); return "dummy".getBytes(); } @Override protected void engineUpdate(byte b) throws SignatureException { throw new UnsupportedOperationException(); } @Override protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { LOG.debug("engineUpdate(b,off,len): off=" + off + "; len=" + len); this.messageDigest.update(b, off, len); byte[] digestValue = this.messageDigest.digest(); byte[] expectedDigestValue = SHA256WithRSAProxySignature.digestValues.get(); if (null == expectedDigestValue) { SHA256WithRSAProxySignature.digestValues.set(digestValue); } else { if (false == Arrays.areEqual(expectedDigestValue, digestValue)) { throw new IllegalStateException("digest value has changed"); } } LOG.debug("digest value: " + Hex.encodeHexString(digestValue)); } @Override protected boolean engineVerify(byte[] sigBytes) throws SignatureException { throw new UnsupportedOperationException(); } public static byte[] getDigestValue() { return SHA256WithRSAProxySignature.digestValues.get(); } public static void setSignatureValue(byte[] signatureValue) { SHA256WithRSAProxySignature.signatureValues.set(signatureValue); } public static void setDigestSignatureValue(byte[] digestValue, byte[] signatureValue) { SHA256WithRSAProxySignature.digestValues.set(digestValue); SHA256WithRSAProxySignature.signatureValues.set(signatureValue); } }