|
| 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.ECPrivateKey; |
| 26 | +import java.security.interfaces.ECPublicKey; |
| 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 | + * Key pair loader for loading key pairs for ECDSA-based algorithms. |
| 34 | + * <p> |
| 35 | + * |
| 36 | + * <b>Example usage for ECDSA:</b> |
| 37 | + * <pre>{@code |
| 38 | + * KeyLoader keyLoader = new EcKeyLoader(); |
| 39 | + * String pemPrivateKey = """ |
| 40 | + * -----BEGIN EC PRIVATE KEY----- |
| 41 | + * ... |
| 42 | + * -----END EC PRIVATE KEY-----"""; |
| 43 | + * ECPrivateKey privateKey = KeyLoader.loadEcdsaPrivateKey(pemPrivateKey); |
| 44 | + * |
| 45 | + * String pemPublicKey = """ |
| 46 | + * -----BEGIN EC PUBLIC KEY----- |
| 47 | + * ... |
| 48 | + * -----END EC PUBLIC KEY-----"""; |
| 49 | + * ECPublicKey publicKey = KeyLoader.loadPublicKey(pemPublicKey); |
| 50 | + * }</pre> |
| 51 | + */ |
| 52 | +public class EcKeyLoader implements KeyLoader { |
| 53 | + |
| 54 | + private final KeyFactory keyFactory; |
| 55 | + |
| 56 | + /** |
| 57 | + * Initialise a key loader for EC-based algorithms. |
| 58 | + * |
| 59 | + * @throws NoSuchAlgorithmException if no {@code Provider} supports a {@code KeyFactorySpi} |
| 60 | + * implementation for the specified algorithm |
| 61 | + */ |
| 62 | + public EcKeyLoader() throws NoSuchAlgorithmException { |
| 63 | + this.keyFactory = KeyFactory.getInstance("EC"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Load ECDSA private key from pem-formatted key text. |
| 68 | + * |
| 69 | + * @param pemKeyText pem-formatted key text |
| 70 | + * @return loaded private key |
| 71 | + * @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance, |
| 72 | + * or EC Key Factory is not loaded, or key spec is invalid |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public ECPrivateKey loadPrivateKey(String pemKeyText) { |
| 76 | + try { |
| 77 | + // remove all unnecessary parts of the pem key text |
| 78 | + pemKeyText = pemKeyText |
| 79 | + .replaceAll("-----BEGIN EC PRIVATE KEY-----", "") |
| 80 | + .replaceAll("-----END EC PRIVATE KEY-----", "") |
| 81 | + .replaceAll("\n", ""); |
| 82 | + var decodedKeyString = Base64.getDecoder().decode(pemKeyText); |
| 83 | + var keySpec = new PKCS8EncodedKeySpec(decodedKeyString); |
| 84 | + |
| 85 | + var _key = keyFactory.generatePrivate(keySpec); |
| 86 | + if (_key instanceof ECPrivateKey privateKey) { |
| 87 | + return privateKey; |
| 88 | + } else { |
| 89 | + throw new KeyLoadingException("Unable to load private key from pem-formatted key text."); |
| 90 | + } |
| 91 | + } catch (InvalidKeySpecException e) { |
| 92 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Load public key from pem-formatted key text. |
| 98 | + * |
| 99 | + * @param pemKeyText pem-formatted key text |
| 100 | + * @return loaded private key |
| 101 | + * @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance, |
| 102 | + * or EC Key Factory is not loaded, or key spec is invalid |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public ECPublicKey loadPublicKey(String pemKeyText) { |
| 106 | + try { |
| 107 | + // remove all unnecessary parts of the pem key text |
| 108 | + pemKeyText = pemKeyText |
| 109 | + .replaceAll("-----BEGIN EC PUBLIC KEY-----", "") |
| 110 | + .replaceAll("-----END EC PUBLIC KEY-----", "") |
| 111 | + .replaceAll("\n", ""); |
| 112 | + var keyBytes = Base64.getDecoder().decode(pemKeyText); |
| 113 | + var spec = new X509EncodedKeySpec(keyBytes); |
| 114 | + var key = keyFactory.generatePublic(spec); |
| 115 | + if (key instanceof ECPublicKey publicKey) { |
| 116 | + return publicKey; |
| 117 | + } else { |
| 118 | + throw new KeyLoadingException("Unable to load private key from pem-formatted key text."); |
| 119 | + } |
| 120 | + } catch (InvalidKeySpecException e) { |
| 121 | + throw new KeyLoadingException("Key spec is invalid.", e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments