aboutsummaryrefslogtreecommitdiffstats
path: root/certService/src/test/java/org/onap/oom/certservice/cmpv2client/impl/protections/PkiTestUtils.java
blob: 74af51c1a964ba6737fdf35c5c17a479afe5cadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nokia.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.oom.certservice.cmpv2client.impl.protections;

import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERGeneralizedTime;
import org.bouncycastle.asn1.cmp.PKIBody;
import org.bouncycastle.asn1.cmp.PKIHeader;
import org.bouncycastle.asn1.cmp.PKIHeaderBuilder;
import org.bouncycastle.asn1.cmp.PKIMessage;
import org.bouncycastle.asn1.crmf.CertReqMessages;
import org.bouncycastle.asn1.crmf.CertReqMsg;
import org.bouncycastle.asn1.crmf.CertRequest;
import org.bouncycastle.asn1.crmf.CertTemplateBuilder;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.cert.cmp.GeneralPKIMessage;
import org.bouncycastle.cert.cmp.ProtectedPKIMessage;
import org.bouncycastle.operator.ContentVerifierProvider;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.util.Date;

final class PkiTestUtils {

    private static final String CN_TEST_SUBJECT = "CN=test1Subject";
    private static final String CN_TEST_ISSUER = "CN=test2Issuer";
    private static final int TEST_CERT_REQUEST_ID = 1432;
    private static final int PVNO = 0;
    private static final String BC_PROVIDER = "BC";
    private static final String RSA = "RSA";

    private PkiTestUtils() {
    }

    static PKIBody getTestPkiBody(AlgorithmIdentifier signingAlgorithm) {
        CertTemplateBuilder certTemplateBuilder =
                new CertTemplateBuilder()
                        .setIssuer(new X500Name(CN_TEST_ISSUER))
                        .setSubject(new X500Name(CN_TEST_SUBJECT))
                        .setSigningAlg(signingAlgorithm);

        CertRequest certRequest = new CertRequest(TEST_CERT_REQUEST_ID, certTemplateBuilder.build(), null);
        CertReqMsg certReqMsg = new CertReqMsg(certRequest, null, null);

        CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
        return new PKIBody(0, certReqMessages);
    }

    static PKIHeader getTestPkiHeader(AlgorithmIdentifier protectionAlgorithm) {
        PKIHeaderBuilder pkiHeader = new PKIHeaderBuilder(
                PVNO,
                new GeneralName(new X500Name(CN_TEST_SUBJECT)),
                new GeneralName(new X500Name(CN_TEST_ISSUER)));
        pkiHeader.setProtectionAlg(protectionAlgorithm);
        pkiHeader.setMessageTime(new DERGeneralizedTime(new Date()));
        return pkiHeader.build();
    }

    static ProtectedPKIMessage getProtectedPkiMessage(PKIHeader pkiHeader, PKIBody pkiBody, DERBitString messageProtection) {
        PKIMessage pkiMessage = new PKIMessage(pkiHeader, pkiBody, messageProtection);
        GeneralPKIMessage generalPkiMessage = new GeneralPKIMessage(pkiMessage);
        return new ProtectedPKIMessage(generalPkiMessage);
    }

    static KeyPair getKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA, BC_PROVIDER);
        return keyPairGenerator.generateKeyPair();
    }

    static ContentVerifierProvider getContentVerifierProvider(PublicKey publicKey) throws OperatorCreationException {
        return new JcaContentVerifierProviderBuilder()
                .setProvider(BC_PROVIDER)
                .build(publicKey);
    }
}