2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nokia.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.cmpv2client.impl;
23 import org.bouncycastle.asn1.DERBitString;
24 import org.bouncycastle.asn1.DERGeneralizedTime;
25 import org.bouncycastle.asn1.cmp.PKIBody;
26 import org.bouncycastle.asn1.cmp.PKIHeader;
27 import org.bouncycastle.asn1.cmp.PKIHeaderBuilder;
28 import org.bouncycastle.asn1.cmp.PKIMessage;
29 import org.bouncycastle.asn1.crmf.CertReqMessages;
30 import org.bouncycastle.asn1.crmf.CertReqMsg;
31 import org.bouncycastle.asn1.crmf.CertRequest;
32 import org.bouncycastle.asn1.crmf.CertTemplateBuilder;
33 import org.bouncycastle.asn1.x500.X500Name;
34 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
35 import org.bouncycastle.asn1.x509.GeneralName;
36 import org.bouncycastle.cert.cmp.GeneralPKIMessage;
37 import org.bouncycastle.cert.cmp.ProtectedPKIMessage;
38 import org.bouncycastle.operator.ContentVerifierProvider;
39 import org.bouncycastle.operator.OperatorCreationException;
40 import org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder;
42 import java.security.KeyPair;
43 import java.security.KeyPairGenerator;
44 import java.security.NoSuchAlgorithmException;
45 import java.security.NoSuchProviderException;
46 import java.security.PublicKey;
47 import java.util.Date;
49 final class PkiTestUtils {
51 private static final String CN_TEST_SUBJECT = "CN=test1Subject";
52 private static final String CN_TEST_ISSUER = "CN=test2Issuer";
53 private static final int TEST_CERT_REQUEST_ID = 1432;
54 private static final int PVNO = 0;
55 private static final String BC_PROVIDER = "BC";
56 private static final String RSA = "RSA";
58 private PkiTestUtils() {
61 static PKIBody getTestPkiBody(AlgorithmIdentifier signingAlgorithm) {
62 CertTemplateBuilder certTemplateBuilder =
63 new CertTemplateBuilder()
64 .setIssuer(new X500Name(CN_TEST_ISSUER))
65 .setSubject(new X500Name(CN_TEST_SUBJECT))
66 .setSigningAlg(signingAlgorithm);
68 CertRequest certRequest = new CertRequest(TEST_CERT_REQUEST_ID, certTemplateBuilder.build(), null);
69 CertReqMsg certReqMsg = new CertReqMsg(certRequest, null, null);
71 CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
72 return new PKIBody(0, certReqMessages);
75 static PKIHeader getTestPkiHeader(AlgorithmIdentifier protectionAlgorithm) {
76 PKIHeaderBuilder pkiHeader = new PKIHeaderBuilder(
78 new GeneralName(new X500Name(CN_TEST_SUBJECT)),
79 new GeneralName(new X500Name(CN_TEST_ISSUER)));
80 pkiHeader.setProtectionAlg(protectionAlgorithm);
81 pkiHeader.setMessageTime(new DERGeneralizedTime(new Date()));
82 return pkiHeader.build();
85 static ProtectedPKIMessage getProtectedPkiMessage(PKIHeader pkiHeader, PKIBody pkiBody, DERBitString messageProtection) {
86 PKIMessage pkiMessage = new PKIMessage(pkiHeader, pkiBody, messageProtection);
87 GeneralPKIMessage generalPkiMessage = new GeneralPKIMessage(pkiMessage);
88 return new ProtectedPKIMessage(generalPkiMessage);
91 static KeyPair getKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException {
92 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA, BC_PROVIDER);
93 return keyPairGenerator.generateKeyPair();
96 static ContentVerifierProvider getContentVerifierProvider(PublicKey publicKey) throws OperatorCreationException {
97 return new JcaContentVerifierProviderBuilder()
98 .setProvider(BC_PROVIDER)