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 "cert_manager_crypto_operation.h"
17 
18 #include <openssl/evp.h>
19 #include <openssl/rand.h>
20 
21 #include "securec.h"
22 
23 #include "cm_log.h"
24 #include "cm_type.h"
25 
26 #define BYTE_SHIFT_10           0x10
27 #define BYTE_SHIFT_8            0x08
28 #define BYTE_SHIFT_6            6
29 #define BASE64_URL_TABLE_SIZE   0x3F
30 #define BASE64_BITS_PER_OCTET   6
31 #define BYTE_LEN                8
32 #define BASE64_CARRY_SIZE       5
33 #define BYTE_INDEX_ZONE         0
34 #define BYTE_INDEX_ONE          1
35 #define BYTE_INDEX_TWO          2
36 #define BYTE_INDEX_THREE        3
37 
38 static const char g_base64UrlTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
39 
Base64UrlEncode(const struct CmBlob * indata,struct CmBlob * uriHash)40 static int32_t Base64UrlEncode(const struct CmBlob *indata, struct CmBlob *uriHash)
41 {
42     if ((indata == NULL) || (uriHash == NULL)) {
43         CM_LOG_E("input param is invalid");
44         return CMR_ERROR_INVALID_ARGUMENT;
45     }
46     int outputLen = (indata->size * BYTE_LEN + BASE64_CARRY_SIZE) / BASE64_BITS_PER_OCTET;
47     uriHash->size = (uint32_t)(outputLen + 1);
48     uriHash->data[outputLen] = '\0';
49     for (int i = 0, j = 0; i < (int)indata->size;) {
50         unsigned int octeta = i < (int)indata->size ? *(indata->data + (i++)) : 0;
51         unsigned int octetb = i < (int)indata->size ? *(indata->data + (i++)) : 0;
52         unsigned int octetc = i < (int)indata->size ? *(indata->data + (i++)) : 0;
53 
54         unsigned int triple = (octeta << BYTE_SHIFT_10) + (octetb << BYTE_SHIFT_8) + octetc;
55 
56         uriHash->data[j++] = g_base64UrlTable[(triple >> BYTE_INDEX_THREE * BYTE_SHIFT_6) & BASE64_URL_TABLE_SIZE];
57         uriHash->data[j++] = g_base64UrlTable[(triple >> BYTE_INDEX_TWO   * BYTE_SHIFT_6) & BASE64_URL_TABLE_SIZE];
58         uriHash->data[j++] = g_base64UrlTable[(triple >> BYTE_INDEX_ONE   * BYTE_SHIFT_6) & BASE64_URL_TABLE_SIZE];
59         uriHash->data[j++] = g_base64UrlTable[(triple >> BYTE_INDEX_ZONE  * BYTE_SHIFT_6) & BASE64_URL_TABLE_SIZE];
60     }
61     return CM_SUCCESS;
62 }
63 
GetNameEncode(const struct CmBlob * inBlob,struct CmBlob * outBlob)64 int32_t GetNameEncode(const struct CmBlob *inBlob, struct CmBlob *outBlob)
65 {
66     if ((inBlob == NULL) || (outBlob == NULL)) {
67         CM_LOG_E("input param is invalid");
68         return CMR_ERROR_INVALID_ARGUMENT;
69     }
70     uint8_t tempBuf[DIGEST_SHA256_LEN] = {0};
71     struct CmBlob inDigest = { DIGEST_SHA256_LEN, tempBuf };
72     int32_t ret = CM_SUCCESS;
73     do {
74         ret = CmGetHash(inBlob, &inDigest);
75         if (ret != CM_SUCCESS) {
76             CM_LOG_E("CmGetHash fail, ret = %d", ret);
77             break;
78         }
79         ret = Base64UrlEncode(&inDigest, outBlob);
80         if (ret != CM_SUCCESS) {
81             CM_LOG_E("Base64UrlEncode fail, ret = %d", ret);
82             break;
83         }
84     } while (0);
85     return ret;
86 }
87 
CmGetRandom(struct CmBlob * random)88 int32_t CmGetRandom(struct CmBlob *random)
89 {
90     if (CmCheckBlob(random) != CM_SUCCESS) {
91         return CMR_ERROR_INVALID_ARGUMENT;
92     }
93 
94     int ret = RAND_bytes(random->data, random->size);
95     if (ret <= 0) {
96         CM_LOG_E("Get random failed");
97         return CMR_ERROR_KEY_OPERATION_FAILED;
98     }
99 
100     return CM_SUCCESS;
101 }
102 
CmGetHash(const struct CmBlob * inData,struct CmBlob * hash)103 int32_t CmGetHash(const struct CmBlob *inData, struct CmBlob *hash)
104 {
105     if ((CmCheckBlob(inData) != CM_SUCCESS) || (CmCheckBlob(hash) != CM_SUCCESS) ||
106         (hash->size < DIGEST_SHA256_LEN)) {
107         CM_LOG_E("invalid input args");
108         return CMR_ERROR_INVALID_ARGUMENT;
109     }
110 
111     const EVP_MD *opensslAlg = EVP_sha256();
112     if (opensslAlg == NULL) {
113         CM_LOG_E("get openssl alg failed");
114         return CMR_ERROR_KEY_OPERATION_FAILED;
115     }
116 
117     int32_t ret = EVP_Digest(inData->data, inData->size, hash->data, &hash->size, opensslAlg, NULL);
118     if (ret <= 0) {
119         CM_LOG_E("digest failed");
120         return CMR_ERROR_KEY_OPERATION_FAILED;
121     }
122     return CM_SUCCESS;
123 }
124 
125