Skip to content

Commit 93077ba

Browse files
committed
Add some TLS exception detail messages
1 parent 04f2f98 commit 93077ba

File tree

5 files changed

+99
-48
lines changed

5 files changed

+99
-48
lines changed

tls/src/main/java/org/bouncycastle/tls/TlsUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4765,7 +4765,8 @@ static void checkTlsFeatures(Certificate serverCertificate, Hashtable clientExte
47654765
{
47664766
if (!(tlsFeaturesSeq.getObjectAt(i) instanceof ASN1Integer))
47674767
{
4768-
throw new TlsFatalAlert(AlertDescription.bad_certificate);
4768+
throw new TlsFatalAlert(AlertDescription.bad_certificate,
4769+
"Server certificate has invalid TLS Features extension");
47694770
}
47704771
}
47714772

@@ -4779,7 +4780,8 @@ static void checkTlsFeatures(Certificate serverCertificate, Hashtable clientExte
47794780
Integer extensionType = Integers.valueOf(tlsExtension.intValue());
47804781
if (clientExtensions.containsKey(extensionType) && !serverExtensions.containsKey(extensionType))
47814782
{
4782-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
4783+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
4784+
"Server extensions missing TLS Feature " + extensionType);
47834785
}
47844786
}
47854787
}

tls/src/main/java/org/bouncycastle/tls/crypto/impl/bc/BcTlsCertificate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ASN1Encodable getSigAlgParams()
102102
return certificate.getSignatureAlgorithm().getParameters();
103103
}
104104

105-
protected boolean supportsKeyUsage(int keyUsageBits)
105+
protected boolean supportsKeyUsage(int keyUsageBit)
106106
{
107107
Extensions exts = certificate.getTBSCertificate().getExtensions();
108108
if (exts != null)
@@ -111,7 +111,7 @@ protected boolean supportsKeyUsage(int keyUsageBits)
111111
if (ku != null)
112112
{
113113
int bits = ku.getBytes()[0] & 0xff;
114-
if ((bits & keyUsageBits) != keyUsageBits)
114+
if ((bits & keyUsageBit) != keyUsageBit)
115115
{
116116
return false;
117117
}

tls/src/main/java/org/bouncycastle/tls/crypto/impl/bc/BcTlsRawKeyCertificate.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public TlsEncryptor createEncryptor(int tlsCertificateRole) throws IOException
9191
// }
9292
}
9393

94-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
94+
throw new TlsFatalAlert(AlertDescription.internal_error);
9595
}
9696

9797
public TlsVerifier createVerifier(short signatureAlgorithm) throws IOException
@@ -146,7 +146,7 @@ public TlsVerifier createVerifier(short signatureAlgorithm) throws IOException
146146
case SignatureAlgorithm.gostr34102012_512:
147147

148148
default:
149-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
149+
throw new TlsFatalAlert(AlertDescription.internal_error);
150150
}
151151
}
152152

@@ -248,7 +248,7 @@ public Tls13Verifier createVerifier(int signatureScheme) throws IOException
248248
// }
249249

250250
default:
251-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
251+
throw new TlsFatalAlert(AlertDescription.internal_error);
252252
}
253253
}
254254

@@ -335,7 +335,7 @@ public DHPublicKeyParameters getPubKeyDH() throws IOException
335335
}
336336
catch (ClassCastException e)
337337
{
338-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
338+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not DH", e);
339339
}
340340
}
341341

@@ -347,7 +347,7 @@ public DSAPublicKeyParameters getPubKeyDSS() throws IOException
347347
}
348348
catch (ClassCastException e)
349349
{
350-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
350+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not DSS", e);
351351
}
352352
}
353353

@@ -359,7 +359,7 @@ public ECPublicKeyParameters getPubKeyEC() throws IOException
359359
}
360360
catch (ClassCastException e)
361361
{
362-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
362+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not EC", e);
363363
}
364364
}
365365

@@ -371,7 +371,7 @@ public Ed25519PublicKeyParameters getPubKeyEd25519() throws IOException
371371
}
372372
catch (ClassCastException e)
373373
{
374-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
374+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not Ed25519", e);
375375
}
376376
}
377377

