Skip to content

Commit bbd0519

Browse files
committed
Object extraction from DN
1 parent 5f5a963 commit bbd0519

File tree

7 files changed

+428
-123
lines changed

7 files changed

+428
-123
lines changed

src/app/sslcertificates.cpp

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,23 +764,23 @@ int SSLCertificates::get_cert_HUM(char* Skey,size_t maxlength) {
764764
return 0;
765765
}
766766

767-
int SSLCertificates::get_CN_from_name(char* CN,size_t maxlength, X509_NAME2* certname)
767+
int SSLCertificates::get_DN_Elmt_from_name(char *CN, size_t maxlength, X509_NAME2 *certname, int NID)
768768
{
769769
int common_name_loc = -1;
770770
X509_NAME_ENTRY *common_name_entry = nullptr;
771771
ASN1_STRING *common_name_asn1 = nullptr;
772772

773-
// Find the position of the CN field in the Subject field of the certificate
774-
common_name_loc = X509_NAME_get_index_by_NID(certname, NID_commonName, -1);
773+
// Find the position of the field in the Subject field of the certificate
774+
common_name_loc = X509_NAME_get_index_by_NID(certname, NID, -1);
775775
if (common_name_loc < 0) {
776776
return 1;
777777
}
778-
// Extract the CN field
778+
// Extract the field
779779
common_name_entry = X509_NAME_get_entry(certname, common_name_loc);
780780
if (common_name_entry == nullptr) {
781781
return 1;
782782
}
783-
// Convert the CN field to a C string
783+
// Convert the field to a C string
784784
common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
785785
if (common_name_asn1 == nullptr) {
786786
return 1;
@@ -803,6 +803,59 @@ int SSLCertificates::get_CN_from_name(char* CN,size_t maxlength, X509_NAME2* cer
803803
return 0;
804804
}
805805

806+
int SSLCertificates::get_CN_from_name(char* CN,size_t maxlength, X509_NAME2* certname)
807+
{
808+
return this->get_DN_Elmt_from_name(CN,maxlength,certname,NID_commonName);
809+
}
810+
811+
int SSLCertificates::get_cert_subject_from_name(certType certORcsr, std::string *oCN, std::string *oC, std::string *oS,
812+
std::string *oL, std::string *oO, std::string *oOU,
813+
std::string *omail)
814+
{
815+
size_t maxlength=this->subjectElmntMaxSize;
816+
char buffer[this->subjectElmntMaxSize];
817+
X509_NAME2* certname;
818+
switch(certORcsr)
819+
{
820+
case Certificate: certname=X509_get_subject_name(this->x509);break;
821+
case CSR: certname=X509_REQ_get_subject_name(this->csr);break;
822+
case NoCert: return 1;
823+
}
824+
*omail="";
825+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,CN_NID) == 0)
826+
{
827+
std::string CN(buffer);
828+
size_t index = CN.find("/emailAddress=");
829+
if (index == std::string::npos)
830+
{ // not found
831+
oCN->assign(buffer);
832+
*omail="";
833+
}
834+
else
835+
{
836+
*oCN=CN.substr(0,index);
837+
*omail=CN.substr(index+14); // remove "/emailAddress"
838+
}
839+
}
840+
else *oCN="";
841+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,C_NID) == 0)
842+
oC->assign(buffer);
843+
else *oC="";
844+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,S_NID) == 0)
845+
oS->assign(buffer);
846+
else *oS="";
847+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,L_NID) == 0)
848+
oL->assign(buffer);
849+
else *oL="";
850+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,O_NID) == 0)
851+
oO->assign(buffer);
852+
else *oO="";
853+
if (this->get_DN_Elmt_from_name(buffer,maxlength,certname,OU_NID) == 0)
854+
oOU->assign(buffer);
855+
else *oOU="";
856+
return 0;
857+
}
858+
806859
int SSLCertificates::get_cert_CN(char* CN,size_t maxlength, X509* cert)
807860
{
808861
if (cert == nullptr) // TODO : check if cert is defined (checking nullptr ids not enough as the structure is defined in constructor). if not, the program crash
@@ -1302,6 +1355,33 @@ int SSLCertificates::x509_extension_add(std::string extensionNameI, std::string
13021355
return 0;
13031356
}
13041357

1358+
int SSLCertificates::x509_extension_get(std::vector<SSLCertificates::x509Extension> *extensions)
1359+
{
1360+
X509_EXTENSION * ext;
1361+
int extNID;
1362+
ASN1_OCTET_STRING * extValue;
1363+
int numExt=X509_get_ext_count(this->x509);
1364+
for (int i=0;i<numExt;i++)
1365+
{
1366+
x509Extension extVal;
1367+
ext=X509_get_ext(this->x509,i);
1368+
if (ext==nullptr) continue;
1369+
1370+
if (X509_EXTENSION_get_critical(ext)==1) extVal.critical=true;
1371+
else extVal.critical=false;
1372+
extValue=X509_EXTENSION_get_data(ext);
1373+
extNID=OBJ_obj2nid(X509_EXTENSION_get_object(ext));
1374+
BASIC_CONSTRAINTS* test;
1375+
AUTHORITY_KEYID *test2;
1376+
// TODO : get extensions and put it in the GUI. Seems every extension type has to be get by specific methods....
1377+
test=nullptr;
1378+
test2=nullptr;
1379+
extensions=nullptr;
1380+
// END TODO
1381+
}
1382+
return 0;
1383+
}
1384+
13051385
int SSLCertificates::add_ext(X509 *cert, int nid, const char *value)
13061386
{
13071387
X509_EXTENSION *ex;

src/app/sslcertificates.h

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class SSLCertificates
4343

4444
enum keyTypes { KeyNone=-1, KeyRSA=1, KeyDSA=2 , KeyEC = 3};
4545
enum certType { NoCert=-1, Certificate=0, CSR=1};
46+
static const size_t subjectElmntMaxSize=100;
47+
enum certSubjectList {
48+
CN_NID = NID_commonName,
49+
C_NID = NID_countryName,
50+
S_NID = NID_stateOrProvinceName,
51+
L_NID = NID_localityName,
52+
O_NID = NID_organizationName,
53+
OU_NID = NID_organizationalUnitName,
54+
email_NID = 0};
4655

4756
/**
4857
* @brief set_key_params : set parameters for all keys
@@ -118,6 +127,15 @@ class SSLCertificates
118127
* check ssl errors
119128
*/
120129
int get_cert_HUM(char* Skey,size_t maxlength);
130+
/**
131+
* @brief get_DN_Elmt_from_name
132+
* @param char * CN : returned element name
133+
* @param size_t maxlength
134+
* @param X509_NAME_st* certname
135+
* @param NID int : NID of name element
136+
* @return 0 = OK, 2 overflow, 1 error/not found.
137+
*/
138+
int get_DN_Elmt_from_name(char* CN,size_t maxlength, X509_NAME2* certname, int NID);
121139
/**
122140
* @brief get_CN_from_name
123141
* @param char * CN
@@ -126,6 +144,10 @@ class SSLCertificates
126144
* @return 0 = OK, 2 overflow, 1 error.
127145
*/
128146
int get_CN_from_name(char* CN,size_t maxlength, X509_NAME2* certname);
147+
148+
int get_cert_subject_from_name(certType certORcsr,std::string* oCN, std::string* oC, std::string* oS,
149+
std::string* oL, std::string* oO, std::string* oOU,
150+
std::string* omail);
129151
/**
130152
* @brief get_cert_CN : get CN of certificate (copy of openssl wiki)
131153
* @param CN : CN of certificate
@@ -318,20 +340,70 @@ class SSLCertificates
318340
typedef struct x509Extension {
319341
char name[50]; //!< Name of extension
320342
int NID; //!< NID of extension
321-
char values[200]; //!< possible values, comma separated
343+
char values[500]; //!< possible values, comma separated
344+
bool critical; //!< used to get extensions
322345
} x509Extension; //!< Structure for x509 extension array
323346
/* not declared as static if it can be read from openssl in future release */
324347
x509Extension X509ExtensionHelp[9] = {
325-
{"basicConstraints",NID_basic_constraints,"CA:TRUE,CA:FALSE,pathlen:<num>"},
326-
{"keyUsage",NID_key_usage,"digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly"},
327-
{"subjectAltName",NID_subject_alt_name,"URI:http://<site>,email:<mail>,IP:<IP4/6>"},
328-
{"crlDistributionPoints",NID_crl_distribution_points,"URI:http://<site>"},
329-
{"extendedKeyUsage",NID_ext_key_usage,"serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,OCSPSigning,ipsecIKE,msCodeInd,msCodeCom,msCTLSign,msEFS"},
330-
{"subjectKeyIdentifier",NID_subject_key_identifier,"<key>"},
331-
{SN_authority_key_identifier,NID_authority_key_identifier,"keyid:<key>"},
332-
{"certificatePolicies",NID_certificate_policies,"1.2.4.5"},
333-
{"policyConstraints",NID_policy_constraints,"requireExplicitPolicy:<num>,inhibitPolicyMapping:<num>"} //!<list of common X509v3 extensions
348+
{"basicConstraints",NID_basic_constraints,"CA:TRUE,CA:FALSE,pathlen:<num>",false},
349+
{"keyUsage",NID_key_usage,"digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",false},
350+
{"subjectAltName",NID_subject_alt_name,"URI:http://<site>,email:<mail>,IP:<IP4/6>",false},
351+
{"crlDistributionPoints",NID_crl_distribution_points,"URI:http://<site>",false},
352+
{"extendedKeyUsage",NID_ext_key_usage,"serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,OCSPSigning,ipsecIKE,msCodeInd,msCodeCom,msCTLSign,msEFS",false},
353+
{"subjectKeyIdentifier",NID_subject_key_identifier,"<key>",false},
354+
{SN_authority_key_identifier,NID_authority_key_identifier,"keyid:<key>",false},
355+
{"certificatePolicies",NID_certificate_policies,"1.2.4.5",false},
356+
{"policyConstraints",NID_policy_constraints,"requireExplicitPolicy:<num>,inhibitPolicyMapping:<num>",false} //!<list of common X509v3 extensions
334357
};
358+
/* From : https://www.openssl.org/docs/man1.1.0/man3/X509V3_EXT_d2i.html
359+
360+
RFC5280
361+
Basic Constraints NID_basic_constraints
362+
Key Usage NID_key_usage
363+
Extended Key Usage NID_ext_key_usage
364+
365+
Subject Key Identifier NID_subject_key_identifier
366+
Authority Key Identifier NID_authority_key_identifier
367+
368+
Private Key Usage Period NID_private_key_usage_period
369+
370+
Subject Alternative Name NID_subject_alt_name
371+
Issuer Alternative Name NID_issuer_alt_name
372+
373+
Authority Information Access NID_info_access
374+
Subject Information Access NID_sinfo_access
375+
376+
Name Constraints NID_name_constraints
377+
378+
Certificate Policies NID_certificate_policies
379+
Policy Mappings NID_policy_mappings
380+
Policy Constraints NID_policy_constraints
381+
Inhibit Any Policy NID_inhibit_any_policy
382+
383+
TLS Feature NID_tlsfeature
384+
385+
RFC5280
386+
CRL Number NID_crl_number
387+
CRL Distribution Points NID_crl_distribution_points
388+
Delta CRL Indicator NID_delta_crl
389+
Freshest CRL NID_freshest_crl
390+
Invalidity Date NID_invalidity_date
391+
Issuing Distribution Point NID_issuing_distribution_point
392+
393+
OSCP
394+
OCSP Nonce NID_id_pkix_OCSP_Nonce
395+
OCSP CRL ID NID_id_pkix_OCSP_CrlID
396+
Acceptable OCSP Responses NID_id_pkix_OCSP_acceptableResponses
397+
OCSP No Check NID_id_pkix_OCSP_noCheck
398+
OCSP Archive Cutoff NID_id_pkix_OCSP_archiveCutoff
399+
OCSP Service Locator NID_id_pkix_OCSP_serviceLocator
400+
Hold Instruction Code NID_hold_instruction_code
401+
402+
RFC6962
403+
CT Precertificate SCTs NID_ct_precert_scts
404+
CT Certificate SCTs NID_ct_cert_scts
405+
*/
406+
335407
int X509ExtensionHelpNum=8; //!< Number of X509ExtensionHelp
336408

337409
/**
@@ -343,7 +415,12 @@ class SSLCertificates
343415
*/
344416
int x509_extension_add(std::string extensionNameI, std::string extensionValI, int extensionCriticalI);
345417

346-
418+
/**
419+
* @brief x509_extension_get : get all X509v3 extensions from cert
420+
* @param extensions vector<x509Extension> * : values returned
421+
* @return 0 : success, 1 : error
422+
*/
423+
int x509_extension_get(std::vector<x509Extension>* extensions);
347424
private:
348425

349426
EVP_MD* useDigest; //!< Digest to use

0 commit comments

Comments
 (0)