Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8282723: Add constructors taking a cause to JSSE exceptions
Reviewed-by: wetmore, iris
  • Loading branch information
XueleiFan committed Mar 20, 2022
1 parent 3f923b8 commit 4df6742
Show file tree
Hide file tree
Showing 30 changed files with 343 additions and 132 deletions.
15 changes: 5 additions & 10 deletions src/java.base/share/classes/javax/net/ssl/SSLException.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,9 +36,7 @@
* @since 1.4
* @author David Brownell
*/
public
class SSLException extends IOException
{
public class SSLException extends IOException {
@java.io.Serial
private static final long serialVersionUID = 4511006460650708967L;

Expand All @@ -48,8 +46,7 @@ class SSLException extends IOException
*
* @param reason describes the problem.
*/
public SSLException(String reason)
{
public SSLException(String reason) {
super(reason);
}

Expand All @@ -66,8 +63,7 @@ public SSLException(String reason)
* @since 1.5
*/
public SSLException(String message, Throwable cause) {
super(message);
initCause(cause);
super(message, cause);
}

/**
Expand All @@ -83,7 +79,6 @@ public SSLException(String message, Throwable cause) {
* @since 1.5
*/
public SSLException(Throwable cause) {
super(cause == null ? null : cause.toString());
initCause(cause);
super(cause);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,20 +23,16 @@
* questions.
*/


package javax.net.ssl;


/**
* Indicates that the client and server could not negotiate the
* desired level of security. The connection is no longer usable.
*
* @since 1.4
* @author David Brownell
*/
public
class SSLHandshakeException extends SSLException
{
public class SSLHandshakeException extends SSLException {
@java.io.Serial
private static final long serialVersionUID = -5045881315018326890L;

Expand All @@ -46,8 +42,23 @@ class SSLHandshakeException extends SSLException
*
* @param reason describes the problem.
*/
public SSLHandshakeException(String reason)
{
public SSLHandshakeException(String reason) {
super(reason);
}

/**
* Creates a {@code SSLHandshakeException} with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 19
*/
public SSLHandshakeException(String message, Throwable cause) {
super(message, cause);
}
}
25 changes: 19 additions & 6 deletions src/java.base/share/classes/javax/net/ssl/SSLKeyException.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,9 +33,7 @@
* @since 1.4
* @author David Brownell
*/
public
class SSLKeyException extends SSLException
{
public class SSLKeyException extends SSLException {
@java.io.Serial
private static final long serialVersionUID = -8071664081941937874L;

Expand All @@ -45,8 +43,23 @@ class SSLKeyException extends SSLException
*
* @param reason describes the problem.
*/
public SSLKeyException(String reason)
{
public SSLKeyException(String reason) {
super(reason);
}

/**
* Creates a {@code SSLKeyException} with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 19
*/
public SSLKeyException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,9 +39,7 @@
* @since 1.4
* @author David Brownell
*/
public
class SSLPeerUnverifiedException extends SSLException
{
public class SSLPeerUnverifiedException extends SSLException {
@java.io.Serial
private static final long serialVersionUID = -8919512675000600547L;

Expand All @@ -51,8 +49,23 @@ class SSLPeerUnverifiedException extends SSLException
*
* @param reason describes the problem.
*/
public SSLPeerUnverifiedException(String reason)
{
public SSLPeerUnverifiedException(String reason) {
super(reason);
}

/**
* Creates a {@code SSLPeerUnverifiedException} with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 19
*/
public SSLPeerUnverifiedException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,9 +33,7 @@
* @since 1.4
* @author David Brownell
*/
public
class SSLProtocolException extends SSLException
{
public class SSLProtocolException extends SSLException {
@java.io.Serial
private static final long serialVersionUID = 5445067063799134928L;

Expand All @@ -45,8 +43,23 @@ class SSLProtocolException extends SSLException
*
* @param reason describes the problem.
*/
public SSLProtocolException(String reason)
{
public SSLProtocolException(String reason) {
super(reason);
}

/**
* Creates a {@code SSLProtocolException} with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 19
*/
public SSLProtocolException(String message, Throwable cause) {
super(message, cause);
}
}
17 changes: 5 additions & 12 deletions src/java.base/share/classes/sun/security/ssl/Alert.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -122,22 +122,15 @@ SSLException createSSLException(String reason, Throwable cause) {
reason = (cause != null) ? cause.getMessage() : "";
}

SSLException ssle;
if (cause instanceof IOException) {
ssle = new SSLException(reason);
return new SSLException(reason, cause);
} else if ((this == UNEXPECTED_MESSAGE)) {
ssle = new SSLProtocolException(reason);
return new SSLProtocolException(reason, cause);
} else if (handshakeOnly) {
ssle = new SSLHandshakeException(reason);
return new SSLHandshakeException(reason, cause);
} else {
ssle = new SSLException(reason);
return new SSLException(reason, cause);
}

if (cause != null) {
ssle.initCause(cause);
}

return ssle;
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -295,8 +295,8 @@ public void consume(ConnectionContext context,
shc.handshakeCredentials.add(
new DHECredentials(peerPublicKey, namedGroup));
} catch (GeneralSecurityException | java.io.IOException e) {
throw (SSLHandshakeException)(new SSLHandshakeException(
"Could not generate DHPublicKey").initCause(e));
throw new SSLHandshakeException(
"Could not generate DHPublicKey", e);
}

// update the states
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -160,8 +160,7 @@ SecretKey getAgreedSecret(
ka.doPhase(peerPublicKey, true);
return ka.generateSecret("TlsPremasterSecret");
} catch (GeneralSecurityException e) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate secret").initCause(e);
throw new SSLHandshakeException("Could not generate secret", e);
}
}

Expand All @@ -177,8 +176,7 @@ SecretKey getAgreedSecret(
PublicKey peerPublicKey = kf.generatePublic(spec);
return getAgreedSecret(peerPublicKey);
} catch (GeneralSecurityException | java.io.IOException e) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate secret").initCause(e);
throw new SSLHandshakeException("Could not generate secret", e);
}
}

Expand All @@ -202,8 +200,8 @@ void checkConstraints(AlgorithmConstraints constraints,
"ECPublicKey does not comply to algorithm constraints");
}
} catch (GeneralSecurityException | java.io.IOException e) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate ECPublicKey").initCause(e);
throw new SSLHandshakeException(
"Could not generate ECPublicKey", e);
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -88,8 +88,7 @@ private SecretKey t12DeriveKey(String algorithm,
context, preMasterSecret);
return kd.deriveKey("MasterSecret", params);
} catch (GeneralSecurityException gse) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate secret").initCause(gse);
throw new SSLHandshakeException("Could not generate secret", gse);
}
}

Expand Down Expand Up @@ -125,8 +124,7 @@ private SecretKey t13DeriveKey(String algorithm,
// derive handshake secret
return hkdf.extract(saltSecret, sharedSecret, algorithm);
} catch (GeneralSecurityException gse) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate secret").initCause(gse);
throw new SSLHandshakeException("Could not generate secret", gse);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -302,8 +302,7 @@ private static SecretKey derivePreSharedKey(CipherSuite.HashAlg hashAlg,
return hkdf.expand(resumptionMasterSecret, hkdfInfo,
hashAlg.hashLength, "TlsPreSharedKey");
} catch (GeneralSecurityException gse) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not derive PSK").initCause(gse);
throw new SSLHandshakeException("Could not derive PSK", gse);
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -52,8 +52,7 @@ public SecretKey deriveKey(String algorithm,
return hkdf.expand(secret, hkdfInfo,
((SecretSizeSpec)keySpec).length, algorithm);
} catch (GeneralSecurityException gse) {
throw (SSLHandshakeException) new SSLHandshakeException(
"Could not generate secret").initCause(gse);
throw new SSLHandshakeException("Could not generate secret", gse);
}
}

Expand Down

1 comment on commit 4df6742

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.