Skip to content

Commit a9c5800

Browse files
authored
Merge pull request #313 from digipost/less-drama-more-information
Less drama, more information
2 parents 064b638 + 4742a3c commit a9c5800

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

lib/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<dependency>
5656
<groupId>no.digipost</groupId>
5757
<artifactId>certificate-validator</artifactId>
58-
<version>3.0.2</version>
58+
<version>3.0.3</version>
5959
<exclusions>
6060
<exclusion>
6161
<groupId>org.bouncycastle</groupId>
@@ -72,13 +72,13 @@
7272
<dependency>
7373
<groupId>org.apache.httpcomponents.core5</groupId>
7474
<artifactId>httpcore5</artifactId>
75-
<version>5.2.2</version>
75+
<version>5.2.3</version>
7676
</dependency>
7777

7878
<dependency>
7979
<groupId>commons-io</groupId>
8080
<artifactId>commons-io</artifactId>
81-
<version>2.13.0</version>
81+
<version>2.14.0</version>
8282
<scope>test</scope>
8383
</dependency>
8484

@@ -124,7 +124,7 @@
124124
<dependency>
125125
<groupId>nl.jqno.equalsverifier</groupId>
126126
<artifactId>equalsverifier</artifactId>
127-
<version>3.15.1</version>
127+
<version>3.15.2</version>
128128
<scope>test</scope>
129129
</dependency>
130130
<dependency>

lib/src/main/java/no/digipost/signature/client/ServiceEnvironment.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String toString() {
8787
}
8888

8989

90-
enum Certificates implements ProvidesCertificateResourcePaths {
90+
enum Certificates {
9191

9292
TEST(
9393
"test/Buypass_Class_3_Test4_CA_3.cer",
@@ -125,10 +125,5 @@ enum Certificates implements ProvidesCertificateResourcePaths {
125125
.collect(toList());
126126
}
127127

128-
@Override
129-
public List<String> certificatePaths() {
130-
return certificatePaths();
131-
}
132-
133128
}
134129

lib/src/main/java/no/digipost/signature/client/core/internal/http/SignatureApiTrustStrategy.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
import no.digipost.signature.client.security.CertificateChainValidation.Result;
66
import org.apache.hc.core5.ssl.TrustStrategy;
77

8+
import java.math.BigInteger;
89
import java.security.cert.X509Certificate;
10+
import java.time.ZoneId;
11+
import java.time.ZonedDateTime;
12+
import java.util.Optional;
13+
14+
import static javax.security.auth.x500.X500Principal.RFC1779;
915

1016
public final class SignatureApiTrustStrategy implements TrustStrategy {
1117

@@ -35,11 +41,23 @@ public boolean isTrusted(X509Certificate[] chain, String authType) {
3541
case TRUSTED_AND_SKIP_FURTHER_VALIDATION: return true;
3642
case TRUSTED: return false;
3743
case UNTRUSTED: default:
38-
String subjectDN = chain[0].getSubjectX500Principal().getName();
44+
String certificateDescription = Optional.ofNullable(chain)
45+
.filter(certs -> certs.length > 0)
46+
.map(certs -> certs[0])
47+
.map(cert -> {
48+
String subjectDN = cert.getSubjectX500Principal().getName(RFC1779);
49+
BigInteger serialNumber = cert.getSerialNumber();
50+
String issuerDN = cert.getIssuerX500Principal().getName(RFC1779);
51+
ZonedDateTime expires = cert.getNotAfter().toInstant().atZone(ZoneId.systemDefault());
52+
return subjectDN + " (serial number " + serialNumber + ", expires " + expires + "), issued by " + issuerDN;
53+
})
54+
.orElse("<no server certificate>");
3955
throw new SecurityException(
4056
"Untrusted server certificate, according to " + certificateChainValidation + ". " +
41-
"Make sure the server URI is correct. Actual certificate: " + subjectDN + ". " +
42-
"This could indicate a misconfiguration of the client or server, or potentially a man-in-the-middle attack.");
57+
"Actual certificate from server response: " + certificateDescription + ". " +
58+
"This normally indicates either a misconfiguration of this client library, or a mixup of URLs used to communicate with the API. " +
59+
"Make sure the request URL is correct, is actually for the API, and it aligns with the configured ServiceEnvironment. " +
60+
"It should e.g. not be a URL that is to be accessed by a user from a web browser.");
4361
}
4462
}
4563

lib/src/main/java/no/digipost/signature/client/security/OrganizationNumberValidation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Result validate(X509Certificate[] certChain) {
2828

2929
@Override
3030
public String toString() {
31-
return getClass().getSimpleName() + " trusting '" + trustedOrganizationNumber + "'";
31+
return getClass().getSimpleName() + " trusting organization number '" + trustedOrganizationNumber + "'";
3232
}
3333

3434
}

0 commit comments

Comments
 (0)