1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
 
package com.sun.deploy.security;

import java.security.PrivateKey;

/**
 * The base class to an RSA or DSA private key for Microsoft Crypto APIs.
 *
 * @see MSCryptoPrivateKey
 *
 * @since 1.5
 * @author  Stanley Man-Kit Ho 
 */
abstract class MSCryptoPrivateKey implements PrivateKey
{

    // Native handle
    protected int hCryptProv = 0;
    protected int hCryptKey = 0;
    
    // Key length
    protected int keyLength = 0;

    // Flag to track if finalize() has been called once
    private boolean finalized = false;

    /**
     * Construct a MSCryptoPrivateKey object.
     */
    protected MSCryptoPrivateKey(int hCryptProv, int hCryptKey, int keyLength)
    {
    this.hCryptProv = hCryptProv;
    this.hCryptKey = hCryptKey;
    this.keyLength = keyLength;
    }

    /**
     * Finalization method
     */
    public void finalize()
    {
    try
    {
        synchronized(this)
        {
        if (finalized == false)
        {
            cleanUp(hCryptProv, hCryptKey);
            hCryptProv = 0;
            hCryptKey = 0;
            finalized = true;

            super.finalize();
        }
        }
    } 
    catch (Throwable e)
    {
        e.printStackTrace();
    }
    }

    /**
     * Native method to cleanup the key handle.
     */
    private native static void cleanUp(int hCryptProv, int hCryptKey);

    /** 
     * Return bit length of the key.
     */
    public int bitLength()
    {
    return keyLength;
    }

    
    /**
     * Return native HCRYPTKEY handle.
     */
    public int getHCryptKey()
    {
    return hCryptKey;
    }    

    /**
     * Return native HCRYPTPROV handle.
     */
    public int getHCryptProvider()
    {
    return hCryptProv;
    }       
    
    /**
     * Returns the standard algorithm name for this key. For
     * example, "DSA" would indicate that this key is a DSA key.
     * See Appendix A in the <a href=
     * "../../../guide/security/CryptoSpec.html#AppA">
     * Java Cryptography Architecture API Specification &amp; Reference </a>
     * for information about standard algorithm names.
     *
     * @return the name of the algorithm associated with this key.
     */
    public abstract String getAlgorithm();

    /**
     * Returns the name of the primary encoding format of this key,
     * or null if this key does not support encoding.
     * The primary encoding format is
     * named in terms of the appropriate ASN.1 data format, if an
     * ASN.1 specification for this key exists.
     * For example, the name of the ASN.1 data format for public
     * keys is <I>SubjectPublicKeyInfo</I>, as
     * defined by the X.509 standard; in this case, the returned format is
     * <code>"X.509"</code>. Similarly,
     * the name of the ASN.1 data format for private keys is
     * <I>PrivateKeyInfo</I>,
     * as defined by the PKCS #8 standard; in this case, the returned format is
     * <code>"PKCS#8"</code>.
     *
     * @return the primary encoding format of the key.
     */
    public String getFormat()
    {
    return null;
    }

    /**
     * Returns the key in its primary encoding format, or null
     * if this key does not support encoding.
     *
     * @return the encoded key, or null if the key does not support
     * encoding.
     */
    public byte[] getEncoded()
    {
    return null;
    } 
}
			
			

Browsed Source: [clear]