jdk/src/share/classes/sun/security/pkcs11/P11DHKeyFactory.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.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * DH KeyFactory implemenation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class P11DHKeyFactory extends P11KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    P11DHKeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        super(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (key instanceof DHPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                DHPublicKey dhKey = (DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    dhKey.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    params.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                // let SunJCE provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    KeyFactory factory = implGetSoftwareFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    key = (PublicKey)factory.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    throw new InvalidKeyException("Could not translate key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                throw new InvalidKeyException("PublicKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        + "of DHPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new InvalidKeyException("Could not create DH public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (key instanceof DHPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                DHPrivateKey dhKey = (DHPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    dhKey.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    params.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // let SunJCE provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    KeyFactory factory = implGetSoftwareFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    key = (PrivateKey)factory.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new InvalidKeyException("Could not translate key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new InvalidKeyException("PrivateKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        + "of DHPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new InvalidKeyException("Could not create DH private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                KeyFactory factory = implGetSoftwareFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                PublicKey key = factory.generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        ("Could not create DH public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (keySpec instanceof DHPublicKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new InvalidKeySpecException("Only DHPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                + "X509EncodedKeySpec supported for DH public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            DHPublicKeySpec ds = (DHPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                ds.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                ds.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                ds.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ("Could not create DH public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                KeyFactory factory = implGetSoftwareFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                PrivateKey key = factory.generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ("Could not create DH private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (keySpec instanceof DHPrivateKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new InvalidKeySpecException("Only DHPrivateKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                + "PKCS8EncodedKeySpec supported for DH private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            DHPrivateKeySpec ds = (DHPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                ds.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ds.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                ds.getG()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                ("Could not create DH private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private PublicKey generatePublic(BigInteger y, BigInteger p, BigInteger g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            new CK_ATTRIBUTE(CKA_VALUE, y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            new CK_ATTRIBUTE(CKA_PRIME, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            new CK_ATTRIBUTE(CKA_BASE, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                (O_IMPORT, CKO_PUBLIC_KEY, CKK_DH, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                (session, keyID, "DH", p.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private PrivateKey generatePrivate(BigInteger x, BigInteger p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            BigInteger g) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            new CK_ATTRIBUTE(CKA_VALUE, x),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            new CK_ATTRIBUTE(CKA_PRIME, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            new CK_ATTRIBUTE(CKA_BASE, g),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_DH, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                (session, keyID, "DH", p.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   215
    <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
   216
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            KeySpec spec = new DHPublicKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                attributes[2].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   230
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else { // X.509 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new InvalidKeySpecException("Only DHPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                + "X509EncodedKeySpec supported for DH public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   237
    <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
   238
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            KeySpec spec = new DHPrivateKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                attributes[2].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 DHPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                + "and PKCS8EncodedKeySpec supported for DH 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("DH", P11Util.getSunJceProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}