简体   繁体   English

X509证书单元测试-我需要创建一个证书和包含它的CRL

[英]X509 certificate unit testing - I need to create a certificate and a CRL which has it

Ok, so I have a method which is able to be passed a CRL and a certificate. 好的,所以我有一个可以通过CRL和证书的方法。 I used it to validate the certificates coming from vendors during the runtime of my application. 我使用它来验证应用程序运行期间来自供应商的证书。

The hardest part of this has been unit testing the damn thing! 其中最难的部分是对可恶的东西进行单元测试!

I need to create a certificate file, and a CRL file, which I can then distribute as resources with the application, and then pass them in during the unit test. 我需要创建一个证书文件和一个CRL文件,然后可以将其作为资源与应用程序一起分发,然后在单元测试期间将它们传递给我。

I have the unit test written also, but with hard coded values - But now I need to know how to create the following : 我也编写了单元测试,但是带有硬编码的值-但是现在我需要知道如何创建以下内容:

two X509 Certificates. 两个X509证书。 One CRL 一份CRL

I need the CRL to have an entry for one of the two X509 certs. 我需要CRL拥有两个X509证书之一的条目。 This will allow me to test for revocation outcome, and also non-revocation. 这将使我能够测试撤销结果以及非撤销。

I have tried the following guide, but security is far from my strong point - and I can't get it to work. 我尝试了以下指南,但是安全性远非我的强项-我无法使其正常工作。

https://blog.didierstevens.com/2013/05/08/howto-make-your-own-cert-and-revocation-list-with-openssl/

Jamie Nguyen has a great guide on creating a Certificate Authority and issuing certificates and CRLs, which can be found here . Jamie Nguyen在创建证书颁发机构以及颁发证书和CRL方面提供了很好的指南,可在此处找到。 This is what I will be loosely referring to while answering your question. 这是我在回答您的问题时会粗略提及的内容。 If at any point you're curious what the output of these commands would look like, I refer you to his site. 如果您随时想知道这些命令的输出是什么样子,那么我会将您引到他的网站。 They are omitted here to keep this already long post manageable. 为了使本已很长的帖子易于管理,此处将其省略。

Basically we'll need to do the following: 基本上,我们需要执行以下操作:

  • Create a self-signed certificate to act as the Certificate Authority 创建一个自签名证书以充当证书颁发机构
  • Use the CA certificate to sign two leaf certificates 使用CA证书签署两个叶子证书
  • Revoke one of the leaf certificates 吊销其中一张叶子证书
  • Publish a CRL 发布CRL

Creating Self-signed CA Certificate 创建自签名CA证书

First we'll need to prepare the CA configuration. 首先,我们需要准备CA配置。 If you don't want to go through his tutorial, you can simply use the following, somewhat abbreviated config: 如果您不想阅读他的教程,则可以简单地使用以下略略的配置:

# OpenSSL root CA configuration file.

[ ca ]
default_ca = ca_default

[ ca_default ]
dir               = /etc/pki/CA
certs             = $dir/certs
crl_dir           = $dir/crl
new_certs_dir     = $dir/newcerts
database          = $dir/db/root-ca.index
serial            = $dir/db/root-ca.serial
RANDFILE          = $dir/private/.rand
private_key       = $dir/private/root-ca.key
certificate       = $dir/certs/root-ca.crt
crlnumber         = $dir/db/root-ca.crlnumber
crl               = $dir/crl/root-ca.crl
crl_extensions    = crl_ext
default_crl_days  = 180
default_md        = sha384
name_opt          = ca_default
cert_opt          = ca_default
default_days      = 375
preserve          = no
policy            = policy_loose

[ policy_strict ]
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ policy_loose ]
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ req ]
default_bits        = 3072
distinguished_name  = req_distinguished_name
string_mask         = utf8only
default_md          = sha384
x509_extensions     = root_ca

