dddeb2d34992d4fa1e764c1ca5e16e41289f4fca
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / certification / CertificationModelFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification;
22
23 import org.onap.oom.certservice.certification.configuration.Cmpv2ServerProvider;
24 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
25 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
26 import org.onap.oom.certservice.certification.exception.DecryptionException;
27 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
28 import org.onap.oom.certservice.certification.model.CertificationModel;
29 import org.onap.oom.certservice.certification.model.CsrModel;
30 import org.onap.oom.certservice.certification.model.X509CertificateModel;
31 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 public class CertificationModelFactory {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationModelFactory.class);
41
42     private final CsrModelFactory csrModelFactory;
43     private final Cmpv2ServerProvider cmpv2ServerProvider;
44     private final CertificationProvider certificationProvider;
45     private final X509CertificateModelFactory x509CertificateModelFactory;
46     private final UpdateRequestTypeDetector updateRequestTypeDetector;
47
48     @Autowired
49     CertificationModelFactory(
50             CsrModelFactory csrModelFactory,
51             Cmpv2ServerProvider cmpv2ServerProvider,
52             CertificationProvider certificationProvider,
53             X509CertificateModelFactory x509CertificateModelFactory,
54             UpdateRequestTypeDetector updateRequestTypeDetector) {
55         this.cmpv2ServerProvider = cmpv2ServerProvider;
56         this.csrModelFactory = csrModelFactory;
57         this.certificationProvider = certificationProvider;
58         this.x509CertificateModelFactory = x509CertificateModelFactory;
59         this.updateRequestTypeDetector = updateRequestTypeDetector;
60     }
61
62     public CertificationModel createCertificationModel(String encodedCsr, String encodedPrivateKey, String caName)
63             throws DecryptionException, CmpClientException {
64         CsrModel csrModel = csrModelFactory.createCsrModel(
65                 new StringBase64(encodedCsr),
66                 new StringBase64(encodedPrivateKey)
67         );
68         LOGGER.debug("Received CSR meta data: \n{}", csrModel);
69
70         Cmpv2Server cmpv2Server = cmpv2ServerProvider.getCmpv2Server(caName);
71         LOGGER.debug("Found server for given CA name: \n{}", cmpv2Server);
72
73         LOGGER.info("Sending sign request for certification model for CA named: {}, and certificate signing request:\n{}",
74                 caName, csrModel);
75         return certificationProvider.signCsr(csrModel, cmpv2Server);
76     }
77
78     public CertificationModel createCertificationModel(CertificateUpdateModel certificateUpdateModel)
79         throws DecryptionException, CmpClientException, CertificateDecryptionException {
80         LOGGER.info("CSR: " + certificateUpdateModel.getEncodedCsr() +
81                 ", old cert: " + certificateUpdateModel.getEncodedOldCert() +
82                 ", CA: " + certificateUpdateModel.getCaName());
83         final CsrModel csrModel = csrModelFactory.createCsrModel(
84             new StringBase64(certificateUpdateModel.getEncodedCsr()),
85             new StringBase64(certificateUpdateModel.getEncodedPrivateKey())
86         );
87         final X509CertificateModel certificateModel = x509CertificateModelFactory.createCertificateModel(
88             new StringBase64(certificateUpdateModel.getEncodedOldCert()));
89
90         Cmpv2Server cmpv2Server = cmpv2ServerProvider.getCmpv2Server(certificateUpdateModel.getCaName());
91         LOGGER.debug("Found server for given CA name: \n{}", cmpv2Server);
92         LOGGER.info("Sending update request for certification model for CA named: {}, and certificate update request:\n{}",
93             certificateUpdateModel.getCaName(), csrModel);
94
95         if (updateRequestTypeDetector.isKur(csrModel.getCertificateData(), certificateModel.getCertificateData())) {
96             LOGGER.info(
97                 "Certificate Signing Request and Old Certificate have the same parameters. Preparing Key Update Request");
98             return certificationProvider.updateCertificate(csrModel, cmpv2Server, certificateUpdateModel);
99         } else {
100             LOGGER.info(
101                 "Certificate Signing Request and Old Certificate have different parameters. Preparing Certification Request");
102             return certificationProvider.certificationRequest(csrModel, cmpv2Server);
103         }
104     }
105 }