1 /*
2  * Copyright (c) 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 #include "tls_utils.h"
17 
18 #include <climits>
19 #include <cstdlib>
20 
21 #include "netstack_log.h"
22 #include "tls.h"
23 
24 namespace OHOS {
25 namespace NetStack {
26 namespace TlsSocket {
IsValidPath(const std::string & filePath)27 bool IsValidPath(const std::string &filePath)
28 {
29     std::string path = filePath.substr(0, CERT_PATH_LEN);
30     return strcmp(path.c_str(), CERT_PATH) == 0;
31 }
32 
CheckFilePath(const std::string & fileName,std::string & realPath)33 bool CheckFilePath(const std::string &fileName, std::string &realPath)
34 {
35     char tmpPath[PATH_MAX] = {0};
36     if (!realpath(static_cast<const char *>(fileName.c_str()), tmpPath)) {
37         NETSTACK_LOGE("file name is error");
38         return false;
39     }
40     if (!IsValidPath(tmpPath)) {
41         NETSTACK_LOGE("file path is error");
42         return false;
43     }
44     realPath = tmpPath;
45     return true;
46 }
47 } // namespace TlsSocket
48 } // namespace NetStack
49 } // namespace OHOS