1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package org.bouncycastle.crypto.tls;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.DSA;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.digests.NullDigest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.signers.DSADigestSigner;
public abstract class TlsDSASigner
extends AbstractTlsSigner
{
public byte[] generateRawSignature(AsymmetricKeyParameter privateKey, byte[] md5AndSha1)
throws CryptoException
{
// Note: Only use the SHA1 part of the hash
Signer signer = makeSigner(new NullDigest(), true,
new ParametersWithRandom(privateKey, this.context.getSecureRandom()));
signer.update(md5AndSha1, 16, 20);
return signer.generateSignature();
}
public boolean verifyRawSignature(byte[] sigBytes, AsymmetricKeyParameter publicKey, byte[] md5AndSha1)
throws CryptoException
{
// Note: Only use the SHA1 part of the hash
Signer signer = makeSigner(new NullDigest(), false, publicKey);
signer.update(md5AndSha1, 16, 20);
return signer.verifySignature(sigBytes);
}
public Signer createSigner(AsymmetricKeyParameter privateKey)
{
return makeSigner(new SHA1Digest(), true, new ParametersWithRandom(privateKey, this.context.getSecureRandom()));
}
public Signer createVerifyer(AsymmetricKeyParameter publicKey)
{
return makeSigner(new SHA1Digest(), false, publicKey);
}
protected Signer makeSigner(Digest d, boolean forSigning, CipherParameters cp)
{
Signer s = new DSADigestSigner(createDSAImpl(), d);
s.init(forSigning, cp);
return s;
}
protected abstract DSA createDSAImpl();
}
|