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