@@ -383,7 +383,7 @@ public Ed448PublicKeyParameters getPubKeyEd448() throws IOException
383383
}
384384
catch (ClassCastException e)
385385
{
386-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
386+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not Ed448", e);
387387
}
388388
}
389389

@@ -395,7 +395,7 @@ public RSAKeyParameters getPubKeyRSA() throws IOException
395395
}
396396
catch (ClassCastException e)
397397
{
398-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
398+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not RSA", e);
399399
}
400400
}
401401

@@ -428,7 +428,7 @@ public TlsCertificate checkUsageInRole(int tlsCertificateRole) throws IOExceptio
428428
}
429429
}
430430

431-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
431+
throw new TlsFatalAlert(AlertDescription.internal_error);
432432
}
433433

434434
protected AsymmetricKeyParameter getPublicKey() throws IOException
@@ -443,7 +443,7 @@ protected AsymmetricKeyParameter getPublicKey() throws IOException
443443
}
444444
}
445445

446-
protected boolean supportsKeyUsage(int keyUsageBits)
446+
protected boolean supportsKeyUsage(int keyUsageBit)
447447
{
448448
return true;
449449
}
@@ -517,12 +517,25 @@ protected boolean supportsSignatureAlgorithm(short signatureAlgorithm, int keyUs
517517
}
518518
}
519519

520-
public void validateKeyUsage(int keyUsageBits)
520+
public void validateKeyUsage(int keyUsageBit)
521521
throws IOException
522522
{
523-
if (!supportsKeyUsage(keyUsageBits))
524-
{
525-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
523+
if (!supportsKeyUsage(keyUsageBit))
524+
{
525+
switch (keyUsageBit)
526+
{
527+
case KeyUsage.digitalSignature:
528+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
529+
"KeyUsage does not allow digital signatures");
530+
case KeyUsage.keyAgreement:
531+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
532+
"KeyUsage does not allow key agreement");
533+
case KeyUsage.keyEncipherment:
534+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
535+
"KeyUsage does not allow key encipherment");
536+
default:
537+
throw new TlsFatalAlert(AlertDescription.internal_error);
538+
}
526539
}
527540
}
528541

@@ -531,7 +544,7 @@ protected void validateRSA_PKCS1()
531544
{
532545
if (!supportsRSA_PKCS1())
533546
{
534-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
547+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "No support for rsa_pkcs1 signature schemes");
535548
}
536549
}
537550

@@ -540,7 +553,8 @@ protected void validateRSA_PSS_PSS(short signatureAlgorithm)
540553
{
541554
if (!supportsRSA_PSS_PSS(signatureAlgorithm))
542555
{
543-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
556+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
557+
"No support for rsa_pss_pss signature schemes");
544558
}
545559
}
546560

@@ -549,7 +563,8 @@ protected void validateRSA_PSS_RSAE()
549563
{
550564
if (!supportsRSA_PSS_RSAE())
551565
{
552-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
566+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
567+
"No support for rsa_pss_rsae signature schemes");
553568
}
554569
}
555570
}

tls/src/main/java/org/bouncycastle/tls/crypto/impl/jcajce/JcaTlsCertificate.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bouncycastle.asn1.ASN1Primitive;
2222
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
2323
import org.bouncycastle.asn1.x509.Certificate;
24+
import org.bouncycastle.asn1.x509.KeyUsage;
2425
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
2526
import org.bouncycastle.jcajce.util.JcaJceHelper;
2627
import org.bouncycastle.tls.AlertDescription;
@@ -132,7 +133,7 @@ public TlsEncryptor createEncryptor(int tlsCertificateRole) throws IOException
132133
// }
133134
}
134135

135-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
136+
throw new TlsFatalAlert(AlertDescription.internal_error);
136137
}
137138

138139
public TlsVerifier createVerifier(short signatureAlgorithm) throws IOException
@@ -187,7 +188,7 @@ public TlsVerifier createVerifier(short signatureAlgorithm) throws IOException
187188
case SignatureAlgorithm.gostr34102012_512:
188189

189190
default:
190-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
191+
throw new TlsFatalAlert(AlertDescription.internal_error);
191192
}
192193
}
193194

@@ -274,7 +275,7 @@ public Tls13Verifier createVerifier(int signatureScheme) throws IOException
274275
// case SignatureScheme.sm2sig_sm3:
275276

