|
81 | 81 | * {@link PEMRecord}. |
82 | 82 | * |
83 | 83 | * <p> The {@linkplain #decode(String, Class)} and |
84 | | - * {@linkplain #decode(InputStream, Class)} methods take a Class parameter |
| 84 | + * {@linkplain #decode(InputStream, Class)} methods take a class parameter |
85 | 85 | * which determines the type of {@code DEREncodable} that is returned. These |
86 | 86 | * methods are useful when extracting or changing the return class. |
87 | 87 | * For example, if the PEM contains both public and private keys, the |
88 | | - * Class parameter can specify which to return. Use |
| 88 | + * class parameter can specify which to return. Use |
89 | 89 | * {@code PrivateKey.class} to return only the private key. |
90 | | - * If the Class parameter is set to {@code X509EncodedKeySpec.class}, the |
| 90 | + * If the class parameter is set to {@code X509EncodedKeySpec.class}, the |
91 | 91 | * public key will be returned in that format. Any type of PEM data can be |
92 | 92 | * decoded into a {@code PEMRecord} by specifying {@code PEMRecord.class}. |
93 | | - * If the Class parameter doesn't match the PEM content, an |
94 | | - * {@code IllegalArgumentException} will be thrown. |
| 93 | + * If the class parameter doesn't match the PEM content, a |
| 94 | + * {@linkplain ClassCastException} will be thrown. |
95 | 95 | * |
96 | 96 | * <p> A new {@code PEMDecoder} instance is created when configured |
97 | 97 | * with {@linkplain #withFactory(Provider)} and/or |
98 | 98 | * {@linkplain #withDecryption(char[])}. {@linkplain #withFactory(Provider)} |
99 | 99 | * configures the decoder to use only {@linkplain KeyFactory} and |
100 | 100 | * {@linkplain CertificateFactory} instances from the given {@code Provider}. |
101 | | - * {@link#withDecryption(char[])} configures the decoder to decrypt all |
| 101 | + * {@linkplain #withDecryption(char[])} configures the decoder to decrypt all |
102 | 102 | * encrypted private key PEM data using the given password. |
103 | 103 | * Configuring an instance for decryption does not prevent decoding with |
104 | 104 | * unencrypted PEM. Any encrypted PEM that fails decryption |
|
117 | 117 | * <p> Here is an example of a {@code PEMDecoder} configured with decryption |
118 | 118 | * and a factory provider: |
119 | 119 | * {@snippet lang = java: |
120 | | - * PEMDecoder pe = PEMDecoder.of().withDecryption(password). |
| 120 | + * PEMDecoder pd = PEMDecoder.of().withDecryption(password). |
121 | 121 | * withFactory(provider); |
122 | | - * byte[] pemData = pe.decode(privKey); |
| 122 | + * byte[] pemData = pd.decode(privKey); |
123 | 123 | * } |
124 | 124 | * |
125 | 125 | * @implNote An implementation may support other PEM types and |
126 | | - * {@code DEREncodables}. This implementation additionally supports PEM types: |
127 | | - * {@code X509 CERTIFICATE}, {@code X.509 CERTIFICATE}, {@code CRL}, |
128 | | - * and {@code RSA PRIVATE KEY}. |
| 126 | + * {@code DEREncodable} objects. This implementation additionally supports |
| 127 | + * the following PEM types: {@code X509 CERTIFICATE}, |
| 128 | + * {@code X.509 CERTIFICATE}, {@code CRL}, and {@code RSA PRIVATE KEY}. |
129 | 129 | * |
130 | 130 | * @see PEMEncoder |
131 | 131 | * @see PEMRecord |
@@ -179,13 +179,13 @@ private DEREncodable decode(PEMRecord pem) { |
179 | 179 | return switch (pem.type()) { |
180 | 180 | case Pem.PUBLIC_KEY -> { |
181 | 181 | X509EncodedKeySpec spec = |
182 | | - new X509EncodedKeySpec(decoder.decode(pem.pem())); |
| 182 | + new X509EncodedKeySpec(decoder.decode(pem.content())); |
183 | 183 | yield getKeyFactory( |
184 | 184 | KeyUtil.getAlgorithm(spec.getEncoded())). |
185 | 185 | generatePublic(spec); |
186 | 186 | } |
187 | 187 | case Pem.PRIVATE_KEY -> { |
188 | | - PKCS8Key p8key = new PKCS8Key(decoder.decode(pem.pem())); |
| 188 | + PKCS8Key p8key = new PKCS8Key(decoder.decode(pem.content())); |
189 | 189 | String algo = p8key.getAlgorithm(); |
190 | 190 | KeyFactory kf = getKeyFactory(algo); |
191 | 191 | DEREncodable d = kf.generatePrivate( |
@@ -216,27 +216,27 @@ yield new KeyPair(getKeyFactory(algo). |
216 | 216 | case Pem.ENCRYPTED_PRIVATE_KEY -> { |
217 | 217 | if (password == null) { |
218 | 218 | yield new EncryptedPrivateKeyInfo(decoder.decode( |
219 | | - pem.pem())); |
| 219 | + pem.content())); |
220 | 220 | } |
221 | | - yield new EncryptedPrivateKeyInfo(decoder.decode(pem.pem())). |
| 221 | + yield new EncryptedPrivateKeyInfo(decoder.decode(pem.content())). |
222 | 222 | getKey(password.getPassword()); |
223 | 223 | } |
224 | 224 | case Pem.CERTIFICATE, Pem.X509_CERTIFICATE, |
225 | 225 | Pem.X_509_CERTIFICATE -> { |
226 | 226 | CertificateFactory cf = getCertFactory("X509"); |
227 | 227 | yield (X509Certificate) cf.generateCertificate( |
228 | | - new ByteArrayInputStream(decoder.decode(pem.pem()))); |
| 228 | + new ByteArrayInputStream(decoder.decode(pem.content()))); |
229 | 229 | } |
230 | 230 | case Pem.X509_CRL, Pem.CRL -> { |
231 | 231 | CertificateFactory cf = getCertFactory("X509"); |
232 | 232 | yield (X509CRL) cf.generateCRL( |
233 | | - new ByteArrayInputStream(decoder.decode(pem.pem()))); |
| 233 | + new ByteArrayInputStream(decoder.decode(pem.content()))); |
234 | 234 | } |
235 | 235 | case Pem.RSA_PRIVATE_KEY -> { |
236 | 236 | KeyFactory kf = getKeyFactory("RSA"); |
237 | 237 | yield kf.generatePrivate( |
238 | 238 | RSAPrivateCrtKeyImpl.getKeySpec(decoder.decode( |
239 | | - pem.pem()))); |
| 239 | + pem.content()))); |
240 | 240 | } |
241 | 241 | default -> pem; |
242 | 242 | }; |
@@ -271,7 +271,6 @@ yield new EncryptedPrivateKeyInfo(decoder.decode(pem.pem())). |
271 | 271 | */ |
272 | 272 | public DEREncodable decode(String str) { |
273 | 273 | Objects.requireNonNull(str); |
274 | | - DEREncodable de; |
275 | 274 | try { |
276 | 275 | return decode(new ByteArrayInputStream( |
277 | 276 | str.getBytes(StandardCharsets.UTF_8))); |
@@ -483,9 +482,6 @@ private CertificateFactory getCertFactory(String algorithm) { |
483 | 482 | * from the specified {@link Provider} to produce cryptographic objects. |
484 | 483 | * Any errors using the {@code Provider} will occur during decoding. |
485 | 484 | * |
486 | | - * <p>If {@code provider} is {@code null}, a new instance is returned with |
487 | | - * the default provider configuration. |
488 | | - * |
489 | 485 | * @param provider the factory provider |
490 | 486 | * @return a new PEMEncoder instance configured to the {@code Provider}. |
491 | 487 | * @throws NullPointerException if {@code provider} is null |
|
0 commit comments