2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Modification copyright 2021 Nokia
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.oom.certservice.cmpv2client.validation;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
28 import org.bouncycastle.asn1.cmp.CertResponse;
29 import org.bouncycastle.asn1.cmp.PKIHeader;
30 import org.bouncycastle.asn1.cmp.PKIMessage;
31 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
32 import org.onap.oom.certservice.certification.configuration.model.CaMode;
33 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
34 import org.onap.oom.certservice.certification.model.CsrModel;
35 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
37 import org.onap.oom.certservice.cmpv2client.impl.CmpUtil;
38 import org.onap.oom.certservice.cmpv2client.impl.PkiStatus;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.security.PublicKey;
43 import java.util.Date;
44 import java.util.Objects;
45 import java.util.Optional;
47 import static org.onap.oom.certservice.cmpv2client.impl.CmpResponseValidationHelper.checkImplicitConfirm;
48 import static org.onap.oom.certservice.cmpv2client.impl.CmpResponseValidationHelper.verifyPasswordBasedProtection;
49 import static org.onap.oom.certservice.cmpv2client.impl.CmpResponseValidationHelper.verifySignature;
51 public class CmpCertificationValidator {
52 private static final String DEFAULT_CA_NAME = "Certification Authority";
53 private static final String DEFAULT_PROFILE = CaMode.RA.getProfile();
54 private static final ASN1ObjectIdentifier PASSWORD_BASED_MAC = new ASN1ObjectIdentifier("1.2.840.113533.7.66.13");
55 private static final Logger LOG = LoggerFactory.getLogger(CmpCertificationValidator.class);
58 final CsrModel csrModel,
59 final Cmpv2Server server,
60 final CloseableHttpClient httpClient,
62 final Date notAfter) {
64 String caName = CmpUtil.isNullOrEmpty(server.getCaName()) ? server.getCaName() : DEFAULT_CA_NAME;
65 String profile = server.getCaMode() != null ? server.getCaMode().getProfile() : DEFAULT_PROFILE;
67 "Validate before creating Certificate Request for CA :{} in Mode {} ", caName, profile);
69 CmpUtil.notNull(csrModel, "CsrModel Instance");
70 CmpUtil.notNull(csrModel.getSubjectData(), "Subject DN");
71 CmpUtil.notNull(csrModel.getPrivateKey(), "Subject private key");
72 CmpUtil.notNull(csrModel.getPublicKey(), "Subject public key");
73 CmpUtil.notNull(server.getIssuerDN(), "Issuer DN");
74 CmpUtil.notNull(server.getUrl(), "External CA URL");
75 CmpUtil.notNull(server.getAuthentication().getIak(), "IAK/RV Password");
76 CmpUtil.notNull(httpClient, "Closeable Http Client");
78 if (notBefore != null && notAfter != null && notBefore.compareTo(notAfter) > 0) {
79 throw new IllegalArgumentException("Before Date is set after the After Date");
83 public void checkCmpResponse(final PKIMessage respPkiMessage, final PublicKey publicKey, final String initAuthPassword)
84 throws CmpClientException {
85 final PKIHeader header = respPkiMessage.getHeader();
86 final AlgorithmIdentifier protectionAlgo = header.getProtectionAlg();
87 verifySignatureWithPublicKey(respPkiMessage, publicKey);
88 if (isPasswordBasedMacAlgorithm(protectionAlgo)) {
89 LOG.info("CMP response is protected by Password Base Mac Algorithm. Attempt to verify protection");
90 verifyPasswordBasedMacProtection(respPkiMessage, initAuthPassword, header, protectionAlgo);
94 public void checkServerResponse(CertResponse certResponse) {
95 if (certResponse.getStatus() != null && certResponse.getStatus().getStatus() != null) {
96 logServerResponse(certResponse);
97 if (certResponse.getStatus().getStatus().intValue() == PkiStatus.REJECTED.getCode()) {
98 String serverMessage = certResponse.getStatus().getStatusString().getStringAt(0).getString();
99 throw new CmpServerException(Optional.ofNullable(serverMessage).orElse("N/A"));
104 private boolean isPasswordBasedMacAlgorithm(AlgorithmIdentifier protectionAlgo) throws CmpClientException {
105 if (Objects.isNull(protectionAlgo)) {
106 LOG.error("CMP response does not contain Protection Algorithm field");
107 throw new CmpClientException("CMP response does not contain Protection Algorithm field");
109 return PASSWORD_BASED_MAC.equals(protectionAlgo.getAlgorithm());
112 private void verifySignatureWithPublicKey(PKIMessage respPkiMessage, PublicKey publicKey)
113 throws CmpClientException {
114 if (Objects.nonNull(publicKey)) {
115 LOG.debug("Verifying signature of the response.");
116 verifySignature(respPkiMessage, publicKey);
118 LOG.error("Public Key is not available, therefore cannot verify signature");
119 throw new CmpClientException(
120 "Public Key is not available, therefore cannot verify signature");
124 private void verifyPasswordBasedMacProtection(PKIMessage respPkiMessage, String initAuthPassword,
125 PKIHeader header, AlgorithmIdentifier protectionAlgo)
126 throws CmpClientException {
127 LOG.debug("Verifying PasswordBased Protection of the Response.");
128 verifyPasswordBasedProtection(respPkiMessage, initAuthPassword, protectionAlgo);
129 checkImplicitConfirm(header);
132 private void logServerResponse(CertResponse certResponse) {
133 if (LOG.isInfoEnabled()) {
134 LOG.info("Response status code: {}", certResponse.getStatus().getStatus());
136 if (certResponse.getStatus().getStatusString() != null) {
137 String serverMessage = certResponse.getStatus().getStatusString().getStringAt(0).getString();
138 LOG.warn("Response status text: {}", serverMessage);
140 if (LOG.isWarnEnabled() && certResponse.getStatus().getFailInfo() != null) {
141 LOG.warn("Response fail info: {}", certResponse.getStatus().getFailInfo());