jdk/src/share/classes/sun/security/pkcs11/P11DSAKeyFactory.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * DSA KeyFactory implemenation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
final class P11DSAKeyFactory extends P11KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    P11DSAKeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            if (key instanceof DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                DSAPublicKey dsaKey = (DSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                DSAParams params = dsaKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    dsaKey.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    params.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    params.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                // let Sun provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                key = new sun.security.provider.DSAPublicKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new InvalidKeyException("PublicKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        + "of DSAPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new InvalidKeyException("Could not create DSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (key instanceof DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                DSAPrivateKey dsaKey = (DSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                DSAParams params = dsaKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    dsaKey.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    params.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    params.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // let Sun provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                key = new sun.security.provider.DSAPrivateKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                throw new InvalidKeyException("PrivateKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        + "of DSAPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidKeyException("Could not create DSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                PublicKey key = new sun.security.provider.DSAPublicKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        ("Could not create DSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (keySpec instanceof DSAPublicKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new InvalidKeySpecException("Only DSAPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                + "X509EncodedKeySpec supported for DSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            DSAPublicKeySpec ds = (DSAPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ds.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                ds.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ds.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ds.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                ("Could not create DSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                PrivateKey key = new sun.security.provider.DSAPrivateKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        ("Could not create DSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (keySpec instanceof DSAPrivateKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new InvalidKeySpecException("Only DSAPrivateKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                + "PKCS8EncodedKeySpec supported for DSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            DSAPrivateKeySpec ds = (DSAPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                ds.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                ds.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                ds.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                ds.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                ("Could not create DSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private PublicKey generatePublic(BigInteger y, BigInteger p, BigInteger q,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            BigInteger g) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            new CK_ATTRIBUTE(CKA_VALUE, y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            new CK_ATTRIBUTE(CKA_PRIME, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            new CK_ATTRIBUTE(CKA_SUBPRIME, q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            new CK_ATTRIBUTE(CKA_BASE, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                (O_IMPORT, CKO_PUBLIC_KEY, CKK_DSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                (session, keyID, "DSA", p.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private PrivateKey generatePrivate(BigInteger x, BigInteger p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            BigInteger q, BigInteger g) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            new CK_ATTRIBUTE(CKA_VALUE, x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            new CK_ATTRIBUTE(CKA_PRIME, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            new CK_ATTRIBUTE(CKA_SUBPRIME, q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            new CK_ATTRIBUTE(CKA_BASE, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_DSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                (session, keyID, "DSA", p.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   211
    <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   212
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (DSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                new CK_ATTRIBUTE(CKA_SUBPRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            KeySpec spec = new DSAPublicKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                attributes[3].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   228
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else { // X.509 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new InvalidKeySpecException("Only DSAPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                + "X509EncodedKeySpec supported for DSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   235
    <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   236
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                new CK_ATTRIBUTE(CKA_SUBPRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            KeySpec spec = new DSAPrivateKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                attributes[3].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   252
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else { // PKCS#8 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new InvalidKeySpecException("Only DSAPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                + "and PKCS8EncodedKeySpec supported for DSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return KeyFactory.getInstance("DSA", P11Util.getSunProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}