[ req_distinguished_name ]
countryName                     = Country Name (2 letter code)
stateOrProvinceName             = State or Province Name
localityName                    = Locality Name
0.organizationName              = Organization Name
organizationalUnitName          = Organizational Unit Name
commonName                      = Common Name
emailAddress                    = Email Address
countryName_default             = US
stateOrProvinceName_default     = MD
localityName_default            =
0.organizationName_default      = LAB
organizationalUnitName_default  =
emailAddress_default            =

[ root_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ usr_cert ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, emailProtection
crlDistributionPoints = URI:http://pki.lab.local/int-ca.crl
authorityInfoAccess = caIssuers;URI:http://pki.lab.local/int-ca.crt

[ server_cert ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
crlDistributionPoints = URI:http://pki.lab.local/int-ca.crl
authorityInfoAccess = caIssuers;URI:http://pki.lab.local/int-ca.crt

[ crl_ext ]
authorityKeyIdentifier=keyid:always
authorityInfoAccess = caIssuers;URI:http://pki.lab.local/int-ca.crt

This should be placed in the working directory as openssl.cnf . 这应该放在openssl.cnf目录下。

You should modify the dir = /etc/pki/CA line to point to the directory you'll be working out of, and possibly change the CDP URLs to point to an actual domain if you want to enable revocation checking via CDP. 您应该修改dir = /etc/pki/CA行以指向您要使用的目录,并且如果要启用通过CDP的吊销检查,则可以更改CDP URL以指向实际的域。

Next we'll need to create some files and directories expected by the CA, then generate an RSA private key for the CA certificate. 接下来,我们需要创建CA期望的一些文件和目录,然后为CA证书生成RSA私钥。 Note: The CA private key will be protected with a passphrase. 注意:CA私钥将使用密码保护。

mkdir db private certs crl newcerts csr & touch db/root-ca.index
echo 1000 > db/root-ca.serial & echo 1000 > db/root-ca.crlnumber
openssl genrsa -aes256 -out private/root-ca.key 4096

Now we need to generate a CA certificate using the new private key. 现在,我们需要使用新的私钥生成CA证书。 You will be asked for the passphrase you entered when you created the private key, then prompted for some information such as Country, State, and Common Name. 系统将要求您提供创建私钥时输入的密码,然后提示您输入一些信息,例如“国家/地区”,“州”和“公用名”。 The common name field is what matters here, and for this test, I'd just put Test Root CA . 通用名称字段在这里很重要,对于此测试,我只需要输入Test Root CA

openssl req -config openssl.cnf -key private/root-ca.key \
    -new -x509 -days 3650 -sha256 -extensions root_ca \
    -out certs/root-ca.crt

Once the command completes successfully, you should see a new certificate at certs/root-ca.crt , which you can view with the following command: 命令成功完成后,您应该在certs/root-ca.crt看到一个新证书,您可以使用以下命令查看该certs/root-ca.crt

openssl x509 -in certs/root-ca.crt -noout -text

Creating Leaf Certificates 创建叶子证书

To create leaf certificates (also known as end entity certificates) you'll need to generate another private key for each of them. 要创建叶子证书(也称为最终实体证书),您需要为每个证书生成另一个私钥。 We'll call them test1 and test2 . 我们将它们称为test1test2

openssl genrsa -aes256 -out private/test1.key 4096
openssl genrsa -aes256 -out private/test2.key 4096

For each of these, you will generate a Certificate Signing Request (CSR) which the CA will use to generate the actual certificate. 对于其中的每一个,您将生成一个证书签名请求(CSR),CA将使用它来生成实际的证书。 You'll be asked similar questions to when you created the CA certificate. 系统将向您询问与创建CA证书时类似的问题。 You could just use Test 1 and Test 2 as the common names. 您可以仅使用Test 1Test 2作为通用名称。

openssl req -config openssl.cnf -key private/test1.key
    -new -sha256 -out csr/test1.req
openssl req -config openssl.cnf -key private/test2.key
    -new -sha256 -out csr/test2.req

You should now have two certificate requests in the csr directory. 现在,您应该在csr目录中有两个证书请求。 We'll use these to generate the certificates. 我们将使用它们来生成证书。 You'll need to enter the CA private key passphrase to sign these requests. 您需要输入CA私钥密码来签署这些请求。

openssl ca -config openssl.cnf -in csr/test1.req -out certs/test1.crt \
    -extensions server_cert -days 365 -notext -md sha256
openssl ca -config openssl.cnf -in csr/test2.req -out certs/test2.crt \
    -extensions server_cert -days 365 -notext -md sha256

Now you should have two shiny new leaf certificates in the certs directory. 现在,您在certs目录中应该有两个闪亮的新叶子证书。 They can be viewed with the below commands. 可以使用以下命令查看它们。

openssl x509 -in certs/test1.crt -noout -text
openssl x509 -in certs/test2.crt -noout -text

Revoking a Certificate 吊销证书

Now comes the fun part. 有趣的来了。 The process of revoking a certificate is similar to the signing. 吊销证书的过程与签名相似。 We will use the CA cert and private key to revoke the leaf of Test 2 . 我们将使用CA证书和私钥来撤销Test 2的叶子。 The following command will require the CA private key passphrase. 以下命令将需要CA私钥密码。

openssl ca -config openssl.cnf -revoke certs/test2.crt

If you're curious, the db/root-ca.index file, which till this point has been recording the serial numbers and timestamps of the certificates we issue, should now show a second revocation timestamp next to Test 2 . 如果您感到好奇,那么db/root-ca.index文件(到目前为止一直在记录我们颁发的证书的序列号和时间戳)现在应该在Test 2旁边显示第二个吊销时间戳。 It's a normal text file. 这是一个普通的文本文件。

Publishing the CRL 发布CRL

Now to generate a CRL we simply do the following (requires CA private key passphrase): 现在,要生成CRL,我们只需执行以下操作(需要CA私钥密码):

openssl ca -config openssl.cnf -gencrl -out crl/root-ca.crl

Viewing the CRL is straightforward. 查看CRL很简单。 Using the below command you should see an entry for the serial number of Test 2 and the timestamp of it's revocation. 使用以下命令,您应该看到Test 2的序列号及其撤销时间戳的条目。

openssl crl -in crl/root-ca.crl -noout -text

To verify the certificate has been revoked, the following command is used: 要验证证书已被吊销,使用以下命令:

cat certs/root-ca.crt crl/root-ca.crl > crl/crl-chain.pem
openssl verify -crl_check -CAfile crl/crl-chain.pem certs/test2.crt

Using this for Test 2 should return a certificate status of revoked. 将其用于Test 2应该返回证书状态为已吊销。 Running the same comand for Test 1 should return OK. Test 1运行相同的命令应该返回OK。

Wrap Up 包起来

Assuming everything worked correctly, the end result should be: 假设一切正常,最终结果应该是:

  • One CA certificate in certs/root-ca.crt certs/root-ca.crt一个CA证书
  • One good leaf certificates in certs/test1.crt certs/test1.crt一张优质叶子证书
  • One revoked leaf certificate in certs/test2.crt certs/test2.crt吊销的叶子证书
  • One CRL in crl/root-ca.crl crl/root-ca.crl一个CRL

Note 1: This is all assuming you will be using OpenSSL on a Linux machine. 注意1:以上全部假设您将在Linux计算机上使用OpenSSL。 If you need instructions for windows and certutil they can be provided. 如果您需要有关Windows和certutil说明,则可以提供它们。

Note 2: I've omitted a lot from this guide that was not directly relevant to your question. 注意2:本指南中我省略了很多与您的问题没有直接关系的内容。 If you want further details on this process, I refer you again to the link posted at the top. 如果您需要有关此过程的更多详细信息,请再次转到顶部发布的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM