forked from AktivCo/2fa-tuner-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkcs11_utils.sh
448 lines (367 loc) · 10.7 KB
/
pkcs11_utils.sh
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash
function check_pin()
{
token=$1
pin=$2
echolog "check user pin for token:$token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -l -p "$pin" --show-info --slot-description "$token" 2>&1`
res=$?
out=`echo -e "$out" | grep "CKR_PIN_LOCKED"`
if [[ "$out" ]]
then
echoerr "pin locked"
return 2
fi
if [[ $res -ne 0 ]]
then
echoerr "incorrect pin\n$out"
else
echolog "correct pin"
fi
return $res
}
function check_admin_pin()
{
token=$1
pin=$2
echolog "check admin pin for token:$token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -l --so-pin "$pin" --login-type so --show-info --slot-description "$token" 2>&1`
res=$?
out=`echo -e "$out" | grep "CKR_PIN_LOCKED"`
if ! [[ -z "$out" ]]
then
echoerr "pin locked"
return 2
fi
if [[ $res -ne 0 ]]
then
echoerr "incorrect pin\n$out"
else
echolog "correct pin"
fi
return $res
}
function import_obj_on_token ()
{
token=$1
type=$2
path_to_obj=$3
label=$4
key_id=$5
echolog "import object located by $path_to_obj with type: $type, id: $key_id, label:$label on token: $token "
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -l -p "$PIN" -y "$type" -w "$path_to_obj" --id "$key_id" --label "$label" --slot-description "$token" 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Can't import object on token:\n$out"
return 1
fi
return 0
}
function get_key_list ()
{
token="$1"
echolog "get_key_list from token: $token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -O --type pubkey --slot-description "$token" 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Error occured while getting key list:\n$out"
return 1
fi
key_ids=`echo -e "$out" | grep -Eo "ID:.*" | awk '{print $2}'`;
echolog "Key list:\n$key_ids"
echo "$key_ids";
return 0
}
function get_cert_list ()
{
token="$1"
echolog "get_cert_list from token: $token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -O --type cert --slot-description "$token" 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Error occured while getting cert list:\n$out"
return 1
fi
cert_ids=`echo -e "$out" | grep -Eo "ID:.*" | awk '{print $2}'`;
echo "$cert_ids";
echolog "Cert list:\n$cert_ids"
return 0
}
function pkcs11_gen_key ()
{
token=$1
key_id=$2
type=$3
label=$4
echolog "pkcs11_gen_key of type: $type with id: $key_id and label: $label on token: $token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" --keypairgen --key-type "$type" -l -p "$PIN" --id "$key_id" --label "$label" --slot-description "$token" 2>&1`
res=$?
if [[ "`echo -e "$out" | grep "Unknown key type"`" ]]
then
echoerr "Тип ключа $type не поддерживается в системе"
return 2
fi
if [[ $res -ne 0 ]]
then
echoerr "Error while creating key:\n$out"
else
echolog "Key is created successfully"
fi
return $res
}
function pkcs11_create_cert_req ()
{
local token="$1"
local key_id="$2"
local subj="$3"
local req_path="$4"
local selfsign="$5"
local key_usage="$6"
local expdays="$7"
echolog "pkcs11_create_cert_req for key_id: $key_id with subj: $subj by path: $req_path on token: $token. Cert is self_signed: $selfsign. keyUsage: $key_usage"
local key_id_ascii="`echo -e "$key_id" | sed 's/../%&/g'`"
echolog "key_id in ascii encoding is $key_id_ascii"
local obj=`get_token_objects "$token" "privkey" "id" "$key_id"
echolog "privkey for cert: $obj"`
local type=`get_object_attribute_value "$obj" "type"`
echolog "privatekey type is $type"
echolog "init pkcs11 engine for work"
if [[ "$type" == "RSA"* || "$type" == "ECDSA"* ]]
then
local engine_path="$PKCS11_ENGINE"
local engine_id=pkcs11
else
local engine_path="$RTENGINE"
local engine_id=rtengine
fi
echolog "engine_path is $engine_path and engine_id is $engine_id"
local serial=`get_token_info "$token" "serial"`
echolog "Token serial is $serial"
keyUsage="$key_usage" envsubst < "$TWO_FA_LIB_DIR/common_files/openssl_ext.cnf" | tee openssl_ext.cnf > /dev/null
rm -f "$req_path"
local extra_args="-outform PEM"
[[ $selfsign -eq 1 ]] && extra_args="-outform DER -x509"
local out=`OPENSSL_CONF="$OPENSSL_CONF" "$OPENSSL" req -engine $engine_id -new -utf8 -key "pkcs11:serial=$serial;id=$key_id_ascii" -keyform engine -passin "pass:$PIN" -subj "$subj" ${key_usage+-config openssl_ext.cnf} -days $expdays -out "$req_path" ${extra_args}`;
[[ ! -f "$req_path" ]] && { echoerr "Can't create self signed cert or req:\n$out"; return 1; }
if [[ $selfsign -eq 1 ]]; then
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -l -p "$PIN" -y cert -w "$req_path" --id $key_id --slot-description "$token" 2>&1`;
[[ $? -ne 0 ]] && { echoerr "Can't move cert on token:\n$out"; return 1; }
fi
return 0
}
function get_token_list ()
{
echolog "get_token_lsit"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -T 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Can't get token list:\n$out"
return 1
fi
token_list=`echo -e "$out" | grep "Slot *" | cut -d ":" -f2- | awk '$1=$1'`
echolog "Token list:\n$token_list"
echo -e "$token_list"
return 0
}
function get_token_info ()
{
token=$1
atr=$2
echolog "get_token_info token: $token atr: $atr"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -T 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Can't get token info:\n$out"
return 1
fi
token_info="`echo -e "$out" | sed -n "/^.*$token.*$/,$ p" | awk '{$1=$1;print}' | sed -E "s/[[:space:]]*:[[:space:]]+/ /" | uniq | awk '/Slot /{++n} n<2'`"
echolog "Token info:\n $token_info"
if [[ "$atr" ]]
then
atr_val=`echo -e "$token_info" | grep "$atr" | cut -f 2`
echolog "Atr: $atr frrom token info for token: $token is $atr_val"
echo -e "$atr_val"
return 0
fi
echo -e "$token_info"
return 0
}
function get_token_objects ()
{
token="$1"
type="$2"
attr="$3"
val="$4"
echolog "get_token_objects from token $token of type: $type with atr: $attr value: $val"
if [[ "$type" ]]
then
type_arg="--type $type"
fi
objs=`pkcs11-tool --module "$LIBRTPKCS11ECP" -O -l -p "$PIN" $type_arg --slot-description "$token"`
if [[ $? -ne 0 ]]
then
echoerr "Error while getting objects from token:\n$out"
return 1
fi
echolog "Object list:\n$objs"
if [[ "$attr" ]]
then
objs=`python3 "$TWO_FA_LIB_DIR/python_utils/parse_objects.py" "$objs" "$type" "$attr" "$val"`
echolog "formated filtered objects:\n$objs"
else
objs=`python3 "$TWO_FA_LIB_DIR/python_utils/parse_objects.py" "$objs"`
echolog "formated objects:\n$objs"
fi
echo -e "$objs"
return 0
}
function get_object_attribute_value ()
{
obj=$1
attr=$2
echolog "get object: $obj attribute: $attr"
out=`echo -e "$obj" | python3 -c "import json,sys; obj=json.load(sys.stdin); print(obj[\"$attr\"])" 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "error occured while getting attr $attr of object $obj:\n$out"
return 1
fi
echolog "Attr $attr value is $out"
echo -e "$out"
return $?
}
function pkcs11_format_token ()
{
local token="$1"
local user_pin="$2"
local admin_pin="$3"
echolog "pkcs11_format_token $token"
list=`get_token_list`
if [[ "`echo -e "$list" | wc -l`" -ne 1 ]]
then
echoerr "Вставленно более одного токена"
return 2
fi
out=`$RTADMIN -z "$LIBRTPKCS11ECP" -f -u "$user_pin" -a "$admin_pin" -q`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during format token:\n$out"
return 1
fi
echolog "Token formated"
PIN=$user_pin
return 0
}
function pkcs11_change_user_pin ()
{
token=$1
old_pin=$PIN
new_pin=$2
echolog "pkcs11_change_user_pin $token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" --change-pin -l -p "$old_pin" --new-pin "$new_pin" --slot-description "$token" 2>&1`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during change user pin:\n$out"
return 1
fi
echolog "user oin changed"
PIN=$new_pin
return 0
}
function pkcs11_change_admin_pin ()
{
local token=$1
local old_pin=$2
local new_pin=$3
echolog "pkcs11_change_admin_pin $token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" -c --login-type so --so-pin "$old_pin" -l --new-pin "$new_pin"`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during change admin pin:\n$out"
return 1
fi
echolog "admin pin changed"
return 0
}
function pkcs11_unlock_pin ()
{
local token=$1
local so_pin=$2
echolog "pkcs11_unlock_pin $token"
list=`get_token_list`
if [[ "`echo -e "$list" | wc -l`" -ne 1 ]]
then
echoerr "Вставленно более одного токена"
return 2
fi
out=`$RTADMIN -z "$LIBRTPKCS11ECP" -q -P -o "$so_pin"`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during unlock user pin:\n$out"
return 1
fi
echolog "User PIN unlocked"
return 0
}
function export_object ()
{
local token=$1
local type=$2
local id=$3
local file=$4
echolog "export_object of type:$type with id: $id from token:$token to file:$file"
pkcs11-tool --module "$LIBRTPKCS11ECP" --slot-description "$token" -r --type "$type" --id "$id" -l -p "$PIN" > "$file"
if [[ $? -ne 0 ]]
then
out=`cat "$file"`
rm "$file"
echoerr "Error occured while export object from token:\n$out"
return 1
fi
return 0
}
function remove_object ()
{
local token=$1
local type=$2
local id=$3
echolog "remove_object of type:$type with id: $id from token:$token"
out=`pkcs11-tool --module "$LIBRTPKCS11ECP" --slot-description "$token" -b --type "$type" --id "$id" -l -p "$PIN"`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during remove object from token:\n$out"
return 1
fi
return 0
}
function unlock_part ()
{
local token=$1
local local_user_pin=$2
local id=$3
local perm=$4
local type=$5
echolog "Unlock partition with id=$id to permissions=$perm of type=$type"
owner=`get_part_info -i $id | cut -f 3`
out="`$RTADMIN -z "$LIBRTPKCS11ECP" -q -C $id $perm $type -O $owner "$local_user_pin" 2>&1`"
if [[ $? -ne 0 ]]
then
echoerr "Error occured during change partiton permissions:\n$out"
return 1
fi
return 0
}
function get_part_info ()
{
local token=$1
local id=$2
echolog "Get info of partition with id=$id"
out="`$RTADMIN -z "$LIBRTPKCS11ECP" -q -i "$id" | sed '$!N;s/\n/ /' | cut -d ":" -f 2- | tr ":" $"\t" | awk '{$1=$1};1' | tr " " $"\t"`"
if [[ $? -ne 0 ]]
then
echoerr "Error occured during getting partition info:\n$out"
return 1
fi
echo -e "$out"
return 0
}