Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
41161 views
/*1* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ec;2627import java.security.AccessController;28import java.security.InvalidParameterException;29import java.security.NoSuchAlgorithmException;30import java.security.PrivilegedAction;31import java.security.Provider;32import java.security.ProviderException;33import java.util.ArrayList;34import java.util.Collection;35import java.util.Collections;36import java.util.HashMap;37import java.util.Iterator;38import java.util.List;3940import sun.security.ec.ed.EdDSAAlgorithmParameters;41import sun.security.ec.ed.EdDSAKeyFactory;42import sun.security.ec.ed.EdDSAKeyPairGenerator;43import sun.security.ec.ed.EdDSASignature;44import sun.security.util.CurveDB;45import sun.security.util.KnownOIDs;46import sun.security.util.NamedCurve;4748import static sun.security.util.SecurityConstants.PROVIDER_VER;49import static sun.security.util.SecurityProviderConstants.*;5051/**52* Provider class for the Elliptic Curve provider.53*/54public final class SunEC extends Provider {5556private static final long serialVersionUID = -2279741672933606418L;5758private static class ProviderServiceA extends ProviderService {59ProviderServiceA(Provider p, String type, String algo, String cn,60HashMap<String, String> attrs) {61super(p, type, algo, cn, getAliases(algo), attrs);62}63}6465private static class ProviderService extends Provider.Service {6667ProviderService(Provider p, String type, String algo, String cn) {68super(p, type, algo, cn, null, null);69}7071ProviderService(Provider p, String type, String algo, String cn,72List<String> aliases, HashMap<String, String> attrs) {73super(p, type, algo, cn, aliases, attrs);74}7576@Override77public Object newInstance(Object ctrParamObj)78throws NoSuchAlgorithmException {79String type = getType();80if (ctrParamObj != null) {81throw new InvalidParameterException82("constructorParameter not used with " + type + " engines");83}8485String algo = getAlgorithm();86try {87if (type.equals("Signature")) {8889if (algo.equalsIgnoreCase("EdDSA")) {90return new EdDSASignature();91} else if (algo.equalsIgnoreCase("Ed25519")) {92return new EdDSASignature.Ed25519();93} else if (algo.equalsIgnoreCase("Ed448")) {94return new EdDSASignature.Ed448();95}9697boolean inP1363 = algo.endsWith("inP1363Format");98if (inP1363) {99algo = algo.substring(0, algo.length() - 13);100}101if (algo.equals("SHA1withECDSA")) {102return (inP1363? new ECDSASignature.SHA1inP1363Format() :103new ECDSASignature.SHA1());104} else if (algo.equals("SHA224withECDSA")) {105return (inP1363? new ECDSASignature.SHA224inP1363Format() :106new ECDSASignature.SHA224());107} else if (algo.equals("SHA256withECDSA")) {108return (inP1363? new ECDSASignature.SHA256inP1363Format() :109new ECDSASignature.SHA256());110} else if (algo.equals("SHA384withECDSA")) {111return (inP1363? new ECDSASignature.SHA384inP1363Format() :112new ECDSASignature.SHA384());113} else if (algo.equals("SHA512withECDSA")) {114return (inP1363? new ECDSASignature.SHA512inP1363Format() :115new ECDSASignature.SHA512());116} else if (algo.equals("NONEwithECDSA")) {117return (inP1363? new ECDSASignature.RawinP1363Format() :118new ECDSASignature.Raw());119} else if (algo.equals("SHA3-224withECDSA")) {120return (inP1363? new ECDSASignature.SHA3_224inP1363Format() :121new ECDSASignature.SHA3_224());122} else if (algo.equals("SHA3-256withECDSA")) {123return (inP1363? new ECDSASignature.SHA3_256inP1363Format() :124new ECDSASignature.SHA3_256());125} else if (algo.equals("SHA3-384withECDSA")) {126return (inP1363? new ECDSASignature.SHA3_384inP1363Format() :127new ECDSASignature.SHA3_384());128} else if (algo.equals("SHA3-512withECDSA")) {129return (inP1363? new ECDSASignature.SHA3_512inP1363Format() :130new ECDSASignature.SHA3_512());131}132} else if (type.equals("KeyFactory")) {133if (algo.equals("EC")) {134return new ECKeyFactory();135} else if (algo.equals("XDH")) {136return new XDHKeyFactory();137} else if (algo.equals("X25519")) {138return new XDHKeyFactory.X25519();139} else if (algo.equals("X448")) {140return new XDHKeyFactory.X448();141} else if (algo.equalsIgnoreCase("EdDSA")) {142return new EdDSAKeyFactory();143} else if (algo.equalsIgnoreCase("Ed25519")) {144return new EdDSAKeyFactory.Ed25519();145} else if (algo.equalsIgnoreCase("Ed448")) {146return new EdDSAKeyFactory.Ed448();147}148} else if (type.equals("AlgorithmParameters")) {149if (algo.equals("EC")) {150return new sun.security.util.ECParameters();151}152} else if (type.equals("KeyPairGenerator")) {153if (algo.equals("EC")) {154return new ECKeyPairGenerator();155} else if (algo.equals("XDH")) {156return new XDHKeyPairGenerator();157} else if (algo.equals("X25519")) {158return new XDHKeyPairGenerator.X25519();159} else if (algo.equals("X448")) {160return new XDHKeyPairGenerator.X448();161} else if (algo.equalsIgnoreCase("EdDSA")) {162return new EdDSAKeyPairGenerator();163} else if (algo.equalsIgnoreCase("Ed25519")) {164return new EdDSAKeyPairGenerator.Ed25519();165} else if (algo.equalsIgnoreCase("Ed448")) {166return new EdDSAKeyPairGenerator.Ed448();167}168} else if (type.equals("KeyAgreement")) {169if (algo.equals("ECDH")) {170return new ECDHKeyAgreement();171} else if (algo.equals("XDH")) {172return new XDHKeyAgreement();173} else if (algo.equals("X25519")) {174return new XDHKeyAgreement.X25519();175} else if (algo.equals("X448")) {176return new XDHKeyAgreement.X448();177}178}179} catch (Exception ex) {180throw new NoSuchAlgorithmException("Error constructing " +181type + " for " + algo + " using SunEC", ex);182}183throw new ProviderException("No impl for " + algo +184" " + type);185}186}187188@SuppressWarnings("removal")189public SunEC() {190super("SunEC", PROVIDER_VER, "Sun Elliptic Curve provider");191AccessController.doPrivileged(new PrivilegedAction<Void>() {192public Void run() {193putEntries();194return null;195}196});197}198199void putEntries() {200HashMap<String, String> ATTRS = new HashMap<>(3);201ATTRS.put("ImplementedIn", "Software");202String ecKeyClasses = "java.security.interfaces.ECPublicKey" +203"|java.security.interfaces.ECPrivateKey";204ATTRS.put("SupportedKeyClasses", ecKeyClasses);205ATTRS.put("KeySize", "256");206207/*208* Key Factory engine209*/210putService(new ProviderService(this, "KeyFactory",211"EC", "sun.security.ec.ECKeyFactory",212List.of("EllipticCurve"), ATTRS));213214/*215* Algorithm Parameter engine216*/217// "AlgorithmParameters.EC SupportedCurves" prop used by unit test218boolean firstCurve = true;219StringBuilder names = new StringBuilder();220221for (NamedCurve namedCurve :222List.of(223CurveDB.lookup("secp256r1"),224CurveDB.lookup("secp384r1"),225CurveDB.lookup("secp521r1"))) {226if (!firstCurve) {227names.append("|");228} else {229firstCurve = false;230}231232names.append("[");233String[] commonNames = namedCurve.getNameAndAliases();234for (String commonName : commonNames) {235names.append(commonName);236names.append(",");237}238239names.append(namedCurve.getObjectId());240names.append("]");241}242243HashMap<String, String> apAttrs = new HashMap<>(ATTRS);244apAttrs.put("SupportedCurves", names.toString());245246putService(new ProviderServiceA(this, "AlgorithmParameters",247"EC", "sun.security.util.ECParameters", apAttrs));248249putXDHEntries();250putEdDSAEntries();251252/*253* Signature engines254*/255putService(new ProviderService(this, "Signature",256"NONEwithECDSA", "sun.security.ec.ECDSASignature$Raw",257null, ATTRS));258putService(new ProviderServiceA(this, "Signature",259"SHA1withECDSA", "sun.security.ec.ECDSASignature$SHA1",260ATTRS));261putService(new ProviderServiceA(this, "Signature",262"SHA224withECDSA", "sun.security.ec.ECDSASignature$SHA224",263ATTRS));264putService(new ProviderServiceA(this, "Signature",265"SHA256withECDSA", "sun.security.ec.ECDSASignature$SHA256",266ATTRS));267putService(new ProviderServiceA(this, "Signature",268"SHA384withECDSA", "sun.security.ec.ECDSASignature$SHA384",269ATTRS));270putService(new ProviderServiceA(this, "Signature",271"SHA512withECDSA", "sun.security.ec.ECDSASignature$SHA512",272ATTRS));273putService(new ProviderServiceA(this, "Signature",274"SHA3-224withECDSA", "sun.security.ec.ECDSASignature$SHA3_224",275ATTRS));276putService(new ProviderServiceA(this, "Signature",277"SHA3-256withECDSA", "sun.security.ec.ECDSASignature$SHA3_256",278ATTRS));279putService(new ProviderServiceA(this, "Signature",280"SHA3-384withECDSA", "sun.security.ec.ECDSASignature$SHA3_384",281ATTRS));282putService(new ProviderServiceA(this, "Signature",283"SHA3-512withECDSA", "sun.security.ec.ECDSASignature$SHA3_512",284ATTRS));285286putService(new ProviderService(this, "Signature",287"NONEwithECDSAinP1363Format",288"sun.security.ec.ECDSASignature$RawinP1363Format"));289putService(new ProviderService(this, "Signature",290"SHA1withECDSAinP1363Format",291"sun.security.ec.ECDSASignature$SHA1inP1363Format"));292putService(new ProviderService(this, "Signature",293"SHA224withECDSAinP1363Format",294"sun.security.ec.ECDSASignature$SHA224inP1363Format"));295putService(new ProviderService(this, "Signature",296"SHA256withECDSAinP1363Format",297"sun.security.ec.ECDSASignature$SHA256inP1363Format"));298putService(new ProviderService(this, "Signature",299"SHA384withECDSAinP1363Format",300"sun.security.ec.ECDSASignature$SHA384inP1363Format"));301putService(new ProviderService(this, "Signature",302"SHA512withECDSAinP1363Format",303"sun.security.ec.ECDSASignature$SHA512inP1363Format"));304305putService(new ProviderService(this, "Signature",306"SHA3-224withECDSAinP1363Format",307"sun.security.ec.ECDSASignature$SHA3_224inP1363Format"));308putService(new ProviderService(this, "Signature",309"SHA3-256withECDSAinP1363Format",310"sun.security.ec.ECDSASignature$SHA3_256inP1363Format"));311putService(new ProviderService(this, "Signature",312"SHA3-384withECDSAinP1363Format",313"sun.security.ec.ECDSASignature$SHA3_384inP1363Format"));314putService(new ProviderService(this, "Signature",315"SHA3-512withECDSAinP1363Format",316"sun.security.ec.ECDSASignature$SHA3_512inP1363Format"));317318/*319* Key Pair Generator engine320*/321putService(new ProviderService(this, "KeyPairGenerator",322"EC", "sun.security.ec.ECKeyPairGenerator",323List.of("EllipticCurve"), ATTRS));324325/*326* Key Agreement engine327*/328putService(new ProviderService(this, "KeyAgreement",329"ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));330}331332private void putXDHEntries() {333334HashMap<String, String> ATTRS = new HashMap<>(1);335ATTRS.put("ImplementedIn", "Software");336337putService(new ProviderService(this, "KeyFactory",338"XDH", "sun.security.ec.XDHKeyFactory", null, ATTRS));339putService(new ProviderServiceA(this, "KeyFactory",340"X25519", "sun.security.ec.XDHKeyFactory.X25519",341ATTRS));342putService(new ProviderServiceA(this, "KeyFactory",343"X448", "sun.security.ec.XDHKeyFactory.X448",344ATTRS));345346putService(new ProviderService(this, "KeyPairGenerator",347"XDH", "sun.security.ec.XDHKeyPairGenerator", null, ATTRS));348putService(new ProviderServiceA(this, "KeyPairGenerator",349"X25519", "sun.security.ec.XDHKeyPairGenerator.X25519",350ATTRS));351putService(new ProviderServiceA(this, "KeyPairGenerator",352"X448", "sun.security.ec.XDHKeyPairGenerator.X448",353ATTRS));354355putService(new ProviderService(this, "KeyAgreement",356"XDH", "sun.security.ec.XDHKeyAgreement", null, ATTRS));357putService(new ProviderServiceA(this, "KeyAgreement",358"X25519", "sun.security.ec.XDHKeyAgreement.X25519",359ATTRS));360putService(new ProviderServiceA(this, "KeyAgreement",361"X448", "sun.security.ec.XDHKeyAgreement.X448",362ATTRS));363}364365private void putEdDSAEntries() {366367HashMap<String, String> ATTRS = new HashMap<>(1);368ATTRS.put("ImplementedIn", "Software");369370putService(new ProviderService(this, "KeyFactory",371"EdDSA", "sun.security.ec.ed.EdDSAKeyFactory", null, ATTRS));372putService(new ProviderServiceA(this, "KeyFactory",373"Ed25519", "sun.security.ec.ed.EdDSAKeyFactory.Ed25519", ATTRS));374putService(new ProviderServiceA(this, "KeyFactory",375"Ed448", "sun.security.ec.ed.EdDSAKeyFactory.Ed448", ATTRS));376377putService(new ProviderService(this, "KeyPairGenerator",378"EdDSA", "sun.security.ec.ed.EdDSAKeyPairGenerator", null, ATTRS));379putService(new ProviderServiceA(this, "KeyPairGenerator",380"Ed25519", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed25519",381ATTRS));382putService(new ProviderServiceA(this, "KeyPairGenerator",383"Ed448", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed448",384ATTRS));385386putService(new ProviderService(this, "Signature",387"EdDSA", "sun.security.ec.ed.EdDSASignature", null, ATTRS));388putService(new ProviderServiceA(this, "Signature",389"Ed25519", "sun.security.ec.ed.EdDSASignature.Ed25519", ATTRS));390putService(new ProviderServiceA(this, "Signature",391"Ed448", "sun.security.ec.ed.EdDSASignature.Ed448", ATTRS));392393}394}395396397