-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8023980: JCE doesn't provide any class to handle RSA private key in PKCS#1 #1787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eed143e
a4cddf4
b48afdd
6f498af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -43,13 +43,15 @@ | |
| * between the following: | ||
| * | ||
| * For public keys: | ||
| * . PublicKey with an X.509 encoding | ||
| * . RSA PublicKey with an X.509 encoding | ||
| * . RSA PublicKey with an PKCS#1 encoding | ||
| * . RSAPublicKey | ||
| * . RSAPublicKeySpec | ||
| * . X509EncodedKeySpec | ||
| * | ||
| * For private keys: | ||
| * . PrivateKey with a PKCS#8 encoding | ||
| * . RSA PrivateKey with a PKCS#8 encoding | ||
| * . RSA PrivateKey with a PKCS#1 encoding | ||
| * . RSAPrivateKey | ||
| * . RSAPrivateCrtKey | ||
| * . RSAPrivateKeySpec | ||
|
|
@@ -95,8 +97,8 @@ static RSAKeyFactory getInstance(KeyType type) { | |
| return new RSAKeyFactory(type); | ||
| } | ||
|
|
||
| // Internal utility method for checking key algorithm | ||
| private static void checkKeyAlgo(Key key, String expectedAlg) | ||
| // pkg-private utility method for checking key algorithm | ||
| static void checkKeyAlgo(Key key, String expectedAlg) | ||
| throws InvalidKeyException { | ||
| String keyAlg = key.getAlgorithm(); | ||
| if (keyAlg == null || !(keyAlg.equalsIgnoreCase(expectedAlg))) { | ||
|
|
@@ -265,14 +267,10 @@ private PublicKey translatePublicKey(PublicKey key) | |
| // catch providers that incorrectly implement RSAPublicKey | ||
| throw new InvalidKeyException("Invalid key", e); | ||
| } | ||
| } else if ("X.509".equals(key.getFormat())) { | ||
| RSAPublicKey translated = new RSAPublicKeyImpl(key.getEncoded()); | ||
| // ensure the key algorithm matches the current KeyFactory instance | ||
| checkKeyAlgo(translated, type.keyAlgo); | ||
| return translated; | ||
| } else { | ||
| throw new InvalidKeyException("Public keys must be instance " | ||
| + "of RSAPublicKey or have X.509 encoding"); | ||
| // create new key based on the format and encoding of current 'key' | ||
| return RSAPublicKeyImpl.newKey(type, key.getFormat(), | ||
| key.getEncoded()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -309,27 +307,18 @@ private PrivateKey translatePrivateKey(PrivateKey key) | |
| // catch providers that incorrectly implement RSAPrivateKey | ||
| throw new InvalidKeyException("Invalid key", e); | ||
| } | ||
| } else if ("PKCS#8".equals(key.getFormat())) { | ||
| RSAPrivateKey translated = | ||
| RSAPrivateCrtKeyImpl.newKey(key.getEncoded()); | ||
| // ensure the key algorithm matches the current KeyFactory instance | ||
| checkKeyAlgo(translated, type.keyAlgo); | ||
| return translated; | ||
| } else { | ||
| throw new InvalidKeyException("Private keys must be instance " | ||
| + "of RSAPrivate(Crt)Key or have PKCS#8 encoding"); | ||
| return RSAPrivateCrtKeyImpl.newKey(type, key.getFormat(), | ||
| key.getEncoded()); | ||
| } | ||
| } | ||
|
|
||
| // internal implementation of generatePublic. See JCA doc | ||
| private PublicKey generatePublic(KeySpec keySpec) | ||
| throws GeneralSecurityException { | ||
| if (keySpec instanceof X509EncodedKeySpec) { | ||
| X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec; | ||
| RSAPublicKey generated = new RSAPublicKeyImpl(x509Spec.getEncoded()); | ||
| // ensure the key algorithm matches the current KeyFactory instance | ||
| checkKeyAlgo(generated, type.keyAlgo); | ||
| return generated; | ||
| return RSAPublicKeyImpl.newKey(type, "X.509", | ||
| ((X509EncodedKeySpec)keySpec).getEncoded()); | ||
| } else if (keySpec instanceof RSAPublicKeySpec) { | ||
| RSAPublicKeySpec rsaSpec = (RSAPublicKeySpec)keySpec; | ||
| try { | ||
|
|
@@ -351,11 +340,8 @@ private PublicKey generatePublic(KeySpec keySpec) | |
| private PrivateKey generatePrivate(KeySpec keySpec) | ||
| throws GeneralSecurityException { | ||
| if (keySpec instanceof PKCS8EncodedKeySpec) { | ||
| PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec; | ||
| RSAPrivateKey generated = RSAPrivateCrtKeyImpl.newKey(pkcsSpec.getEncoded()); | ||
| // ensure the key algorithm matches the current KeyFactory instance | ||
| checkKeyAlgo(generated, type.keyAlgo); | ||
| return generated; | ||
| return RSAPrivateCrtKeyImpl.newKey(type, "PKCS#8", | ||
| ((PKCS8EncodedKeySpec)keySpec).getEncoded()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you clean up the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better that you do it this time? Just so that the backport won't miss it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or if you integrated before me, I will manually merge the changes and clean up the getEncoded() also. |
||
| } else if (keySpec instanceof RSAPrivateCrtKeySpec) { | ||
| RSAPrivateCrtKeySpec rsaSpec = (RSAPrivateCrtKeySpec)keySpec; | ||
| try { | ||
|
|
@@ -395,7 +381,8 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) | |
| try { | ||
| // convert key to one of our keys | ||
| // this also verifies that the key is a valid RSA key and ensures | ||
| // that the encoding is X.509/PKCS#8 for public/private keys | ||
| // that the encoding is X.509/PKCS#8 or PKCS#1 for public/private | ||
| // keys | ||
| key = engineTranslateKey(key); | ||
| } catch (InvalidKeyException e) { | ||
| throw new InvalidKeySpecException(e); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.