1
package org.bouncycastle.jce.provider;
3
import java.security.AlgorithmParameters;
4
import java.security.InvalidKeyException;
5
import java.security.InvalidParameterException;
6
import java.security.PrivateKey;
7
import java.security.PublicKey;
8
import java.security.SecureRandom;
9
import java.security.Signature;
10
import java.security.SignatureException;
11
import java.security.interfaces.RSAPrivateKey;
12
import java.security.interfaces.RSAPublicKey;
13
import java.security.spec.AlgorithmParameterSpec;
14
import java.security.spec.PSSParameterSpec;
16
import org.bouncycastle.crypto.AsymmetricBlockCipher;
17
import org.bouncycastle.crypto.CryptoException;
18
import org.bouncycastle.crypto.Digest;
19
import org.bouncycastle.crypto.digests.SHA1Digest;
20
import org.bouncycastle.crypto.digests.SHA224Digest;
21
import org.bouncycastle.crypto.digests.SHA256Digest;
22
import org.bouncycastle.crypto.digests.SHA384Digest;
23
import org.bouncycastle.crypto.digests.SHA512Digest;
24
import org.bouncycastle.crypto.engines.RSABlindedEngine;
25
import org.bouncycastle.crypto.params.ParametersWithRandom;
26
import org.bouncycastle.crypto.signers.PSSSigner;
28
public class JDKPSSSigner
31
private AsymmetricBlockCipher signer;
32
private Digest digest;
33
private int saltLength;
34
private AlgorithmParameters engineParams;
35
private PSSSigner pss;
37
protected JDKPSSSigner(
39
AsymmetricBlockCipher signer,
48
this.saltLength = digest.getDigestSize();
56
protected void engineInitVerify(
58
throws InvalidKeyException
60
if (!(publicKey instanceof RSAPublicKey))
62
throw new InvalidKeyException("Supplied key is not a RSAPublicKey instance");
65
pss = new PSSSigner(signer, digest, saltLength);
67
RSAUtil.generatePublicKeyParameter((RSAPublicKey)publicKey));
70
protected void engineInitSign(
71
PrivateKey privateKey,
73
throws InvalidKeyException
75
if (!(privateKey instanceof RSAPrivateKey))
77
throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
80
pss = new PSSSigner(signer, digest, saltLength);
81
pss.init(true, new ParametersWithRandom(RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey), random));
84
protected void engineInitSign(
85
PrivateKey privateKey)
86
throws InvalidKeyException
88
if (!(privateKey instanceof RSAPrivateKey))
90
throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
93
pss = new PSSSigner(signer, digest, saltLength);
94
pss.init(true, RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey));
97
protected void engineUpdate(
99
throws SignatureException
104
protected void engineUpdate(
108
throws SignatureException
110
pss.update(b, off, len);
113
protected byte[] engineSign()
114
throws SignatureException
118
return pss.generateSignature();
120
catch (CryptoException e)
122
throw new SignatureException(e.getMessage());
126
protected boolean engineVerify(
128
throws SignatureException
130
return pss.verifySignature(sigBytes);
133
protected void engineSetParameter(
134
AlgorithmParameterSpec params)
135
throws InvalidParameterException
137
if (params instanceof PSSParameterSpec)
139
saltLength = ((PSSParameterSpec)params).getSaltLength();
143
throw new InvalidParameterException("Only PSSParameterSpec supported");
147
protected AlgorithmParameters engineGetParameters()
149
if (engineParams == null)
153
engineParams = AlgorithmParameters.getInstance("PSS", "BC");
154
engineParams.init(new PSSParameterSpec(saltLength));
158
throw new RuntimeException(e.toString());
166
* @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
168
protected void engineSetParameter(
172
throw new UnsupportedOperationException("engineSetParameter unsupported");
175
protected Object engineGetParameter(
178
throw new UnsupportedOperationException("engineGetParameter unsupported");
181
static public class PSSwithRSA
186
super("SHA1withRSAandMGF1", new RSABlindedEngine(), null);
190
static public class SHA1withRSA
195
super("SHA1withRSAandMGF1", new RSABlindedEngine(), new SHA1Digest());
199
static public class SHA224withRSA
202
public SHA224withRSA()
204
super("SHA224withRSAandMGF1", new RSABlindedEngine(), new SHA224Digest());
208
static public class SHA256withRSA
211
public SHA256withRSA()
213
super("SHA256withRSAandMGF1", new RSABlindedEngine(), new SHA256Digest());
217
static public class SHA384withRSA
220
public SHA384withRSA()
222
super("SHA384withRSAandMGF1", new RSABlindedEngine(), new SHA384Digest());
226
static public class SHA512withRSA
229
public SHA512withRSA()
231
super("SHA512withRSAandMGF1", new RSABlindedEngine(), new SHA512Digest());