• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.api.model;
17 
18 import com.ohos.hapsigntool.api.ServiceApi;
19 import com.ohos.hapsigntool.error.CustomException;
20 import com.ohos.hapsigntool.error.ERROR;
21 import com.ohos.hapsigntool.utils.StringUtils;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 
25 import java.util.HashMap;
26 
27 /**
28  * Options parameter class.
29  *
30  * @since 2021/12/28
31  */
32 public class Options extends HashMap<String, Object> {
33     /**
34      * Serial version UID.
35      */
36     private static final long serialVersionUID = 1L;
37 
38     /**
39      * Empty char.
40      */
41     private static final char[] NO_CHAR = {};
42 
43     /**
44      * Logger.
45      */
46     private static final Logger logger = LogManager.getLogger(ServiceApi.class);
47 
48     /**
49      * App cert file parameter name.
50      */
51     public static final String APP_CERT_FILE = "appCertFile";
52 
53     /**
54      * BasicConstraints parameter name.
55      */
56     public static final String BASIC_CONSTRAINTS = "basicConstraints";
57 
58     /**
59      * BasicConstraintsCa parameter name.
60      */
61     public static final String BASIC_CONSTRAINTS_CA = "basicConstraintsCa";
62 
63     /**
64      * BasicConstraintsCritical parameter name.
65      */
66     public static final String BASIC_CONSTRAINTS_CRITICAL = "basicConstraintsCritical";
67 
68     /**
69      * BasicConstraintsPathLen parameter name.
70      */
71     public static final String BASIC_CONSTRAINTS_PATH_LEN = "basicConstraintsPathLen";
72 
73     /**
74      * End file type of cert. values in: cert / certChain
75      */
76     public static final String OUT_FORM = "outForm";
77 
78     /**
79      * Ca cert file parameter name.
80      */
81     public static final String CA_CERT_FILE = "rootCaCertFile";
82 
83     /**
84      * Sub cert for sign
85      */
86     public static final String SUB_CA_CERT_FILE = "subCaCertFile";
87 
88     /**
89      * Ext cfg file parameter name.
90      */
91     public static final String EXT_CFG_FILE = "extCfgFile";
92 
93     /**
94      * Ext key usage parameter name.
95      */
96     public static final String EXT_KEY_USAGE = "extKeyUsage";
97 
98     /**
99      * Ext key usage critical parameter name.
100      */
101     public static final String EXT_KEY_USAGE_CRITICAL = "extKeyUsageCritical";
102 
103     /**
104      * In file parameter name.
105      */
106     public static final String IN_FILE = "inFile";
107 
108     /**
109      * In file parameter form.
110      */
111     public static final String IN_FORM = "inForm";
112 
113     /**
114      * Issuer parameter name.
115      */
116     public static final String ISSUER = "issuer";
117 
118     /**
119      * Issuer key alias parameter name.
120      */
121     public static final String ISSUER_KEY_ALIAS = "issuerKeyAlias";
122 
123     /**
124      * Issuer key right parameter name.
125      */
126     public static final String ISSUER_KEY_RIGHTS = "issuerKeyPwd";
127 
128     /**
129      * Issuer Key Store File parameter name.
130      */
131     public static final String ISSUER_KEY_STORE_FILE = "issuerKeystoreFile";
132 
133     /**
134      * Issuer Key Store Pwd parameter name.
135      */
136     public static final String ISSUER_KEY_STORE_RIGHTS = "issuerKeystorePwd";
137 
138     /**
139      * Key alg parameter name.
140      */
141     public static final String KEY_ALG = "keyAlg";
142 
143     /**
144      * Key alias parameter name.
145      */
146     public static final String KEY_ALIAS = "keyAlias";
147 
148     /**
149      * Key right parameter name.
150      */
151     public static final String KEY_RIGHTS = "keyPwd";
152 
153     /**
154      * Key size parameter name.
155      */
156     public static final String KEY_SIZE = "keySize";
157 
158     /**
159      * Keystore file parameter name.
160      */
161     public static final String KEY_STORE_FILE = "keystoreFile";
162 
163     /**
164      * Keystore right parameter name.
165      */
166     public static final String KEY_STORE_RIGHTS = "keystorePwd";
167 
168     /**
169      * Key usage parameter name.
170      */
171     public static final String KEY_USAGE = "keyUsage";
172 
173     /**
174      * Key usage critical parameter name.
175      */
176     public static final String KEY_USAGE_CRITICAL = "keyUsageCritical";
177 
178     /**
179      * Mode parameter name.
180      */
181     public static final String MODE = "mode";
182 
183     /**
184      * Out file parameter name.
185      */
186     public static final String OUT_FILE = "outFile";
187 
188     /**
189      * Out file parameter name.
190      */
191     public static final String OUT_CERT_CHAIN = "outCertChain";
192 
193     /**
194      * Out file parameter name.
195      */
196     public static final String OUT_PROFILE = "outProfile";
197 
198     /**
199      * Profile cert file parameter name.
200      */
201     public static final String PROFILE_CERT_FILE = "profileCertFile";
202 
203     /**
204      * Profile file parameter name.
205      */
206     public static final String PROFILE_FILE = "profileFile";
207 
208     /**
209      * Profile file is signed or not.
210      */
211     public static final String PROFILE_SIGNED = "profileSigned";
212 
213     /**
214      * Sign alg parameter name.
215      */
216     public static final String SIGN_ALG = "signAlg";
217 
218     /**
219      * Subject parameter name.
220      */
221     public static final String SUBJECT = "subject";
222 
223     /**
224      * Trusted app source file parameter name.
225      */
226     public static final String TRUSTED_APP_SOURCE_FILE = "trustedAppSourceFile";
227 
228     /**
229      * Trusted root ca file parameter name.
230      */
231     public static final String TRUSTED_ROOT_CA_FILE = "trustedRootCaFile";
232 
233     /**
234      * Validity parameter name.
235      */
236     public static final String VALIDITY = "validity";
237 
238     /**
239      * All usages included in the extended key usage.
240      */
241     public static final String EXT_KEY_USAGE_SCOPE = "clientAuthentication,serverAuthentication,codeSignature,"
242             + "emailProtection,smartCardLogin,timestamp,ocspSignature";
243 
244     /**
245      * Key usage includes all usages.
246      */
247     public static final String KEY_USAGE_SCOPE = "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
248             + "keyAgreement,certificateSignature,crlSignature,encipherOnly,decipherOnly";
249 
250     /**
251      * Out form includes all forms.
252      */
253     public static final String OUT_FORM_SCOPE = "cert,certChain";
254 
255     /**
256      * Check required key, throw exception
257      *
258      * @param keys keys to check
259      */
required(String... keys)260     public void required(String... keys) {
261         for (String key : keys) {
262             if (!StringUtils.isEmpty(key) && !this.containsKey(key)) {
263                 CustomException.throwException(ERROR.COMMAND_ERROR, String.format("Params '%s' is required", key));
264             }
265         }
266     }
267 
268     /**
269      * Get char value of key.
270      *
271      * @param key key
272      * @return value of key
273      */
getChars(String key)274     public char[] getChars(String key) {
275         Object value = this.get(key);
276         if (value instanceof char[]) {
277             return (char[]) value;
278         }
279         return NO_CHAR;
280     }
281 
282     /**
283      * Get string value of key.
284      *
285      * @param key      key
286      * @param defValue defValue
287      * @return string value of key
288      */
getString(String key, String defValue)289     public String getString(String key, String defValue) {
290         Object value = this.get(key);
291         if (!(value instanceof String)) {
292             return defValue;
293         }
294         return (String) value;
295     }
296 
297     /**
298      * Get string value of key or def value.
299      *
300      * @param key key
301      * @return string value of key
302      */
getString(String key)303     public String getString(String key) {
304         return getString(key, "");
305     }
306 
307     /**
308      * Get boolean value of key.
309      *
310      * @param key      key
311      * @param defValue defValue
312      * @return boolean value of key
313      */
getBoolean(String key, boolean defValue)314     public boolean getBoolean(String key, boolean defValue) {
315         Object value = this.get(key);
316         boolean result = defValue;
317         if (value instanceof Boolean) {
318             result = (boolean) value;
319         }
320         if (value instanceof String) {
321             if ("true".equalsIgnoreCase((String) value)) {
322                 result = true;
323             }
324             if ("false".equalsIgnoreCase((String) value)) {
325                 result = false;
326             }
327         }
328         return result;
329     }
330 
331     /**
332      * Get boolean value of key or def value.
333      *
334      * @param key key
335      * @return boolean value of key
336      */
getBoolean(String key)337     public boolean getBoolean(String key) {
338         return getBoolean(key, false);
339     }
340 
341     /**
342      * Get int value of key.
343      *
344      * @param key      key
345      * @param defValue defValue
346      * @return Value of key
347      */
getInt(String key, int defValue)348     public int getInt(String key, int defValue) {
349         Object value = this.get(key);
350         if (value instanceof Integer) {
351             return (int) value;
352         }
353         if (value instanceof String) {
354             try {
355                 defValue = Integer.parseInt((String) value);
356             } catch (NumberFormatException exception) {
357                 logger.debug(exception.getMessage(), exception);
358                 return defValue;
359             }
360         }
361         return defValue;
362     }
363 
364     /**
365      * Get int value of key or def value.
366      *
367      * @param key key
368      * @return Value of key
369      */
getInt(String key)370     public int getInt(String key) {
371         return getInt(key, 0);
372     }
373 }
374 
375