Skip to content

Commit f1cc943

Browse files
committed
Align PemCertificateParser with main code
1 parent 9e6645e commit f1cc943

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PemCertificateParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.regex.Matcher;
2828
import java.util.regex.Pattern;
2929

30+
import org.jspecify.annotations.Nullable;
31+
3032
import org.springframework.util.Assert;
3133
import org.springframework.util.CollectionUtils;
3234

@@ -54,7 +56,7 @@ private PemCertificateParser() {
5456
* @param text the text to parse
5557
* @return the parsed certificates
5658
*/
57-
static List<X509Certificate> parse(String text) {
59+
static @Nullable List<X509Certificate> parse(@Nullable String text) {
5860
if (text == null) {
5961
return null;
6062
}

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PemPrivateKeyParser.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import javax.crypto.SecretKeyFactory;
4545
import javax.crypto.spec.PBEKeySpec;
4646

47+
import org.jspecify.annotations.Nullable;
48+
4749
import org.springframework.boot.buildpack.platform.docker.ssl.PemPrivateKeyParser.DerElement.TagType;
4850
import org.springframework.boot.buildpack.platform.docker.ssl.PemPrivateKeyParser.DerElement.ValueType;
4951
import org.springframework.util.Assert;
@@ -119,6 +121,7 @@ private static PKCS8EncodedKeySpec createKeySpecForPkcs1Rsa(byte[] bytes, String
119121

120122
private static PKCS8EncodedKeySpec createKeySpecForSec1Ec(byte[] bytes, String password) {
121123
DerElement ecPrivateKey = DerElement.of(bytes);
124+
Assert.state(ecPrivateKey != null, "Unable to find private key");
122125
Assert.state(ecPrivateKey.isType(ValueType.ENCODED, TagType.SEQUENCE),
123126
"Key spec should be an ASN.1 encoded sequence");
124127
DerElement version = DerElement.of(ecPrivateKey.getContents());
@@ -133,7 +136,7 @@ private static PKCS8EncodedKeySpec createKeySpecForSec1Ec(byte[] bytes, String p
133136
return createKeySpecForAlgorithm(bytes, ELLIPTIC_CURVE_ALGORITHM, getEcParameters(parameters));
134137
}
135138

136-
private static EncodedOid getEcParameters(DerElement parameters) {
139+
private static EncodedOid getEcParameters(@Nullable DerElement parameters) {
137140
if (parameters == null) {
138141
return ELLIPTIC_CURVE_384_BIT;
139142
}
@@ -145,7 +148,7 @@ private static EncodedOid getEcParameters(DerElement parameters) {
145148
}
146149

147150
private static PKCS8EncodedKeySpec createKeySpecForAlgorithm(byte[] bytes, EncodedOid algorithm,
148-
EncodedOid parameters) {
151+
@Nullable EncodedOid parameters) {
149152
try {
150153
DerEncoder encoder = new DerEncoder();
151154
encoder.integer(0x00); // Version 0
@@ -163,6 +166,7 @@ private static PKCS8EncodedKeySpec createKeySpecForAlgorithm(byte[] bytes, Encod
163166

164167
private static PKCS8EncodedKeySpec createKeySpecForPkcs8(byte[] bytes, String password) {
165168
DerElement ecPrivateKey = DerElement.of(bytes);
169+
Assert.state(ecPrivateKey != null, "Unable to find private key");
166170
Assert.state(ecPrivateKey.isType(ValueType.ENCODED, TagType.SEQUENCE),
167171
"Key spec should be an ASN.1 encoded sequence");
168172
DerElement version = DerElement.of(ecPrivateKey.getContents());
@@ -187,7 +191,7 @@ private static PKCS8EncodedKeySpec createKeySpecForPkcs8Encrypted(byte[] bytes,
187191
* @param text the text to parse
188192
* @return the parsed private key
189193
*/
190-
static PrivateKey parse(String text) {
194+
static @Nullable PrivateKey parse(String text) {
191195
return parse(text, null);
192196
}
193197

@@ -198,7 +202,7 @@ static PrivateKey parse(String text) {
198202
* @param password the password used to decrypt an encrypted private key
199203
* @return the parsed private key
200204
*/
201-
static PrivateKey parse(String text, String password) {
205+
static @Nullable PrivateKey parse(@Nullable String text, @Nullable String password) {
202206
if (text == null) {
203207
return null;
204208
}
@@ -223,18 +227,18 @@ private static class PemParser {
223227

224228
private final Pattern pattern;
225229

226-
private final BiFunction<byte[], String, PKCS8EncodedKeySpec> keySpecFactory;
230+
private final BiFunction<byte[], @Nullable String, PKCS8EncodedKeySpec> keySpecFactory;
227231

228232
private final String[] algorithms;
229233

230-
PemParser(String header, String footer, BiFunction<byte[], String, PKCS8EncodedKeySpec> keySpecFactory,
231-
String... algorithms) {
234+
PemParser(String header, String footer,
235+
BiFunction<byte[], @Nullable String, PKCS8EncodedKeySpec> keySpecFactory, String... algorithms) {
232236
this.pattern = Pattern.compile(header + BASE64_TEXT + footer, Pattern.CASE_INSENSITIVE);
233237
this.keySpecFactory = keySpecFactory;
234238
this.algorithms = algorithms;
235239
}
236240

237-
PrivateKey parse(String text, String password) {
241+
@Nullable PrivateKey parse(String text, @Nullable String password) {
238242
Matcher matcher = this.pattern.matcher(text);
239243
return (!matcher.find()) ? null : parse(decodeBase64(matcher.group(BASE64_TEXT_GROUP)), password);
240244
}
@@ -244,7 +248,7 @@ private static byte[] decodeBase64(String content) {
244248
return Base64.getDecoder().decode(contentBytes);
245249
}
246250

247-
private PrivateKey parse(byte[] bytes, String password) {
251+
private @Nullable PrivateKey parse(byte[] bytes, @Nullable String password) {
248252
PKCS8EncodedKeySpec keySpec = this.keySpecFactory.apply(bytes, password);
249253
if (keySpec.getAlgorithm() != null) {
250254
try {
@@ -276,7 +280,7 @@ static class DerEncoder {
276280

277281
private final ByteArrayOutputStream stream = new ByteArrayOutputStream();
278282

279-
void objectIdentifier(EncodedOid encodedOid) throws IOException {
283+
void objectIdentifier(@Nullable EncodedOid encodedOid) throws IOException {
280284
int code = (encodedOid != null) ? 0x06 : 0x05;
281285
codeLengthBytes(code, (encodedOid != null) ? encodedOid.toByteArray() : null);
282286
}
@@ -293,7 +297,7 @@ void sequence(byte[] bytes) throws IOException {
293297
codeLengthBytes(0x30, bytes);
294298
}
295299

296-
void codeLengthBytes(int code, byte[] bytes) throws IOException {
300+
void codeLengthBytes(int code, byte @Nullable [] bytes) throws IOException {
297301
this.stream.write(code);
298302
int length = (bytes != null) ? bytes.length : 0;
299303
if (length <= 127) {
@@ -316,7 +320,7 @@ void codeLengthBytes(int code, byte[] bytes) throws IOException {
316320
}
317321
}
318322

319-
private static byte[] bytes(int... elements) {
323+
private static byte @Nullable [] bytes(int @Nullable ... elements) {
320324
if (elements == null) {
321325
return null;
322326
}
@@ -405,11 +409,11 @@ ByteBuffer getContents() {
405409
return this.contents;
406410
}
407411

408-
static DerElement of(byte[] bytes) {
412+
static @Nullable DerElement of(byte[] bytes) {
409413
return of(ByteBuffer.wrap(bytes));
410414
}
411415

412-
static DerElement of(ByteBuffer bytes) {
416+
static @Nullable DerElement of(ByteBuffer bytes) {
413417
return (bytes.remaining() > 0) ? new DerElement(bytes) : null;
414418
}
415419

@@ -444,7 +448,7 @@ static class Pkcs8PrivateKeyDecryptor {
444448

445449
public static final String PBES2_ALGORITHM = "PBES2";
446450

447-
static PKCS8EncodedKeySpec decrypt(byte[] bytes, String password) {
451+
static PKCS8EncodedKeySpec decrypt(byte[] bytes, @Nullable String password) {
448452
Assert.state(password != null, "Password is required for an encrypted private key");
449453
try {
450454
EncryptedPrivateKeyInfo keyInfo = new EncryptedPrivateKeyInfo(bytes);
@@ -461,7 +465,7 @@ static PKCS8EncodedKeySpec decrypt(byte[] bytes, String password) {
461465
}
462466
}
463467

464-
private static String getEncryptionAlgorithm(AlgorithmParameters algParameters, String algName) {
468+
private static String getEncryptionAlgorithm(@Nullable AlgorithmParameters algParameters, String algName) {
465469
if (algParameters != null && PBES2_ALGORITHM.equals(algName)) {
466470
return algParameters.toString();
467471
}

0 commit comments

Comments
 (0)