2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aaf.certservice.certification;
23 import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator;
24 import org.bouncycastle.util.io.pem.PemObjectGenerator;
25 import org.bouncycastle.util.io.pem.PemWriter;
26 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
27 import org.onap.aaf.certservice.certification.model.CertificationModel;
28 import org.onap.aaf.certservice.certification.model.CsrModel;
29 import org.onap.aaf.certservice.cmpv2client.api.CmpClient;
30 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
36 import java.io.IOException;
37 import java.io.StringWriter;
38 import java.security.cert.X509Certificate;
39 import java.util.List;
40 import java.util.stream.Collectors;
43 public class CertificationProvider {
45 private static final Logger LOGGER = LoggerFactory.getLogger(CertificationProvider.class);
47 private final CmpClient cmpClient;
50 public CertificationProvider(CmpClient cmpClient) {
51 this.cmpClient = cmpClient;
54 public CertificationModel signCsr(CsrModel csrModel, Cmpv2Server server)
55 throws CmpClientException {
56 List<List<X509Certificate>> certificates = cmpClient.createCertificate(csrModel, server);
57 return new CertificationModel(convertFromX509CertificateListToPemList(certificates.get(0)),
58 convertFromX509CertificateListToPemList(certificates.get(1)));
61 private static List<String> convertFromX509CertificateListToPemList(List<X509Certificate> certificates) {
62 return certificates.stream().map(CertificationProvider::convertFromX509CertificateToPem).filter(cert -> !cert.isEmpty())
63 .collect(Collectors.toList());
66 private static String convertFromX509CertificateToPem(X509Certificate certificate) {
67 StringWriter sw = new StringWriter();
68 try (PemWriter pw = new PemWriter(sw)) {
69 PemObjectGenerator gen = new JcaMiscPEMGenerator(certificate);
71 } catch (IOException e) {
72 LOGGER.error("Exception occurred during convert of X509 certificate", e);