qdevice-net-nss.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2015-2026 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sslerr.h>
  35. #include <secerr.h>
  36. #include "log.h"
  37. #include "qdevice-net-nss.h"
  38. #include "qdevice-net-instance.h"
  39. #include "qnet-config.h"
  40. SECStatus
  41. qdevice_net_nss_bad_cert_hook(void *arg, PRFileDesc *fd) {
  42. if (PR_GetError() == SEC_ERROR_EXPIRED_CERTIFICATE ||
  43. PR_GetError() == SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE ||
  44. PR_GetError() == SEC_ERROR_CRL_EXPIRED ||
  45. PR_GetError() == SEC_ERROR_KRL_EXPIRED ||
  46. PR_GetError() == SSL_ERROR_EXPIRED_CERT_ALERT) {
  47. log(LOG_WARNING, "Server certificate is expired.");
  48. return (SECSuccess);
  49. }
  50. log_nss(LOG_ERR, "Server certificate verification failure.");
  51. return (SECFailure);
  52. }
  53. SECStatus
  54. qdevice_net_nss_get_client_auth_data(void *arg, PRFileDesc *sock, struct CERTDistNamesStr *caNames,
  55. struct CERTCertificateStr **pRetCert, struct SECKEYPrivateKeyStr **pRetKey)
  56. {
  57. struct qdevice_net_instance *instance;
  58. log(LOG_DEBUG, "Sending client auth data.");
  59. instance = (struct qdevice_net_instance *)arg;
  60. instance->tls_client_cert_sent = 1;
  61. return (NSS_GetClientAuthData((void *)instance->advanced_settings->net_nss_client_cert_nickname,
  62. sock, caNames, pRetCert, pRetKey));
  63. }
  64. static void
  65. qdevice_net_nss_dump_cert_info(const char *prefix, CERTCertificate *cert)
  66. {
  67. char *nss_subject, *nss_issuer;
  68. const char *subject, *issuer;
  69. SECOidData *signature_oid_data, *public_key_oid_data;
  70. const char *signature_algorithm_desc, *public_key_algorithm_desc;
  71. nss_subject = CERT_NameToAscii(&cert->subject);
  72. subject = (nss_subject != NULL ? nss_subject : "Unknown");
  73. nss_issuer = CERT_NameToAscii(&cert->issuer);
  74. issuer = (nss_issuer != NULL ? nss_issuer : "Unknown");
  75. signature_oid_data = SECOID_FindOID(&cert->signature.algorithm);
  76. signature_algorithm_desc = ((signature_oid_data != NULL && signature_oid_data->desc != NULL) ?
  77. signature_oid_data->desc : "Unknown");
  78. public_key_oid_data = SECOID_FindOID(&cert->subjectPublicKeyInfo.algorithm.algorithm);
  79. public_key_algorithm_desc = ((public_key_oid_data != NULL && public_key_oid_data->desc != NULL) ?
  80. public_key_oid_data->desc : "Unknown");
  81. log(LOG_DEBUG, " %s certificate Subject: %s, Issuer: %s, Signature Algorithm: %s, Public Key Algorithm: %s",
  82. prefix, subject, issuer, signature_algorithm_desc, public_key_algorithm_desc);
  83. if (nss_subject != NULL) {
  84. PORT_Free(nss_subject);
  85. }
  86. if (nss_issuer != NULL) {
  87. PORT_Free(nss_issuer);
  88. }
  89. }
  90. void
  91. qdevice_net_nss_handshake_callback(PRFileDesc *fd, void *client_data)
  92. {
  93. SSLChannelInfo ci = { 0 };
  94. CERTCertificate *cert;
  95. if (SSL_GetChannelInfo(fd, &ci, sizeof(ci)) == SECSuccess) {
  96. /*
  97. * No easy way to decode this numbers so they are written unencoded.
  98. * Values are in the /usr/include/nss3/sslt.h
  99. * - SSLKEAType - ssl_kea_ecdh = 4, ssl_kea_ecdh_hybrid = 8 (PQC)
  100. * - SSLNamedGroup - ssl_grp_ec_secp256r1 = 23,ssl_grp_kem_mlkem768x25519 = 4588 (PQC)
  101. */
  102. log(LOG_DEBUG, "Using TLS channel protocol version: %04x, keaType: %u, keaGroup: %u",
  103. ci.protocolVersion, ci.keaType, ci.keaGroup);
  104. } else {
  105. log_nss(LOG_WARNING, "qdevice_net_nss_handshake_callback SSL_GetChannelInfo error");
  106. }
  107. cert = SSL_PeerCertificate(fd);
  108. if (cert != NULL) {
  109. qdevice_net_nss_dump_cert_info("Peer", cert);
  110. CERT_DestroyCertificate(cert);
  111. }
  112. cert = SSL_LocalCertificate(fd);
  113. if (cert != NULL) {
  114. qdevice_net_nss_dump_cert_info("Local", cert);
  115. CERT_DestroyCertificate(cert);
  116. }
  117. }