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.onap.aaf.certservice.cmpv2client.model.Cmpv2CertificationModel;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
37 import java.io.IOException;
38 import java.io.StringWriter;
39 import java.security.cert.X509Certificate;
40 import java.util.List;
41 import java.util.stream.Collectors;
44 public class CertificationProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(CertificationProvider.class);
48 private final CmpClient cmpClient;
51 public CertificationProvider(CmpClient cmpClient) {
52 this.cmpClient = cmpClient;
55 public CertificationModel signCsr(CsrModel csrModel, Cmpv2Server server)
56 throws CmpClientException {
57 Cmpv2CertificationModel certificates = cmpClient.createCertificate(csrModel, server);
58 return new CertificationModel(convertFromX509CertificateListToPemList(certificates.getCertificateChain()),
59 convertFromX509CertificateListToPemList(certificates.getTrustedCertificates()));
62 private static List<String> convertFromX509CertificateListToPemList(List<X509Certificate> certificates) {
63 return certificates.stream().map(CertificationProvider::convertFromX509CertificateToPem).filter(cert -> !cert.isEmpty())
64 .collect(Collectors.toList());
67 private static String convertFromX509CertificateToPem(X509Certificate certificate) {
68 StringWriter sw = new StringWriter();
69 try (PemWriter pw = new PemWriter(sw)) {
70 PemObjectGenerator gen = new JcaMiscPEMGenerator(certificate);
72 } catch (IOException e) {
73 LOGGER.error("Exception occurred during convert of X509 certificate", e);