|
| 1 | +package org.bouncycastle.tls.crypto.test; |
| 2 | + |
| 3 | +import java.io.OutputStream; |
| 4 | +import java.security.InvalidAlgorithmParameterException; |
| 5 | +import java.security.InvalidKeyException; |
| 6 | +import java.security.KeyPair; |
| 7 | +import java.security.KeyPairGenerator; |
| 8 | +import java.security.PrivateKey; |
| 9 | +import java.security.Provider; |
| 10 | +import java.security.PublicKey; |
| 11 | +import java.security.SecureRandom; |
| 12 | +import java.security.Signature; |
| 13 | +import java.security.SignatureException; |
| 14 | +import java.security.SignatureSpi; |
| 15 | +import java.security.spec.AlgorithmParameterSpec; |
| 16 | + |
| 17 | +import junit.framework.TestCase; |
| 18 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 19 | +import org.bouncycastle.tls.SignatureScheme; |
| 20 | +import org.bouncycastle.tls.crypto.TlsStreamSigner; |
| 21 | +import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCrypto; |
| 22 | +import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCryptoProvider; |
| 23 | +import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsRSAPSSSigner; |
| 24 | +import org.bouncycastle.util.Strings; |
| 25 | + |
| 26 | +/** |
| 27 | + * Regression test for RSA-PSS CertificateVerify generation when the private key is held by an |
| 28 | + * alternate provider that registers only the generic "RSASSA-PSS" signature name and takes the |
| 29 | + * digest from a PSSParameterSpec - the shape of SunMSCAPI (Windows smart card keys) since JDK 13. |
| 30 | + */ |
| 31 | +public class JcaTlsRSAPSSAltProviderTest |
| 32 | + extends TestCase |
| 33 | +{ |
| 34 | + private static final BouncyCastleProvider BC = new BouncyCastleProvider(); |
| 35 | + |
| 36 | + public void testRSAPSSSignatureWithGenericPSSOnlyAltProvider() |
| 37 | + throws Exception |
| 38 | + { |
| 39 | + KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", BC); |
| 40 | + kpGen.initialize(2048, new SecureRandom()); |
| 41 | + KeyPair kp = kpGen.generateKeyPair(); |
| 42 | + |
| 43 | + JcaTlsCrypto crypto = new JcaTlsCryptoProvider() |
| 44 | + .setProvider(BC) |
| 45 | + .setAlternateProvider(new GenericPSSOnlyProvider()) |
| 46 | + .create(new SecureRandom()); |
| 47 | + |
| 48 | + implTestSignatureScheme(crypto, kp, SignatureScheme.rsa_pss_rsae_sha256, "SHA256withRSAandMGF1"); |
| 49 | + implTestSignatureScheme(crypto, kp, SignatureScheme.rsa_pss_rsae_sha384, "SHA384withRSAandMGF1"); |
| 50 | + implTestSignatureScheme(crypto, kp, SignatureScheme.rsa_pss_rsae_sha512, "SHA512withRSAandMGF1"); |
| 51 | + } |
| 52 | + |
| 53 | + private void implTestSignatureScheme(JcaTlsCrypto crypto, KeyPair kp, int signatureScheme, String verifyAlgorithm) |
| 54 | + throws Exception |
| 55 | + { |
| 56 | + PrivateKey opaqueKey = new OpaquePrivateKey(kp.getPrivate()); |
| 57 | + |
| 58 | + JcaTlsRSAPSSSigner signer = new JcaTlsRSAPSSSigner(crypto, opaqueKey, signatureScheme); |
| 59 | + |
| 60 | + TlsStreamSigner streamSigner = signer.getStreamSigner( |
| 61 | + SignatureScheme.getSignatureAndHashAlgorithm(signatureScheme)); |
| 62 | + |
| 63 | + byte[] message = Strings.toByteArray("RSA-PSS via generic-name-only alternate provider"); |
| 64 | + |
| 65 | + OutputStream sOut = streamSigner.getOutputStream(); |
| 66 | + sOut.write(message, 0, message.length); |
| 67 | + sOut.close(); |
| 68 | + |
| 69 | + byte[] signature = streamSigner.getSignature(); |
| 70 | + |
| 71 | + Signature verifier = Signature.getInstance(verifyAlgorithm, BC); |
| 72 | + verifier.initVerify(kp.getPublic()); |
| 73 | + verifier.update(message, 0, message.length); |
| 74 | + |
| 75 | + assertTrue(SignatureScheme.getText(signatureScheme), verifier.verify(signature)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Mimics SunMSCAPI: the only registered signature is the generic "RSASSA-PSS", the digest must |
| 80 | + * be supplied via setParameter, and only the provider's own (opaque) key type is accepted. |
| 81 | + */ |
| 82 | + public static class GenericPSSOnlyProvider |
| 83 | + extends Provider |
| 84 | + { |
| 85 | + GenericPSSOnlyProvider() |
| 86 | + { |
| 87 | + super("GenericPSSOnly", 1.0, "signs with the generic RSASSA-PSS algorithm name only"); |
| 88 | + |
| 89 | + put("Signature.RSASSA-PSS", GenericPSSSignatureSpi.class.getName()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static class OpaquePrivateKey |
| 94 | + implements PrivateKey |
| 95 | + { |
| 96 | + final PrivateKey inner; |
| 97 | + |
| 98 | + OpaquePrivateKey(PrivateKey inner) |
| 99 | + { |
| 100 | + this.inner = inner; |
| 101 | + } |
| 102 | + |
| 103 | + public String getAlgorithm() |
| 104 | + { |
| 105 | + return inner.getAlgorithm(); |
| 106 | + } |
| 107 | + |
| 108 | + public String getFormat() |
| 109 | + { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + public byte[] getEncoded() |
| 114 | + { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static class GenericPSSSignatureSpi |
| 120 | + extends SignatureSpi |
| 121 | + { |
| 122 | + private final Signature delegate; |
| 123 | + private boolean parameterSet = false; |
| 124 | + |
| 125 | + public GenericPSSSignatureSpi() |
| 126 | + throws Exception |
| 127 | + { |
| 128 | + this.delegate = Signature.getInstance("RSASSA-PSS", BC); |
| 129 | + } |
| 130 | + |
| 131 | + protected void engineInitSign(PrivateKey privateKey) |
| 132 | + throws InvalidKeyException |
| 133 | + { |
| 134 | + if (!(privateKey instanceof OpaquePrivateKey)) |
| 135 | + { |
| 136 | + throw new InvalidKeyException("key type not supported by this provider"); |
| 137 | + } |
| 138 | + |
| 139 | + delegate.initSign(((OpaquePrivateKey)privateKey).inner); |
| 140 | + } |
| 141 | + |
| 142 | + protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
| 143 | + throws InvalidKeyException |
| 144 | + { |
| 145 | + if (!(privateKey instanceof OpaquePrivateKey)) |
| 146 | + { |
| 147 | + throw new InvalidKeyException("key type not supported by this provider"); |
| 148 | + } |
| 149 | + |
| 150 | + delegate.initSign(((OpaquePrivateKey)privateKey).inner, random); |
| 151 | + } |
| 152 | + |
| 153 | + protected void engineInitVerify(PublicKey publicKey) |
| 154 | + throws InvalidKeyException |
| 155 | + { |
| 156 | + delegate.initVerify(publicKey); |
| 157 | + } |
| 158 | + |
| 159 | + protected void engineSetParameter(AlgorithmParameterSpec params) |
| 160 | + throws InvalidAlgorithmParameterException |
| 161 | + { |
| 162 | + delegate.setParameter(params); |
| 163 | + parameterSet = true; |
| 164 | + } |
| 165 | + |
| 166 | + protected void engineUpdate(byte b) |
| 167 | + throws SignatureException |
| 168 | + { |
| 169 | + checkParameterSet(); |
| 170 | + delegate.update(b); |
| 171 | + } |
| 172 | + |
| 173 | + protected void engineUpdate(byte[] b, int off, int len) |
| 174 | + throws SignatureException |
| 175 | + { |
| 176 | + checkParameterSet(); |
| 177 | + delegate.update(b, off, len); |
| 178 | + } |
| 179 | + |
| 180 | + protected byte[] engineSign() |
| 181 | + throws SignatureException |
| 182 | + { |
| 183 | + checkParameterSet(); |
| 184 | + return delegate.sign(); |
| 185 | + } |
| 186 | + |
| 187 | + protected boolean engineVerify(byte[] sigBytes) |
| 188 | + throws SignatureException |
| 189 | + { |
| 190 | + checkParameterSet(); |
| 191 | + return delegate.verify(sigBytes); |
| 192 | + } |
| 193 | + |
| 194 | + protected void engineSetParameter(String param, Object value) |
| 195 | + { |
| 196 | + throw new UnsupportedOperationException("engineSetParameter unsupported"); |
| 197 | + } |
| 198 | + |
| 199 | + protected Object engineGetParameter(String param) |
| 200 | + { |
| 201 | + throw new UnsupportedOperationException("engineGetParameter unsupported"); |
| 202 | + } |
| 203 | + |
| 204 | + private void checkParameterSet() |
| 205 | + throws SignatureException |
| 206 | + { |
| 207 | + // like SunMSCAPI, the generic algorithm has no default digest |
| 208 | + if (!parameterSet) |
| 209 | + { |
| 210 | + throw new SignatureException("parameters required for RSASSA-PSS signature"); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments