-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_keybox.sh
48 lines (40 loc) · 1.52 KB
/
generate_keybox.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
#!/bin/bash
DEVICE_ID="Device-$(openssl rand -hex 8)"
TITLE="TEE"
CA_KEY="ca.key"
CA_CRT="ca.crt"
DEVICE_KEY="device.key"
DEVICE_CSR="device.csr"
DEVICE_CRT="device.crt"
KEYBOX_XML="keybox.xml"
# 生成 CA 私钥和自签名证书(有效期 10 年)
openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out $CA_KEY
openssl req -key $CA_KEY -new -x509 -days 3650 -subj "/CN=My Root CA/title=$TITLE" -out $CA_CRT
# 生成设备私钥和 CSR(证书签名请求)
openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out $DEVICE_KEY
openssl req -new -key $DEVICE_KEY -subj "/CN=My Device/title=$TITLE" -out $DEVICE_CSR
# 由 CA 签署设备证书(有效期 1 年)
openssl x509 -req -in $DEVICE_CSR -CA $CA_CRT -CAkey $CA_KEY -CAcreateserial -days 365 -out $DEVICE_CRT
# 读取私钥和证书内容,并保留格式
DEVICE_KEY_PEM=$(awk '{print " "$0}' $DEVICE_KEY)
DEVICE_CRT_PEM=$(awk '{print " "$0}' $DEVICE_CRT)
cat > $KEYBOX_XML <<EOF
<?xml version="1.0"?>
<AndroidAttestation>
<NumberOfKeyboxes>1</NumberOfKeyboxes>
<Keybox DeviceID="$DEVICE_ID">
<Key algorithm="ecdsa">
<PrivateKey format="pem">
$DEVICE_KEY_PEM
</PrivateKey>
<CertificateChain>
<NumberOfCertificates>1</NumberOfCertificates>
<Certificate format="pem">
$DEVICE_CRT_PEM
</Certificate>
</CertificateChain>
</Key>
</Keybox>
</AndroidAttestation>
EOF
echo "Keybox 文件已生成: $KEYBOX_XML"