View Javadoc
1   /*
2    * Copyright 2026 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.util.AbstractReferenceCounted;
19  import io.netty.util.IllegalReferenceCountException;
20  
21  /**
22   * A non-owning wrapper for an {@link OpenSslCredential} pointer.
23   *
24   * <p>This class is used when we need to expose an SSL_CREDENTIAL pointer that is managed
25   * by OpenSSL itself (e.g., the credential selected during the handshake). Unlike
26   * {@link DefaultOpenSslCredential}, this wrapper does not free the underlying credential
27   * when its reference count reaches zero, as the lifetime is managed externally.
28   *
29   * <p>This is a BoringSSL-specific feature.
30   */
31  final class NonOwnedOpenSslCredential extends AbstractReferenceCounted implements OpenSslCredentialPointer {
32  
33      private final long credential;
34      private final CredentialType type;
35      private volatile boolean released;
36  
37      /**
38       * Creates a new non-owning credential wrapper.
39       *
40       * @param credential the native SSL_CREDENTIAL pointer (must not be 0)
41       * @param type the credential type
42       */
43      NonOwnedOpenSslCredential(long credential, CredentialType type) {
44          if (credential == 0) {
45              throw new IllegalArgumentException("credential pointer must not be 0");
46          }
47          this.credential = credential;
48          this.type = type;
49      }
50  
51      @Override
52      public long credentialAddress() {
53          if (released) {
54              throw new IllegalReferenceCountException();
55          }
56          return credential;
57      }
58  
59      @Override
60      public CredentialType type() {
61          return type;
62      }
63  
64      @Override
65      public OpenSslCredential retain() {
66          return (OpenSslCredential) super.retain();
67      }
68  
69      @Override
70      public OpenSslCredential retain(int increment) {
71          return (OpenSslCredential) super.retain(increment);
72      }
73  
74      @Override
75      public OpenSslCredential touch() {
76          return (OpenSslCredential) super.touch();
77      }
78  
79      @Override
80      public OpenSslCredential touch(Object hint) {
81          return this;
82      }
83  
84      @Override
85      protected void deallocate() {
86          released = true;
87      }
88  }