11package org .bouncycastle .jcajce .provider .asymmetric ;
22
3+ import java .util .Enumeration ;
34import java .util .HashMap ;
5+ import java .util .Hashtable ;
46import java .util .Map ;
7+ import java .util .Vector ;
58
9+ import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
610import org .bouncycastle .asn1 .nist .NISTObjectIdentifiers ;
711import org .bouncycastle .asn1 .sec .SECObjectIdentifiers ;
812import org .bouncycastle .asn1 .teletrust .TeleTrusTObjectIdentifiers ;
13+ import org .bouncycastle .asn1 .x9 .ECNamedCurveTable ;
914import org .bouncycastle .asn1 .x9 .X9ObjectIdentifiers ;
1015import org .bouncycastle .internal .asn1 .bsi .BSIObjectIdentifiers ;
1116import org .bouncycastle .internal .asn1 .cms .CMSObjectIdentifiers ;
1217import org .bouncycastle .internal .asn1 .eac .EACObjectIdentifiers ;
1318import org .bouncycastle .jcajce .provider .asymmetric .ec .KeyFactorySpi ;
1419import org .bouncycastle .jcajce .provider .config .ConfigurableProvider ;
1520import org .bouncycastle .jcajce .provider .util .AsymmetricAlgorithmProvider ;
21+ import org .bouncycastle .jce .spec .ECNamedCurveParameterSpec ;
1622import org .bouncycastle .util .Properties ;
1723
1824public class EC
1925{
2026 private static final String PREFIX = "org.bouncycastle.jcajce.provider.asymmetric" + ".ec." ;
2127
2228 private static final Map <String , String > generalEcAttributes = new HashMap <String , String >();
29+ private static final Map <String , String > ecSupportCurves = new HashMap <>();
2330
2431 static
2532 {
2633 generalEcAttributes .put ("SupportedKeyClasses" , "java.security.interfaces.ECPublicKey|java.security.interfaces.ECPrivateKey" );
2734 generalEcAttributes .put ("SupportedKeyFormats" , "PKCS#8|X.509" );
35+ Enumeration names = ECNamedCurveTable .getNames ();
36+ Hashtable oidToNames = new Hashtable ();
37+
38+ // 1. Group names by OID
39+ while (names .hasMoreElements ())
40+ {
41+ String name = (String )names .nextElement ();
42+ ECNamedCurveParameterSpec spec = org .bouncycastle .jce .ECNamedCurveTable .getParameterSpec (name );
43+ if (spec == null )
44+ {
45+ continue ;
46+ }
47+
48+ ASN1ObjectIdentifier oid = ECNamedCurveTable .getOID (name );
49+ if (oid == null )
50+ {
51+ continue ;
52+ }
53+
54+ String oidStr = oid .getId ();
55+ Vector v = (Vector )oidToNames .get (oidStr );
56+ if (v == null )
57+ {
58+ v = new Vector ();
59+ oidToNames .put (oidStr , v );
60+ }
61+ if (!v .contains (name ))
62+ {
63+ v .addElement (name );
64+ }
65+ }
66+
67+ Enumeration oids = oidToNames .keys ();
68+ Vector results = new Vector ();
69+
70+ while (oids .hasMoreElements ())
71+ {
72+ String oidStr = (String )oids .nextElement ();
73+ Vector namesForOid = (Vector )oidToNames .get (oidStr );
74+
75+ StringBuffer sb = new StringBuffer ();
76+ sb .append ("[" );
77+ ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier (oidStr );
78+
79+ if (X9ObjectIdentifiers .prime256v1 .equals (oid ))
80+ {
81+ sb .append ("secp256r1,NIST P-256,X9.62 prime256v1" );
82+ }
83+ else if (X9ObjectIdentifiers .prime192v1 .equals (oid ))
84+ {
85+ sb .append ("secp192r1,NIST P-192,X9.62 prime192v1" );
86+ }
87+ else
88+ {
89+ if (oid .on (X9ObjectIdentifiers .primeCurve ) || oid .on (X9ObjectIdentifiers .cTwoCurve ))
90+ {
91+ sb .append ("X9.62 " );
92+ }
93+ // Append all curve names separated by commas
94+ for (int i = 0 ; i < namesForOid .size (); i ++)
95+ {
96+ if (i > 0 )
97+ {
98+ sb .append ("," );
99+ }
100+ String name = (String )namesForOid .elementAt (i );
101+ if ((oid .on (SECObjectIdentifiers .ellipticCurve ) || X9ObjectIdentifiers .prime256v1 .equals (oid ) || X9ObjectIdentifiers .prime192v1 .equals (oid ))
102+ && (name .startsWith ("K-" ) || name .startsWith ("B-" ) || (name .startsWith ("P-" ))))
103+ {
104+ sb .append ("NIST " );
105+ }
106+ sb .append (name );
107+ }
108+ }
109+ sb .append ("," ).append (oidStr ).append ("]" );
110+ results .addElement (sb .toString ());
111+ }
112+
113+ // 3. Join all results with '|'
114+ StringBuffer output = new StringBuffer ();
115+ for (int i = 0 ; i < results .size (); i ++)
116+ {
117+ if (i > 0 )
118+ {
119+ output .append ("|" );
120+ }
121+ output .append ((String )results .elementAt (i ));
122+ }
123+
124+ ecSupportCurves .put ("SupportedCurves" , output .toString ());
28125 }
29126
30127 public static class Mappings
@@ -36,12 +133,12 @@ public Mappings()
36133
37134 public void configure (ConfigurableProvider provider )
38135 {
39- provider .addAlgorithm ("AlgorithmParameters.EC" , PREFIX + "AlgorithmParametersSpi" );
136+ provider .addAlgorithm ("AlgorithmParameters.EC" , PREFIX + "AlgorithmParametersSpi" , ecSupportCurves );
40137
41138 provider .addAlgorithm ("KeyAgreement.ECDH" , PREFIX + "KeyAgreementSpi$DH" , generalEcAttributes );
42139 provider .addAlgorithm ("KeyAgreement.ECDHC" , PREFIX + "KeyAgreementSpi$DHC" , generalEcAttributes );
43140 provider .addAlgorithm ("KeyAgreement.ECCDH" , PREFIX + "KeyAgreementSpi$DHC" , generalEcAttributes );
44-
141+
45142 provider .addAlgorithm ("KeyAgreement.ECCDHU" , PREFIX + "KeyAgreementSpi$DHUC" , generalEcAttributes );
46143
47144 provider .addAlgorithm ("KeyAgreement.ECDHWITHSHA1KDF" , PREFIX + "KeyAgreementSpi$DHwithSHA1KDFAndSharedInfo" , generalEcAttributes );
@@ -267,7 +364,7 @@ public void configure(ConfigurableProvider provider)
267364 addSignatureAlgorithm (provider , "SHA3-512" , "ECDSA" , PREFIX + "SignatureSpi$ecDSASha3_512" , NISTObjectIdentifiers .id_ecdsa_with_sha3_512 , generalEcAttributes );
268365 addSignatureAlgorithm (provider , "SHAKE128" , "ECDSA" , PREFIX + "SignatureSpi$ecDSAShake128" , CMSObjectIdentifiers .id_ecdsa_with_shake128 , generalEcAttributes );
269366 addSignatureAlgorithm (provider , "SHAKE256" , "ECDSA" , PREFIX + "SignatureSpi$ecDSAShake256" , CMSObjectIdentifiers .id_ecdsa_with_shake256 , generalEcAttributes );
270- addSignatureAlgorithm (provider , "RIPEMD160" , "ECDSA" , PREFIX + "SignatureSpi$ecDSARipeMD160" ,TeleTrusTObjectIdentifiers .ecSignWithRipemd160 , generalEcAttributes );
367+ addSignatureAlgorithm (provider , "RIPEMD160" , "ECDSA" , PREFIX + "SignatureSpi$ecDSARipeMD160" , TeleTrusTObjectIdentifiers .ecSignWithRipemd160 , generalEcAttributes );
271368
272369 provider .addAlgorithm ("Signature.SHA1WITHECNR" , PREFIX + "SignatureSpi$ecNR" , generalEcAttributes );
273370 provider .addAlgorithm ("Signature.SHA224WITHECNR" , PREFIX + "SignatureSpi$ecNR224" , generalEcAttributes );
0 commit comments