-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.c
189 lines (152 loc) · 4.59 KB
/
auth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by a8398 on 2020/5/28.
//
#include "auth.h"
#include "include/dev_manager.h"
#include "include/util.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "include/cJSON.h"
#include "sm3.h"
extern const char *gAppName;
extern const char *gFileName;
extern const char *gAdminPIN;
extern const char *gUserPIN;
int AUTHFILE_CheckArgs(AUTHFILE *authfile) {
if (authfile == NULL) {
return ERR_NULL_PTR;
}
if (strlen(authfile->user) == 0) {
return ERR_USER;
}
if (strlen(authfile->enc_passwd) == 0) {
return ERR_PASSWD;
}
return 0;
}
int AUTHFILE_Read(AUTHFILE *authfile) {
ULONG ret = 0;
DEVMANAGER *devman = DEVMANAGER_new();
ret = DEVMANAGER_OpenDev(devman);
if (ret!=SAR_OK){
SOFT_LOG_ERR("OpenDev error ret= %x\n", ret);
goto END;
}
ret = DEVMANAGER_OpenApp(devman);
if (ret!=SAR_OK){
SOFT_LOG_ERR("OpenApp error ret= %x , does ukey inited\n", ret);
goto END;
}
ret = DEVMANAGER_VerifyPIN(devman);
if (ret != SAR_OK) {
SOFT_LOG_ERR("VerifyPIN error ret= %x\n", ret);
goto END;
}
unsigned char jsonStr[1024];
memset(jsonStr, 0, sizeof jsonStr);
ULONG size = sizeof jsonStr;
//获取文件有效数据大小
ret = SKF_ReadFile(devman->happlication, devman->fileName, 0, sizeof jsonStr, NULL, &size);
if (ret != SAR_OK) {
SOFT_LOG_ERR("SKF_ReadFile ret = %d \n", ret);
goto END;
}
if (size == 0)//有文件,但是没有内容
{
goto END;
}
ret = SKF_ReadFile(devman->happlication, devman->fileName, 0, size, jsonStr, &size);
if (ret != SAR_OK) {
SOFT_LOG_ERR("SKF_ReadFile ret = %d \n", ret);
goto END;
}
SOFT_LOG_DEBUG("SKF_ReadFile readlen = %d \n jsonStr = %s \n", size, jsonStr);
AUTHFILE_Unmarshal(authfile, jsonStr);
END:
DEVMANAGER_free(devman);
return ret;
}
int AUTHFILE_Write(AUTHFILE *authfile) {
int ret = 0;
DEVMANAGER *devman = DEVMANAGER_new();
ret = DEVMANAGE_PromtDevname(devman);
if (ret!=SAR_OK){
SOFT_LOG_ERR("DEVMANAGE_PromtDevname");
goto END;
}
ret = DEVMANAGER_OpenDev(devman);
if (ret!=SAR_OK){
SOFT_LOG_ERR("DEVMANAGER_OpenDev");
goto END;
}
ret = DEVMANAGER_OpenApp(devman);
if (ret!=SAR_OK){
SOFT_LOG_ERR("DEVMANAGER_OpenApp");
goto END;
}
ret = DEVMANAGER_VerifyPIN(devman);
if (ret != SAR_OK) {
SOFT_LOG_ERR("DEVMANAGER_VerifyPIN error ret= %x\n", ret);
goto END;
}
unsigned char jsonStr[1024];
int size = sizeof jsonStr;
AUTHFILE_EncryptPwd(authfile,NULL);
AUTHFILE_Marshal(authfile, jsonStr, &size);
ret = SKF_WriteFile(devman->happlication, devman->fileName, 0, jsonStr, size);
if (ret != SAR_OK) {
SOFT_LOG_ERR("SKF_WriteFile ret = %d \n", ret);
goto END;
}
SOFT_LOG_DEBUG("SKF_WriteFile ret = %d \n", ret);
END:
DEVMANAGER_free(devman);
return ret;
}
//AUTHFILE_EncryptPwd 计算pwd的摘要
int AUTHFILE_EncryptPwd(AUTHFILE *authfile, char *pwd) {
char *tpwd = NULL;
unsigned char digest[33];
memset(digest,0,sizeof digest);
if (pwd != NULL) {
tpwd = pwd;
} else if (strlen(authfile->enc_passwd) != 0) {
tpwd = authfile->enc_passwd;
}else{
return -1;//没有可用的密码
}
sm3((unsigned char*)tpwd, strlen(tpwd),digest);
BufToHex((char*)digest,32,authfile->enc_passwd);
return 0;
}
//AUTHFILE结构体导出为json字符串
int AUTHFILE_Marshal(AUTHFILE *authptr, BYTE *buf, int *len) {
cJSON *root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "inited", cJSON_CreateNumber(authptr->inited));
cJSON_AddItemToObject(root, "user", cJSON_CreateString(authptr->user));
cJSON_AddItemToObject(root, "enc_passwd", cJSON_CreateString(authptr->enc_passwd));
char *dumpstr = cJSON_Print(root);
if (buf == NULL) {
*len = strlen(dumpstr);
goto END;
}
if (*len <= strlen(dumpstr)) {
goto END;
}
strcpy((char*)buf, dumpstr);
*len = strlen(dumpstr);
END:
free(dumpstr);
cJSON_Delete(root);
return 0;
}
//json字符串导入为AUTHFILE结构体
int AUTHFILE_Unmarshal(AUTHFILE *authptr, BYTE *serial) {
cJSON *root = cJSON_Parse((char*)serial);
authptr->inited = cJSON_GetObjectItem(root, "inited")->valueint;
strcpy(authptr->user, cJSON_GetObjectItem(root, "user")->valuestring);
strcpy(authptr->enc_passwd, cJSON_GetObjectItem(root, "enc_passwd")->valuestring);
cJSON_Delete(root);
return 0;
}