View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.security.x509;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.File;
21  import java.io.IOException;
22  import java.security.GeneralSecurityException;
23  import java.security.PrivateKey;
24  import java.security.cert.CRLException;
25  import java.security.cert.CertificateException;
26  import java.security.cert.CertificateFactory;
27  import java.security.cert.CertificateParsingException;
28  import java.security.cert.X509CRL;
29  import java.security.cert.X509Certificate;
30  import java.util.Collection;
31  import java.util.LinkedList;
32  import java.util.List;
33  
34  import javax.security.auth.x500.X500Principal;
35  
36  import org.apache.commons.ssl.TrustMaterial;
37  import org.bouncycastle.asn1.ASN1InputStream;
38  import org.bouncycastle.asn1.DERObject;
39  import org.bouncycastle.asn1.DERObjectIdentifier;
40  import org.bouncycastle.asn1.DERSequence;
41  import org.bouncycastle.asn1.DERSet;
42  import org.bouncycastle.asn1.DERString;
43  import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
44  import org.bouncycastle.asn1.x509.X509Extensions;
45  import org.bouncycastle.x509.extension.SubjectKeyIdentifierStructure;
46  import org.bouncycastle.x509.extension.X509ExtensionUtil;
47  import org.opensaml.xml.security.SecurityException;
48  import org.opensaml.xml.security.SecurityHelper;
49  import org.opensaml.xml.util.DatatypeHelper;
50  import org.opensaml.xml.util.IPAddressHelper;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Utility class for working with X509 objects.
56   */
57  public class X509Util {
58  
59      /** Encoding used to store a key or certificate in a file. */
60      public static enum ENCODING_FORMAT {
61          PEM, DER
62      };
63  
64      /** Common Name (CN) OID. */
65      public static final String CN_OID = "2.5.4.3";
66  
67      /** RFC 2459 Other Subject Alt Name type. */
68      public static final Integer OTHER_ALT_NAME = new Integer(0);
69  
70      /** RFC 2459 RFC 822 (email address) Subject Alt Name type. */
71      public static final Integer RFC822_ALT_NAME = new Integer(1);
72  
73      /** RFC 2459 DNS Subject Alt Name type. */
74      public static final Integer DNS_ALT_NAME = new Integer(2);
75  
76      /** RFC 2459 X.400 Address Subject Alt Name type. */
77      public static final Integer X400ADDRESS_ALT_NAME = new Integer(3);
78  
79      /** RFC 2459 Directory Name Subject Alt Name type. */
80      public static final Integer DIRECTORY_ALT_NAME = new Integer(4);
81  
82      /** RFC 2459 EDI Party Name Subject Alt Name type. */
83      public static final Integer EDI_PARTY_ALT_NAME = new Integer(5);
84  
85      /** RFC 2459 URI Subject Alt Name type. */
86      public static final Integer URI_ALT_NAME = new Integer(6);
87  
88      /** RFC 2459 IP Address Subject Alt Name type. */
89      public static final Integer IP_ADDRESS_ALT_NAME = new Integer(7);
90  
91      /** RFC 2459 Registered ID Subject Alt Name type. */
92      public static final Integer REGISTERED_ID_ALT_NAME = new Integer(8);
93  
94      /** Class logger. */
95      private static Logger log = LoggerFactory.getLogger(X509Util.class);
96  
97      /** Constructed. */
98      protected X509Util() {
99  
100     }
101     
102     /**
103      * Determines the certificate, from the collection, associated with the private key.
104      * 
105      * @param certs certificates to check
106      * @param privateKey entity's private key
107      * 
108      * @return the certificate associated with entity's private key or null if not certificate in the collection is
109      *         associated with the given private key
110      * 
111      * @throws SecurityException thrown if the public or private keys checked are of an unsupported type
112      * 
113      * @since 1.2
114      */
115     public static X509Certificate determineEntityCertificate(Collection<X509Certificate> certs, PrivateKey privateKey)
116             throws SecurityException {
117         if (certs == null || privateKey == null) {
118             return null;
119         }
120 
121         for (X509Certificate certificate : certs) {
122             if (SecurityHelper.matchKeyPair(certificate.getPublicKey(), privateKey)) {
123                 return certificate;
124             }
125         }
126 
127         return null;
128     }
129 
130     /**
131      * Gets the commons names that appear within the given distinguished name. The returned list provides the names in
132      * the order they appeared in the DN.
133      * 
134      * @param dn the DN to extract the common names from
135      * 
136      * @return the common names that appear in the DN in the order they appear or null if the given DN is null
137      */
138     public static List<String> getCommonNames(X500Principal dn) {
139         if (dn == null) {
140             return null;
141         }
142 
143         log.debug("Extracting CNs from the following DN: {}", dn.toString());
144         List<String> commonNames = new LinkedList<String>();
145         try {
146             ASN1InputStream asn1Stream = new ASN1InputStream(dn.getEncoded());
147             DERObject parent = asn1Stream.readObject();
148 
149             String cn = null;
150             DERObject dnComponent;
151             DERSequence grandChild;
152             DERObjectIdentifier componentId;
153             for (int i = 0; i < ((DERSequence) parent).size(); i++) {
154                 dnComponent = ((DERSequence) parent).getObjectAt(i).getDERObject();
155                 if (!(dnComponent instanceof DERSet)) {
156                     log.debug("No DN components.");
157                     continue;
158                 }
159 
160                 // Each DN component is a set
161                 for (int j = 0; j < ((DERSet) dnComponent).size(); j++) {
162                     grandChild = (DERSequence) ((DERSet) dnComponent).getObjectAt(j).getDERObject();
163 
164                     if (grandChild.getObjectAt(0) != null
165                             && grandChild.getObjectAt(0).getDERObject() instanceof DERObjectIdentifier) {
166                         componentId = (DERObjectIdentifier) grandChild.getObjectAt(0).getDERObject();
167 
168                         if (CN_OID.equals(componentId.getId())) {
169                             // OK, this dn component is actually a cn attribute
170                             if (grandChild.getObjectAt(1) != null
171                                     && grandChild.getObjectAt(1).getDERObject() instanceof DERString) {
172                                 cn = ((DERString) grandChild.getObjectAt(1).getDERObject()).getString();
173                                 commonNames.add(cn);
174                             }
175                         }
176                     }
177                 }
178             }
179 
180             asn1Stream.close();
181 
182             return commonNames;
183 
184         } catch (IOException e) {
185             log.error("Unable to extract common names from DN: ASN.1 parsing failed: " + e);
186             return null;
187         }
188     }
189 
190     /**
191      * Gets the list of alternative names of a given name type.
192      * 
193      * @param certificate the certificate to extract the alternative names from
194      * @param nameTypes the name types
195      * 
196      * @return the alt names, of the given type, within the cert
197      */
198     public static List getAltNames(X509Certificate certificate, Integer[] nameTypes) {
199         if (certificate == null) {
200             return null;
201         }
202 
203         List<Object> names = new LinkedList<Object>();
204         Collection<List<?>> altNames = null;
205         try {
206             altNames = X509ExtensionUtil.getSubjectAlternativeNames(certificate);
207         } catch (CertificateParsingException e) {
208             log.error("Encountered an problem trying to extract Subject Alternate "
209                     + "Name from supplied certificate: " + e);
210             return names;
211         }
212 
213         if (altNames != null) {
214             // 0th position represents the alt name type
215             // 1st position contains the alt name data
216             for (List altName : altNames) {
217                 for (Integer nameType : nameTypes) {
218                     if (altName.get(0).equals(nameType)) {
219                         names.add(convertAltNameType(nameType, altName.get(1)));
220                         break;
221                     }
222                 }
223             }
224         }
225 
226         return names;
227     }
228 
229     /**
230      * Gets the common name components of the issuer and all the subject alt names of a given type.
231      * 
232      * @param certificate certificate to extract names from
233      * @param altNameTypes type of alt names to extract
234      * 
235      * @return list of subject names in the certificate
236      */
237     @SuppressWarnings("unchecked")
238     public static List getSubjectNames(X509Certificate certificate, Integer[] altNameTypes) {
239         List issuerNames = new LinkedList();
240 
241         List<String> entityCertCNs = X509Util.getCommonNames(certificate.getSubjectX500Principal());
242         issuerNames.add(entityCertCNs.get(0));
243         issuerNames.addAll(X509Util.getAltNames(certificate, altNameTypes));
244 
245         return issuerNames;
246     }
247 
248     /**
249      * Get the plain (non-DER encoded) value of the Subject Key Identifier extension of an X.509 certificate, if
250      * present.
251      * 
252      * @param certificate an X.509 certificate possibly containing a subject key identifier
253      * @return the plain (non-DER encoded) value of the Subject Key Identifier extension, or null if the certificate
254      *         does not contain the extension
255      * @throws IOException
256      */
257     public static byte[] getSubjectKeyIdentifier(X509Certificate certificate) {
258         byte[] derValue = certificate.getExtensionValue(X509Extensions.SubjectKeyIdentifier.getId());
259         if (derValue == null || derValue.length == 0) {
260             return null;
261         }
262 
263         SubjectKeyIdentifier ski = null;
264         try {
265             ski = new SubjectKeyIdentifierStructure(derValue);
266         } catch (IOException e) {
267             log.error("Unable to extract subject key identifier from certificate: ASN.1 parsing failed: " + e);
268             return null;
269         }
270 
271         if (ski != null) {
272             return ski.getKeyIdentifier();
273         } else {
274             return null;
275         }
276     }
277     
278     /**
279      * Decodes X.509 certificates in DER or PEM format.
280      * 
281      * @param certs encoded certs
282      * 
283      * @return decoded certs
284      * 
285      * @throws CertificateException thrown if the certificates can not be decoded
286      * 
287      * @since 1.2
288      */
289     public static Collection<X509Certificate> decodeCertificate(File certs) throws CertificateException{
290         if(!certs.exists()){
291             throw new CertificateException("Certificate file " + certs.getAbsolutePath() + " does not exist");
292         }
293         
294         if(!certs.canRead()){
295             throw new CertificateException("Certificate file " + certs.getAbsolutePath() + " is not readable");
296         }
297         
298         try{
299             return decodeCertificate(DatatypeHelper.fileToByteArray(certs));
300         }catch(IOException e){
301             throw new CertificateException("Error reading certificate file " + certs.getAbsolutePath(), e);
302         }
303     }
304 
305     /**
306      * Decodes X.509 certificates in DER or PEM format.
307      * 
308      * @param certs encoded certs
309      * 
310      * @return decoded certs
311      * 
312      * @throws CertificateException thrown if the certificates can not be decoded
313      */
314     @SuppressWarnings("unchecked")
315     public static Collection<X509Certificate> decodeCertificate(byte[] certs) throws CertificateException {
316         try {
317             TrustMaterial tm = new TrustMaterial(certs);
318             return tm.getCertificates();
319         } catch (Exception e) {
320             throw new CertificateException("Unable to decode X.509 certificates", e);
321         }
322     }
323     
324     /**
325      * Decodes CRLS in DER or PKCS#7 format. If in PKCS#7 format only the CRLs are decode, the rest of the content is
326      * ignored.
327      * 
328      * @param crls encoded CRLs
329      * 
330      * @return decoded CRLs
331      * 
332      * @throws CRLException thrown if the CRLs can not be decoded
333      * 
334      * @since 1.2
335      */
336     public static Collection<X509CRL> decodeCRLs(File crls) throws CRLException{
337         if(!crls.exists()){
338             throw new CRLException("CRL file " + crls.getAbsolutePath() + " does not exist");
339         }
340         
341         if(!crls.canRead()){
342             throw new CRLException("CRL file " + crls.getAbsolutePath() + " is not readable");
343         }
344         
345         try{
346             return decodeCRLs(DatatypeHelper.fileToByteArray(crls));
347         }catch(IOException e){
348             throw new CRLException("Error reading CRL file " + crls.getAbsolutePath(), e);
349         }
350     }
351 
352     /**
353      * Decodes CRLS in DER or PKCS#7 format. If in PKCS#7 format only the CRLs are decode, the rest of the content is
354      * ignored.
355      * 
356      * @param crls encoded CRLs
357      * 
358      * @return decoded CRLs
359      * 
360      * @throws CRLException thrown if the CRLs can not be decoded
361      */
362     @SuppressWarnings("unchecked")
363     public static Collection<X509CRL> decodeCRLs(byte[] crls) throws CRLException {
364         try {
365             CertificateFactory cf = CertificateFactory.getInstance("X.509");
366             return (Collection<X509CRL>) cf.generateCRLs(new ByteArrayInputStream(crls));
367         } catch (GeneralSecurityException e) {
368             throw new CRLException("Unable to decode X.509 certificates");
369         }
370     }
371 
372     /**
373      * Gets a formatted string representing identifier information from the supplied credential.
374      * 
375      * <p>
376      * This could for example be used in logging messages.
377      * </p>
378      * 
379      * <p>
380      * Often it will be the case that a given credential that is being evaluated will NOT have a value for the entity ID
381      * property. So extract the certificate subject DN, and if present, the credential's entity ID.
382      * </p>
383      * 
384      * @param credential the credential for which to produce a token.
385      * @param handler the X.500 DN handler to use. If null, a new instance of {@link InternalX500DNHandler} will be
386      *            used.
387      * 
388      * @return a formatted string containing identifier information present in the credential
389      */
390     public static String getIdentifiersToken(X509Credential credential, X500DNHandler handler) {
391         X500DNHandler x500DNHandler;
392         if (handler != null) {
393             x500DNHandler = handler;
394         } else {
395             x500DNHandler = new InternalX500DNHandler();
396         }
397         X500Principal x500Principal = credential.getEntityCertificate().getSubjectX500Principal();
398         StringBuilder builder = new StringBuilder();
399         builder.append('[');
400         builder.append(String.format("subjectName='%s'", x500DNHandler.getName(x500Principal)));
401         if (!DatatypeHelper.isEmpty(credential.getEntityId())) {
402             builder.append(String.format(" |credential entityID='%s'", DatatypeHelper.safeTrimOrNullString(credential
403                     .getEntityId())));
404         }
405         builder.append(']');
406         return builder.toString();
407     }
408 
409     /**
410      * Convert types returned by Bouncy Castle X509ExtensionUtil.getSubjectAlternativeNames(X509Certificate) to be
411      * consistent with what is documented for: java.security.cert.X509Certificate#getSubjectAlternativeNames.
412      * 
413      * @param nameType the alt name type
414      * @param nameValue the alt name value
415      * @return converted representation of name value, based on type
416      */
417     private static Object convertAltNameType(Integer nameType, Object nameValue) {
418         if (DIRECTORY_ALT_NAME.equals(nameType) || DNS_ALT_NAME.equals(nameType) || RFC822_ALT_NAME.equals(nameType)
419                 || URI_ALT_NAME.equals(nameType) || REGISTERED_ID_ALT_NAME.equals(nameType)) {
420 
421             // these are just strings in the appropriate format already, return as-is
422             return nameValue;
423         }
424 
425         if (IP_ADDRESS_ALT_NAME.equals(nameType)) {
426             // this is a byte[], IP addr in network byte order
427             return IPAddressHelper.addressToString((byte[]) nameValue);
428         }
429 
430         if (EDI_PARTY_ALT_NAME.equals(nameType) || X400ADDRESS_ALT_NAME.equals(nameType)
431                 || OTHER_ALT_NAME.equals(nameType)) {
432 
433             // these have no defined representation, just return a DER-encoded byte[]
434             return ((DERObject) nameValue).getDEREncoded();
435         }
436 
437         log.warn("Encountered unknown alt name type '{}', adding as-is", nameType);
438         return nameValue;
439     }
440 }