Skip to content

Commit 5f7d871

Browse files
shazinjzheaux
authored andcommitted
Add X.509 Certificate Support
Closes gh-9736
1 parent c173e80 commit 5f7d871

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

core/src/main/java/org/springframework/security/converter/RsaKeyConverters.java

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,15 @@
1717
package org.springframework.security.converter;
1818

1919
import java.io.BufferedReader;
20+
import java.io.ByteArrayInputStream;
21+
import java.io.IOException;
2022
import java.io.InputStream;
2123
import java.io.InputStreamReader;
2224
import java.security.KeyFactory;
2325
import java.security.NoSuchAlgorithmException;
26+
import java.security.cert.CertificateException;
27+
import java.security.cert.CertificateFactory;
28+
import java.security.cert.X509Certificate;
2429
import java.security.interfaces.RSAPrivateKey;
2530
import java.security.interfaces.RSAPublicKey;
2631
import java.security.spec.PKCS8EncodedKeySpec;
@@ -36,6 +41,7 @@
3641
* Used for creating {@link java.security.Key} converter instances
3742
*
3843
* @author Josh Cummings
44+
* @author Shazin Sadakath
3945
* @since 5.2
4046
*/
4147
public final class RsaKeyConverters {
@@ -50,6 +56,10 @@ public final class RsaKeyConverters {
5056

5157
private static final String X509_PEM_FOOTER = DASHES + "END PUBLIC KEY" + DASHES;
5258

59+
private static final String X509_CERT_HEADER = DASHES + "BEGIN CERTIFICATE" + DASHES;
60+
61+
private static final String X509_CERT_FOOTER = DASHES + "END CERTIFICATE" + DASHES;
62+
5363
private RsaKeyConverters() {
5464
}
5565

@@ -91,8 +101,8 @@ public static Converter<InputStream, RSAPrivateKey> pkcs8() {
91101
}
92102

93103
/**
94-
* Construct a {@link Converter} for converting a PEM-encoded X.509 RSA Public Key
95-
* into a {@link RSAPublicKey}.
104+
* Construct a {@link Converter} for converting a PEM-encoded X.509 RSA Public Key or
105+
* X.509 Certificate into a {@link RSAPublicKey}.
96106
*
97107
* This converter does not close the {@link InputStream} in order to avoid making
98108
* non-portable assumptions about the streams' origin and further use.
@@ -101,27 +111,52 @@ public static Converter<InputStream, RSAPrivateKey> pkcs8() {
101111
*/
102112
public static Converter<InputStream, RSAPublicKey> x509() {
103113
KeyFactory keyFactory = rsaFactory();
114+
CertificateFactory certificateFactory = x509CertificateFactory();
104115
return (source) -> {
105116
List<String> lines = readAllLines(source);
106-
Assert.isTrue(!lines.isEmpty() && lines.get(0).startsWith(X509_PEM_HEADER),
107-
"Key is not in PEM-encoded X.509 format, please check that the header begins with -----"
108-
+ X509_PEM_HEADER + "-----");
117+
Assert.isTrue(
118+
!lines.isEmpty()
119+
&& (lines.get(0).startsWith(X509_PEM_HEADER) || lines.get(0).startsWith(X509_CERT_HEADER)),
120+
"Key is not in PEM-encoded X.509 format or a valid X.509 certificate, please check that the header begins with "
121+
+ X509_PEM_HEADER + " or " + X509_CERT_HEADER);
109122
StringBuilder base64Encoded = new StringBuilder();
110123
for (String line : lines) {
111-
if (RsaKeyConverters.isNotX509Wrapper(line)) {
124+
if (RsaKeyConverters.isNotX509PemWrapper(line) && isNotX509CertificateWrapper(line)) {
112125
base64Encoded.append(line);
113126
}
114127
}
115128
byte[] x509 = Base64.getDecoder().decode(base64Encoded.toString());
116-
try {
117-
return (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(x509));
129+
if (lines.get(0).startsWith(X509_PEM_HEADER)) {
130+
try {
131+
return (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(x509));
132+
}
133+
catch (Exception ex) {
134+
throw new IllegalArgumentException(ex);
135+
}
118136
}
119-
catch (Exception ex) {
120-
throw new IllegalArgumentException(ex);
137+
if (lines.get(0).startsWith(X509_CERT_HEADER)) {
138+
try (InputStream x509CertStream = new ByteArrayInputStream(x509)) {
139+
X509Certificate certificate = (X509Certificate) certificateFactory
140+
.generateCertificate(x509CertStream);
141+
return (RSAPublicKey) certificate.getPublicKey();
142+
}
143+
catch (CertificateException | IOException ex) {
144+
throw new IllegalArgumentException(ex);
145+
}
121146
}
147+
return null;
122148
};
123149
}
124150

151+
private static CertificateFactory x509CertificateFactory() {
152+
try {
153+
return CertificateFactory.getInstance("X.509");
154+
}
155+
catch (CertificateException ex) {
156+
throw new IllegalArgumentException(ex);
157+
}
158+
}
159+
125160
private static List<String> readAllLines(InputStream source) {
126161
BufferedReader reader = new BufferedReader(new InputStreamReader(source));
127162
return reader.lines().collect(Collectors.toList());
@@ -140,8 +175,12 @@ private static boolean isNotPkcs8Wrapper(String line) {
140175
return !PKCS8_PEM_HEADER.equals(line) && !PKCS8_PEM_FOOTER.equals(line);
141176
}
142177

143-
private static boolean isNotX509Wrapper(String line) {
178+
private static boolean isNotX509PemWrapper(String line) {
144179
return !X509_PEM_HEADER.equals(line) && !X509_PEM_FOOTER.equals(line);
145180
}
146181

182+
private static boolean isNotX509CertificateWrapper(String line) {
183+
return !X509_CERT_HEADER.equals(line) && !X509_CERT_FOOTER.equals(line);
184+
}
185+
147186
}

core/src/test/java/org/springframework/security/converter/RsaKeyConvertersTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,6 +93,20 @@ public class RsaKeyConvertersTests {
9393
+ "-----END PUBLIC KEY-----";
9494
// @formatter:on
9595

96+
// @formatter:off
97+
private static final String X509_CERTIFICATE = "-----BEGIN CERTIFICATE-----\n" +
98+
"MIIBqDCCARECBgF5zJA6MjANBgkqhkiG9w0BAQsFADAaMRgwFgYDVQQDEw9TaGF6\n" +
99+
"aW4gU2FkYWthdGgwHhcNMjEwNjAxMTE1MTE0WhcNMjEwNTE3MjAwOTI1WjAaMRgw\n" +
100+
"FgYDVQQDEw9TaGF6aW4gU2FkYWthdGgwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\n" +
101+
"AoGBAKsKpS6sliNSri3koOAgzS7Nz2cpl0tGpNP3GPuUYVMP4MA0LJ2+blxjxUcn\n" +
102+
"oIajtaf9HljFetKVjyARp1zjZ3Oxm//lfmyqqI5KDUjqe5J2rdtbdFCH9FXUEoGD\n" +
103+
"mu2ameR9lAfxtaGI58DGS9uJ5hvGJoIvLiaDUfv1qZ+kIwG7AgMBAAEwDQYJKoZI\n" +
104+
"hvcNAQELBQADgYEAWdIIi4cGPod5O/V7K0QSTXZRLRIKFQ7qhn5XTNlMUnFnwp7c\n" +
105+
"8O8EsOiCKAZeVvgRnurFkxAlVnpxmdktZ9j+mv2mrMGKJxYkZcBkFh++DRixpY8N\n" +
106+
"zBLbxZJ9kcOHWWDA602FMbNIEL1OiHrfggsPk3sckSaSg4d7UoP9T6+uqq8=\n" +
107+
"-----END CERTIFICATE-----";
108+
// @formatter:on
109+
96110
private static final String MALFORMED_X509_KEY = "malformed";
97111

98112
private final Converter<InputStream, RSAPublicKey> x509 = RsaKeyConverters.x509();
@@ -112,11 +126,17 @@ public void pkcs8WhenConvertingPkcs1PrivateKeyThenIllegalArgumentException() {
112126
}
113127

114128
@Test
115-
public void x509WhenConverteringX509PublicKeyThenOk() {
129+
public void x509WhenConvertingX509PublicKeyThenOk() {
116130
RSAPublicKey key = this.x509.convert(toInputStream(X509_PUBLIC_KEY));
117131
Assertions.assertThat(key.getModulus().bitLength()).isEqualTo(1024);
118132
}
119133

134+
@Test
135+
public void x509WhenConvertingX509CertificateThenOk() {
136+
RSAPublicKey key = this.x509.convert(toInputStream(X509_CERTIFICATE));
137+
Assertions.assertThat(key.getModulus().bitLength()).isEqualTo(1024);
138+
}
139+
120140
@Test
121141
public void x509WhenConvertingDerEncodedX509PublicKeyThenIllegalArgumentException() {
122142
assertThatIllegalArgumentException().isThrownBy(() -> this.x509.convert(toInputStream(MALFORMED_X509_KEY)));

0 commit comments

Comments
 (0)