2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.oom.certservice.api;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.responses.ApiResponses;
29 import io.swagger.v3.oas.annotations.tags.Tag;
30 import org.onap.oom.certservice.certification.CertificationResponseModelFactory;
31 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
32 import org.onap.oom.certservice.certification.exception.DecryptionException;
33 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
34 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
35 import org.onap.oom.certservice.certification.model.CertificationResponseModel;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestHeader;
45 import org.springframework.web.bind.annotation.RestController;
49 @Tag(name = "CertificationService")
50 public class CertificationController {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CertificationController.class);
54 private final CertificationResponseModelFactory certificationResponseModelFactory;
57 CertificationController(CertificationResponseModelFactory certificationResponseModelFactory) {
58 this.certificationResponseModelFactory = certificationResponseModelFactory;
62 * Request for signing certificate by given CA.
64 * @param caName the name of Certification Authority that will sign root certificate
65 * @param encodedCsr Certificate Sign Request encoded in Base64 form
66 * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form
67 * @return JSON containing trusted certificates and certificate chain
69 @GetMapping(value = "v1/certificate/{caName}", produces = "application/json")
70 @ApiResponses(value = {
71 @ApiResponse(responseCode = "200", description = "Certificate successfully signed"),
72 @ApiResponse(responseCode = "400", description = "Given CSR or/and PK is incorrect",
73 content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
74 @ApiResponse(responseCode = "404", description = "CA not found for given name",
75 content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
76 @ApiResponse(responseCode = "500", description = "Something went wrong during connectiion to CMPv2 server",
77 content = @Content(schema = @Schema(implementation = ErrorResponseModel.class)))
80 summary = "sign certificate",
81 description = "Web endpoint for requesting certificate signing. Used by system components to gain certificate signed by CA.",
82 tags = {"CertificationService"})
83 public ResponseEntity<CertificationResponseModel> signCertificate(
84 @Parameter(description = "Name of certification authority that will sign CSR.")
85 @PathVariable String caName,
86 @Parameter(description = "Certificate signing request in form of PEM object encoded in Base64 (with header and footer).")
87 @RequestHeader("CSR") String encodedCsr,
88 @Parameter(description = "Private key in form of PEM object encoded in Base64 (with header and footer).")
89 @RequestHeader("PK") String encodedPrivateKey
90 ) throws DecryptionException, CmpClientException {
91 caName = replaceWhiteSpaceChars(caName);
92 LOGGER.info("Received certificate signing request for CA named: {}", caName);
93 CertificationResponseModel certificationResponseModel = certificationResponseModelFactory
94 .provideCertificationModelFromInitialRequest(encodedCsr, encodedPrivateKey, caName);
95 return new ResponseEntity<>(certificationResponseModel, HttpStatus.OK);
99 * Request for updating certificate by given CA.
101 * @param caName the name of Certification Authority that will sign root certificate
102 * @param encodedCsr Certificate Sign Request encoded in Base64 form
103 * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form
104 * @param encodedOldCert Certificate (signed by Certification Authority) that should be renewed
105 * @param encodedOldPrivateKey Old private key corresponding with old certificate
106 * @return JSON containing trusted certificates and certificate chain
108 @GetMapping(value = "v1/certificate-update/{caName}", produces = "application/json")
109 public ResponseEntity<CertificationResponseModel> updateCertificate(
110 @PathVariable String caName,
111 @RequestHeader("CSR") String encodedCsr,
112 @RequestHeader("PK") String encodedPrivateKey,
113 @RequestHeader("OLD_CERT") String encodedOldCert,
114 @RequestHeader("OLD_PK") String encodedOldPrivateKey
115 ) throws DecryptionException, CmpClientException, CertificateDecryptionException {
116 caName = replaceWhiteSpaceChars(caName);
117 LOGGER.info("Received certificate update request for CA named: {}", caName);
118 CertificateUpdateModel certificateUpdateModel = new CertificateUpdateModel.CertificateUpdateModelBuilder()
119 .setEncodedCsr(encodedCsr)
120 .setEncodedPrivateKey(encodedPrivateKey)
121 .setEncodedOldCert(encodedOldCert)
122 .setEncodedOldPrivateKey(encodedOldPrivateKey)
125 CertificationResponseModel certificationResponseModel = certificationResponseModelFactory
126 .provideCertificationModelFromUpdateRequest(certificateUpdateModel);
127 return new ResponseEntity<>(certificationResponseModel, HttpStatus.OK);
130 private String replaceWhiteSpaceChars(String text) {
131 return text.replaceAll("[\n\r\t]", "_");