|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 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 | + * http://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 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.security.impl; |
| 19 | + |
| 20 | +import com.onixbyte.security.KeyLoader; |
| 21 | +import com.onixbyte.security.exception.KeyLoadingException; |
| 22 | + |
| 23 | +import java.security.KeyFactory; |
| 24 | +import java.security.NoSuchAlgorithmException; |
| 25 | +import java.security.interfaces.RSAPrivateKey; |
| 26 | +import java.security.interfaces.RSAPublicKey; |
| 27 | +import java.security.spec.InvalidKeySpecException; |
| 28 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 29 | +import java.security.spec.X509EncodedKeySpec; |
| 30 | +import java.util.Base64; |
| 31 | + |
| 32 | +/** |
| 33 | + * A class responsible for loading RSA keys from PEM formatted text. |
| 34 | + * <p> |
| 35 | + * This class implements the {@link KeyLoader} interface and provides methods to load both private |
| 36 | + * and public RSA keys. The keys are expected to be in the standard PEM format, which includes |
| 37 | + * Base64-encoded key content surrounded by header and footer lines. The class handles the decoding |
| 38 | + * of Base64 content and the generation of keys using the RSA key factory. |
| 39 | + * <p> |
| 40 | + * Any exceptions encountered during the loading process are encapsulated in a |
| 41 | + * {@link KeyLoadingException}, allowing for flexible error handling. |
| 42 | + * |
| 43 | + * @author siujamo |
| 44 | + * @see KeyLoader |
| 45 | + * @see KeyLoadingException |
| 46 | + */ |
| 47 | +public class RsaKeyLoader implements KeyLoader { |
| 48 | + |
| 49 | + private final Base64.Decoder decoder; |
| 50 | + private final KeyFactory keyFactory; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructs an instance of {@code RsaKeyLoader}. |
| 54 | + * <p> |
| 55 | + * This constructor initialises the Base64 decoder and the RSA {@link KeyFactory}. It may throw |
| 56 | + * a {@link KeyLoadingException} if the RSA algorithm is not available. |
| 57 | + */ |
| 58 | + public RsaKeyLoader() { |
| 59 | + try { |
| 60 | + this.decoder = Base64.getDecoder(); |
| 61 | + this.keyFactory = KeyFactory.getInstance("RSA"); |
| 62 | + } catch (NoSuchAlgorithmException e) { |
| 63 | + throw new KeyLoadingException(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Loads an RSA private key from a given PEM formatted key text. |
| 69 | + * <p> |
| 70 | + * This method extracts the raw key content from the provided PEM text, decodes the |
| 71 | + * Base64-encoded content, and generates an instance of {@link RSAPrivateKey}. If the key cannot |
| 72 | + * be loaded due to invalid specifications or types, a {@link KeyLoadingException} is thrown. |
| 73 | + * |
| 74 | + * @param pemKeyText the PEM formatted private key text |
| 75 | + * @return an instance of {@link RSAPrivateKey} |
| 76 | + * @throws KeyLoadingException if the key loading process encounters an error |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public RSAPrivateKey loadPrivateKey(String pemKeyText) { |
| 80 | + // Extract the raw key content |
| 81 | + var rawKeyContent = getRawContent(pemKeyText); |
| 82 | + |
| 83 | + // Decode the Base64-encoded content |
| 84 | + var keyBytes = decoder.decode(rawKeyContent); |
| 85 | + |
| 86 | + // Create a PKCS8EncodedKeySpec from the decoded bytes |
| 87 | + var keySpec = new PKCS8EncodedKeySpec(keyBytes); |
| 88 | + |
| 89 | + try { |
| 90 | + // Get an RSA KeyFactory and generate the private key |
| 91 | + var _key = keyFactory.generatePrivate(keySpec); |
| 92 | + if (_key instanceof RSAPrivateKey key) { |
| 93 | + return key; |
| 94 | + } else { |
| 95 | + throw new KeyLoadingException("Unable to load private key from pem-formatted key text."); |
| 96 | + } |
| 97 | + } catch (InvalidKeySpecException e) { |
| 98 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Loads an RSA public key from a given PEM formatted key text. |
| 104 | + * <p> |
| 105 | + * This method extracts the raw key content from the provided PEM text, decodes the |
| 106 | + * Base64-encoded content, and generates an instance of {@link RSAPublicKey}. If the key cannot |
| 107 | + * be loaded due to invalid specifications or types, a {@link KeyLoadingException} is thrown. |
| 108 | + * |
| 109 | + * @param pemKeyText the PEM formatted public key text |
| 110 | + * @return an instance of {@link RSAPublicKey} |
| 111 | + * @throws KeyLoadingException if the key loading process encounters an error |
| 112 | + */ |
| 113 | + @Override |
| 114 | + public RSAPublicKey loadPublicKey(String pemKeyText) { |
| 115 | + // Extract the raw key content |
| 116 | + var rawKeyContent = getRawContent(pemKeyText); |
| 117 | + |
| 118 | + // Decode the Base64-encoded content |
| 119 | + var keyBytes = decoder.decode(rawKeyContent); |
| 120 | + |
| 121 | + // Create an X509EncodedKeySpec from the decoded bytes |
| 122 | + var keySpec = new X509EncodedKeySpec(keyBytes); |
| 123 | + |
| 124 | + // Get an RSA KeyFactory and generate the public key |
| 125 | + try { |
| 126 | + var _key = keyFactory.generatePublic(keySpec); |
| 127 | + if (_key instanceof RSAPublicKey key) { |
| 128 | + return key; |
| 129 | + } else { |
| 130 | + throw new KeyLoadingException("Unable to load public key from pem-formatted key text."); |
| 131 | + } |
| 132 | + } catch (InvalidKeySpecException e) { |
| 133 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments