jdk/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/Key.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 19832 jdk/src/windows/classes/sun/security/mscapi/Key.java@80fff95a633a
child 40389 c6df8bba0b71
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19832
80fff95a633a 8024501: sun.security.mscapi.Key has no definition of serialVersionUID
xuelei
parents: 11521
diff changeset
     2
 * Copyright (c) 2005, 2013, 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.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
    28
import sun.security.util.Length;
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The handle for an RSA or DSA key using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @see DSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @see RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
    40
abstract class Key implements java.security.Key, Length
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
19832
80fff95a633a 8024501: sun.security.mscapi.Key has no definition of serialVersionUID
xuelei
parents: 11521
diff changeset
    42
    private static final long serialVersionUID = -1088859394025049194L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // Native handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected long hCryptProv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected long hCryptKey = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Key length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected int keyLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Construct a Key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected Key(long hCryptProv, long hCryptKey, int keyLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.hCryptProv = hCryptProv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.hCryptKey = hCryptKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.keyLength = keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Finalization method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected void finalize() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            synchronized(this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                cleanUp(hCryptProv, hCryptKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                hCryptProv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                hCryptKey = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
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
     * Native method to cleanup the key handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private native static void cleanUp(long hCryptProv, long hCryptKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Return bit length of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
    87
    @Override
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
    88
    public int length()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Return native HCRYPTKEY handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public long getHCryptKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return hCryptKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Return native HCRYPTPROV handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public long getHCryptProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return hCryptProv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns the standard algorithm name for this key. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * example, "DSA" would indicate that this key is a DSA key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * "../../../guide/security/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the name of the algorithm associated with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public abstract String getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns the name of the primary encoding format of this key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * or null if this key does not support encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The primary encoding format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * named in terms of the appropriate ASN.1 data format, if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * ASN.1 specification for this key exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * For example, the name of the ASN.1 data format for public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * keys is <I>SubjectPublicKeyInfo</I>, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * defined by the X.509 standard; in this case, the returned format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>"X.509"</code>. Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the name of the ASN.1 data format for private keys is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <I>PrivateKeyInfo</I>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * as defined by the PKCS #8 standard; in this case, the returned format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>"PKCS#8"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the primary encoding format of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public String getFormat()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the key in its primary encoding format, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * if this key does not support encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return the encoded key, or null if the key does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public byte[] getEncoded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected native static String getContainerName(long hCryptProv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected native static String getKeyType(long hCryptKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}