1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl;
17
18 import io.netty.internal.tcnative.SSL;
19 import io.netty5.util.AbstractReferenceCounted;
20 import io.netty5.util.IllegalReferenceCountException;
21 import io.netty5.util.ResourceLeakDetector;
22 import io.netty5.util.ResourceLeakDetectorFactory;
23 import io.netty5.util.ResourceLeakTracker;
24
25 import java.security.cert.X509Certificate;
26
27 final class DefaultOpenSslKeyMaterial extends AbstractReferenceCounted implements OpenSslKeyMaterial {
28
29 private static final ResourceLeakDetector<DefaultOpenSslKeyMaterial> leakDetector =
30 ResourceLeakDetectorFactory.instance().newResourceLeakDetector(DefaultOpenSslKeyMaterial.class);
31 private final ResourceLeakTracker<DefaultOpenSslKeyMaterial> leak;
32 private final X509Certificate[] x509CertificateChain;
33 private long chain;
34 private long privateKey;
35
36 DefaultOpenSslKeyMaterial(long chain, long privateKey, X509Certificate[] x509CertificateChain) {
37 this.chain = chain;
38 this.privateKey = privateKey;
39 this.x509CertificateChain = x509CertificateChain;
40 leak = leakDetector.track(this);
41 }
42
43 @Override
44 public X509Certificate[] certificateChain() {
45 return x509CertificateChain.clone();
46 }
47
48 @Override
49 public long certificateChainAddress() {
50 if (refCnt() <= 0) {
51 throw new IllegalReferenceCountException();
52 }
53 return chain;
54 }
55
56 @Override
57 public long privateKeyAddress() {
58 if (refCnt() <= 0) {
59 throw new IllegalReferenceCountException();
60 }
61 return privateKey;
62 }
63
64 @Override
65 protected void deallocate() {
66 SSL.freeX509Chain(chain);
67 chain = 0;
68 SSL.freePrivateKey(privateKey);
69 privateKey = 0;
70 if (leak != null) {
71 boolean closed = leak.close(this);
72 assert closed;
73 }
74 }
75
76 @Override
77 public DefaultOpenSslKeyMaterial retain() {
78 if (leak != null) {
79 leak.record();
80 }
81 super.retain();
82 return this;
83 }
84
85 @Override
86 public DefaultOpenSslKeyMaterial retain(int increment) {
87 if (leak != null) {
88 leak.record();
89 }
90 super.retain(increment);
91 return this;
92 }
93
94 @Override
95 public DefaultOpenSslKeyMaterial touch() {
96 if (leak != null) {
97 leak.record();
98 }
99 super.touch();
100 return this;
101 }
102
103 @Override
104 public DefaultOpenSslKeyMaterial touch(Object hint) {
105 if (leak != null) {
106 leak.record(hint);
107 }
108 return this;
109 }
110
111 @Override
112 public boolean release() {
113 if (leak != null) {
114 leak.record();
115 }
116 return super.release();
117 }
118
119 @Override
120 public boolean release(int decrement) {
121 if (leak != null) {
122 leak.record();
123 }
124 return super.release(decrement);
125 }
126 }