This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF9160 Unable to disable certificate validation when connecting to https

Hi all!
Since I do not have a system for updating the certificate when changing the server, I decided to disable certificate verification when connecting to the https server. Here is the code:

static int tls_setup(int fd, const char * _addr)
{
	/* Set up TLS peer verification */
	enum {
		NONE = 0,
		OPTIONAL = 1,
		REQUIRED = 2,
	};

	int verify = NONE;
	int err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
	if (err) {
		LOG_ERR("Failed to setup peer verification, err %d", errno);
		return err;
	}
	return 0;
}

Since the certificate is not needed I do not execute the cert_provision function.
Here is the server connection code:

bool net_connect(const char * _addr, uint16_t _port, Socket_t * _out_socket, struct addrinfo **_out_addrinfo)
{
	int err;

	err = getaddrinfo(_addr, NULL, &m_Hints, _out_addrinfo);
	if (err) {
		LOG_ERR("getaddrinfo() failed, err %d", errno);
		return false;
	}

	((struct sockaddr_in *)(*_out_addrinfo)->ai_addr)->sin_port = htons(_port);

	*_out_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
	if (*_out_socket == -1) {
		LOG_ERR("Failed to open socket!");
		clean_up(*_out_socket, _out_addrinfo);
		return false;
	}

	/* Setup TLS socket options */
	err = tls_setup(*_out_socket, _addr);
	if (err) {
		clean_up(*_out_socket, _out_addrinfo);
		return false;
	}

	LOG_INF("Connecting to %s", _addr);
	err = connect(*_out_socket, (*_out_addrinfo)->ai_addr, sizeof(struct sockaddr_in));
	if (err) {
		LOG_ERR("connect() failed, err: %d", errno);
		clean_up(*_out_socket, _out_addrinfo);
		return false;
	}

	return true;
}

I end up getting "connect() failed, err: 95" when I try to connect to the server.

I also noticed one feature, on the nrf9160 into which I previously downloaded the certificate, the connection is successful. However, on the new (clean) nrf9160 I get the error "connect() failed, err: 95". It seems that the certificate remains in the modem and therefore on the old nrf9160 I can connect but not on the new one.

As a result, it is not clear how to work without knowing the CA certificate. 
Thanks!

Parents
  • Hi,

    When using TLS, the modem requires either a root CA certificate or a pre-shared key (PSK) at a minimum.

    But, you can still disable peer verification. It will then accept any server, even if the certificates does not match.

    Best regards,

    Didrik

  • Okay, so if this certificate expires, will the connection still work?

    There is one more not clear feature:
    I disable peer verification and download the CA certificate. Then I connect to the domain first.example.com, but I cannot connect to example.com. At the same time, first.example.com and example.com have certificates from letsencrypt. Why is that?

  • Yury Morgunov said:
    if this certificate expires, will the connection still work?

     Yes, it should. You can easily test this by writing a wrong certificate to the modem, e.g. Google's, AWS's, one you create yourself. You should still be able to connect to your server.

     

    Yury Morgunov said:
    I disable peer verification and download the CA certificate. Then I connect to the domain first.example.com, but I cannot connect to example.com. At the same time, first.example.com and example.com have certificates from letsencrypt. Why is that?

     What error code do you get?

    Still connect() failed with -95?

    Do you have any logs from the server that could inform us about what is wrong?

    A modem trace could let us inspect the IP traffic between the modem and the server, which should give a good indication about what the problem is.

  • Error code remains the same 95

    #define EOPNOTSUPP 95 / * Operation not supported on socket * /

    When testing, I used GlobalSign-Root-CA-R2 which is located in the folder with the example

    Unfortunately now it is difficult to get trace log from the modem since I am using my board. The NRF9160 DK is currently not available.

    If it's not difficult for you, try connecting for example to https://www.integrasources.com

Reply Children
Related