View Javadoc
1   /*
2    * Copyright 2016 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.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.util.AbstractReferenceCounted;
21  import io.netty.util.IllegalReferenceCountException;
22  import io.netty.util.internal.ObjectUtil;
23  
24  /**
25   * A PEM encoded value.
26   *
27   * @see PemEncoded
28   * @see PemPrivateKey#toPEM(ByteBufAllocator, boolean, java.security.PrivateKey)
29   * @see PemX509Certificate#toPEM(ByteBufAllocator, boolean, java.security.cert.X509Certificate[])
30   */
31  class PemValue extends AbstractReferenceCounted implements PemEncoded {
32  
33      private final ByteBuf content;
34  
35      private final boolean sensitive;
36  
37      PemValue(ByteBuf content, boolean sensitive) {
38          this.content = ObjectUtil.checkNotNull(content, "content");
39          this.sensitive = sensitive;
40      }
41  
42      @Override
43      public boolean isSensitive() {
44          return sensitive;
45      }
46  
47      @Override
48      public ByteBuf content() {
49          int count = refCnt();
50          if (count <= 0) {
51              throw new IllegalReferenceCountException(count);
52          }
53  
54          return content;
55      }
56  
57      @Override
58      public PemValue copy() {
59          return replace(content.copy());
60      }
61  
62      @Override
63      public PemValue duplicate() {
64          return replace(content.duplicate());
65      }
66  
67      @Override
68      public PemValue retainedDuplicate() {
69          return replace(content.retainedDuplicate());
70      }
71  
72      @Override
73      public PemValue replace(ByteBuf content) {
74          return new PemValue(content, sensitive);
75      }
76  
77      @Override
78      public PemValue touch() {
79          return (PemValue) super.touch();
80      }
81  
82      @Override
83      public PemValue touch(Object hint) {
84          content.touch(hint);
85          return this;
86      }
87  
88      @Override
89      public PemValue retain() {
90          return (PemValue) super.retain();
91      }
92  
93      @Override
94      public PemValue retain(int increment) {
95          return (PemValue) super.retain(increment);
96      }
97  
98      @Override
99      protected void deallocate() {
100         if (sensitive) {
101             SslUtils.zeroout(content);
102         }
103         content.release();
104     }
105 }