1 package org.apache.maven.continuum.project.builder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.cert.CertificateException;
29 import java.security.cert.X509Certificate;
30 import javax.net.ssl.TrustManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import javax.net.ssl.X509TrustManager;
33
34
35
36
37
38
39 public class EasyX509TrustManager
40 implements X509TrustManager
41 {
42 private static final Logger log = LoggerFactory.getLogger( EasyX509TrustManager.class );
43
44 private X509TrustManager standardTrustManager = null;
45
46
47
48
49 public EasyX509TrustManager( KeyStore keystore )
50 throws NoSuchAlgorithmException, KeyStoreException
51 {
52 super();
53 TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
54 factory.init( keystore );
55 TrustManager[] trustmanagers = factory.getTrustManagers();
56 if ( trustmanagers.length == 0 )
57 {
58 throw new NoSuchAlgorithmException( "no trust manager found" );
59 }
60 this.standardTrustManager = (X509TrustManager) trustmanagers[0];
61 }
62
63
64
65
66 public void checkClientTrusted( X509Certificate[] certificates, String authType )
67 throws CertificateException
68 {
69 standardTrustManager.checkClientTrusted( certificates, authType );
70 }
71
72
73
74
75 public void checkServerTrusted( X509Certificate[] certificates, String authType )
76 throws CertificateException
77 {
78 if ( ( certificates != null ) && log.isDebugEnabled() )
79 {
80 log.debug( "Server certificate chain:" );
81 for ( int i = 0; i < certificates.length; i++ )
82 {
83 log.debug( "X509Certificate[" + i + "]=" + certificates[i] );
84 }
85 }
86 if ( ( certificates != null ) && ( certificates.length == 1 ) )
87 {
88 certificates[0].checkValidity();
89 }
90 else
91 {
92 standardTrustManager.checkServerTrusted( certificates, authType );
93 }
94 }
95
96
97
98
99 public X509Certificate[] getAcceptedIssuers()
100 {
101 return this.standardTrustManager.getAcceptedIssuers();
102 }
103
104 }