View Javadoc
1   /*
2    * Copyright 2015 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 javax.security.cert.CertificateException;
19  import javax.security.cert.CertificateExpiredException;
20  import javax.security.cert.CertificateNotYetValidException;
21  import javax.security.cert.X509Certificate;
22  import java.math.BigInteger;
23  import java.security.InvalidKeyException;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.NoSuchProviderException;
26  import java.security.Principal;
27  import java.security.PublicKey;
28  import java.security.SignatureException;
29  import java.util.Date;
30  
31  final class OpenSslJavaxX509Certificate extends X509Certificate {
32      private final byte[] bytes;
33      private X509Certificate wrapped;
34  
35      public OpenSslJavaxX509Certificate(byte[] bytes) {
36          this.bytes = bytes;
37      }
38  
39      @Override
40      public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
41          unwrap().checkValidity();
42      }
43  
44      @Override
45      public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
46          unwrap().checkValidity(date);
47      }
48  
49      @Override
50      public int getVersion() {
51          return unwrap().getVersion();
52      }
53  
54      @Override
55      public BigInteger getSerialNumber() {
56          return unwrap().getSerialNumber();
57      }
58  
59      @Override
60      public Principal getIssuerDN() {
61          return unwrap().getIssuerDN();
62      }
63  
64      @Override
65      public Principal getSubjectDN() {
66          return unwrap().getSubjectDN();
67      }
68  
69      @Override
70      public Date getNotBefore() {
71          return unwrap().getNotBefore();
72      }
73  
74      @Override
75      public Date getNotAfter() {
76          return unwrap().getNotAfter();
77      }
78  
79      @Override
80      public String getSigAlgName() {
81          return unwrap().getSigAlgName();
82      }
83  
84      @Override
85      public String getSigAlgOID() {
86          return unwrap().getSigAlgOID();
87      }
88  
89      @Override
90      public byte[] getSigAlgParams() {
91          return unwrap().getSigAlgParams();
92      }
93  
94      @Override
95      public byte[] getEncoded() {
96          return bytes.clone();
97      }
98  
99      @Override
100     public void verify(PublicKey key)
101             throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException,
102                    SignatureException {
103         unwrap().verify(key);
104     }
105 
106     @Override
107     public void verify(PublicKey key, String sigProvider)
108             throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException,
109                    SignatureException {
110         unwrap().verify(key, sigProvider);
111     }
112 
113     @Override
114     public String toString() {
115         return unwrap().toString();
116     }
117 
118     @Override
119     public PublicKey getPublicKey() {
120         return unwrap().getPublicKey();
121     }
122 
123     private X509Certificate unwrap() {
124         X509Certificate wrapped = this.wrapped;
125         if (wrapped == null) {
126             try {
127                 wrapped = this.wrapped = X509Certificate.getInstance(bytes);
128             } catch (CertificateException e) {
129                 throw new IllegalStateException(e);
130             }
131         }
132         return wrapped;
133     }
134 }