In the last article asymmetric encryption –RSA principle analysis, we understand the mathematical principle of RSA encryption, RSA security is currently recognized, but it also has disadvantages: As the encryption speed and performance are low, large files are not suitable for direct encryption. Raw data to be encrypted is usually processed using Base64, Hash, and symmetric encryption before being encrypted using RSA. Therefore, RSA is mainly used for digital signature, which verifies the identity and validity of data. The authoritative certificate authority – lying money CA, is the use of RSA this set of algorithms, this article we together with the command line to gradually dismantle the certificate generation process, uncover the secret behind the certificate. Talk is cheap, show you the code, Come on!

What is the OpenSSL

SSL is familiar. When you visit a website, the letters that start with HTTPS indicate that the data being transferred between you and the server is encrypted, and the encryption protocol used here is SSL (a later version of it, TLS). In other words, HTTP becomes HTTPS when it is wrapped in a layer of SSL encryption. Of course, SSL/TLS is also used in many protocols, such as VPN, encrypted email protocols, etc. What is OpenSSL? In SSL protocol, we use many cryptography methods to protect data, including symmetric cryptography, public key cryptography, digital signature, certificate, integrity check, pseudo random number generation, etc. Because of the complexity of these algorithms and operations, the open source community has developed a library that provides standard methods that other developers can use to implement SSL encryption/decryption operations. Therefore, OpenSSL is an open source Cryptography Toolkit. Not surprisingly, OpenSSL also supports RSA, so let’s use OpenSSL to demonstrate each step of certificate signing on a macOS terminal.

RSA encryption, certificate generation terminal demonstration

For easy observation, please open up and go to an empty folder to start your performance ~ enter:

openssl genrsa -out private.pem 2048
Copy the code

A private.pem file appears in the directory. This file contains the private key information.

openssl rsa -in private.pem -pubout -out public.pem
Copy the code

View the contents of the file separately:

cat private.pem
Copy the code
cat public.pem
Copy the code

You get something like this:

-----BEGIN RSA PRIVATE KEY----- Base64 encoded content -----END RSA PRIVATE KEY-----Copy the code
-- -- -- -- -- BEGIN PUBLIC KEY -- -- -- -- -- Base64 encoded content -- -- -- -- -- END PUBLIC KEY -- -- -- -- --Copy the code

Now let’s write a random TXT file as the data to be encrypted:

echo "hello word\!" > message.txt
Copy the code

Encrypt it with a public key:

openssl rsautl -encrypt -in message.txt -inkey public.pem -pubin -out enc.txt
Copy the code

If you look at the encrypted file: cat enc.txt, all you see is a bunch of gibberish decrypting the encrypted file with the private key:

openssl rsautl -decrypt -in enc.txt -inkey private.pem -out dec.txt
Copy the code

Decrypt: cat dec.txt: cat dec.txt: cat dec.txt: cat dec.txt: cat dec.txt: cat dec.txt: cat dec.txt

openssl rsautl -sign -in message.txt -inkey private.pem -out enc.bat
Copy the code

Decrypt an encrypted file with a public key:

openssl rsautl -verify -in enc.bat -inkey public.pem -pubin -out dec.txt
Copy the code

Now that we have done RSA encryption and decryption, what does the certificate we are developing have to do with them? All iOS developers request CSR files from CA with key strings. In fact, they generate corresponding public keys according to the current Mac built-in private key (of course, you can also specify your own private key). The generated CSR file contains the original public key certificate file + your server information and unit information. Let’s do this with a command:

openssl req -new -key private.pem -out rsacert.csr
Copy the code

Enter the following information as prompted and press Enter:

Country Name (2 letter code) []:
State or Province Name (full name) []:
Locality Name (eg, city) []:
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:
Email Address []:
A challenge password []:
Copy the code

After getting the CSR file with the public key information, the next step is to submit it to the CA certification center for signature, that is, to ask the CA to stamp it (a stamp of 5,000 dollars a year @_@, if your HTTPS uses a self-signed certificate, there will be an unsafe message when others access it because you do not have the CA stamp, it is not recognized as secure) :

openssl x509 -req -days 3650 -in rsacert.csr -signkey private.pem -out rsacert.crt
Copy the code

CRT file is Base64 encoded, we need to convert the certificate to DER binary format to directly use:

openssl x509 -outform der -in rsacert.crt -out rsacert.der
Copy the code

The rsacert.der file is the certificate that contains our public key information!

openssl pkcs12 -export -out p.p12 -inkey private.pem -in rsacert.crt
Copy the code


supplement

The internal data structure of the. Pem file can be accessed by command

