某些应用程序要求使用对称密钥对通信进行加密和解密。在此过程中,应创建一个对称密钥并存储它。
如果您的站点有随机数生成器,可使用此生成器为该密钥创建随机数。此过程不使用您站点的随机数生成器。
使用以下方法之一。
以文件存储的密钥的优点是,您可以从该文件提取要在应用程序的密钥文件中使用的密钥,如 /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 ]
存储密钥的文件名。
无论对称密钥的长度如何,值均为 generic。对于特定算法,请指定 aes、arcfour、des 或 3des。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥类型。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
密钥的长度(位)。该数字必须可以被 8 整除。请勿对 des 或 3des 指定此值。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥长度。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
将密钥显示至终端窗口。缺省情况下,print 的值为 n。
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 ]
密钥的用户指定标签。可以从密钥库中按密钥标签检索密钥。
无论对称密钥的长度如何,值均为 generic。对于特定算法,请指定 aes、arcfour、des 或 3des。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥类型。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
密钥的长度(位)。该数字必须可以被 8 整除。请勿对 des 或 3des 指定此值。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥长度。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
令牌名称。缺省情况下,令牌是 Sun Software PKCS#11 softtoken。
指定密钥的敏感度。当此值为 y 时,不可使用 print=y 参数显示密钥。缺省情况下,sensitive 的值为 n。
指定可从密钥库提取密钥。指定 n 可阻止提取密钥。
将密钥显示至终端窗口。缺省情况下,print 的值为 n。
在使用此方法之前,必须先完成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 ]
密钥的用户指定标签。可以从密钥库中按密钥标签检索密钥。
无论对称密钥的长度如何,值均为 generic。对于特定算法,请指定 aes、arcfour、des 或 3des。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥类型。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
密钥的长度(位)。该数字必须可以被 8 整除。请勿对 des 或 3des 指定此值。
对于 FIPS 140 认可的算法,选择已针对 FIPS 验证的密钥长度。请参见Using a FIPS 140 Enabled System in Oracle Solaris 11.2 中的FIPS 140 Algorithms in the Cryptographic Framework。
令牌名称。缺省情况下,该令牌为 NSS 内部令牌。
NSS 数据库的目录路径。缺省情况下,directory 是当前目录。
NSS 数据库的前缀。缺省为无前缀。
根据密钥的存储位置,使用以下命令之一。
% pktool list keystore=file objtype=key [infile=key-fn] Found n keys. Key #1 - keytype:location (keylen)
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。
在以下示例中,用户首先创建一个 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。