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    *   http://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      public 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 new PemValue(content.copy(), sensitive);
60      }
61  
62      @Override
63      public PemValue duplicate() {
64          return new PemValue(content.duplicate(), sensitive);
65      }
66  
67      @Override
68      public PemValue retain() {
69          return (PemValue) super.retain();
70      }
71  
72      @Override
73      public PemValue retain(int increment) {
74          return (PemValue) super.retain(increment);
75      }
76  
77      @Override
78      protected void deallocate() {
79          if (sensitive) {
80              SslUtils.zeroout(content);
81          }
82          content.release();
83      }
84  }