1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.hap.config; 17 18 import com.ohos.hapsigntool.adapter.LocalizationAdapter; 19 import com.ohos.hapsigntool.entity.Options; 20 import com.ohos.hapsigntool.entity.SignatureAlgorithm; 21 import com.ohos.hapsigntool.signer.ISigner; 22 import com.ohos.hapsigntool.signer.SignerFactory; 23 24 import java.security.cert.X509CRL; 25 import java.security.cert.X509Certificate; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Sign config super class 32 * 33 * @since 2021-12-13 34 */ 35 public class SignerConfig { 36 /** 37 * params inputted by users 38 */ 39 private Options options; 40 41 /** 42 * certificate chain used for sign hap 43 */ 44 private List<X509Certificate> certificates; 45 46 /** 47 * certificate revocation list return from server 48 */ 49 private List<X509CRL> x509CRLs; 50 51 /** 52 * Signature Algorithms used for sign hap 53 */ 54 private List<SignatureAlgorithm> signatureAlgorithms; 55 56 /** 57 * parameters for sign hap 58 */ 59 private Map<String, String> signParamMap = new HashMap<String, String>(); 60 61 /** 62 * Signer used for sign hap 63 */ 64 private ISigner signer; 65 66 /** 67 * Minimum api version to run the application 68 */ 69 private int compatibleVersion; 70 71 /** 72 * Get options. 73 * 74 * @return options 75 */ getOptions()76 public Options getOptions() { 77 return options; 78 } 79 80 /** 81 * set options. 82 * 83 * @param options parameters 84 */ setOptions(Options options)85 public void setOptions(Options options) { 86 this.options = options; 87 } 88 89 /** 90 * Get certificates. 91 * 92 * @return certificates 93 */ getCertificates()94 public List<X509Certificate> getCertificates() { 95 if (isInputCertChainNotEmpty() || signer == null) { 96 return certificates; 97 } 98 return signer.getCertificates(); 99 } 100 101 /** 102 * set certificate 103 * 104 * @param certificates certificate chain 105 */ setCertificates(List<X509Certificate> certificates)106 public void setCertificates(List<X509Certificate> certificates) { 107 this.certificates = certificates; 108 } 109 110 /** 111 * get crl 112 * 113 * @return crl list 114 */ getX509CRLs()115 public List<X509CRL> getX509CRLs() { 116 if (isInputCertChainNotEmpty() || isInputCrlNotEmpty() || signer == null) { 117 return x509CRLs; 118 } 119 return signer.getCrls(); 120 } 121 122 /** 123 * set crl 124 * 125 * @param crls cert revocation list 126 */ setX509CRLs(List<X509CRL> crls)127 public void setX509CRLs(List<X509CRL> crls) { 128 this.x509CRLs = crls; 129 } 130 131 /** 132 * get signature algorithm 133 * 134 * @return signature algorithm 135 */ getSignatureAlgorithms()136 public List<SignatureAlgorithm> getSignatureAlgorithms() { 137 return signatureAlgorithms; 138 } 139 140 /** 141 * set signature algorithm 142 * 143 * @param signatureAlgorithms sign algorithm 144 */ setSignatureAlgorithms(List<SignatureAlgorithm> signatureAlgorithms)145 public void setSignatureAlgorithms(List<SignatureAlgorithm> signatureAlgorithms) { 146 this.signatureAlgorithms = signatureAlgorithms; 147 } 148 149 /** 150 * get param map 151 * 152 * @return param map 153 */ getSignParamMap()154 public Map<String, String> getSignParamMap() { 155 return this.signParamMap; 156 } 157 158 /** 159 * set param map 160 * 161 * @param params params map 162 */ setParameters(Map<String, String> params)163 public void setParameters(Map<String, String> params) { 164 this.signParamMap = params; 165 } 166 167 /** 168 * get signer 169 * 170 * @return content signer 171 */ getSigner()172 public ISigner getSigner() { 173 if (signer == null) { 174 signer = new SignerFactory().getSigner(new LocalizationAdapter(options)); 175 } 176 return signer; 177 } 178 179 /** 180 * get compatible version 181 * 182 * @return compatible version 183 */ getCompatibleVersion()184 public int getCompatibleVersion() { 185 return compatibleVersion; 186 } 187 188 /** 189 * set param compatible version 190 * 191 * @param compatibleVersion compatible version 192 */ setCompatibleVersion(int compatibleVersion)193 public void setCompatibleVersion(int compatibleVersion) { 194 this.compatibleVersion = compatibleVersion; 195 } 196 isInputCertChainNotEmpty()197 private boolean isInputCertChainNotEmpty() { 198 return certificates != null && !certificates.isEmpty(); 199 } 200 isInputCrlNotEmpty()201 private boolean isInputCrlNotEmpty() { 202 return x509CRLs != null && !x509CRLs.isEmpty(); 203 } 204 205 /** 206 * create a copy of this object 207 * 208 * @return a copy of this object 209 */ copy()210 public SignerConfig copy() { 211 SignerConfig signerConfig = new SignerConfig(); 212 signerConfig.setParameters(signParamMap); 213 signerConfig.setCertificates(certificates); 214 signerConfig.setOptions(options); 215 signerConfig.setSignatureAlgorithms(signatureAlgorithms); 216 signerConfig.setCompatibleVersion(compatibleVersion); 217 signerConfig.setX509CRLs(x509CRLs); 218 signerConfig.getSigner(); 219 return signerConfig; 220 } 221 } 222