Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods for OpenSSL to generate self-signed certificates

2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

OpenSSL has a large number of command lines that are intimidating.

This is because OpenSSL is really involved in a lot of areas, such as a variety of symmetric / asymmetric algorithms, random numbers, signatures, encryption, certificates, structural analysis, the construction of PKI system, etc., behind these areas there are a lot of RFC documents, OpenSSL implements these theoretical things as algorithms for us to learn and use.

Algorithms can be implemented in two forms: API and tools.

The former is a library used by programmers, while the latter is compiled native code-it has three names: commands, tools, and applications.

For example, the command line that generates a self-signed certificate:

Openssl req-x509-newkey rsa:8192-keyout rca.key-out rca.cer-days 365

Where "req" is "command" (also known as "tool" and "application"),-days is "option", and 365is "parameter".

As the saying goes, "everything stays the same". Although its command line is large and complex, it still has rules to follow, such as-inform specifies the input file format and-outform specifies the output file format. These two options are repeated in multiple commands. There are options with the same name, but have different functions for different commands, such as the-in option, if you cooperate with the req command, you will enter a certificate request file, and if you cooperate with the x509 command, you will enter a certificate in X.509 format.

Given the flexibility of the OpenlSSL command line, the same function can be done by different command lines. Here are some of my summaries.

Of course, there are probably command lines that are not included. You are welcome to write in the comments section. I will list you as a thank you author in the text.

Let's finish this interesting "project" together-find the same for Open SSL!

Current version number: OpenSSL for Windows 1.1.1c 28 May 2019

Method 1:req command (no request file is required)

:: generate self-signed certificate and private key openssl req-x509-newkey rsa:8192-keyout rca.key-out rca.cer-days 365

Method 2:x509 command (request file required)

:: generate request file and private key openssl req-new-out ca.csr-keyout ca.key:: generate self-signed certificate openssl x509-signkey ca.key-req-days 365-in ca.csr-out cacert.pem

Method 3:ca command (request file required)

:: create the file and directory structure required by the ca command d:&cd\ & md ca1&cd\ ca1&md democa&md democa\ newcerts&md democa\ privatecd. > democa\ index.txt&echo ca01 > democa\ serial:: generate the request file and private key The private key protection password is 1234:openssl req-new-out ca.csr-keyout ca.key-days 3650-subj / C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-1/CN=CA1/emailAddress=ca1@tiger.com-set_serial 0xca01-passout pass:1234:: to generate a self-signed certificate openssl ca-selfsign-in ca.csr-keyfile ca.key-out ca.cer-outdir. -passin pass:1234-days 3650-create_serial-extensions v3_ca

Method 4:ca command resignature

Read the previous self-signed certificate ca1.cer and resign with the new DN:

:: create the file and directory structure required for the ca command d:&cd\ & md ca1&cd\ ca1&md democa&md democa\ newcerts&md democa\ privatecd. > democa\ index.txt&echo ca01 > democa\ serial:: generate a self-signed CA1 root certificate and private key The private key protection password is 1234:openssl req-x509-newkey rsa:8192-keyout ca1.key-out ca1.cer-days 3650-subj / C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-1/CN=CA1/emailAddress=ca1@tiger.com-set_serial 0xca01-passout pass:1234:: to re-sign the CA1 root certificate with a new DN name, and the new self-signed certificate output is ca1-ok.ceropenssl ca-ss_cert ca1.cer-keyfile ca1.key-cert ca1.cer-out ca1-ok.cer-outdir. -create_serial-policy policy_anything-batch-passin pass:1234-subj / C=CN/O=aa/ST=bb/CN=CA1

As two ways for CA to sign a certificate to (an intermediate CA or final entity)

They are the X509 command and the CA command, which are detailed in my other two articles, and I won't repeat them here.

OpenSSL builds CA and CA chains and batch processes for issuing certificates to hosts (using the CA command)

OpenSSL builds CA and CA chains and batch processes for issuing certificates to hosts (using x509 command)

Three methods of generating RSA Private key

Method 1:genrsa command:

Openssl genrsa-out ca.key 8192

Method 2:genpkey command:

: protect the private key openssl genpkey-out rsa_pri.key-outform PEM-pass pass:123456-aes-128-cbc-algorithm RSA-pkeyopt rsa_keygen_bits:8192 with password 123456

Method 3:req command

Openssl req-x509-newkey rsa:8192-keyout rca.key-out rca.cer-days 3650

Two methods of RSA encryption

Method 1:pkeyutl command:

: public key encryption openssl rsautl-encrypt-in a.txt-out 1.enc-inkey ca.pub-pubin:: private key decryption openssl rsautl-decrypt-in 1.enc-out dec.txt-inkey ca.key

Note: the drawback of this command is that it can only encrypt and sign short files. If it is too common, the following error will be reported:

Method 2:rsautl command:

Experiments show that for the current version of OpenSSL, the maximum file that this command can handle is 1013 bytes.

: public key encryption openssl rsautl-encrypt-in a.txt-out 1.enc-inkey ca.pub-pubin:: private key decryption openssl rsautl-decrypt-in 1.enc-out dec.txt-inkey ca.key

