SSL secure network communication under computer

SSL Secure Socket Layer

SSL Secure Socket Layer is developed by Netspace to ensure the integrity and confidentiality of data transmission on the Internet. The current version is 3.0, and the latest version is TLS1.2 Transport Layer Security. The difference between TLS and SSL is minimal and can be understood as an updated version of SSL.

communication process

SSL is between the application layer and the network layer, at the transport layer. The data of the application is encrypted by the SSL in the transport layer, and its own SSL header is added, and the encrypted data is transmitted to the network layer.

Through openssl, you can issue certificates by yourself, and after you have the certificate and private key, you can conduct SSL secure communication.

The process of calling the interface provided by SSL is shown in the following figure:

  1. The client's browser transmits the version number, encryption algorithm, random number, and other information required for other communications to the server of the SSL protocol it supports;

  2. According to the information sent by the client, the server selects the SSL protocol version supported by both parties, the encryption algorithm and other information, and transmits its own certificate to the client (this step may have security risks, such as forging the SSL protocol version that the client can support. low, which may cause the server to use an SSL version with a low security level to communicate);

  3. The client uses the information sent by the server to verify the legitimacy of the server, including whether the certificate has expired, whether the certificate issuing authority is reliable, whether the public key matches, whether the domain name on the certificate matches that on the server, etc. If the verification fails, it will be interrupted. Open the communication, and continue the communication if the verification is passed;

  4. The client randomly generates a symmetric password for communication encryption, encrypts it with the public key, and then transmits the encrypted pre-master password to the server;

  5. The server decrypts with its own private key to obtain the pre-master password used for communication encryption, and then uses a series of algorithms to generate the master communication password (the client will also generate the same master communication password in the same way);

  6. The client sends a message to the server, indicating that the subsequent data communication will use the master password for encryption and decryption as the master communication password generated in the above stages, and at the same time informs the server that the client's handshake process is over;

  7. The server also wants the client to specify the communication password and confirm the end of the handshake process;

  8. The handshake process ends. The SSL secure channel data communication begins, and the client and server begin to use the same symmetric key for data communication, and at the same time perform communication integrity verification.

After SSL communication is established

It can be seen from the above communication process that after the ssl communication is established, regardless of some loopholes in the ssl version, the communication process is very secure, which can effectively prevent man-in-the-middle attacks and operator hijacking. Integrity is effectively guaranteed. Therefore, the related attacks against SSL must be established in the initialization phase of its session. For example, the man-in-the-middle attack must be implemented in the process of session establishment, that is, during the communication process, the attacker forges a certificate and deceives the client by claiming that the certificate is the server certificate. The client makes it trust the certificate and uses the certificate to communicate, so that the information of the user's communication will be monitored by a third party, and the confidentiality and integrity of the communication will be destroyed. This attack method also has certain limitations. When a fake certificate is used to establish communication, the general attacker will not obtain a certificate certified by an official organization, and the client's browser will issue a warning to remind that the communication is not safe. You can refer to visit The prompt of the browser at 12306 (because 12306 uses a certificate issued by its self-built ca, which has not been certified by an official organization, the browser will by default consider its certificate to be insecure and raise an alarm).

Bypass browser validation

Although verifying the validity of the certificate through the browser will increase the cost of the attack, in some special cases, the verification of the browser will still be bypassed. For example: when we use packet capture tools such as fiddler and Charles, we can manually add their certificates as trusted certificates, so when we use these tools to perform logical analysis on the relevant system locally, we will not receive any Alert to the browser. Moreover, the verification of the browser is only for web-side applications. For mobile-side and other non-web-side applications, protection can only be done in other ways. A common implementation method is to use SSL Pinning, that is, certificate binding.

There are three protocols in the working principle of SSL

1. Handshake Protocol

The Handshake Protocol is the first sub-protocol used by clients and servers to communicate with SSL connections. The handshake protocol consists of a series of messages between the client and the server. The most complex protocol in SSL is the handshake protocol. The protocol allows servers and clients to authenticate each other, negotiate encryption and MAC algorithms, and keep SSL keys secret to protect data sent in SSL records. The handshake protocol is used before the application's data transfer.

2. Recording Agreement

After the successful handshake between the client and the server, the record protocol is used, that is, the client and the server authenticate each other and determine the algorithm used for the secure information exchange, and enter the SSL record protocol, which provides two service connections for SSL:

  • (1) Confidentiality: implemented using the secret key defined by the handshake protocol
  • (2) Integrity: The handshake protocol defines the MAC to ensure message integrity

