Skip to content

Commit 0fd7ba8

Browse files
Add comprehensive CERTS cell diagnostic logging
Add round-trip verification of Type 2 RSA identity cert: - Parse cert DER back to X509 and check key type/bits - Log cert sizes for all 4 cert types - Hex dump first 40 bytes of CERTS cell payload Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ca8bcf4 commit 0fd7ba8

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/protocol/link_protocol.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <algorithm>
55
#include <chrono>
66
#include <cstring>
7+
#include <openssl/evp.h>
78
#include <openssl/rand.h>
89
#include <openssl/sha.h>
10+
#include <openssl/x509.h>
911

1012
namespace tor::protocol {
1113

@@ -157,8 +159,30 @@ CertsHandler::create_certs_cell(
157159
// Type 2: RSA identity cert (self-signed X.509)
158160
auto cert2 = rsa_identity.create_identity_cert();
159161
if (!cert2) {
162+
LOG_WARN("OR CERTS: failed to create RSA identity cert");
160163
return std::unexpected(LinkProtocolError::CertificateError);
161164
}
165+
LOG_INFO("OR CERTS: Type 2 RSA identity cert: {} bytes", cert2->size());
166+
167+
// Debug: verify the cert round-trips correctly
168+
{
169+
const unsigned char* p = cert2->data();
170+
X509* x509_check = d2i_X509(nullptr, &p, static_cast<long>(cert2->size()));
171+
if (x509_check) {
172+
EVP_PKEY* pk = X509_get0_pubkey(x509_check);
173+
if (pk) {
174+
int key_type = EVP_PKEY_base_id(pk);
175+
int key_bits = EVP_PKEY_bits(pk);
176+
LOG_INFO("OR CERTS: Type 2 cert key: type={} bits={} (expect type=6[RSA] bits=1024)",
177+
key_type, key_bits);
178+
} else {
179+
LOG_WARN("OR CERTS: Type 2 cert has NULL public key!");
180+
}
181+
X509_free(x509_check);
182+
} else {
183+
LOG_WARN("OR CERTS: Type 2 cert failed DER parse!");
184+
}
185+
}
162186

163187
// Type 4: Ed25519 signing key, certified by identity key
164188
auto cert4 = build_ed25519_cert(
@@ -203,7 +227,23 @@ CertsHandler::create_certs_cell(
203227
payload.write_u16(static_cast<uint16_t>(cert7->size()));
204228
payload.write_bytes(*cert7);
205229

206-
return core::VariableCell(0, core::CellCommand::CERTS, payload.take());
230+
auto payload_data = payload.take();
231+
LOG_INFO("OR CERTS: total payload {} bytes: N={} cert2={} cert4={} cert5={} cert7={}",
232+
payload_data.size(), 4, cert2->size(), cert4.size(), cert5.size(), cert7->size());
233+
234+
// Dump first 20 hex bytes of the payload for debugging
235+
{
236+
std::string hex;
237+
for (size_t i = 0; i < std::min(payload_data.size(), size_t(40)); ++i) {
238+
char buf[4];
239+
snprintf(buf, sizeof(buf), "%02x", payload_data[i]);
240+
hex += buf;
241+
if (i < 39) hex += " ";
242+
}
243+
LOG_INFO("OR CERTS: payload hex: {}", hex);
244+
}
245+
246+
return core::VariableCell(0, core::CellCommand::CERTS, std::move(payload_data));
207247
}
208248

209249
std::expected<std::vector<crypto::TorCertificate>, LinkProtocolError>

0 commit comments

Comments
 (0)