|
2 | 2 | * @file TlsServer.hpp |
3 | 3 | * @author Nils Henrich |
4 | 4 | * @brief TLS server for encrypted data transfer with authentication. |
5 | | - * @version 3.2.0 |
| 5 | + * @version 3.2.1 |
6 | 6 | * @date 2021-12-27 |
7 | 7 | * |
8 | | - * @copyright Copyright (c) 2021 |
| 8 | + * @copyright Copyright (c) 2025 |
9 | 9 | * |
10 | 10 | */ |
11 | 11 |
|
@@ -62,40 +62,42 @@ namespace tcp |
62 | 62 |
|
63 | 63 | /** |
64 | 64 | * @brief Get specific subject part as string of the certificate of a specific connected client (Identified by its TCP ID). |
| 65 | + * Throw Server_error if client ID is not found or NID is invalid. |
65 | 66 | * |
66 | 67 | * @param clientId |
67 | | - * @param tlsSocket |
68 | 68 | * @param subjPart |
69 | 69 | * @return string |
70 | 70 | */ |
71 | | - ::std::string getSubjPartFromClientCert(const int clientId, const SSL *tlsSocket, const int subjPart) |
| 71 | + ::std::string getSubjPartFromClientCert(const int clientId, const int subjPart) |
72 | 72 | { |
73 | 73 | char buf[256]{0}; |
| 74 | + ::std::lock_guard<::std::mutex> lck{activeConnections_m}; |
74 | 75 |
|
75 | | - // If TLS socket is null, get socket from list of connected clients |
76 | | - if (!tlsSocket) |
| 76 | + // Check if client is connected |
| 77 | + if (activeConnections.find(clientId) == activeConnections.end()) |
77 | 78 | { |
78 | | - ::std::lock_guard<::std::mutex> lck{activeConnections_m}; |
79 | | - if (activeConnections.find(clientId) == activeConnections.end()) |
80 | | - { |
81 | 79 | #ifdef DEVELOP |
82 | | - ::std::cerr << DEBUGINFO << ": No connected client " << clientId << ::std::endl; |
| 80 | + ::std::cerr << DEBUGINFO << ": No connected client " << clientId << ::std::endl; |
83 | 81 | #endif // DEVELOP |
84 | 82 |
|
85 | | - return ""; |
86 | | - } |
87 | | - |
88 | | - tlsSocket = activeConnections[clientId].get(); |
| 83 | + throw Server_error("No connected client " + ::std::to_string(clientId) + " to read certificate subject part from"); |
89 | 84 | } |
90 | 85 |
|
91 | 86 | // Read client certificate from TLS channel |
92 | | - ::std::unique_ptr<X509, void (*)(X509 *)> remoteCert{SSL_get_peer_certificate(tlsSocket), X509_free}; |
| 87 | + ::std::unique_ptr<X509, void (*)(X509 *)> remoteCert{SSL_get_peer_certificate(activeConnections[clientId].get()), X509_free}; |
93 | 88 |
|
94 | | - // Get wholw subject part from client certificate |
| 89 | + // Get whole subject part from client certificate |
95 | 90 | X509_NAME *remoteCertSubject{X509_get_subject_name(remoteCert.get())}; |
96 | 91 |
|
97 | 92 | // Get specific part from subject |
98 | | - X509_NAME_get_text_by_NID(remoteCertSubject, subjPart, buf, 256); |
| 93 | + if (-1 == X509_NAME_get_text_by_NID(remoteCertSubject, subjPart, buf, 256)) |
| 94 | + { |
| 95 | +#ifdef DEVELOP |
| 96 | + ::std::cerr << DEBUGINFO << ": Invalid NID " << subjPart << " for certificate subject" << ::std::endl; |
| 97 | +#endif // DEVELOP |
| 98 | + |
| 99 | + throw Server_error("Invalid NID " + ::std::to_string(subjPart) + " for client certificate subject"); |
| 100 | + } |
99 | 101 |
|
100 | 102 | // Return subject part as string |
101 | 103 | return ::std::string(buf); |
|
0 commit comments