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