|
| 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 | +} |
0 commit comments