2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Copyright (C) 2021 Nokia.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.oom.certservice.cmpv2client.validation;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
27 import org.bouncycastle.asn1.cmp.CertResponse;
28 import org.bouncycastle.asn1.cmp.PKIHeader;
29 import org.bouncycastle.asn1.cmp.PKIMessage;
30 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
31 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
32 import org.onap.oom.certservice.certification.model.CsrModel;
33 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
34 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
35 import org.onap.oom.certservice.cmpv2client.impl.CmpUtil;
36 import org.onap.oom.certservice.cmpv2client.impl.PkiStatus;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import java.security.PublicKey;
41 import java.util.Date;
42 import java.util.Objects;
43 import java.util.Optional;
45 import static org.onap.oom.certservice.cmpv2client.validation.CmpResponseValidationHelper.checkImplicitConfirm;
46 import static org.onap.oom.certservice.cmpv2client.validation.CmpResponseValidationHelper.verifyPasswordBasedProtection;
47 import static org.onap.oom.certservice.cmpv2client.validation.CmpResponseValidationHelper.verifySignature;
49 public class CmpCertificationValidator {
50 private static final String DEFAULT_CA_NAME = "Certification Authority";
51 private static final ASN1ObjectIdentifier PASSWORD_BASED_MAC = new ASN1ObjectIdentifier("1.2.840.113533.7.66.13");
52 private static final Logger LOG = LoggerFactory.getLogger(CmpCertificationValidator.class);
55 final CsrModel csrModel,
56 final Cmpv2Server server,
57 final CloseableHttpClient httpClient,
59 final Date notAfter) {
61 String caName = CmpUtil.isNullOrEmpty(server.getCaName()) ? server.getCaName() : DEFAULT_CA_NAME;
63 "Validate before creating Certificate Request for CA: {}", caName);
65 CmpUtil.notNull(csrModel, "CsrModel Instance");
66 CmpUtil.notNull(csrModel.getSubjectData(), "Subject DN");
67 CmpUtil.notNull(csrModel.getPrivateKey(), "Subject private key");
68 CmpUtil.notNull(csrModel.getPublicKey(), "Subject public key");
69 CmpUtil.notNull(server.getIssuerDN(), "Issuer DN");
70 CmpUtil.notNull(server.getUrl(), "External CA URL");
71 CmpUtil.notNull(server.getAuthentication().getIak(), "IAK/RV Password");
72 CmpUtil.notNull(httpClient, "Closeable Http Client");
74 if (notBefore != null && notAfter != null && notBefore.compareTo(notAfter) > 0) {
75 throw new IllegalArgumentException("Before Date is set after the After Date");
77 LOG.info("Validation completed successfully.");
80 public void checkCmpResponse(final PKIMessage respPkiMessage, final PublicKey publicKey, final String initAuthPassword)
81 throws CmpClientException {
82 final PKIHeader header = respPkiMessage.getHeader();
83 final AlgorithmIdentifier protectionAlgo = header.getProtectionAlg();
84 verifySignatureWithPublicKey(respPkiMessage, publicKey);
85 if (isPasswordBasedMacAlgorithm(protectionAlgo)) {
86 LOG.info("CMP response is protected by Password Base Mac Algorithm. Attempt to verify protection");
87 verifyPasswordBasedMacProtection(respPkiMessage, initAuthPassword, header, protectionAlgo);
91 public void checkServerResponse(CertResponse certResponse) {
92 if (certResponse.getStatus() != null && certResponse.getStatus().getStatus() != null) {
93 logServerResponse(certResponse);
94 if (certResponse.getStatus().getStatus().intValue() == PkiStatus.REJECTED.getCode()) {
95 String serverMessage = certResponse.getStatus().getStatusString().getStringAt(0).getString();
96 throw new CmpServerException(Optional.ofNullable(serverMessage).orElse("N/A"));
101 private boolean isPasswordBasedMacAlgorithm(AlgorithmIdentifier protectionAlgo) throws CmpClientException {
102 if (Objects.isNull(protectionAlgo)) {
103 LOG.error("CMP response does not contain Protection Algorithm field");
104 throw new CmpClientException("CMP response does not contain Protection Algorithm field");
106 return PASSWORD_BASED_MAC.equals(protectionAlgo.getAlgorithm());
109 private void verifySignatureWithPublicKey(PKIMessage respPkiMessage, PublicKey publicKey)
110 throws CmpClientException {
111 if (Objects.nonNull(publicKey)) {
112 LOG.debug("Verifying signature of the response.");
113 verifySignature(respPkiMessage, publicKey);
115 LOG.error("Public Key is not available, therefore cannot verify signature");
116 throw new CmpClientException(
117 "Public Key is not available, therefore cannot verify signature");
121 private void verifyPasswordBasedMacProtection(PKIMessage respPkiMessage, String initAuthPassword,
122 PKIHeader header, AlgorithmIdentifier protectionAlgo)
123 throws CmpClientException {
124 LOG.debug("Verifying PasswordBased Protection of the Response.");
125 verifyPasswordBasedProtection(respPkiMessage, initAuthPassword, protectionAlgo);
126 checkImplicitConfirm(header);
129 private void logServerResponse(CertResponse certResponse) {
130 LOG.info("Response status code: {}", certResponse.getStatus().getStatus());
131 if (certResponse.getStatus().getStatusString() != null) {
132 String serverMessage = certResponse.getStatus().getStatusString().getStringAt(0).getString();
133 LOG.warn("Response status text: {}", serverMessage);
135 if (LOG.isWarnEnabled() && certResponse.getStatus().getFailInfo() != null) {
136 LOG.warn("Response fail info: {}", certResponse.getStatus().getFailInfo());