0

My system is Linux.

I try to create a tlsv1.2 https server with Openssl which need check client's certificate, but when Client tries to hands shake with Server, after Client sends Certificate to Server, Server will reply Unknown CA.enter image description here

I can confirm that it is the problem of the certificate, because I will SSL_CTX_set_verify(IEctx, SSL_VERIFY_PEER, NULL) After the comment is removed, the handshake can be completed.

Is it because the server does not trust this certificate?

Does the server set the trust certificate through SSL_CTX_set_client_CA_list()?

    init_openssl_library();
    SSL_load_error_strings();
    // build the SSL objects...
    const SSL_METHOD* method = TLS_server_method();
    SSLeay_add_ssl_algorithms();
    if (!(NULL != method)) fprintf(stderr, "SSL Method Error\n");

    IEctx = SSL_CTX_new(method);
    if (!(IEctx != NULL)) fprintf(stderr, "CXT New Error\n");
    SSL_CTX_set_cipher_list(IEctx, "ECDHE-ECDSA-AES128-CCM8");
    SSL_CTX_set_verify(IEctx, SSL_VERIFY_PEER, NULL);

    SSL_CTX_load_verify_locations(IEctx,"sat-root.crt",NULL);
    STACK_OF(X509_NAME)  *list;
    list = SSL_load_client_CA_file("Root.pem");
        
    if (list == NULL) {
        printf("Failed to load client CA file from \"Root.pem\"\n");
        //return -1;
    }
    SSL_CTX_set_client_CA_list(IEctx, SSL_load_client_CA_file("Root.pem"));
    SSL_CTX_use_certificate_file(IEctx, "sat-root.pem", SSL_FILETYPE_PEM);//root CA
    SSL_CTX_use_certificate_chain_file(IEctx, "sat-cli_mca-mica-dev_55937-13-1_56000020.pem");//Chain CA
    if (SSL_CTX_use_PrivateKey_file(IEctx, "sat-key_dev_55937-13-1_56000020.pem", SSL_FILETYPE_PEM) <= 0) {//Private Key
        ERR_print_errors_fp(stdout);
        return 1;
    }
    if (!SSL_CTX_check_private_key(IEctx)) {//Check Private key
        ERR_print_errors_fp(stdout);
        return 1;
    }
    if (!IEctx) {
        ERR_print_errors_fp(stderr);
    }
    IEssl = SSL_new(IEctx);
    SSL_set_fd(IEssl, listener);
    FD_SET(listener, &master);
    if (select(fdmax + 1, &read_fds, NULL, NULL, &tv) == -1) {
        perror("select");
        return 4;
    }
    for (int i = 0; i <= fdmax; i++) {
        if (FD_ISSET(i, &read_fds)) { // 我們找到一個!!
            DC_DEBUG_NL("we find one");
            if (i == listener) {
                DC_DEBUG_NL("i == listener");
                // handle new connections
                addrlen = sizeof remoteaddr;
                newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);

                if (newfd == -1) {
                    perror("accept");
                } else {
                    DC_DEBUG_NL("add new fd");
                    SSL_set_fd(IEssl, newfd);  //新增到 master set
                    if (newfd > fdmax) {     //持續追蹤最大的 fd
                        fdmax = newfd;
                    }
                    int r = SSL_accept(IEssl);
                    if (r == 1){
                        DC_DEBUG_NL("scuess to accept");
                    }
                    nbytes = SSL_read(IEssl, IEEEBuf, sizeof(IEEEBuf) - 1);
                    DC_DEBUG_NL("IEEEBuf[nbytes]=%s", IEEEBuf);
                    string webpage = "/ntfy/0";
                    string RequestBody = "";
                    string SendstrPacket = "";
                    char chRequest[500];
                    SendstrPacket = https.TransToPacketFormat(P_IE2030, RequestBody, webpage, "", H_Create);
                    StrToChar(chRequest, SendstrPacket);
                    int result = SSL_write(IEssl, chRequest, strlen(chRequest));
                    if(result != -1){
                        DC_DEBUG_NL("scuess to write");
                    }
                }

            }
Niu Ar
  • 1
  • 1
  • Yes, it is because the server doesn't trust the client's certificate. Solutions: (1) import the client certificate into the server's truststore, or (2) have the client certificates signed by a trusted CA. In the long run you will find (2) far simpler. – user207421 Jul 06 '20 at 10:08
  • the certificate is signed by a official CA, I must trust it, I suspect that I have not included this CA in the trust list, How to build a trusted CA list, is SSL_CTX_set_client_CA_list(list)? The "list" is .pem? – Niu Ar Jul 06 '20 at 13:16
  • `SSL_CTX_set_client_CA_list` only sets the names of allowed CAs sent to the client when requesting a client cert. It does not affect which CAs are trusted by the server. See https://stackoverflow.com/q/25156180/1216776 – stark Jul 06 '20 at 13:50

0 Answers0