Note: if you want RSA encryption (and signature below) to have no size limit, you can use gpg4win.

Four methods for signing / verifying signatures

Method 1:dgst command:

There is no limit to the size of the signed file; the principle is to generate a hash value of the file to be signed, and then encrypt the hash value with the public / private key. :: use the private key key.pem to generate a binary signature file for 1.zip. The algorithm is SHA-256, and the signature file is 1.sig. Note that you cannot use the-hex option: openssl dgst-sha256-sign key.pem-out 1.sig 1.zipSHA-256: compare the generated signature file 1.sig with the source file 1.zip with the public key. Note that the signature algorithm is the same as the signature verification algorithm: openssl dgst-sha256-verify pub.pem-signature 1.sig 1.zipvision: same as above, except that the signature openssl dgst-sha256-prverify key.pem-signature 1.sig 1.zip is verified with the private key.

Method 2:md5 command (the syntax is exactly the same as dgst):

There is no limit to the size of the signed file; the principle is to generate a hash value of the file to be signed, and then encrypt the hash value with the public / private key. :: use the private key ca.key to sign the file text.txt and generate a signature file named sign: openssl md5-sha256-sign ca.key-out sign test.txt:: uses the public key ca.pub to compare the generated signature file 1.sig with the source file 1.zip. Note that the signature algorithm and verification algorithm should be the same: openssl md5-sha256-verify ca.pub-signature sign test.txt:: as above Just use the private key to verify the signature openssl md5-sha256-prverify ca1.key-signature sign test.txt

Method 3:rsautl command:

:: rsautl can only sign small files. In my experiment, it cannot be larger than 1013 bytes:: private key signature (in fact, private key encryption file) openssl rsautl-sign-in abc.txt-out abc.sig-inkey ca.key:: public key verification (in fact, decryption with public key). If it is passed, the original data will be restored, otherwise an error will be reported: openssl rsautl-verify-in abc.sig-out abc.vfy-inkey ca.pub-pubin method 1:pkeyutl order:

Method 4:pkeyutl command:

:: pkeyutl can only sign hash values. Experimental goal: generate a hash value for test.txt, then sign the hash value, and finally verify the signature of the hash value. :: first use the dgst command to generate a binary hash value of the file test.txt. The file name is test.sigopenssl dgst-sha256-binary-out test.sig test.txt::. Sign the hash value with the private key (that is, the private key encrypted hash value). Generate a signature file md.sig:openssl pkeyutl-sign-inkey ca.key-keyform PEM-in test.sig-out md.sig:: to read in the public key, hash value signature md.sig, hash value test.sig to verify that the signature is correct. The principle is to decrypt md.sig with the public key, and the result is the same as that of test.sig, that is, it is verified by openssl pkeyutl-verify-inkey ca.pub-keyform PEM-pubin-in test.sig-sigfile md.sig.

Four methods of extracting Public key

Method 1:rsa command:

Openssl rsa-in ca.key-pubout-out ca.pub

Method 2:pkey command:

Openssl pkey-in ca.key-pubout-out ca.pub

Method 3:req command:

Extract the public key from the request file:

Openssl req-in host.csr-pubkey-out host.pub

Method 4:x509 command:

Extract the public key of the subject (consumer) in the certificate:

Openssl x509-in ca.cer-pubkey-noout-out ca.pub

Create certificate chain 2-party method

Method 1: double-click the certificate, click "install Certificate", import the root certificate into the "trusted Root Certificate Authority", and import the intermediate certificate into the "Intermediate Certificate Authority", which imports the certificate chain to the windows certificate storehouse, the process is clear at a glance, screenshot.

Method 2: create a certificate chain through the copy command, focusing on the intermediate certificate in front, the back row in turn, and the root certificate at last:

Copy ca3.cer+ca2.cer+ca1.cer ca3-chain.cer

Verification certificate chain 3-party method

Method 1: corresponding to method 1 of creating certificate chain, by importing root certificate and intermediate certificate into Windows certificate store on Web client, the application (such as Valley browser) can verify the certificate chain through CryptoAPI. To verify manually, double-click the certificate and a hierarchical certificate chain structure appears:

Method 2: corresponding to method 2 of creating certificate chain, use the verify command:

Openssl verify-CAfile ca-chain.cer host1.cer

The-CApath option of the method 3:x509 command

This method is troublesome to operate under Windows. First set up a directory, such as yz, and then copy the root certificate and all intermediate CA certificates to that directory, but rename it to xxxxxxxx.0, where eight x are hash values output with openssl x509-in xx.cer-subject_hash-noout, followed by .0. For example, ca1 is the root certificate, ca2.cer and ca3.cer are intermediate certificates, and host1 is the SSL certificate signed by ca3 to the user. Copy ca1.cer, ca2.cer, and ca3.cer to the yz directory, and then rename them in turn. For example, if the output hash value of openssl x509-in ca1.cer-subject_hash-noout is 8607f596, rename ca1.cer to 8607f596.0, and execute the command after the renaming is completed:

Openssl verify-CApath yz host1.cer

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report