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 basic knowledge of OpenSSL cryptography in linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the basic knowledge of OpenSSL cryptography in linux, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

A brief history of OpenSSL

Secure Sockets layer Secure Socket Layer (SSL) is an encryption protocol released by Netscape in 1995. The protocol layer can be located on top of HTTP, thus providing S: secure secure for HTTPS. The SSL protocol provides a variety of security services, including two services that are critical to HTTPS:

Peer authentication Peer authentication

(also known as mutual challenge): each side of the connection authenticates the identity of the other side. If Alice and Bob are going to exchange messages through SSL, each person authenticates each other first.

Confidentiality Confidentiality

The sender encrypts the message before sending it over the channel The receiver then decrypts each received message. This process protects the network conversation. Even if the eavesdropper Eve intercepts an encrypted message from Alice to Bob (that is, a man-in-the-middle attack), Eve will find that he cannot computationally decrypt the message.

In turn, these two key SSL services are associated with other services that attract less attention. For example, SSL supports message integrity to ensure that the messages received are the same as those sent. This function is achieved through the hash function, which is also provided with the OpenSSL toolkit.

There are several versions of SSL (for example, SSLv2 and SSLv3), and a similar protocol Transport layer Security Transport Layer Security (TLS) based on SSLv3 emerged in 1999. TLSv1 and SSLv3 are similar, but not enough to work with each other. However, SSL/TLS is often referred to as the same protocol. For example, even if you are using TLS (instead of SSL), OpenSSL functions often include SSL in their names. In addition, invoking the OpenSSL command-line utility starts with openssl.

In addition to man pages, OpenSSL documentation is fragmented, and given the size of the OpenSSL toolkit, these pages are difficult to find and use. The command line and code examples can bring the main topics together. Let's start with a familiar example (using HTTPS to visit the website), and then use that example to select the encrypted part we are interested in to talk about.

A HTTPS client

The client program shown here connects to the Google through HTTPS:

/ * compilation: gcc-o client client.c-lssl-lcrypto * / # include # include # include / * BasicInput/Output streams * / # include / * errors * / # include / * core library * / # define BuffSize 1024 void report_and_exit (const char* msg) {perror (msg); ERR_print_errors_fp (stderr); exit (- 1);} void init_ssl () {SSL_load_error_strings (); SSL_library_init () } void cleanup (SSL_CTX* ctx, BIO* bio) {SSL_CTX_free (ctx); BIO_free_all (bio);} void secure_connect (const char* hostname) {char name [BuffSize]; char request [BuffSize]; char response [BuffSize]; const SSL_METHOD* method = TLSv1_2_client_method (); if (NULL = = method) report_and_exit ("TLSv1_2_client_method..."); SSL_CTX* ctx = SSL_CTX_new (method) If (NULL = = ctx) report_and_exit ("SSL_CTX_new..."); BIO* bio = BIO_new_ssl_connect (ctx); if (NULL = = bio) report_and_exit ("BIO_new_ssl_connect..."); SSL* ssl = NULL; / * link bio channel, SSL session and server endpoint * / sprintf (name, "% SJV% s", hostname, "https") BIO_get_ssl (bio, & ssl); / * session * / SSL_set_mode (ssl, SSL_MODE_AUTO_RETRY); / * robustness * / BIO_set_conn_hostname (bio, name) / * prepare to connect * / / * attempt to connect * / if (BIO_do_connect (bio) | Alice's private key |-- > Bob's msg +-+-+

In theory, it is possible to decrypt a message without a private key of Alice, but in practice, it is not computationally possible if you use an encryption key pair like RSA.

Now, for the second example, sign the document to prove its authenticity. The signing algorithm uses the private key in the key pair to handle the encrypted hash of the document to be signed:

+-+ Hash of document--- > | Alice's private key |-- > Alice's digital signature of the document +-+

Suppose Alice signed the contract sent to Bob digitally. Bob can then use the public key in the Alice key pair to verify the signature:

+-+ Alice's digital signature of the document--- > | Alice's public key |-- > verified or not +-+

Without Alice's private key, Alice's signature cannot be easily forged: therefore, it is necessary for Alice to keep her private key secret.

In the client program, with the exception of digital certificates, this security is not explicitly demonstrated. The next article populates more detailed information with examples using OpenSSL utilities and library functions.

OpenSSL on the command line