276277
default:
277-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
278+
throw new TlsFatalAlert(AlertDescription.internal_error);
278279
}
279280
}
280281

@@ -329,7 +330,7 @@ DHPublicKey getPubKeyDH() throws IOException
329330
}
330331
catch (ClassCastException e)
331332
{
332-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
333+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not DH", e);
333334
}
334335
}
335336

@@ -341,7 +342,7 @@ DSAPublicKey getPubKeyDSS() throws IOException
341342
}
342343
catch (ClassCastException e)
343344
{
344-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
345+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not DSS", e);
345346
}
346347
}
347348

@@ -353,7 +354,7 @@ ECPublicKey getPubKeyEC() throws IOException
353354
}
354355
catch (ClassCastException e)
355356
{
356-
throw new TlsFatalAlert(AlertDescription.certificate_unknown, e);
357+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not EC", e);
357358
}
358359
}
359360

@@ -365,7 +366,7 @@ PublicKey getPubKeyEd25519() throws IOException
365366
// Oracle provider (Java 15+) returns the key as an EdDSA one
366367
if (!("EdDSA".equals(publicKey.getAlgorithm()) && publicKey.toString().indexOf("Ed25519") >= 0))
367368
{
368-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
369+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not Ed25519");
369370
}
370371
}
371372
return publicKey;
@@ -379,14 +380,15 @@ PublicKey getPubKeyEd448() throws IOException
379380
// Oracle provider (Java 15+) returns the key as an EdDSA one
380381
if (!("EdDSA".equals(publicKey.getAlgorithm()) && publicKey.toString().indexOf("Ed448") >= 0))
381382
{
382-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
383+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "Public key not Ed448");
383384
}
384385
}
385386
return publicKey;
386387
}
387388

388389
PublicKey getPubKeyRSA() throws IOException
389390
{
391+
// TODO[tls] How to reliably check that this is an RSA key?
390392
return getPublicKey();
391393
}
392394

@@ -470,7 +472,7 @@ public TlsCertificate checkUsageInRole(int tlsCertificateRole) throws IOExceptio
470472
}
471473
}
472474

473-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
475+
throw new TlsFatalAlert(AlertDescription.internal_error);
474476
}
475477

476478
protected boolean implSupportsSignatureAlgorithm(short signatureAlgorithm) throws IOException
@@ -574,7 +576,20 @@ protected void validateKeyUsageBit(int keyUsageBit)
574576
{
575577
if (!supportsKeyUsageBit(keyUsageBit))
576578
{
577-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
579+
switch (keyUsageBit)
580+
{
581+
case KeyUsage.digitalSignature:
582+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
583+
"KeyUsage does not allow digital signatures");
584+
case KeyUsage.keyAgreement:
585+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
586+
"KeyUsage does not allow key agreement");
587+
case KeyUsage.keyEncipherment:
588+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
589+
"KeyUsage does not allow key encipherment");
590+
default:
591+
throw new TlsFatalAlert(AlertDescription.internal_error);
592+
}
578593
}
579594
}
580595

@@ -583,7 +598,7 @@ protected void validateRSA_PKCS1()
583598
{
584599
if (!supportsRSA_PKCS1())
585600
{
586-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
601+
throw new TlsFatalAlert(AlertDescription.certificate_unknown, "No support for rsa_pkcs1 signature schemes");
587602
}
588603
}
589604

@@ -592,7 +607,8 @@ protected void validateRSA_PSS_PSS(short signatureAlgorithm)
592607
{
593608
if (!supportsRSA_PSS_PSS(signatureAlgorithm))
594609
{
595-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
610+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
611+
"No support for rsa_pss_pss signature schemes");
596612
}
597613
}
598614

@@ -601,7 +617,8 @@ protected void validateRSA_PSS_RSAE()
601617
{
602618
if (!supportsRSA_PSS_RSAE())
603619
{
604-
throw new TlsFatalAlert(AlertDescription.certificate_unknown);
620+
throw new TlsFatalAlert(AlertDescription.certificate_unknown,
621+
"No support for rsa_pss_rsae signature schemes");
605622
}
606623
}
607624
}

0 commit comments

Comments
 (0)