|
5 | 5 | import java.security.AlgorithmParameters;
|
6 | 6 | import java.security.GeneralSecurityException;
|
7 | 7 | import java.security.KeyFactory;
|
8 |
| -import java.security.NoSuchAlgorithmException; |
9 |
| -import java.security.NoSuchProviderException; |
10 | 8 | import java.security.PrivateKey;
|
11 | 9 | import java.security.Provider;
|
12 | 10 | import java.security.PublicKey;
|
|
18 | 16 | import java.security.interfaces.RSAPublicKey;
|
19 | 17 | import java.security.spec.DSAPrivateKeySpec;
|
20 | 18 | import java.security.spec.DSAPublicKeySpec;
|
21 |
| -import java.security.spec.ECGenParameterSpec; |
22 | 19 | import java.security.spec.ECParameterSpec;
|
23 | 20 | import java.security.spec.ECPrivateKeySpec;
|
24 | 21 | import java.security.spec.ECPublicKeySpec;
|
25 |
| -import java.security.spec.InvalidParameterSpecException; |
26 | 22 | import java.security.spec.KeySpec;
|
27 | 23 | import java.security.spec.PKCS8EncodedKeySpec;
|
28 | 24 | import java.security.spec.RSAPrivateCrtKeySpec;
|
|
48 | 44 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
49 | 45 | import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
50 | 46 | import org.bouncycastle.asn1.x9.ECNamedCurveTable;
|
| 47 | +import org.bouncycastle.asn1.x9.X962Parameters; |
51 | 48 | import org.bouncycastle.asn1.x9.X9ECParameters;
|
52 | 49 | import org.bouncycastle.asn1.x9.X9ECParametersHolder;
|
53 | 50 | import org.bouncycastle.asn1.x9.X9ECPoint;
|
|
75 | 72 | import org.bouncycastle.bcpg.X25519SecretBCPGKey;
|
76 | 73 | import org.bouncycastle.bcpg.X448PublicBCPGKey;
|
77 | 74 | import org.bouncycastle.bcpg.X448SecretBCPGKey;
|
78 |
| -import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; |
| 75 | +import org.bouncycastle.crypto.CryptoServicesRegistrar; |
79 | 76 | import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
|
80 | 77 | import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
|
81 | 78 | import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
|
82 | 79 | import org.bouncycastle.jce.interfaces.ElGamalPublicKey;
|
| 80 | +import org.bouncycastle.math.ec.ECCurve; |
83 | 81 | import org.bouncycastle.math.ec.ECPoint;
|
84 |
| - |
85 | 82 | import org.bouncycastle.math.ec.rfc7748.X25519;
|
86 | 83 | import org.bouncycastle.math.ec.rfc7748.X448;
|
87 | 84 | import org.bouncycastle.math.ec.rfc8032.Ed25519;
|
@@ -236,7 +233,7 @@ else if (EdECObjectIdentifiers.id_X448.equals(ecdhPub.getCurveOID()))
|
236 | 233 | }
|
237 | 234 | case PublicKeyAlgorithmTags.ECDSA:
|
238 | 235 | {
|
239 |
| - return implGetPrivateKeyEC("ECDSA", (ECDSAPublicBCPGKey)pubPk.getKey(), (ECSecretBCPGKey)privPk); |
| 236 | + return implGetPrivateKeyEC("EC", (ECDSAPublicBCPGKey)pubPk.getKey(), (ECSecretBCPGKey)privPk); |
240 | 237 | }
|
241 | 238 | // Legacy EdDSA (legacy Ed448, legacy Ed25519)
|
242 | 239 | case PublicKeyAlgorithmTags.EDDSA_LEGACY:
|
@@ -349,7 +346,7 @@ else if (ecdhK.getCurveOID().equals(EdECObjectIdentifiers.id_X448))
|
349 | 346 | }
|
350 | 347 | case PublicKeyAlgorithmTags.ECDSA:
|
351 | 348 | {
|
352 |
| - return implGetPublicKeyEC("ECDSA", (ECDSAPublicBCPGKey) publicPk.getKey()); |
| 349 | + return implGetPublicKeyEC("EC", (ECDSAPublicBCPGKey) publicPk.getKey()); |
353 | 350 | }
|
354 | 351 | // Legacy EdDSA (legacy Ed448, legacy Ed25519)
|
355 | 352 | case PublicKeyAlgorithmTags.EDDSA_LEGACY:
|
@@ -420,11 +417,11 @@ else if (ecdhK.getCurveOID().equals(EdECObjectIdentifiers.id_X448))
|
420 | 417 | }
|
421 | 418 |
|
422 | 419 | private ECParameterSpec getECParameterSpec(ASN1ObjectIdentifier curveOid, X9ECParameters x9Params)
|
423 |
| - throws InvalidParameterSpecException, NoSuchProviderException, NoSuchAlgorithmException |
| 420 | + throws IOException, GeneralSecurityException |
424 | 421 | {
|
425 | 422 | AlgorithmParameters params = helper.createAlgorithmParameters("EC");
|
426 | 423 |
|
427 |
| - params.init(new ECGenParameterSpec(ECNamedCurveTable.getName(curveOid))); |
| 424 | + params.init(new X962Parameters(curveOid).getEncoded()); |
428 | 425 |
|
429 | 426 | return params.getParameterSpec(ECParameterSpec.class);
|
430 | 427 | }
|
@@ -560,7 +557,7 @@ private BCPGKey getPublicBCPGKey(int algorithm, PGPAlgorithmParameters algorithm
|
560 | 557 | // BCECPublicKey uses explicit parameter encoding, so we need to find the named curve manually
|
561 | 558 | if (X9ObjectIdentifiers.id_ecPublicKey.equals(curveOid))
|
562 | 559 | {
|
563 |
| - enc = getNamedCurveOID((BCECPublicKey) pubKey); |
| 560 | + enc = getNamedCurveOID(X962Parameters.getInstance(keyInfo.getAlgorithm().getParameters())); |
564 | 561 | ASN1ObjectIdentifier nCurveOid = ASN1ObjectIdentifier.getInstance(enc);
|
565 | 562 | if (nCurveOid != null)
|
566 | 563 | {
|
@@ -685,15 +682,29 @@ else if (pubKey.getAlgorithm().regionMatches(true, 0, "XDH", 0, 3))
|
685 | 682 | }
|
686 | 683 | }
|
687 | 684 |
|
688 |
| - private ASN1Encodable getNamedCurveOID(BCECPublicKey pubKey) |
| 685 | + private ASN1Encodable getNamedCurveOID(X962Parameters ecParams) |
689 | 686 | {
|
| 687 | + ECCurve curve = null; |
| 688 | + if (ecParams.isNamedCurve()) |
| 689 | + { |
| 690 | + return ASN1ObjectIdentifier.getInstance(ecParams.getParameters()); |
| 691 | + } |
| 692 | + else if (ecParams.isImplicitlyCA()) |
| 693 | + { |
| 694 | + curve = ((X9ECParameters)CryptoServicesRegistrar.getProperty(CryptoServicesRegistrar.Property.EC_IMPLICITLY_CA)).getCurve(); |
| 695 | + } |
| 696 | + else |
| 697 | + { |
| 698 | + curve = X9ECParameters.getInstance(ecParams.getParameters()).getCurve(); |
| 699 | + } |
| 700 | + |
690 | 701 | // Iterate through all registered curves to find applicable OID
|
691 | 702 | Enumeration names = ECNamedCurveTable.getNames();
|
692 | 703 | while (names.hasMoreElements())
|
693 | 704 | {
|
694 |
| - String name = (String) names.nextElement(); |
| 705 | + String name = (String)names.nextElement(); |
695 | 706 | X9ECParameters parms = ECNamedCurveTable.getByName(name);
|
696 |
| - if (pubKey.getParameters().getCurve().equals(parms.getCurve())) |
| 707 | + if (curve.equals(parms.getCurve())) |
697 | 708 | {
|
698 | 709 | return ECNamedCurveTable.getOID(name);
|
699 | 710 | }
|
@@ -763,7 +774,7 @@ private PublicKey implGetPublicKeyX509(byte[] pEnc, int pEncOff, ASN1ObjectIdent
|
763 | 774 | }
|
764 | 775 |
|
765 | 776 | private PrivateKey implGetPrivateKeyEC(String keyAlgorithm, ECPublicBCPGKey ecPub, ECSecretBCPGKey ecPriv)
|
766 |
| - throws GeneralSecurityException, PGPException |
| 777 | + throws GeneralSecurityException, PGPException, IOException |
767 | 778 | {
|
768 | 779 | ASN1ObjectIdentifier curveOid = ecPub.getCurveOID();
|
769 | 780 | ECPrivateKeySpec ecPrivSpec = new ECPrivateKeySpec(ecPriv.getX(), getECParameterSpec(curveOid, JcaJcePGPUtil.getX9Parameters(curveOid)));
|
|
0 commit comments