001package org.apache.commons.ssl.org.bouncycastle.asn1.nist;
002
003import java.util.Enumeration;
004import java.util.Hashtable;
005
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
007import org.apache.commons.ssl.org.bouncycastle.asn1.sec.SECNamedCurves;
008import org.apache.commons.ssl.org.bouncycastle.asn1.sec.SECObjectIdentifiers;
009import org.apache.commons.ssl.org.bouncycastle.asn1.x9.X9ECParameters;
010import org.bouncycastle.util.Strings;
011
012/**
013 * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-3
014 */
015public class NISTNamedCurves
016{
017    static final Hashtable objIds = new Hashtable();
018    static final Hashtable names = new Hashtable();
019
020    static void defineCurve(String name, ASN1ObjectIdentifier oid)
021    {
022        objIds.put(name, oid);
023        names.put(oid, name);
024    }
025
026    static
027    {
028        defineCurve("B-571", SECObjectIdentifiers.sect571r1);
029        defineCurve("B-409", SECObjectIdentifiers.sect409r1);
030        defineCurve("B-283", SECObjectIdentifiers.sect283r1);
031        defineCurve("B-233", SECObjectIdentifiers.sect233r1);
032        defineCurve("B-163", SECObjectIdentifiers.sect163r2);
033        defineCurve("K-571", SECObjectIdentifiers.sect571k1);
034        defineCurve("K-409", SECObjectIdentifiers.sect409k1);
035        defineCurve("K-283", SECObjectIdentifiers.sect283k1);
036        defineCurve("K-233", SECObjectIdentifiers.sect233k1);
037        defineCurve("K-163", SECObjectIdentifiers.sect163k1);
038        defineCurve("P-521", SECObjectIdentifiers.secp521r1);
039        defineCurve("P-384", SECObjectIdentifiers.secp384r1);
040        defineCurve("P-256", SECObjectIdentifiers.secp256r1);
041        defineCurve("P-224", SECObjectIdentifiers.secp224r1);
042        defineCurve("P-192", SECObjectIdentifiers.secp192r1);
043    }
044
045    public static X9ECParameters getByName(
046        String  name)
047    {
048        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)objIds.get(Strings.toUpperCase(name));
049
050        if (oid != null)
051        {
052            return getByOID(oid);
053        }
054
055        return null;
056    }
057
058    /**
059     * return the X9ECParameters object for the named curve represented by
060     * the passed in object identifier. Null if the curve isn't present.
061     *
062     * @param oid an object identifier representing a named curve, if present.
063     */
064    public static X9ECParameters getByOID(
065        ASN1ObjectIdentifier  oid)
066    {
067        return SECNamedCurves.getByOID(oid);
068    }
069
070    /**
071     * return the object identifier signified by the passed in name. Null
072     * if there is no object identifier associated with name.
073     *
074     * @return the object identifier associated with name, if present.
075     */
076    public static ASN1ObjectIdentifier getOID(
077        String  name)
078    {
079        return (ASN1ObjectIdentifier)objIds.get(Strings.toUpperCase(name));
080    }
081
082    /**
083     * return the named curve name represented by the given object identifier.
084     */
085    public static String getName(
086        ASN1ObjectIdentifier  oid)
087    {
088        return (String)names.get(oid);
089    }
090
091    /**
092     * returns an enumeration containing the name strings for curves
093     * contained in this structure.
094     */
095    public static Enumeration getNames()
096    {
097        return objIds.keys();
098    }
099}