08-ssl-connect-cert-auth-enc.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <errno.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <mosquitto.h>
  7. static int run = -1;
  8. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  9. {
  10. if(rc){
  11. exit(1);
  12. }else{
  13. mosquitto_disconnect(mosq);
  14. }
  15. }
  16. void on_disconnect(struct mosquitto *mosq, void *obj, int rc)
  17. {
  18. run = rc;
  19. }
  20. static int password_callback(char* buf, int size, int rwflag, void* userdata)
  21. {
  22. strncpy(buf, "password", size);
  23. buf[size-1] = '\0';
  24. return strlen(buf);
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. int rc;
  29. struct mosquitto *mosq;
  30. int port = atoi(argv[1]);
  31. mosquitto_lib_init();
  32. mosq = mosquitto_new("08-ssl-connect-crt-auth-enc", true, NULL);
  33. if(mosq == NULL){
  34. return 1;
  35. }
  36. mosquitto_tls_set(mosq, "../ssl/test-root-ca.crt", "../ssl/certs", "../ssl/client-encrypted.crt", "../ssl/client-encrypted.key", password_callback);
  37. mosquitto_connect_callback_set(mosq, on_connect);
  38. mosquitto_disconnect_callback_set(mosq, on_disconnect);
  39. rc = mosquitto_connect(mosq, "localhost", port, 60);
  40. while(run == -1){
  41. mosquitto_loop(mosq, -1, 1);
  42. }
  43. mosquitto_destroy(mosq);
  44. mosquitto_lib_cleanup();
  45. return run;
  46. }