1 # Step by step to generate sample self-signed X.509 certificate chain and sign data with PKCS7 structure
3 This 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.
5 ## How to generate a self-signed X.509 certificate chain via OPENSSL
6 * Set OPENSSL environment.
8 NOTE: Below steps are required for Windows. Linux may already have the OPENSSL environment correctly.
10 set OPENSSL_HOME=c:\home\openssl\openssl-[version]
11 set OPENSSL_CONF=%OPENSSL_HOME%\apps\openssl.cnf
13 When 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).
15 The user need check the openssl.cnf file, to find your CA path setting, e.g. check if the path exists in [ CA_default ] section.
18 dir = ./demoCA # Where everything is kept
20 You may need the following steps for initialization:
24 echo.>./demoCA/index.txt
25 echo 01 > ./demoCA/serial
26 mkdir ./demoCA/newcerts
28 OpenSSL 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.
29 The following sample sections were used when generating test certificates in this readme.
33 default_keyfile = privkey.pem
34 distinguished_name = req_distinguished_name
35 attributes = req_attributes
36 x509_extensions = v3_ca # The extensions to add to the self signed cert
39 # Extensions for a typical Root CA.
40 subjectKeyIdentifier=hash
41 authorityKeyIdentifier=keyid:always,issuer
42 basicConstraints = critical,CA:true
43 keyUsage = critical, digitalSignature, cRLSign, keyCertSign
45 [ v3_intermediate_ca ]
46 # Extensions for a typical intermediate CA.
47 subjectKeyIdentifier = hash
48 authorityKeyIdentifier = keyid:always,issuer
49 basicConstraints = critical, CA:true
50 keyUsage = critical, digitalSignature, cRLSign, keyCertSign
53 # Extensions for user end certificates.
54 basicConstraints = CA:FALSE
55 nsCertType = client, email
56 subjectKeyIdentifier = hash
57 authorityKeyIdentifier = keyid,issuer
58 keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
59 extendedKeyUsage = clientAuth, emailProtection
62 * Generate the certificate chain:
64 NOTE: User MUST set a UNIQUE "Common Name" on the different certificate
66 1) Generate the Root Pair:
70 openssl genrsa -aes256 -out TestRoot.key 2048
72 Generate a self-signed root certificate:
74 openssl req -extensions v3_ca -new -x509 -days 3650 -key TestRoot.key -out TestRoot.crt
75 openssl x509 -in TestRoot.crt -out TestRoot.cer -outform DER
76 openssl x509 -inform DER -in TestRoot.cer -outform PEM -out TestRoot.pub.pem
78 2) Generate the Intermediate Pair:
80 Generate the intermediate key:
82 openssl genrsa -aes256 -out TestSub.key 2048
84 Generate the intermediate certificate:
86 openssl req -new -days 3650 -key TestSub.key -out TestSub.csr
87 openssl ca -extensions v3_intermediate_ca -in TestSub.csr -days 3650 -out TestSub.crt -cert TestRoot.crt -keyfile TestRoot.key
88 openssl x509 -in TestSub.crt -out TestSub.cer -outform DER
89 openssl x509 -inform DER -in TestSub.cer -outform PEM -out TestSub.pub.pem
91 3) Generate User Key Pair for Data Signing:
95 openssl genrsa -aes256 -out TestCert.key 2048
97 Generate User certificate:
99 openssl req -new -days 3650 -key TestCert.key -out TestCert.csr
100 openssl ca -extensions usr_cert -in TestCert.csr -days 3650 -out TestCert.crt -cert TestSub.crt -keyfile TestSub.key
101 openssl x509 -in TestCert.crt -out TestCert.cer -outform DER
102 openssl x509 -inform DER -in TestCert.cer -outform PEM -out TestCert.pub.pem
104 Convert Key and Certificate for signing. Password is removed with -nodes flag for convenience in this sample.
106 openssl pkcs12 -export -out TestCert.pfx -inkey TestCert.key -in TestCert.crt
107 openssl pkcs12 -in TestCert.pfx -nodes -out TestCert.pem
109 * Verify Data Signing & Verification with new X.509 Certificate Chain
111 1) Sign a Binary File to generate a detached PKCS7 signature:
113 openssl smime -sign -binary -signer TestCert.pem -outform DER -md sha256 -certfile TestSub.pub.pem -out test.bin.p7 -in test.bin
115 2) Verify PKCS7 Signature of a Binary File:
117 openssl smime -verify -inform DER -in test.bin.p7 -content test.bin -CAfile TestRoot.pub.pem -out test.org.bin