如何使用 pktool 命令生成对称密钥
某些应用程序要求使用对称密钥对通信进行加密和解密。在此过程中,应创建一个对称密钥并存储它。
如果您的站点有随机数生成器,可使用此生成器为该密钥创建随机数。此过程不使用您站点的随机数生成器。
(u53ef选)
如果计划使用密钥库,需要创建一个。
有关创建和初始化 PKCS #11 密钥库的信息,请参见如何使用 pktool setpin 命令生成口令短语。
要创建和初始化 NSS 数据库,请参见Example 4–5 中的示例命令。
生成一个随机数以用作对称密钥。
使用以下方法之一。
生成一个密钥并将其存储于文件中。
以文件存储的密钥的优点是,您可以从该文件提取要在应用程序的密钥文件中使用的密钥,如 /etc/inet/secret/ipseckeys 文件或 IPsec。用法语句显示了参数。
% pktool genkey keystore=file
...genkey keystore=file
outkey=key-fn
[ keytype=aes|arcfour|des|3des|generic ]
[ keylen=key-size (AES, ARCFOUR or GENERIC only)]
[ print=y|n ]
outkey=key-fn
存储密钥的文件名。
keytype=specific-symmetric-algorithm
keylen=size-in-bits
print=n
将密钥显示至终端窗口。缺省情况下,print 的值为 n。
生成一个密钥并将其存储于 PKCS #11 密钥库中。
PKCS #11 密钥库的优点是,您可以按密钥标签检索密钥。此方法适用于加密和解密文件所用的密钥。在使用此方法之前,必须先完成Step 1。用法语句显示了参数。包围着 keystore 参数的括号表示未指定 keystore 参数时,密钥将存储在 PKCS #11 密钥库中。
$ pktool genkey keystore=pkcs11
...genkey [ keystore=pkcs11 ]
label=key-label
[ keytype=aes|arcfour|des|3des|generic ]
[ keylen=key-size (AES, ARCFOUR or GENERIC only)]
[ token=token[:manuf[:serial]]]
[ sensitive=y|n ]
[ extractable=y|n ]
[ print=y|n ]
label=key-label
密钥的用户指定标签。可以从密钥库中按密钥标签检索密钥。
keytype=specific-symmetric-algorithm
keylen=size-in-bits
token=token
令牌名称。缺省情况下,令牌是 Sun Software PKCS#11 softtoken。
sensitive=n
指定密钥的敏感度。当此值为 y 时,不可使用 print=y 参数显示密钥。缺省情况下,sensitive 的值为 n。
extractable=y
指定可从密钥库提取密钥。指定 n 可阻止提取密钥。
print=n
将密钥显示至终端窗口。缺省情况下,print 的值为 n。
生成一个密钥并将其存储于 NSS 密钥库中。
在使用此方法之前,必须先完成Step 1。用法语句显示了参数。
$ pktool genkey keystore=nss
...genkey keystore=nss
label=key-label
[ keytype=aes|arcfour|des|3des|generic ]
[ keylen=key-size (AES, ARCFOUR or GENERIC only)]
[ token=token[:manuf[:serial]]]
[ dir=directory-path ]
[ prefix=DBprefix ]
label=key-label
密钥的用户指定标签。可以从密钥库中按密钥标签检索密钥。
keytype=specific-symmetric-algorithm
keylen=size-in-bits
token=token
令牌名称。缺省情况下,该令牌为 NSS 内部令牌。
dir=directory
NSS 数据库的目录路径。缺省情况下,directory 是当前目录。
prefix=directory
NSS 数据库的前缀。缺省为无前缀。
(u53ef选)
检验密钥是否存在。
根据密钥的存储位置,使用以下命令之一。
检验 key-fn 文件中的密钥。
% pktool list keystore=file objtype=key [infile=key-fn]
Found n keys.
Key #1 - keytype:location (keylen)
检验 PKCS #11 或 NSS 密钥库中的密钥。
For PKCS #11, use the following command:
$ pktool list keystore=pkcs11 objtype=key
Enter PIN for keystore:
Found n keys.
Key #1 - keytype:location (keylen)
或者,在命令中将 keystore=pkcs11 替换为 keystore=nss。
示例 3-1使用 pktool 命令创建对称密钥
在以下示例中,用户首先创建一个 PKCS #11 密钥库,随后为应用程序生成一个较大的对称密钥。最后,用户验证密钥是否位于密钥库中。
请注意,PKCS #11 密钥库的初始口令是 changeme。NSS 密钥库的初始口令是空口令。
# pktool setpin
Create new passphrase:Type password
Re-enter new passphrase:Retype password
Passphrase changed.
% pktool genkey label=specialappkey keytype=generic keylen=1024
Enter PIN for Sun Software PKCS#11 softtoken :Type password
% pktool list objtype=key
Enter PIN for Sun Software PKCS#11 softtoken :Type password
No. Key Type Key Len. Key Label
----------------------------------------------------
Symmetric keys:
1 Symmetric 1024 specialappkey
示例 3-2使用 pktool 命令创建 FIPS 认可的 AES 密钥
在下面的示例中,将使用 FIPS 认可的算法和密钥长度创建一个用于 AES 算法的密钥。该密钥存储在本地文件中以供日后解密之用。该命令使用 400 权限保护文件。创建了密钥后,print=y 选项将在终端窗口显示生成的密钥。
拥有密钥文件的用户将使用 od 命令检索密钥。
% pktool genkey keystore=file outkey=256bit.file1 keytype=aes keylen=256 print=y
Key Value ="aaa2df1d10f02eaee2595d48964847757a6a49cf86c4339cd5205c24ac8c8873"
% od -x 256bit.file1
0000000 aaa2 df1d 10f0 2eae e259 5d48 9648 4775
0000020 7a6a 49cf 86c4 339c d520 5c24 ac8c 8873
0000040
示例 3-3为 IPsec 安全关联创建对称密钥
在以下示例中,管理员手动为 IPsec SA 创建密钥材料并将其存储在文件中。然后,管理员将密钥复制到 /etc/inet/secret/ipseckeys 文件,并销毁原始文件。
首先,管理员创建并显示 IPsec 策略要求的密钥:
# pktool genkey keystore=file outkey=ipencrin1 keytype=generic keylen=192 print=y
Key Value ="294979e512cb8e79370dabecadc3fcbb849e78d2d6bd2049"
# pktool genkey keystore=file outkey=ipencrout1 keytype=generic keylen=192 print=y
Key Value ="9678f80e33406c86e3d1686e50406bd0434819c20d09d204"
# pktool genkey keystore=file outkey=ipspi1 keytype=generic keylen=32 print=y
Key Value ="acbeaa20"
# pktool genkey keystore=file outkey=ipspi2 keytype=generic keylen=32 print=y
Key Value ="19174215"
# pktool genkey keystore=file outkey=ipsha21 keytype=generic keylen=256 print=y
Key Value ="659c20f2d6c3f9570bcee93e96d95e2263aca4eeb3369f72c5c786af4177fe9e"
# pktool genkey keystore=file outkey=ipsha22 keytype=generic keylen=256 print=y
Key Value ="b041975a0e1fce0503665c3966684d731fa3dbb12fcf87b0a837b2da5d82c810"
然后,管理员创建以下 /etc/inet/secret/ipseckeys 文件:
## SPI values require a leading 0x.
## Backslashes indicate command continuation.
##
## for outbound packets on this system
add esp spi 0xacbeaa20 \
src 192.168.1.1 dst 192.168.2.1 \
encr_alg aes auth_alg sha256 \
encrkey 294979e512cb8e79370dabecadc3fcbb849e78d2d6bd2049 \
authkey 659c20f2d6c3f9570bcee93e96d95e2263aca4eeb3369f72c5c786af4177fe9e
##
## for inbound packets
add esp spi 0x19174215 \
src 192.168.2.1 dst 192.168.1.1 \
encr_alg aes auth_alg sha256 \
encrkey 9678f80e33406c86e3d1686e50406bd0434819c20d09d204 \
authkey b041975a0e1fce0503665c3966684d731fa3dbb12fcf87b0a837b2da5d82c810
在验证 ipseckeys 文件的语法有效之后,管理员将销毁原始密钥文件。
# ipseckey -c /etc/inet/secret/ipseckeys
# rm ipencrin1 ipencrout1 ipspi1 ipspi2 ipsha21 ipsha22
管理员通过使用 ssh 命令或其他安全机制,将 ipseckeys 文件复制到通信系统。在通信系统中,将反转保护。ipseckeys 文件中的第一个条目保护传入数据包,第二个条目保护传出数据包。通信系统中没有生成任何密钥。
接下来的步骤
要继续使用该密钥创建文件的消息验证代码 (Message Authentication Code, MAC),请参阅如何计算文件的 MAC。