src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CPublicKey.java
author weijun
Sat, 27 Apr 2019 18:21:57 +0800
changeset 54639 2cac7d48db4c
parent 53007 e2798bf6318a
child 54827 01fa7f06f806
permissions -rw-r--r--
8223003: SunMSCAPI keys are not cleaned up Reviewed-by: igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
     2
 * Copyright (c) 2005, 2019, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    29
import java.security.AlgorithmParameters;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    30
import java.security.KeyException;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    31
import java.security.KeyFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyRep;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    33
import java.security.ProviderException;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    34
import java.security.PublicKey;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    35
import java.security.interfaces.ECPublicKey;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    36
import java.security.interfaces.RSAPublicKey;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    37
import java.security.spec.ECParameterSpec;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    38
import java.security.spec.ECPoint;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    39
import java.security.spec.ECPublicKeySpec;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    40
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    42
import sun.security.rsa.RSAUtil.KeyType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.rsa.RSAPublicKeyImpl;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    44
import sun.security.util.ECKeySizeParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The handle for an RSA public key using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    51
public abstract class CPublicKey extends CKey implements PublicKey {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    52
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
    53
    private static final long serialVersionUID = -2289561342425825391L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
    54
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    55
    protected byte[] encoding = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
    56
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    57
    public static class CECPublicKey extends CPublicKey implements ECPublicKey {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    58
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    59
        private ECPoint w = null;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    60
        private static final long serialVersionUID = 12L;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    61
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
    62
        CECPublicKey(NativeHandles handles, int keyLength) {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
    63
            super("EC", handles, keyLength);
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    64
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    65
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    66
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    67
        public ECPoint getW() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    68
            if (w == null) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    69
                // See CKey::generateECBlob.
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    70
                try {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    71
                    byte[] blob = getPublicKeyBlob(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    72
                            handles.hCryptProv, handles.hCryptKey);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    73
                    int len = blob[8] & 0xff;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    74
                    byte[] x = Arrays.copyOfRange(blob, 8, 8 + len);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    75
                    byte[] y = Arrays.copyOfRange(blob, 8 + len, 8 + len + len);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    76
                    w = new ECPoint(new BigInteger(1, x), new BigInteger(1, y));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    77
                } catch (KeyException e) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    78
                    throw new ProviderException(e);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    79
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    80
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    81
            return w;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    82
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    83
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    84
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    85
        public byte[] getEncoded() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    86
            if (encoding == null) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    87
                try {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    88
                    encoding = KeyFactory.getInstance("EC").generatePublic(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    89
                                new ECPublicKeySpec(getW(), getParams()))
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    90
                            .getEncoded();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    91
                } catch (Exception e) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    92
                    // ignore
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    93
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    94
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    95
            return encoding;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    96
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    97
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    98
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    99
        public ECParameterSpec getParams() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   100
            try {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   101
                AlgorithmParameters ap = AlgorithmParameters.getInstance("EC");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   102
                ap.init(new ECKeySizeParameterSpec(keyLength));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   103
                return ap.getParameterSpec(ECParameterSpec.class);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   104
            } catch (Exception e) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   105
                throw new ProviderException(e);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   106
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   107
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   108
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   109
        public String toString() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   110
            StringBuffer sb = new StringBuffer();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   111
            sb.append(algorithm + "PublicKey [size=").append(keyLength)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   112
                    .append("]\n  ECPoint: ").append(getW())
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   113
                    .append("\n  params: ").append(getParams());
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   114
            return sb.toString();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   115
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   116
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   117
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   118
    public static class CRSAPublicKey extends CPublicKey implements RSAPublicKey {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   119
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   120
        private BigInteger modulus = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   121
        private BigInteger exponent = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   122
        private static final long serialVersionUID = 12L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   124
        CRSAPublicKey(NativeHandles handles, int keyLength) {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   125
            super("RSA", handles, keyLength);
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   126
        }
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
   127
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   128
        public String toString() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   129
            StringBuffer sb = new StringBuffer();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   130
            sb.append(algorithm + "PublicKey [size=").append(keyLength)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   131
                    .append(" bits, type=").append(getKeyType(handles.hCryptKey))
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   132
                    .append(", container=").append(getContainerName(handles.hCryptProv))
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   133
                    .append("]\n  modulus: ").append(getModulus())
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   134
                    .append("\n  public exponent: ").append(getPublicExponent());
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   135
            return sb.toString();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   136
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   138
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   139
        public BigInteger getPublicExponent() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   140
            if (exponent == null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   141
                try {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   142
                    byte[] publicKeyBlob = getPublicKeyBlob(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   143
                            handles.hCryptProv, handles.hCryptKey);
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   144
                    exponent = new BigInteger(1, getExponent(publicKeyBlob));
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   145
                } catch (KeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   146
                    throw new ProviderException(e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   147
                }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   148
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   149
            return exponent;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   150
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   152
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   153
        public BigInteger getModulus() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   154
            if (modulus == null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   155
                try {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   156
                    byte[] publicKeyBlob = getPublicKeyBlob(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   157
                            handles.hCryptProv, handles.hCryptKey);
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   158
                    modulus = new BigInteger(1, getModulus(publicKeyBlob));
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   159
                } catch (KeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   160
                    throw new ProviderException(e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   161
                }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   162
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   163
            return modulus;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   164
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   166
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   167
        public byte[] getEncoded() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   168
            if (encoding == null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   169
                try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   170
                    encoding = RSAPublicKeyImpl.newKey(KeyType.RSA, null,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   171
                            getModulus(), getPublicExponent()).getEncoded();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   172
                } catch (KeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   173
                    // ignore
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   174
                }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   175
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   176
            return encoding;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   177
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   179
        private native byte[] getExponent(byte[] keyBlob) throws KeyException;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   180
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   181
        private native byte[] getModulus(byte[] keyBlob) throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   184
    // Called by native code inside security.cpp
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   185
    static CPublicKey of(
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   186
            String alg, long hCryptProv, long hCryptKey, int keyLength) {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   187
        return of(alg, new NativeHandles(hCryptProv, hCryptKey), keyLength);
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   188
    }
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   189
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   190
    public static CPublicKey of(
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   191
            String alg, NativeHandles handles, int keyLength) {
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   192
        switch (alg) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   193
            case "RSA":
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   194
                return new CRSAPublicKey(handles, keyLength);
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   195
            case "EC":
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   196
                return new CECPublicKey(handles, keyLength);
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   197
            default:
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   198
                throw new AssertionError("Unsupported algorithm: " + alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   202
    protected CPublicKey(
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   203
            String alg, NativeHandles handles, int keyLength) {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 53007
diff changeset
   204
        super(alg, handles, keyLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   207
    @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   208
    public String getFormat() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 50204
diff changeset
   209
        return "X.509";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    protected Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return new KeyRep(KeyRep.Type.PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   219
    // Returns the CAPI or CNG representation of the key.
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   220
    native byte[] getPublicKeyBlob(long hCryptProv, long hCryptKey)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   221
            throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}