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.certification.model;
23 import java.security.KeyFactory;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.PrivateKey;
26 import java.security.spec.InvalidKeySpecException;
27 import java.security.spec.PKCS8EncodedKeySpec;
28 import java.util.Objects;
29 import org.bouncycastle.util.io.pem.PemObject;
30 import org.onap.oom.certservice.certification.PemObjectFactory;
31 import org.onap.oom.certservice.certification.StringBase64;
32 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
34 public final class CertificateUpdateModel {
36 private final String encodedCsr;
37 private final String encodedPrivateKey;
38 private final String encodedOldCert;
39 private final String encodedOldPrivateKey;
40 private final String caName;
41 private static final PemObjectFactory PEM_OBJECT_FACTORY = new PemObjectFactory();
43 private CertificateUpdateModel(String encodedCsr, String encodedPrivateKey, String encodedOldCert,
44 String encodedOldPrivateKey, String caName) {
45 this.encodedCsr = encodedCsr;
46 this.encodedPrivateKey = encodedPrivateKey;
47 this.encodedOldCert = encodedOldCert;
48 this.encodedOldPrivateKey = encodedOldPrivateKey;
52 public String getEncodedCsr() {
56 public String getEncodedPrivateKey() {
57 return encodedPrivateKey;
60 public String getEncodedOldCert() {
61 return encodedOldCert;
64 public String getEncodedOldPrivateKey() {
65 return encodedOldPrivateKey;
68 public String getCaName() {
72 public PrivateKey getOldPrivateKeyObject()
73 throws KeyDecryptionException, InvalidKeySpecException, NoSuchAlgorithmException {
75 StringBase64 stringBase64 = new StringBase64(encodedOldPrivateKey);
76 PemObject pemObject = stringBase64.asString()
77 .flatMap(PEM_OBJECT_FACTORY::createPemObject)
79 () -> new KeyDecryptionException("Incorrect Key, decryption failed")
81 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
82 KeyFactory keyFactory = KeyFactory.getInstance("RSA");
83 return keyFactory.generatePrivate(keySpec);
87 public boolean equals(Object o) {
88 if (this == o) return true;
89 if (o == null || getClass() != o.getClass()) return false;
90 CertificateUpdateModel that = (CertificateUpdateModel) o;
91 return Objects.equals(encodedCsr, that.encodedCsr)
92 && Objects.equals(encodedPrivateKey, that.encodedPrivateKey)
93 && Objects.equals(encodedOldCert, that.encodedOldCert)
94 && Objects.equals(encodedOldPrivateKey, that.encodedOldPrivateKey)
95 && Objects.equals(caName, that.caName);
99 public int hashCode() {
100 return Objects.hash(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey, caName);
103 public static class CertificateUpdateModelBuilder {
105 private String encodedCsr;
106 private String encodedPrivateKey;
107 private String encodedOldCert;
108 private String encodedOldPrivateKey;
109 private String caName;
111 public CertificateUpdateModelBuilder setEncodedCsr(String encodedCsr) {
112 this.encodedCsr = encodedCsr;
116 public CertificateUpdateModelBuilder setEncodedPrivateKey(String encodedPrivateKey) {
117 this.encodedPrivateKey = encodedPrivateKey;
121 public CertificateUpdateModelBuilder setEncodedOldCert(String encodedOldCert) {
122 this.encodedOldCert = encodedOldCert;
126 public CertificateUpdateModelBuilder setEncodedOldPrivateKey(String encodedOldPrivateKey) {
127 this.encodedOldPrivateKey = encodedOldPrivateKey;
131 public CertificateUpdateModelBuilder setCaName(String caName) {
132 this.caName = caName;
136 public CertificateUpdateModel build() {
137 return new CertificateUpdateModel(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey, caName);