1616// under the License.
1717package org .apache .cloudstack .network .lb ;
1818
19+ import java .io .IOException ;
20+ import java .io .StringReader ;
21+ import java .io .UnsupportedEncodingException ;
22+ import java .net .URLDecoder ;
23+ import java .security .InvalidAlgorithmParameterException ;
24+ import java .security .InvalidKeyException ;
25+ import java .security .KeyPair ;
26+ import java .security .MessageDigest ;
27+ import java .security .NoSuchAlgorithmException ;
28+ import java .security .NoSuchProviderException ;
29+ import java .security .Principal ;
30+ import java .security .PrivateKey ;
31+ import java .security .PublicKey ;
32+ import java .security .SecureRandom ;
33+ import java .security .Security ;
34+ import java .security .cert .CertPathBuilder ;
35+ import java .security .cert .CertPathBuilderException ;
36+ import java .security .cert .CertStore ;
37+ import java .security .cert .Certificate ;
38+ import java .security .cert .CertificateEncodingException ;
39+ import java .security .cert .CollectionCertStoreParameters ;
40+ import java .security .cert .PKIXBuilderParameters ;
41+ import java .security .cert .TrustAnchor ;
42+ import java .security .cert .X509CertSelector ;
43+ import java .security .cert .X509Certificate ;
44+ import java .util .ArrayList ;
45+ import java .util .HashSet ;
46+ import java .util .List ;
47+ import java .util .Set ;
48+
49+ import javax .crypto .BadPaddingException ;
50+ import javax .crypto .Cipher ;
51+ import javax .crypto .IllegalBlockSizeException ;
52+ import javax .crypto .NoSuchPaddingException ;
53+ import javax .ejb .Local ;
54+ import javax .inject .Inject ;
55+
1956import com .cloud .event .ActionEvent ;
2057import com .cloud .event .EventTypes ;
21- import com .cloud .network .dao .*;
58+ import com .cloud .network .dao .LoadBalancerCertMapDao ;
59+ import com .cloud .network .dao .LoadBalancerCertMapVO ;
60+ import com .cloud .network .dao .LoadBalancerVO ;
61+ import com .cloud .network .dao .SslCertDao ;
62+ import com .cloud .network .dao .SslCertVO ;
2263import com .cloud .network .lb .CertService ;
2364import com .cloud .network .rules .LoadBalancer ;
2465import com .cloud .user .dao .AccountDao ;
2566import com .cloud .utils .db .EntityManager ;
2667import com .cloud .utils .exception .CloudRuntimeException ;
68+ import org .apache .commons .io .IOUtils ;
69+ import org .apache .log4j .Logger ;
70+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
71+ import org .bouncycastle .openssl .PEMReader ;
72+ import org .bouncycastle .openssl .PasswordFinder ;
2773
2874import org .apache .cloudstack .api .command .user .loadbalancer .DeleteSslCertCmd ;
2975import org .apache .cloudstack .api .command .user .loadbalancer .ListSslCertsCmd ;
3783
3884import org .apache .cloudstack .acl .SecurityChecker ;
3985import org .apache .cloudstack .context .CallContext ;
40- import org .apache .commons .io .IOUtils ;
41- import org .apache .log4j .Logger ;
42- import org .bouncycastle .jce .provider .BouncyCastleProvider ;
43- import org .bouncycastle .openssl .PEMReader ;
44- import org .bouncycastle .openssl .PasswordFinder ;
45-
46- import javax .crypto .BadPaddingException ;
47- import javax .crypto .Cipher ;
48- import javax .crypto .IllegalBlockSizeException ;
49- import javax .crypto .NoSuchPaddingException ;
50- import javax .ejb .Local ;
51- import javax .inject .Inject ;
52-
53- import java .io .IOException ;
54- import java .io .StringReader ;
55- import java .io .UnsupportedEncodingException ;
56- import java .net .URLDecoder ;
57- import java .security .*;
58- import java .security .cert .*;
59- import java .security .cert .Certificate ;
60- import java .util .ArrayList ;
61- import java .util .HashSet ;
62- import java .util .List ;
63- import java .util .Set ;
6486
6587@ Local (value = {CertService .class })
6688public class CertServiceImpl implements CertService {
6789
6890 private static final Logger s_logger = Logger .getLogger (CertServiceImpl .class );
6991
7092 @ Inject AccountManager _accountMgr ;
71- @ Inject AccountDao _accountDao ;
72- @ Inject SslCertDao _sslCertDao ;
73- @ Inject LoadBalancerCertMapDao _lbCertDao ;
74- @ Inject EntityManager _entityMgr ;
93+ @ Inject
94+ AccountDao _accountDao ;
95+ @ Inject
96+ SslCertDao _sslCertDao ;
97+ @ Inject
98+ LoadBalancerCertMapDao _lbCertDao ;
99+ @ Inject
100+ EntityManager _entityMgr ;
75101
76102
77103
@@ -92,6 +118,7 @@ public SslCertResponse uploadSslCert(UploadSslCertCmd certCmd) {
92118
93119 validate (cert , key , password , chain );
94120 s_logger .debug ("Certificate Validation succeeded" );
121+
95122 String fingerPrint = generateFingerPrint (parseCertificate (cert ));
96123
97124 Long accountId = CallContext .current ().getCallingAccount ().getId ();
@@ -365,14 +392,17 @@ private void validateChain(List<Certificate> chain, Certificate cert) {
365392 params = new PKIXBuilderParameters (anchors , target );
366393 params .setRevocationEnabled (false );
367394 params .addCertStore (CertStore .getInstance ("Collection" , new CollectionCertStoreParameters (certs )));
368- CertPathBuilder builder = CertPathBuilder .getInstance ("PKIX" );
395+ CertPathBuilder builder = CertPathBuilder .getInstance ("PKIX" , "BC" );
369396 builder .build (params );
397+
370398 } catch (InvalidAlgorithmParameterException e ) {
371399 throw new IllegalArgumentException ("Invalid certificate chain" , e );
372400 } catch (CertPathBuilderException e ) {
373401 throw new IllegalArgumentException ("Invalid certificate chain" , e );
374402 } catch (NoSuchAlgorithmException e ) {
375403 throw new IllegalArgumentException ("Invalid certificate chain" , e );
404+ } catch (NoSuchProviderException e ) {
405+ throw new CloudRuntimeException ("No provider for certificate validation" , e );
376406 }
377407
378408 }
@@ -485,4 +515,4 @@ public boolean getPasswordRequested(){
485515 return password_requested ;
486516 }
487517 }
488- }
518+ }
0 commit comments