Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11DSAKeyFactory.java
41154 views
/*1* Copyright (c) 2003, 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.pkcs11;2627import java.math.BigInteger;2829import java.security.*;30import java.security.interfaces.*;31import java.security.spec.*;3233import static sun.security.pkcs11.TemplateManager.*;34import sun.security.pkcs11.wrapper.*;35import static sun.security.pkcs11.wrapper.PKCS11Constants.*;3637/**38* DSA KeyFactory implementation.39*40* @author Andreas Sterbenz41* @since 1.542*/43final class P11DSAKeyFactory extends P11KeyFactory {4445P11DSAKeyFactory(Token token, String algorithm) {46super(token, algorithm);47}4849PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {50try {51if (key instanceof DSAPublicKey) {52DSAPublicKey dsaKey = (DSAPublicKey)key;53DSAParams params = dsaKey.getParams();54return generatePublic(55dsaKey.getY(),56params.getP(),57params.getQ(),58params.getG()59);60} else if ("X.509".equals(key.getFormat())) {61// let Sun provider parse for us, then recurse62byte[] encoded = key.getEncoded();63key = new sun.security.provider.DSAPublicKey(encoded);64return implTranslatePublicKey(key);65} else {66throw new InvalidKeyException("PublicKey must be instance "67+ "of DSAPublicKey or have X.509 encoding");68}69} catch (PKCS11Exception e) {70throw new InvalidKeyException("Could not create DSA public key", e);71}72}7374PrivateKey implTranslatePrivateKey(PrivateKey key)75throws InvalidKeyException {76try {77if (key instanceof DSAPrivateKey) {78DSAPrivateKey dsaKey = (DSAPrivateKey)key;79DSAParams params = dsaKey.getParams();80return generatePrivate(81dsaKey.getX(),82params.getP(),83params.getQ(),84params.getG()85);86} else if ("PKCS#8".equals(key.getFormat())) {87// let Sun provider parse for us, then recurse88byte[] encoded = key.getEncoded();89key = new sun.security.provider.DSAPrivateKey(encoded);90return implTranslatePrivateKey(key);91} else {92throw new InvalidKeyException("PrivateKey must be instance "93+ "of DSAPrivateKey or have PKCS#8 encoding");94}95} catch (PKCS11Exception e) {96throw new InvalidKeyException("Could not create DSA private key", e);97}98}99100// see JCA spec101protected PublicKey engineGeneratePublic(KeySpec keySpec)102throws InvalidKeySpecException {103token.ensureValid();104if (keySpec instanceof X509EncodedKeySpec) {105try {106byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();107PublicKey key = new sun.security.provider.DSAPublicKey(encoded);108return implTranslatePublicKey(key);109} catch (InvalidKeyException e) {110throw new InvalidKeySpecException111("Could not create DSA public key", e);112}113}114if (keySpec instanceof DSAPublicKeySpec == false) {115throw new InvalidKeySpecException("Only DSAPublicKeySpec and "116+ "X509EncodedKeySpec supported for DSA public keys");117}118try {119DSAPublicKeySpec ds = (DSAPublicKeySpec)keySpec;120return generatePublic(121ds.getY(),122ds.getP(),123ds.getQ(),124ds.getG()125);126} catch (PKCS11Exception e) {127throw new InvalidKeySpecException128("Could not create DSA public key", e);129}130}131132// see JCA spec133protected PrivateKey engineGeneratePrivate(KeySpec keySpec)134throws InvalidKeySpecException {135token.ensureValid();136if (keySpec instanceof PKCS8EncodedKeySpec) {137try {138byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();139PrivateKey key = new sun.security.provider.DSAPrivateKey(encoded);140return implTranslatePrivateKey(key);141} catch (GeneralSecurityException e) {142throw new InvalidKeySpecException143("Could not create DSA private key", e);144}145}146if (keySpec instanceof DSAPrivateKeySpec == false) {147throw new InvalidKeySpecException("Only DSAPrivateKeySpec and "148+ "PKCS8EncodedKeySpec supported for DSA private keys");149}150try {151DSAPrivateKeySpec ds = (DSAPrivateKeySpec)keySpec;152return generatePrivate(153ds.getX(),154ds.getP(),155ds.getQ(),156ds.getG()157);158} catch (PKCS11Exception e) {159throw new InvalidKeySpecException160("Could not create DSA private key", e);161}162}163164private PublicKey generatePublic(BigInteger y, BigInteger p, BigInteger q,165BigInteger g) throws PKCS11Exception {166CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {167new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),168new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA),169new CK_ATTRIBUTE(CKA_VALUE, y),170new CK_ATTRIBUTE(CKA_PRIME, p),171new CK_ATTRIBUTE(CKA_SUBPRIME, q),172new CK_ATTRIBUTE(CKA_BASE, g),173};174attributes = token.getAttributes175(O_IMPORT, CKO_PUBLIC_KEY, CKK_DSA, attributes);176Session session = null;177try {178session = token.getObjSession();179long keyID = token.p11.C_CreateObject(session.id(), attributes);180return P11Key.publicKey181(session, keyID, "DSA", p.bitLength(), attributes);182} finally {183token.releaseSession(session);184}185}186187private PrivateKey generatePrivate(BigInteger x, BigInteger p,188BigInteger q, BigInteger g) throws PKCS11Exception {189CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {190new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),191new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA),192new CK_ATTRIBUTE(CKA_VALUE, x),193new CK_ATTRIBUTE(CKA_PRIME, p),194new CK_ATTRIBUTE(CKA_SUBPRIME, q),195new CK_ATTRIBUTE(CKA_BASE, g),196};197attributes = token.getAttributes198(O_IMPORT, CKO_PRIVATE_KEY, CKK_DSA, attributes);199Session session = null;200try {201session = token.getObjSession();202long keyID = token.p11.C_CreateObject(session.id(), attributes);203return P11Key.privateKey204(session, keyID, "DSA", p.bitLength(), attributes);205} finally {206token.releaseSession(session);207}208}209210<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,211Session[] session) throws PKCS11Exception, InvalidKeySpecException {212if (keySpec.isAssignableFrom(DSAPublicKeySpec.class)) {213session[0] = token.getObjSession();214CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {215new CK_ATTRIBUTE(CKA_VALUE),216new CK_ATTRIBUTE(CKA_PRIME),217new CK_ATTRIBUTE(CKA_SUBPRIME),218new CK_ATTRIBUTE(CKA_BASE),219};220long keyID = key.getKeyID();221try {222token.p11.C_GetAttributeValue(session[0].id(), keyID, attributes);223} finally {224key.releaseKeyID();225}226KeySpec spec = new DSAPublicKeySpec(227attributes[0].getBigInteger(),228attributes[1].getBigInteger(),229attributes[2].getBigInteger(),230attributes[3].getBigInteger()231);232return keySpec.cast(spec);233} else { // X.509 handled in superclass234throw new InvalidKeySpecException("Only DSAPublicKeySpec and "235+ "X509EncodedKeySpec supported for DSA public keys");236}237}238239<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,240Session[] session) throws PKCS11Exception, InvalidKeySpecException {241if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class)) {242session[0] = token.getObjSession();243CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {244new CK_ATTRIBUTE(CKA_VALUE),245new CK_ATTRIBUTE(CKA_PRIME),246new CK_ATTRIBUTE(CKA_SUBPRIME),247new CK_ATTRIBUTE(CKA_BASE),248};249long keyID = key.getKeyID();250try {251token.p11.C_GetAttributeValue(session[0].id(), keyID, attributes);252} finally {253key.releaseKeyID();254}255KeySpec spec = new DSAPrivateKeySpec(256attributes[0].getBigInteger(),257attributes[1].getBigInteger(),258attributes[2].getBigInteger(),259attributes[3].getBigInteger()260);261return keySpec.cast(spec);262} else { // PKCS#8 handled in superclass263throw new InvalidKeySpecException("Only DSAPrivateKeySpec "264+ "and PKCS8EncodedKeySpec supported for DSA private keys");265}266}267268KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {269return KeyFactory.getInstance("DSA", P11Util.getSunProvider());270}271272}273274275