23
23
package com .xebialabs .overthere .cifs .winrm ;
24
24
25
25
import java .io .BufferedReader ;
26
+ import java .io .File ;
27
+ import java .io .FileInputStream ;
28
+ import java .io .FileNotFoundException ;
26
29
import java .io .IOException ;
27
30
import java .io .InputStream ;
28
31
import java .io .InputStreamReader ;
35
38
import java .net .URL ;
36
39
import java .security .GeneralSecurityException ;
37
40
import java .security .KeyManagementException ;
41
+ import java .security .KeyStore ;
38
42
import java .security .KeyStoreException ;
39
43
import java .security .NoSuchAlgorithmException ;
40
44
import java .security .Principal ;
41
45
import java .security .PrivilegedActionException ;
42
46
import java .security .PrivilegedExceptionAction ;
43
47
import java .security .UnrecoverableKeyException ;
48
+ import java .security .cert .CertificateException ;
44
49
import java .util .Iterator ;
45
50
import java .util .List ;
46
51
import java .util .UUID ;
@@ -111,6 +116,7 @@ public class WinRmClient {
111
116
private String winRmTimeout ;
112
117
private int winRmEnvelopSize ;
113
118
private String winRmLocale ;
119
+ private boolean clientCertificate ;
114
120
private WinrmHttpsCertificateTrustStrategy httpsCertTrustStrategy ;
115
121
private WinrmHttpsHostnameVerificationStrategy httpsHostnameVerifyStrategy ;
116
122
private boolean kerberosUseHttpSpn ;
@@ -408,6 +414,12 @@ private Document doSendRequest(final Document requestDocument, final SoapAction
408
414
final HttpContext context = new BasicHttpContext ();
409
415
final HttpPost request = new HttpPost (targetURL .toURI ());
410
416
417
+ /* http://www.dmtf.org/sites/default/files/standards/documents/DSP0226_1.0.0.pdf
418
+ * Web Services for Management (WS-Management) Specification, v1.0.0, page 119, section C.3.5 */
419
+ if (clientCertificate ) {
420
+ request .setHeader ("Authorization" , "http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/https/mutual" );
421
+ }
422
+
411
423
if (soapAction != null ) {
412
424
request .setHeader ("SOAPAction" , soapAction .getValue ());
413
425
}
@@ -442,10 +454,12 @@ private Document doSendRequest(final Document requestDocument, final SoapAction
442
454
}
443
455
}
444
456
445
- private void configureHttpClient (final DefaultHttpClient httpclient ) throws GeneralSecurityException {
457
+ private void configureHttpClient (final DefaultHttpClient httpclient ) throws GeneralSecurityException , IOException {
446
458
configureTrust (httpclient );
447
459
448
- configureAuthentication (httpclient , BASIC , new BasicUserPrincipal (username ));
460
+ if (!clientCertificate ) {
461
+ configureAuthentication (httpclient , BASIC , new BasicUserPrincipal (username ));
462
+ }
449
463
450
464
if (enableKerberos ) {
451
465
String spnServiceClass = kerberosUseHttpSpn ? "HTTP" : "WSMAN" ;
@@ -463,15 +477,29 @@ private void configureHttpClient(final DefaultHttpClient httpclient) throws Gene
463
477
}
464
478
465
479
private void configureTrust (final DefaultHttpClient httpclient ) throws NoSuchAlgorithmException ,
466
- KeyManagementException , KeyStoreException , UnrecoverableKeyException {
480
+ KeyManagementException , KeyStoreException , UnrecoverableKeyException , CertificateException , IOException {
467
481
468
482
if (!"https" .equalsIgnoreCase (targetURL .getProtocol ())) {
469
483
return ;
470
484
}
471
485
472
486
final TrustStrategy trustStrategy = httpsCertTrustStrategy .getStrategy ();
473
487
final X509HostnameVerifier hostnameVerifier = httpsHostnameVerifyStrategy .getVerifier ();
474
- final SSLSocketFactory socketFactory = new SSLSocketFactory (trustStrategy , hostnameVerifier );
488
+ final SSLSocketFactory socketFactory ;
489
+
490
+ if (clientCertificate ) {
491
+ KeyStore clientStore = KeyStore .getInstance ("PKCS12" );
492
+ FileInputStream instream = new FileInputStream (new File (username ));
493
+ try {
494
+ clientStore .load (instream , password .toCharArray ());
495
+ } finally {
496
+ instream .close ();
497
+ }
498
+ socketFactory = new SSLSocketFactory (null , clientStore , password , null , null , trustStrategy , hostnameVerifier );
499
+ } else {
500
+ socketFactory = new SSLSocketFactory (trustStrategy , hostnameVerifier );
501
+ }
502
+
475
503
final Scheme sch = new Scheme ("https" , 443 , socketFactory );
476
504
httpclient .getConnectionManager ().getSchemeRegistry ().register (sch );
477
505
}
@@ -577,6 +605,10 @@ public void setWinRmLocale(String locale) {
577
605
this .winRmLocale = locale ;
578
606
}
579
607
608
+ public void setClientCertificate (boolean WinrmClientCertificate ) {
609
+ this .clientCertificate = WinrmClientCertificate ;
610
+ }
611
+
580
612
public void setHttpsCertTrustStrategy (WinrmHttpsCertificateTrustStrategy httpsCertTrustStrategy ) {
581
613
this .httpsCertTrustStrategy = httpsCertTrustStrategy ;
582
614
}
0 commit comments