|
| 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 | +public class RsaKeyLoader implements KeyLoader { |
| 33 | + |
| 34 | + private final Base64.Decoder decoder; |
| 35 | + private final KeyFactory keyFactory; |
| 36 | + |
| 37 | + public RsaKeyLoader() { |
| 38 | + try { |
| 39 | + this.decoder = Base64.getDecoder(); |
| 40 | + this.keyFactory = KeyFactory.getInstance("RSA"); |
| 41 | + } catch (NoSuchAlgorithmException e) { |
| 42 | + throw new KeyLoadingException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public RSAPrivateKey loadPrivateKey(String pemKeyText) { |
| 48 | + // Extract the raw key content |
| 49 | + var rawKeyContent = getRawContent(pemKeyText); |
| 50 | + |
| 51 | + // Decode the Base64-encoded content |
| 52 | + var keyBytes = decoder.decode(rawKeyContent); |
| 53 | + |
| 54 | + // Create a PKCS8EncodedKeySpec from the decoded bytes |
| 55 | + var keySpec = new PKCS8EncodedKeySpec(keyBytes); |
| 56 | + |
| 57 | + try { |
| 58 | + // Get an RSA KeyFactory and generate the private key |
| 59 | + var _key = keyFactory.generatePrivate(keySpec); |
| 60 | + if (_key instanceof RSAPrivateKey key) { |
| 61 | + return key; |
| 62 | + } else { |
| 63 | + throw new KeyLoadingException("Unable to load private key from pem-formatted key text."); |
| 64 | + } |
| 65 | + } catch (InvalidKeySpecException e) { |
| 66 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public RSAPublicKey loadPublicKey(String pemKeyText) { |
| 72 | + // Extract the raw key content |
| 73 | + var rawKeyContent = getRawContent(pemKeyText); |
| 74 | + |
| 75 | + // Decode the Base64-encoded content |
| 76 | + var keyBytes = decoder.decode(rawKeyContent); |
| 77 | + |
| 78 | + // Create an X509EncodedKeySpec from the decoded bytes |
| 79 | + var keySpec = new X509EncodedKeySpec(keyBytes); |
| 80 | + |
| 81 | + // Get an RSA KeyFactory and generate the public key |
| 82 | + try { |
| 83 | + var _key = keyFactory.generatePublic(keySpec); |
| 84 | + if (_key instanceof RSAPublicKey key) { |
| 85 | + return key; |
| 86 | + } else { |
| 87 | + throw new KeyLoadingException("Unable to load public key from pem-formatted key text."); |
| 88 | + } |
| 89 | + } catch (InvalidKeySpecException e) { |
| 90 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments