Skip to content

Commit 6f7b9bb

Browse files
Migrate spring-security-rsa into spring-security-crypto
Closes gh-14202
1 parent 45f8ab3 commit 6f7b9bb

17 files changed

+1320
-1
lines changed

crypto/spring-security-crypto.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ apply plugin: 'io.spring.convention.spring-module'
33
dependencies {
44
management platform(project(":spring-security-dependencies"))
55
optional 'org.springframework:spring-jcl'
6+
optional 'org.springframework:spring-core'
67
optional 'org.bouncycastle:bcpkix-jdk15on'
7-
8+
89
testImplementation "org.assertj:assertj-core"
910
testImplementation "org.junit.jupiter:junit-jupiter-api"
1011
testImplementation "org.junit.jupiter:junit-jupiter-params"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2013-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.crypto.encrypt;
18+
19+
import java.io.InputStream;
20+
import java.security.KeyFactory;
21+
import java.security.KeyPair;
22+
import java.security.KeyStore;
23+
import java.security.PublicKey;
24+
import java.security.cert.Certificate;
25+
import java.security.interfaces.RSAPrivateCrtKey;
26+
import java.security.spec.RSAPublicKeySpec;
27+
28+
import org.springframework.core.io.Resource;
29+
import org.springframework.util.StringUtils;
30+
31+
/**
32+
* @author Dave Syer
33+
* @author Tim Ysewyn
34+
* @since 6.3
35+
*/
36+
public class KeyStoreKeyFactory {
37+
38+
private final Resource resource;
39+
40+
private final char[] password;
41+
42+
private KeyStore store;
43+
44+
private final Object lock = new Object();
45+
46+
private final String type;
47+
48+
public KeyStoreKeyFactory(Resource resource, char[] password) {
49+
this(resource, password, type(resource));
50+
}
51+
52+
private static String type(Resource resource) {
53+
String ext = StringUtils.getFilenameExtension(resource.getFilename());
54+
return (ext != null) ? ext : "jks";
55+
}
56+
57+
public KeyStoreKeyFactory(Resource resource, char[] password, String type) {
58+
this.resource = resource;
59+
this.password = password;
60+
this.type = type;
61+
}
62+
63+
public KeyPair getKeyPair(String alias) {
64+
return getKeyPair(alias, this.password);
65+
}
66+
67+
public KeyPair getKeyPair(String alias, char[] password) {
68+
try {
69+
synchronized (this.lock) {
70+
if (this.store == null) {
71+
synchronized (this.lock) {
72+
this.store = KeyStore.getInstance(this.type);
73+
try (InputStream stream = this.resource.getInputStream()) {
74+
this.store.load(stream, this.password);
75+
}
76+
}
77+
}
78+
}
79+
RSAPrivateCrtKey key = (RSAPrivateCrtKey) this.store.getKey(alias, password);
80+
Certificate certificate = this.store.getCertificate(alias);
81+
PublicKey publicKey = null;
82+
if (certificate != null) {
83+
publicKey = certificate.getPublicKey();
84+
}
85+
else if (key != null) {
86+
RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
87+
publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
88+
}
89+
return new KeyPair(publicKey, key);
90+
}
91+
catch (Exception ex) {
92+
throw new IllegalStateException("Cannot load keys from store: " + this.resource, ex);
93+
}
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2013-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.crypto.encrypt;
18+
19+
/**
20+
* @author Dave Syer
21+
* @since 6.3
22+
*/
23+
public enum RsaAlgorithm {
24+
25+
DEFAULT("RSA", 117), OAEP("RSA/ECB/OAEPPadding", 86);
26+
27+
private final String name;
28+
29+
private final int maxLength;
30+
31+
RsaAlgorithm(String name, int maxLength) {
32+
this.name = name;
33+
this.maxLength = maxLength;
34+
}
35+
36+
public String getJceName() {
37+
return this.name;
38+
}
39+
40+
public int getMaxLength() {
41+
return this.maxLength;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)