Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse CN from DN using OpenSSL facilities #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions SSLBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,13 @@ ip::tcp::endpoint SSLBridge::getRemoteEndpoint() {
}

void SSLBridge::setServerName() {
X509 *serverCertificate = getServerCertificate();
X509_NAME *serverNameField = X509_get_subject_name(serverCertificate);
char *serverNameStr = X509_NAME_oneline(serverNameField, NULL, 0);

this->serverName = std::string((const char*)serverNameStr);
int commonNameIndex;

if ((commonNameIndex = this->serverName.find("CN=")) != std::string::npos)
this->serverName = this->serverName.substr(commonNameIndex+3);

free(serverNameStr);
X509 *serverCertificate = getServerCertificate();
X509_NAME *ptr = X509_get_subject_name(serverCertificate);
int sz = X509_NAME_get_text_by_NID(ptr, NID_commonName, NULL, 0) + 1;
char *commonNameStr = (char*)malloc(sz);
X509_NAME_get_text_by_NID(ptr, NID_commonName, commonNameStr, sz);
this->serverName = std::string((const char*)commonNameStr);
free(commonNameStr);
}

void SSLBridge::handshakeWithClient(CertificateManager &manager, bool wildcardOK) {
Expand Down
16 changes: 6 additions & 10 deletions certificate/Certificate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,12 @@ class Certificate {
}

void parseCommonName(X509 *cert) {
std::string distinguishedName(cert->name);
std::string::size_type cnIndex = distinguishedName.find("CN=");

if (cnIndex == std::string::npos) throw BadCertificateException();

std::string commonName = distinguishedName.substr(cnIndex+3);
std::string::size_type nullIndex = commonName.find("\\x00");

if (nullIndex != std::string::npos) this->name = commonName.substr(0, nullIndex);
else this->name = commonName;
X509_NAME *ptr = X509_get_subject_name(cert);
int sz = X509_NAME_get_text_by_NID(ptr, NID_commonName, NULL, 0) + 1;
char *commonNameStr = (char*)malloc(sz);
X509_NAME_get_text_by_NID(ptr, NID_commonName, commonNameStr, sz);
this->name = std::string((const char*)commonNameStr);
free(commonNameStr);
}

public:
Expand Down