1 /* |
1 /* |
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
28 import sun.security.util.Length; |
28 import sun.security.util.Length; |
29 |
29 |
30 /** |
30 /** |
31 * The handle for an RSA or DSA key using the Microsoft Crypto API. |
31 * The handle for an RSA or DSA key using the Microsoft Crypto API. |
32 * |
32 * |
33 * @see DSAPrivateKey |
|
34 * @see RSAPrivateKey |
33 * @see RSAPrivateKey |
35 * @see RSAPublicKey |
34 * @see RSAPublicKey |
36 * |
35 * |
37 * @since 1.6 |
36 * @since 1.6 |
38 * @author Stanley Man-Kit Ho |
37 * @author Stanley Man-Kit Ho |
39 */ |
38 */ |
40 abstract class Key implements java.security.Key, Length |
39 abstract class Key implements java.security.Key, Length |
41 { |
40 { |
42 private static final long serialVersionUID = -1088859394025049194L; |
41 private static final long serialVersionUID = -1088859394025049194L; |
43 |
42 |
44 // Native handle |
43 static class NativeHandles { |
45 protected long hCryptProv = 0; |
44 long hCryptProv = 0; |
46 protected long hCryptKey = 0; |
45 long hCryptKey = 0; |
|
46 |
|
47 public NativeHandles(long hCryptProv, long hCryptKey) { |
|
48 this.hCryptProv = hCryptProv; |
|
49 this.hCryptKey = hCryptKey; |
|
50 } |
|
51 |
|
52 /** |
|
53 * Finalization method |
|
54 */ |
|
55 protected void finalize() throws Throwable |
|
56 { |
|
57 try { |
|
58 synchronized(this) |
|
59 { |
|
60 cleanUp(hCryptProv, hCryptKey); |
|
61 hCryptProv = 0; |
|
62 hCryptKey = 0; |
|
63 } |
|
64 |
|
65 } finally { |
|
66 super.finalize(); |
|
67 } |
|
68 } |
|
69 } |
|
70 |
|
71 protected NativeHandles handles; |
47 |
72 |
48 // Key length |
73 // Key length |
49 protected int keyLength = 0; |
74 protected int keyLength = 0; |
50 |
75 |
51 /** |
76 /** |
52 * Construct a Key object. |
77 * Construct a Key object. |
53 */ |
78 */ |
54 protected Key(long hCryptProv, long hCryptKey, int keyLength) |
79 protected Key(NativeHandles handles, int keyLength) |
55 { |
80 { |
56 this.hCryptProv = hCryptProv; |
81 this.handles = handles; |
57 this.hCryptKey = hCryptKey; |
|
58 this.keyLength = keyLength; |
82 this.keyLength = keyLength; |
59 } |
|
60 |
|
61 /** |
|
62 * Finalization method |
|
63 */ |
|
64 protected void finalize() throws Throwable |
|
65 { |
|
66 try { |
|
67 synchronized(this) |
|
68 { |
|
69 cleanUp(hCryptProv, hCryptKey); |
|
70 hCryptProv = 0; |
|
71 hCryptKey = 0; |
|
72 } |
|
73 |
|
74 } finally { |
|
75 super.finalize(); |
|
76 } |
|
77 } |
83 } |
78 |
84 |
79 /** |
85 /** |
80 * Native method to cleanup the key handle. |
86 * Native method to cleanup the key handle. |
81 */ |
87 */ |
94 /** |
100 /** |
95 * Return native HCRYPTKEY handle. |
101 * Return native HCRYPTKEY handle. |
96 */ |
102 */ |
97 public long getHCryptKey() |
103 public long getHCryptKey() |
98 { |
104 { |
99 return hCryptKey; |
105 return handles.hCryptKey; |
100 } |
106 } |
101 |
107 |
102 /** |
108 /** |
103 * Return native HCRYPTPROV handle. |
109 * Return native HCRYPTPROV handle. |
104 */ |
110 */ |
105 public long getHCryptProvider() |
111 public long getHCryptProvider() |
106 { |
112 { |
107 return hCryptProv; |
113 return handles.hCryptProv; |
108 } |
114 } |
109 |
115 |
110 /** |
116 /** |
111 * Returns the standard algorithm name for this key. For |
117 * Returns the standard algorithm name for this key. For |
112 * example, "DSA" would indicate that this key is a DSA key. |
118 * example, "RSA" would indicate that this key is a RSA key. |
113 * See Appendix A in the <a href= |
119 * See Appendix A in the <a href= |
114 * "../../../guide/security/CryptoSpec.html#AppA"> |
120 * "../../../guide/security/CryptoSpec.html#AppA"> |
115 * Java Cryptography Architecture API Specification & Reference </a> |
121 * Java Cryptography Architecture API Specification & Reference </a> |
116 * for information about standard algorithm names. |
122 * for information about standard algorithm names. |
117 * |
123 * |