View Javadoc

1   /*
2    * Copyright 2014 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 java.io.ByteArrayInputStream;
19  import java.math.BigInteger;
20  import java.security.InvalidKeyException;
21  import java.security.NoSuchAlgorithmException;
22  import java.security.NoSuchProviderException;
23  import java.security.Principal;
24  import java.security.PublicKey;
25  import java.security.SignatureException;
26  import java.security.cert.CertificateEncodingException;
27  import java.security.cert.CertificateException;
28  import java.security.cert.CertificateExpiredException;
29  import java.security.cert.CertificateNotYetValidException;
30  import java.security.cert.X509Certificate;
31  import java.util.Date;
32  import java.util.Set;
33  
34  final class OpenSslX509Certificate extends X509Certificate {
35  
36      private final byte[] bytes;
37      private X509Certificate wrapped;
38  
39      public OpenSslX509Certificate(byte[] bytes) {
40          this.bytes = bytes;
41      }
42  
43      @Override
44      public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
45          unwrap().checkValidity();
46      }
47  
48      @Override
49      public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
50          unwrap().checkValidity(date);
51      }
52  
53      @Override
54      public int getVersion() {
55          return unwrap().getVersion();
56      }
57  
58      @Override
59      public BigInteger getSerialNumber() {
60          return unwrap().getSerialNumber();
61      }
62  
63      @Override
64      public Principal getIssuerDN() {
65          return unwrap().getIssuerDN();
66      }
67  
68      @Override
69      public Principal getSubjectDN() {
70          return unwrap().getSubjectDN();
71      }
72  
73      @Override
74      public Date getNotBefore() {
75          return unwrap().getNotBefore();
76      }
77  
78      @Override
79      public Date getNotAfter() {
80          return unwrap().getNotAfter();
81      }
82  
83      @Override
84      public byte[] getTBSCertificate() throws CertificateEncodingException {
85          return unwrap().getTBSCertificate();
86      }
87  
88      @Override
89      public byte[] getSignature() {
90          return unwrap().getSignature();
91      }
92  
93      @Override
94      public String getSigAlgName() {
95          return unwrap().getSigAlgName();
96      }
97  
98      @Override
99      public String getSigAlgOID() {
100         return unwrap().getSigAlgOID();
101     }
102 
103     @Override
104     public byte[] getSigAlgParams() {
105         return unwrap().getSigAlgParams();
106     }
107 
108     @Override
109     public boolean[] getIssuerUniqueID() {
110         return unwrap().getIssuerUniqueID();
111     }
112 
113     @Override
114     public boolean[] getSubjectUniqueID() {
115         return unwrap().getSubjectUniqueID();
116     }
117 
118     @Override
119     public boolean[] getKeyUsage() {
120         return unwrap().getKeyUsage();
121     }
122 
123     @Override
124     public int getBasicConstraints() {
125         return unwrap().getBasicConstraints();
126     }
127 
128     @Override
129     public byte[] getEncoded() {
130         return bytes.clone();
131     }
132 
133     @Override
134     public void verify(PublicKey key)
135             throws CertificateException, NoSuchAlgorithmException,
136             InvalidKeyException, NoSuchProviderException, SignatureException {
137         unwrap().verify(key);
138     }
139 
140     @Override
141     public void verify(PublicKey key, String sigProvider)
142             throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
143             NoSuchProviderException, SignatureException {
144         unwrap().verify(key, sigProvider);
145     }
146 
147     @Override
148     public String toString() {
149         return unwrap().toString();
150     }
151 
152     @Override
153     public PublicKey getPublicKey() {
154         return unwrap().getPublicKey();
155     }
156 
157     @Override
158     public boolean hasUnsupportedCriticalExtension() {
159         return unwrap().hasUnsupportedCriticalExtension();
160     }
161 
162     @Override
163     public Set<String> getCriticalExtensionOIDs() {
164         return unwrap().getCriticalExtensionOIDs();
165     }
166 
167     @Override
168     public Set<String> getNonCriticalExtensionOIDs() {
169         return unwrap().getNonCriticalExtensionOIDs();
170     }
171 
172     @Override
173     public byte[] getExtensionValue(String oid) {
174         return unwrap().getExtensionValue(oid);
175     }
176 
177     private X509Certificate unwrap() {
178         X509Certificate wrapped = this.wrapped;
179         if (wrapped == null) {
180             try {
181                 wrapped = this.wrapped = (X509Certificate) SslContext.X509_CERT_FACTORY.generateCertificate(
182                         new ByteArrayInputStream(bytes));
183             } catch (CertificateException e) {
184                 throw new IllegalStateException(e);
185             }
186         }
187         return wrapped;
188     }
189 }