openssl rsa -in private.pem -text -out ptivate.txt
Copy the code

If there are some unknown things in the above command, it is probably you are not familiar with the certificate related file encoding format, protocol and extension, do not be alarmed, I have prepared for you to add food, remember not only silent collection acya! Focus on not getting lost ~ \(^o^)/

Certificate file encoding format, extension, protocol popular science — high-energy warning

We use x.509 to generate certificates. For those who have experienced certificates, they must be familiar with pem, DER, PFX, JKS, KDB, CER, key, CSR, CRT, CRL, OCSP, SCEP and so on. Take a look and correct any inaccuracies in the description:

  • Coding format
    • DER :(short for Distinguished Encoding Rules), which is the default format for most browsers and is stored in ASN1 DER format, which is header free. It can contain all private keys, public keys, and certificates.
    • PEM :(Privacy Enhanced Mail) is the default information storage method adopted by OpenSSL. It stores Base64 encoded DER data surrounded by ASCII headers and is therefore suitable for text mode transfer between systems. It can include all private keys, public keys, and certificates.
  • File extension
    • .pem – (Privacy Enhanced E-mail) DER encoded CERTIFICATE And Base64 encoded data are stored in “—–BEGIN CERTIFICATE—–” and “—–END CERTIFICATE—–“
    • Cer,.crt,.der – Are usually certificates in der binary format, but Base64 encoding is also common
    • .p7b,.p7C-PKCS #7 SignedData structure without data, Just Certificate (S) or CRL(S), encrypted message syntax (PKCS7), is a format standard for storing various messages. These messages include: data, signed data, digital envelopes, signed digital envelopes, digest data, and encrypted data.
    • P12-pkcs #12 format, containing a certificate and possibly a password-protected private key
    • .pfx-pfx, the format before PKCS#12 (usually in PKCS#12 format, such as those generated by IIS PFX files)
    • .jks – It is usually possible to convert the “.key file +.crt file “format used by Apache/OpenSSL to a standard Java Key Store(JKS) file. JKS file format is widely used in JAVA – based WEB server, application server, middleware. You can import JKS files into TOMCAT, WEBLOGIC, etc
    • .kdb – It is usually possible to convert the “.key file +.crt file “format used by Apache/OpenSSL to a standard IBM KDB file. KDB file format is widely used in IBM WEB server, application server, middleware. You can import KDB files into IBM HTTP Server, IBM Websphere, and so on
    • . CSR – Certificate Signing Request Before generating an X509 digital certificate, the user submits the certificate application file and the CA issues the certificate.
    • Key – Indicates a private key file in PEM format or DER format
    • . CRL – Certificate Revocation List is a signed data structure that contains a List of revoked certificates. A CRL is a form of announcing the revocation status of a certificate. Like a blacklist of credit cards, a CRL is used to announce that some digital certificates are no longer valid. A CRL is an offline certificate status information. It is updated periodically. A CRL can be classified into a full CRL and an incremental CRL. A full CRL contains all revoked certificate information, while an incremental CRL indicates the revoked certificate information by a series of CRLS. The CRL issued each time is an incremental expansion of the previously issued CRL. Basic CRL information includes the serial number, revocation time, reason, signer, and CRL signature of the revoked certificate. CRL – based authentication is not strict certificate authentication. CRL indicates that a certificate revoked in a CRL is invalid. However, it cannot give the status of a certificate that is not in a CRL. If strict authentication is implemented, online authentication, that is, OCSP authentication, is required. A SET of electronic documents, usually signed by a CA, containing the unique identifier (certificate serial number) of the revoked certificate. A CRL is used to list expired or revoked digital certificates. It is updated every once in a while, so you must download the list regularly to get the latest information
  • agreement
    • OCSP – Online Certificate Status Protocol (RFC2560), used to indicate the Certificate Status in real time. By querying the OCSP service to determine the status of a certificate, the OCSP client can provide users with the validity information of one or more digital certificates. It establishes a real-time response mechanism so that users can confirm the validity of each certificate in real time and solve the security problems caused by THE CRL. OCSP can be implemented through HTTP protocol. Rfc2560 defines the message format of the OCSP client and server
    • SCEP – Simple Certificate Enrollment Protocol. File-based Certificate Enrollment requires copying and pasting text files from your local computer to the Certificate distribution center, and from the Certificate distribution center to your local computer. SCEP can handle this automatically but CRLs still needs to manually copy and paste between the local computer and the CA distribution center

See you next time. Same rules. If you have any questions, please feel free to leave a comment.

Please follow my official wechat account: One item for the Future: Scatter Flower ✧(≖ ≖✿)