• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 #include "params_parser.h"
17 
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include "hcf_string.h"
21 #include "log.h"
22 
23 static const HcfParaConfig PARAM_CONFIG[] = {
24     {"ECC224",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_224},
25     {"ECC256",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_256},
26     {"ECC384",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_384},
27     {"ECC521",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_521},
28 
29     {"AES128",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_128},
30     {"AES192",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_192},
31     {"AES256",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_256},
32     {"3DES192",      HCF_ALG_KEY_TYPE,       HCF_ALG_3DES_192},
33 
34     {"ECB",          HCF_ALG_MODE,           HCF_ALG_MODE_ECB},
35     {"CBC",          HCF_ALG_MODE,           HCF_ALG_MODE_CBC},
36     {"CTR",          HCF_ALG_MODE,           HCF_ALG_MODE_CTR},
37     {"OFB",          HCF_ALG_MODE,           HCF_ALG_MODE_OFB},
38     {"CFB",          HCF_ALG_MODE,           HCF_ALG_MODE_CFB},
39     {"CFB1",         HCF_ALG_MODE,           HCF_ALG_MODE_CFB1},
40     {"CFB8",         HCF_ALG_MODE,           HCF_ALG_MODE_CFB8},
41     {"CFB64",        HCF_ALG_MODE,           HCF_ALG_MODE_CFB64},
42     {"CFB128",       HCF_ALG_MODE,           HCF_ALG_MODE_CFB128},
43     {"CCM",          HCF_ALG_MODE,           HCF_ALG_MODE_CCM},
44     {"GCM",          HCF_ALG_MODE,           HCF_ALG_MODE_GCM},
45 
46     {"NoPadding",    HCF_ALG_PADDING_TYPE,   HCF_ALG_NOPADDING},
47     {"PKCS5",        HCF_ALG_PADDING_TYPE,   HCF_ALG_PADDING_PKCS5},
48     {"PKCS7",        HCF_ALG_PADDING_TYPE,   HCF_ALG_PADDING_PKCS7},
49 
50     {"RSA512",        HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_512},
51     {"RSA768",        HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_768},
52     {"RSA1024",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_1024},
53     {"RSA2048",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_2048},
54     {"RSA3072",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_3072},
55     {"RSA4096",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_4096},
56     {"RSA8192",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_8192},
57 
58     {"PKCS1",        HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PKCS1_PADDING},
59     {"PKCS1_OAEP",   HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING},
60     {"PSS",          HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PSS_PADDING},
61 
62     {"MD5",          HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_MD5},
63     {"SHA1",         HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA1},
64     {"SHA224",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA224},
65     {"SHA256",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA256},
66     {"SHA384",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA384},
67     {"SHA512",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA512},
68 
69     {"MGF1_MD5",          HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_MD5},
70     {"MGF1_SHA1",         HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA1},
71     {"MGF1_SHA224",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA224},
72     {"MGF1_SHA256",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA256},
73     {"MGF1_SHA384",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA384},
74     {"MGF1_SHA512",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA512},
75 
76     {"PRIMES_2",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_2},
77     {"PRIMES_3",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_3},
78     {"PRIMES_4",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_4},
79     {"PRIMES_5",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_5},
80 
81 };
82 
FindConfig(const HcString * tag)83 static const HcfParaConfig *FindConfig(const HcString* tag)
84 {
85     if (tag == NULL) {
86         return NULL;
87     }
88 
89     for (uint32_t i = 0; i < sizeof(PARAM_CONFIG) / sizeof(HcfParaConfig); ++i) {
90         if (StringCompare(tag, PARAM_CONFIG[i].tag) == 0) {
91             return &PARAM_CONFIG[i];
92         }
93     }
94     return NULL;
95 }
96 
ParseAndSetParameter(const char * paramsStr,void * params,SetParameterFunc setFunc)97 HcfResult ParseAndSetParameter(const char *paramsStr, void *params, SetParameterFunc setFunc)
98 {
99     if (paramsStr == NULL || setFunc == NULL) {
100         return HCF_INVALID_PARAMS;
101     }
102     HcString str = CreateString();
103     HcString subStr = CreateString();
104     if (!StringSetPointer(&str, paramsStr)) {
105         DeleteString(&subStr);
106         DeleteString(&str);
107         return HCF_INVALID_PARAMS;
108     }
109     HcfResult ret = HCF_INVALID_PARAMS;
110     uint32_t pos = 0;
111     do {
112         int findPos = StringFind(&str, '|', pos);
113         if (findPos >= 0) {
114             if (!StringSubString(&str, pos, findPos - pos, &subStr)) {
115                 LOGE("StringSubString failed!");
116                 break;
117             }
118             ret = (*setFunc)(FindConfig(&subStr), params);
119             if (ret != HCF_SUCCESS) {
120                 break;
121             }
122             pos = findPos + 1;
123         } else {
124             uint32_t strLen = StringLength(&str);
125             if (strLen < pos) {
126                 break;
127             }
128             if (!StringSubString(&str, pos, strLen - pos, &subStr)) {
129                 LOGE("get last string failed!");
130                 break;
131             }
132             ret = (*setFunc)(FindConfig(&subStr), params);
133             break;
134         }
135     } while (true);
136 
137     DeleteString(&subStr);
138     DeleteString(&str);
139     return ret;
140 }
141