1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.ssl;
17
18 import io.netty.internal.tcnative.SSL;
19 import io.netty.util.AbstractReferenceCounted;
20 import io.netty.util.IllegalReferenceCountException;
21 import io.netty.util.internal.EmptyArrays;
22
23 import javax.security.auth.Destroyable;
24 import java.security.PrivateKey;
25 import java.security.cert.X509Certificate;
26
27 final class OpenSslPrivateKey extends AbstractReferenceCounted implements PrivateKey {
28
29 private long privateKeyAddress;
30
31 OpenSslPrivateKey(long privateKeyAddress) {
32 this.privateKeyAddress = privateKeyAddress;
33 }
34
35 @Override
36 public String getAlgorithm() {
37 return "unknown";
38 }
39
40 @Override
41 public String getFormat() {
42
43 return null;
44 }
45
46 @Override
47 public byte[] getEncoded() {
48 return null;
49 }
50
51 private long privateKeyAddress() {
52 if (refCnt() <= 0) {
53 throw new IllegalReferenceCountException();
54 }
55 return privateKeyAddress;
56 }
57
58 @Override
59 protected void deallocate() {
60 SSL.freePrivateKey(privateKeyAddress);
61 privateKeyAddress = 0;
62 }
63
64 @Override
65 public OpenSslPrivateKey retain() {
66 super.retain();
67 return this;
68 }
69
70 @Override
71 public OpenSslPrivateKey retain(int increment) {
72 super.retain(increment);
73 return this;
74 }
75
76 @Override
77 public OpenSslPrivateKey touch() {
78 super.touch();
79 return this;
80 }
81
82 @Override
83 public OpenSslPrivateKey touch(Object hint) {
84 return this;
85 }
86
87
88
89
90
91
92
93
94 @Override
95 public void destroy() {
96 release(refCnt());
97 }
98
99
100
101
102
103
104
105
106 @Override
107 public boolean isDestroyed() {
108 return refCnt() == 0;
109 }
110
111
112
113
114
115
116
117 OpenSslKeyMaterial newKeyMaterial(long certificateChain, X509Certificate[] chain) {
118 return new OpenSslPrivateKeyMaterial(certificateChain, chain);
119 }
120
121
122 final class OpenSslPrivateKeyMaterial extends AbstractReferenceCounted implements OpenSslKeyMaterial {
123
124
125 long certificateChain;
126 private final X509Certificate[] x509CertificateChain;
127
128 OpenSslPrivateKeyMaterial(long certificateChain, X509Certificate[] x509CertificateChain) {
129 this.certificateChain = certificateChain;
130 this.x509CertificateChain = x509CertificateChain == null ?
131 EmptyArrays.EMPTY_X509_CERTIFICATES : x509CertificateChain;
132 OpenSslPrivateKey.this.retain();
133 }
134
135 @Override
136 public X509Certificate[] certificateChain() {
137 return x509CertificateChain.clone();
138 }
139
140 @Override
141 public long certificateChainAddress() {
142 if (refCnt() <= 0) {
143 throw new IllegalReferenceCountException();
144 }
145 return certificateChain;
146 }
147
148 @Override
149 public long privateKeyAddress() {
150 if (refCnt() <= 0) {
151 throw new IllegalReferenceCountException();
152 }
153 return OpenSslPrivateKey.this.privateKeyAddress();
154 }
155
156 @Override
157 public OpenSslKeyMaterial touch(Object hint) {
158 OpenSslPrivateKey.this.touch(hint);
159 return this;
160 }
161
162 @Override
163 public OpenSslKeyMaterial retain() {
164 super.retain();
165 return this;
166 }
167
168 @Override
169 public OpenSslKeyMaterial retain(int increment) {
170 super.retain(increment);
171 return this;
172 }
173
174 @Override
175 public OpenSslKeyMaterial touch() {
176 OpenSslPrivateKey.this.touch();
177 return this;
178 }
179
180 @Override
181 protected void deallocate() {
182 releaseChain();
183 OpenSslPrivateKey.this.release();
184 }
185
186 private void releaseChain() {
187 SSL.freeX509Chain(certificateChain);
188 certificateChain = 0;
189 }
190 }
191 }