]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Pkcs7Sign/Readme.md
BaseTools:replace the chinese quotation mark with unicode "
[mirror_edk2.git] / BaseTools / Source / Python / Pkcs7Sign / Readme.md
CommitLineData
fdaf7842
JY
1# Step by step to generate sample self-signed X.509 certificate chain and sign data with PKCS7 structure\r
2\r
3This readme demonstrates how to generate 3-layer X.509 certificate chain (RootCA -> IntermediateCA -> SigningCert) with OpenSSL commands, and user MUST set a UNIQUE Subject Name ("Common Name") on these three different certificates.\r
4\r
5## How to generate a self-signed X.509 certificate chain via OPENSSL\r
6* Set OPENSSL environment.\r
7\r
8NOTE: Below steps are required for Windows. Linux may already have the OPENSSL environment correctly.\r
9\r
10 set OPENSSL_HOME=c:\home\openssl\openssl-[version]\r
11 set OPENSSL_CONF=%OPENSSL_HOME%\apps\openssl.cnf\r
12\r
76912197 13When a user uses OpenSSL (req or ca command) to generate the certificates, OpenSSL will use the openssl.cnf file as the configuration data (can use "-config path/to/openssl.cnf" to describe the specific config file).\r
fdaf7842
JY
14\r
15The user need check the openssl.cnf file, to find your CA path setting, e.g. check if the path exists in [ CA_default ] section.\r
16\r
17 [ CA_default ]\r
18 dir = ./demoCA # Where everything is kept\r
19\r
20You may need the following steps for initialization:\r
21\r
22 rd ./demoCA /S/Q\r
23 mkdir ./demoCA\r
f536d7c3 24 echo.>./demoCA/index.txt\r
fdaf7842
JY
25 echo 01 > ./demoCA/serial\r
26 mkdir ./demoCA/newcerts\r
27\r
f536d7c3
LQ
28OpenSSL will apply the options from the specified sections in openssl.cnf when creating certificates or certificate signing requests. Make sure your configuration in openssl.cnf is correct and rational for certificate constraints.\r
29The following sample sections were used when generating test certificates in this readme.\r
30 ...\r
31 [ req ]\r
32 default_bits = 2048\r
33 default_keyfile = privkey.pem\r
34 distinguished_name = req_distinguished_name\r
35 attributes = req_attributes\r
36 x509_extensions = v3_ca # The extensions to add to the self signed cert\r
37 ...\r
38 [ v3_ca ]\r
39 # Extensions for a typical Root CA.\r
40 subjectKeyIdentifier=hash\r
41 authorityKeyIdentifier=keyid:always,issuer\r
42 basicConstraints = critical,CA:true\r
43 keyUsage = critical, digitalSignature, cRLSign, keyCertSign\r
44 ...\r
45 [ v3_intermediate_ca ]\r
46 # Extensions for a typical intermediate CA.\r
47 subjectKeyIdentifier = hash\r
48 authorityKeyIdentifier = keyid:always,issuer\r
49 basicConstraints = critical, CA:true\r
50 keyUsage = critical, digitalSignature, cRLSign, keyCertSign\r
51 ...\r
52 [ usr_cert ]\r
53 # Extensions for user end certificates.\r
54 basicConstraints = CA:FALSE\r
55 nsCertType = client, email\r
56 subjectKeyIdentifier = hash\r
57 authorityKeyIdentifier = keyid,issuer\r
58 keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment\r
59 extendedKeyUsage = clientAuth, emailProtection\r
60 ...\r
61\r
fdaf7842
JY
62* Generate the certificate chain:\r
63\r
64NOTE: User MUST set a UNIQUE "Common Name" on the different certificate\r
65\r
661) Generate the Root Pair:\r
67\r
68Generate a root key:\r
69\r
70 openssl genrsa -aes256 -out TestRoot.key 2048\r
71\r
72Generate a self-signed root certificate:\r
73\r
f536d7c3 74 openssl req -extensions v3_ca -new -x509 -days 3650 -key TestRoot.key -out TestRoot.crt\r
fdaf7842
JY
75 openssl x509 -in TestRoot.crt -out TestRoot.cer -outform DER\r
76 openssl x509 -inform DER -in TestRoot.cer -outform PEM -out TestRoot.pub.pem\r
77\r
782) Generate the Intermediate Pair:\r
79\r
80Generate the intermediate key:\r
81\r
82 openssl genrsa -aes256 -out TestSub.key 2048\r
83\r
84Generate the intermediate certificate:\r
85\r
86 openssl req -new -days 3650 -key TestSub.key -out TestSub.csr\r
f536d7c3 87 openssl ca -extensions v3_intermediate_ca -in TestSub.csr -days 3650 -out TestSub.crt -cert TestRoot.crt -keyfile TestRoot.key\r
fdaf7842
JY
88 openssl x509 -in TestSub.crt -out TestSub.cer -outform DER\r
89 openssl x509 -inform DER -in TestSub.cer -outform PEM -out TestSub.pub.pem\r
90\r
913) Generate User Key Pair for Data Signing:\r
92\r
93Generate User key:\r
94\r
95 openssl genrsa -aes256 -out TestCert.key 2048\r
96\r
97Generate User certificate:\r
98\r
99 openssl req -new -days 3650 -key TestCert.key -out TestCert.csr\r
f536d7c3 100 openssl ca -extensions usr_cert -in TestCert.csr -days 3650 -out TestCert.crt -cert TestSub.crt -keyfile TestSub.key\r
fdaf7842
JY
101 openssl x509 -in TestCert.crt -out TestCert.cer -outform DER\r
102 openssl x509 -inform DER -in TestCert.cer -outform PEM -out TestCert.pub.pem\r
103\r
104Convert Key and Certificate for signing. Password is removed with -nodes flag for convenience in this sample.\r
105\r
106 openssl pkcs12 -export -out TestCert.pfx -inkey TestCert.key -in TestCert.crt\r
107 openssl pkcs12 -in TestCert.pfx -nodes -out TestCert.pem\r
108\r
109* Verify Data Signing & Verification with new X.509 Certificate Chain\r
110\r
1111) Sign a Binary File to generate a detached PKCS7 signature:\r
112\r
113 openssl smime -sign -binary -signer TestCert.pem -outform DER -md sha256 -certfile TestSub.pub.pem -out test.bin.p7 -in test.bin\r
114\r
1152) Verify PKCS7 Signature of a Binary File:\r
116\r
117 openssl smime -verify -inform DER -in test.bin.p7 -content test.bin -CAfile TestRoot.pub.pem -out test.org.bin\r
118\r
526dd024
KM
119## Generate DSC PCD include files for Certificate\r
120\r
121The `BinToPcd` utility can be used to convert the binary Certificate file to a\r
122text file can be included from a DSC file to set a PCD to the contents of the\r
123Certificate file.\r
124\r
125The following 2 PCDs can be set to the PKCS7 Certificate value. The first one\r
126supports a single certificate. The second one supports multiple certificate\r
127values using the XDR format.\r
128* `gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer`\r
129* `gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr`\r
130\r
131Generate DSC PCD include files:\r
132```\r
133BinToPcd.py -i TestRoot.cer -p gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer -o TestRoot.cer.gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer.inc\r
134BinToPcd.py -i TestRoot.cer -p gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr -x -o TestRoot.cer.gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr.inc\r
135```\r
136\r
137These files can be used in `!include` statements in DSC file PCD sections. For example:\r
138\r
139* Platform scoped fixed at build PCD section\r
140```\r
141[PcdsFixedAtBuild]\r
142 !include BaseTools/Source/Python/Pkcs7Sign/TestRoot.cer.gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer.inc\r
143```\r
144\r
145* Platform scoped patchable in module PCD section\r
146```\r
147[PcdsPatchableInModule]\r
148 !include BaseTools/Source/Python/Pkcs7Sign/TestRoot.cer.gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr.inc\r
149```\r
150\r
151* Module scoped fixed at build PCD section\r
152```\r
153[Components]\r
154 FmpDevicePkg/FmpDxe/FmpDxe.inf {\r
155 <PcdsFixedAtBuild>\r
156 !include BaseTools/Source/Python/Pkcs7Sign/TestRoot.cer.gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr.inc\r
157 }\r
158```\r