View Javadoc
1   /*
2    * Copyright 2018 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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.ResourceLeakDetector;
22  import io.netty.util.ResourceLeakDetectorFactory;
23  import io.netty.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 }