3. Alert Protocol

When the client and server find an error, they send an alert message to each other. If it is a fatal error, the algorithm closes the SSL connection immediately, and both parties also delete the relevant session number, secret and key first. Each alarm message has a total of 2 bytes, the first byte indicates the error type, if it is an alarm, the value is 1, if it is a fatal error, the value is 2; the second byte specifies the actual error type.

Certificate Workflow

  1. A user connects to your Web site, which is protected by a server certificate. (It can be identified by checking whether the URL starts with "https:", or the browser will provide you with relevant information).

  2. Your server responds and automatically transmits your website's digital certificate to the user, which is used to authenticate your website.

  3. The user's web browser program generates a unique "session key" that encrypts all communications with the website.

  4. The user's browser encrypts the chat key with the website's public key so that only your website can read the chat key.

Server-side code implementation

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define CERT_FILE  "./cert/server.crt"
#define KEY_FILE   "./cert/server.key"

int main(int argc, char *argv[]) {
    SSL_CTX *ctx;
    int listenfd, newfd;
    struct sockaddr_in ser, cli;

    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    if (NULL == (ctx = SSL_CTX_new(SSLv23_server_method()))) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }
    if (SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM) != 1) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }
    if (SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM) != 1) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }
    if (1 != SSL_CTX_check_private_key(ctx)) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&ser, 0, sizeof(ser));
    ser.sin_family = AF_INET;
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    ser.sin_port = htons(atoi(argv[1]));

    if (-1 == bind(listenfd, (struct sockaddr*)&ser, sizeof(struct sockaddr))) {
        perror("bind");
        exit(-1);
    }
    if (-1 == listen(listenfd, 5)) {
        perror("listen");
        exit(-1);
    }

    for (;;) {
        socklen_t size = sizeof(struct sockaddr);
        if (-1 == (newfd = accept(listenfd, (struct sockaddr*)&cli, &size))) {
            perror("accept");
            break;
        }
        SSL *ssl = SSL_new(ctx);
        SSL_set_fd(ssl, newfd);
        if (-1 == SSL_accept(ssl)) {
            ERR_print_errors_fp(stderr);
            close(newfd);
            break;
        }
        {
            char buf[256]; int len;
            while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) {
                buf[len] = 0;
                printf("%s", buf);
                SSL_write(ssl, buf, len);
            }
            SSL_shutdown(ssl);
            SSL_free(ssl);
            close(newfd);
        }
    }
    SSL_CTX_free(ctx);
    close(listenfd);
    return 0;
}

Client-side code implementation

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
int main(int argc, char *argv[]) {
    SSL_CTX *ctx;
    int sockfd;
    struct sockaddr_in ser;

    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    if (NULL == (ctx = SSL_CTX_new(SSLv23_client_method()))) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&ser, 0, sizeof(ser));
    ser.sin_family = AF_INET;
    ser.sin_addr.s_addr = inet_addr(argv[1]);
    ser.sin_port = htons(atoi(argv[2]));
    if (-1 == connect(sockfd, (struct sockaddr*)&ser, sizeof(ser))) {
        perror("connect");
        exit(-1);
    }
    SSL *ssl = SSL_new(ctx);
    SSL_set_fd(ssl, sockfd);
    if (-1 == SSL_connect(ssl)) {
        ERR_print_errors_fp(stderr);
        exit(-1);
    }
    {
        char buf[256]; int len;
        freopen("/etc/passwd", "r", stdin);
        while (fgets(buf, sizeof(buf), stdin)) {
            len = strlen(buf);
            if (SSL_write(ssl, buf, len) > 0) {
                buf[0] = 0;
                len = SSL_read(ssl, buf, sizeof(buf));
                if (len > 0) {
                    buf[len] = 0;
                    printf("%s", buf);
                }
            }
        }
        SSL_shutdown(ssl);
        SSL_free(ssl);
    }
    close(sockfd);
    SSL_CTX_free(ctx);
    return 0;
}

resource portal

  • L pay attention to [be a tender programmer] public account
  • Reply to [python information] [2020 Autumn Recruitment] in the background of the [Be a Tender Programmer] public account to get the corresponding surprise!
  • Blog address built by myself: Nightmare Back Blog


"❤️Thank you all"

  • Like and support it, so that more people can see this content
  • You are welcome to share your thoughts with me in the comment area, and you are also welcome to record your thinking process in the comment area

Tags: SSL security https computer Network Communications

Posted by kevincro on Sun, 01 May 2022 21:31:08 +0300