In the meantime, let's take a look at the OpenSSL command line utility: in particular, the utility that checks the certificate from the Web server during the TLS handshake. To invoke the OpenSSL utility, you can use the openssl command, and then add a combination of parameters and flags to specify the desired action.

Look at the following command:

Openssl list-cipher-algorithms

This output is a list of the related algorithms that make up the encryption algorithm suite cipher suite. The following is the beginning of the list, with comments clarifying the acronym:

AES-128-CBC # # Advanced Encryption Standard, Cipher Block ChainingAES-128-CBC-HMAC-SHA1 # # Hash-based Message Authentication Code with SHA1 hashesAES-128-CBC-HMAC-SHA256 # # ditto, but SHA256 rather than SHA1...

The next command uses the parameter s_client to open a secure connection to www.google.com and displays all information about the connection on the screen:

Openssl s_client-connect www.google.com:443-showcerts

Port number 443 is the standard port number used by the Web server to receive HTTPS instead of HTTP connections. (for HTTP, the standard port is 80) the Web address www.google.com:443 also appears in the code of the client program. If the connection attempt is successful, three digital certificates from Google are displayed along with information about the secure session, the cryptographic algorithm suite in use, and related projects. For example, this is the partial output at the beginning, which declares that the certificate chain is coming. The certificate is encoded as base64:

Certificate chain 0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com i:/C=US/O=Google Trust Services/CN=Google Internet Authority G3-BEGIN CERTIFICATE-MIIEijCCA3KgAwIBAgIQdCea9tmy/T6rK/dDD1isujANBgkqhkiG9w0BAQsFADBUMQswCQYDVQQGEwJVUzEeMBwGA1UEChMVR29vZ2xlIFRydXN0IFNlcnZpY2VzMSUw...

Major websites such as Google usually send multiple certificates for authentication.

The output ends with summary information about the TLS session, including details of the cryptographic algorithm suite:

SSL-Session: Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Session-ID: A2BBF0E4991E6BBBC318774EEE37CFCB23095CC7640FFC752448D07C7F438573...

The protocol TLS 1.2, which is used in the client program, uniquely identifies the connection between the openssl utility and the Google Web server. Cipher entries can be resolved in the following ways:

ECDHE (

Elliptic Curve Diffie-Hellman (temporary) Elliptic Curve Diffie Hellman Ephemeral

Is an efficient and efficient algorithm for managing TLS handshakes In particular, ECDHE solves the "key distribution problem" by ensuring that both sides of the connection (for example, the client program and the Google Web server) use the same encryption / decryption key, called the session key. Follow-up articles will delve into this detail.

RSA (Rivest Shamir Adleman) is the main public key cryptosystem, named after three scholars who first described the system in the late 1970s. The key pair being used is generated using the RSA algorithm.

AES128 (Advanced encryption Standard Advanced Encryption Standard) is a block encryption algorithm block cipher, which is used to encrypt and decrypt bit blocks blocks of bits. Another algorithm is the streaming encryption algorithm stream cipher, which encrypts and decrypts one bit at a time. This encryption algorithm is a symmetric encryption algorithm because the same key is used for encryption and decryption, which first causes the problem of key distribution. AES supports 128,192, and 256-bit key sizes: the larger the key, the better the security.

In general, the key size of symmetric encryption systems such as AES is smaller than that of asymmetric (key pair-based) systems such as RSA. For example, the 1024-bit RSA key is relatively small, while the 256bit key is currently the largest in AES.

GCM (Galois counter mode Galois Counter Mode) handles encryption algorithms (in this case AES128) that are repeatedly applied during a secure conversation. The size of the AES128 block is only 128bit, and the secure conversation is likely to contain multiple AES128 blocks from one side to the other. GCM is very effective and is usually used with AES128.

SHA256 (256bit secure hash algorithm Secure Hash Algorithm 256bits) is the cryptographic hash algorithm we are using. The size of the generated hash is 256 bits, although it can be even larger with SHA.

The cryptographic algorithm suite is constantly developing. For example, not long ago, Google used the RC4 streaming encryption algorithm (Ron's Cipher version 4, which was later developed by RSA's Ron Rivest). RC4 now has a known vulnerability, which probably partly led to the conversion of Google to AES128.

Thank you for reading this article carefully. I hope the article "what is the basic knowledge of OpenSSL cryptography in linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Servers

Wechat

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

12
Report