[OOM-CERT-SERVICE] Add Key Update Request functionality
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / certification / model / CertificateUpdateModel.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.model;
22
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;
33
34 public final class CertificateUpdateModel {
35
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();
42
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;
49         this.caName = caName;
50     }
51
52     public String getEncodedCsr() {
53         return encodedCsr;
54     }
55
56     public String getEncodedPrivateKey() {
57         return encodedPrivateKey;
58     }
59
60     public String getEncodedOldCert() {
61         return encodedOldCert;
62     }
63
64     public String getEncodedOldPrivateKey() {
65         return encodedOldPrivateKey;
66     }
67
68     public String getCaName() {
69         return caName;
70     }
71
72     public PrivateKey getOldPrivateKeyObject()
73         throws KeyDecryptionException, InvalidKeySpecException, NoSuchAlgorithmException {
74
75         StringBase64 stringBase64 = new StringBase64(encodedOldPrivateKey);
76         PemObject pemObject = stringBase64.asString()
77             .flatMap(PEM_OBJECT_FACTORY::createPemObject)
78             .orElseThrow(
79                 () -> new KeyDecryptionException("Incorrect Key, decryption failed")
80             );
81         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
82         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
83         return keyFactory.generatePrivate(keySpec);
84     }
85
86     @Override
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);
96     }
97
98     @Override
99     public int hashCode() {
100         return Objects.hash(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey, caName);
101     }
102
103     public static class CertificateUpdateModelBuilder {
104
105         private String encodedCsr;
106         private String encodedPrivateKey;
107         private String encodedOldCert;
108         private String encodedOldPrivateKey;
109         private String caName;
110
111         public CertificateUpdateModelBuilder setEncodedCsr(String encodedCsr) {
112             this.encodedCsr = encodedCsr;
113             return this;
114         }
115
116         public CertificateUpdateModelBuilder setEncodedPrivateKey(String encodedPrivateKey) {
117             this.encodedPrivateKey = encodedPrivateKey;
118             return this;
119         }
120
121         public CertificateUpdateModelBuilder setEncodedOldCert(String encodedOldCert) {
122             this.encodedOldCert = encodedOldCert;
123             return this;
124         }
125
126         public CertificateUpdateModelBuilder setEncodedOldPrivateKey(String encodedOldPrivateKey) {
127             this.encodedOldPrivateKey = encodedOldPrivateKey;
128             return this;
129         }
130
131         public CertificateUpdateModelBuilder setCaName(String caName) {
132             this.caName = caName;
133             return this;
134         }
135
136         public CertificateUpdateModel build() {
137             return new CertificateUpdateModel(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey, caName);
138         }
139     }
140 }