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