15ebdc799SAdrien Destugues /* 25ebdc799SAdrien Destugues * Copyright 2014 Haiku, Inc. 35ebdc799SAdrien Destugues * Distributed under the terms of the MIT License. 45ebdc799SAdrien Destugues */ 55ebdc799SAdrien Destugues 65ebdc799SAdrien Destugues 75ebdc799SAdrien Destugues #include <Certificate.h> 85ebdc799SAdrien Destugues 95ebdc799SAdrien Destugues #include <String.h> 105ebdc799SAdrien Destugues 115ebdc799SAdrien Destugues #include "CertificatePrivate.h" 125ebdc799SAdrien Destugues 135ebdc799SAdrien Destugues 14159d1fb6SAdrien Destugues #ifdef OPENSSL_ENABLED 15159d1fb6SAdrien Destugues 16159d1fb6SAdrien Destugues 17385a7d89SAdrien Destugues static time_t 18385a7d89SAdrien Destugues parse_ASN1(ASN1_GENERALIZEDTIME *asn1) 195ebdc799SAdrien Destugues { 205ebdc799SAdrien Destugues // Get the raw string data out of the ASN1 container. It looks like this: 215ebdc799SAdrien Destugues // "YYMMDDHHMMSSZ" 225ebdc799SAdrien Destugues struct tm time; 235ebdc799SAdrien Destugues 245ebdc799SAdrien Destugues if (sscanf((char*)asn1->data, "%2d%2d%2d%2d%2d%2d", &time.tm_year, 255ebdc799SAdrien Destugues &time.tm_mon, &time.tm_mday, &time.tm_hour, &time.tm_min, 265ebdc799SAdrien Destugues &time.tm_sec) == 6) 275ebdc799SAdrien Destugues return mktime(&time); 285ebdc799SAdrien Destugues 295ebdc799SAdrien Destugues return B_BAD_DATA; 305ebdc799SAdrien Destugues } 315ebdc799SAdrien Destugues 325ebdc799SAdrien Destugues 33385a7d89SAdrien Destugues static BString 34385a7d89SAdrien Destugues decode_X509_NAME(X509_NAME* name) 355ebdc799SAdrien Destugues { 365ebdc799SAdrien Destugues int len = X509_NAME_get_text_by_NID(name, 0, NULL, 0); 375ebdc799SAdrien Destugues char buffer[len]; 385ebdc799SAdrien Destugues X509_NAME_get_text_by_NID(name, 0, buffer, len); 395ebdc799SAdrien Destugues 405ebdc799SAdrien Destugues return BString(buffer); 415ebdc799SAdrien Destugues } 425ebdc799SAdrien Destugues 435ebdc799SAdrien Destugues 445ebdc799SAdrien Destugues // #pragma mark - BCertificate 455ebdc799SAdrien Destugues 465ebdc799SAdrien Destugues 475ebdc799SAdrien Destugues BCertificate::BCertificate(Private* data) 485ebdc799SAdrien Destugues { 495ebdc799SAdrien Destugues fPrivate = data; 505ebdc799SAdrien Destugues } 515ebdc799SAdrien Destugues 525ebdc799SAdrien Destugues 535ebdc799SAdrien Destugues BCertificate::~BCertificate() 545ebdc799SAdrien Destugues { 555ebdc799SAdrien Destugues delete fPrivate; 565ebdc799SAdrien Destugues } 575ebdc799SAdrien Destugues 585ebdc799SAdrien Destugues 595ebdc799SAdrien Destugues BString 605ebdc799SAdrien Destugues BCertificate::String() 615ebdc799SAdrien Destugues { 625ebdc799SAdrien Destugues BIO *buffer = BIO_new(BIO_s_mem()); 635ebdc799SAdrien Destugues X509_print_ex(buffer, fPrivate->fX509, XN_FLAG_COMPAT, X509_FLAG_COMPAT); 645ebdc799SAdrien Destugues 655ebdc799SAdrien Destugues char* pointer; 665ebdc799SAdrien Destugues long length = BIO_get_mem_data(buffer, &pointer); 675ebdc799SAdrien Destugues BString result(pointer, length); 685ebdc799SAdrien Destugues 695ebdc799SAdrien Destugues BIO_free(buffer); 705ebdc799SAdrien Destugues return result; 715ebdc799SAdrien Destugues } 725ebdc799SAdrien Destugues 735ebdc799SAdrien Destugues 74*67af469eSAdrien Destugues time_t 755ebdc799SAdrien Destugues BCertificate::StartDate() 765ebdc799SAdrien Destugues { 775ebdc799SAdrien Destugues return parse_ASN1(X509_get_notBefore(fPrivate->fX509)); 785ebdc799SAdrien Destugues } 795ebdc799SAdrien Destugues 805ebdc799SAdrien Destugues 81*67af469eSAdrien Destugues time_t 825ebdc799SAdrien Destugues BCertificate::ExpirationDate() 835ebdc799SAdrien Destugues { 845ebdc799SAdrien Destugues return parse_ASN1(X509_get_notAfter(fPrivate->fX509)); 855ebdc799SAdrien Destugues } 865ebdc799SAdrien Destugues 875ebdc799SAdrien Destugues 885ebdc799SAdrien Destugues BString 895ebdc799SAdrien Destugues BCertificate::Issuer() 905ebdc799SAdrien Destugues { 915ebdc799SAdrien Destugues X509_NAME* name = X509_get_issuer_name(fPrivate->fX509); 925ebdc799SAdrien Destugues return decode_X509_NAME(name); 935ebdc799SAdrien Destugues } 945ebdc799SAdrien Destugues 955ebdc799SAdrien Destugues 965ebdc799SAdrien Destugues BString 975ebdc799SAdrien Destugues BCertificate::Subject() 985ebdc799SAdrien Destugues { 995ebdc799SAdrien Destugues X509_NAME* name = X509_get_subject_name(fPrivate->fX509); 1005ebdc799SAdrien Destugues return decode_X509_NAME(name); 1015ebdc799SAdrien Destugues } 1025ebdc799SAdrien Destugues 1035ebdc799SAdrien Destugues 1045ebdc799SAdrien Destugues // #pragma mark - BCertificate::Private 1055ebdc799SAdrien Destugues 1065ebdc799SAdrien Destugues 1075ebdc799SAdrien Destugues BCertificate::Private::Private(X509* data) 1085ebdc799SAdrien Destugues : fX509(data) 1095ebdc799SAdrien Destugues { 1105ebdc799SAdrien Destugues } 111159d1fb6SAdrien Destugues 112159d1fb6SAdrien Destugues #else 113159d1fb6SAdrien Destugues 114159d1fb6SAdrien Destugues 115159d1fb6SAdrien Destugues BCertificate::BCertificate(Private* data) 116159d1fb6SAdrien Destugues { 117159d1fb6SAdrien Destugues } 118159d1fb6SAdrien Destugues 119159d1fb6SAdrien Destugues 120159d1fb6SAdrien Destugues BCertificate::~BCertificate() 121159d1fb6SAdrien Destugues { 122159d1fb6SAdrien Destugues } 123159d1fb6SAdrien Destugues 124159d1fb6SAdrien Destugues 125159d1fb6SAdrien Destugues BString 126159d1fb6SAdrien Destugues BCertificate::String() 127159d1fb6SAdrien Destugues { 128159d1fb6SAdrien Destugues return BString(); 129159d1fb6SAdrien Destugues } 130159d1fb6SAdrien Destugues 131159d1fb6SAdrien Destugues 132*67af469eSAdrien Destugues time_t 133159d1fb6SAdrien Destugues BCertificate::StartDate() 134159d1fb6SAdrien Destugues { 135159d1fb6SAdrien Destugues return B_NOT_SUPPORTED; 136159d1fb6SAdrien Destugues } 137159d1fb6SAdrien Destugues 138159d1fb6SAdrien Destugues 139*67af469eSAdrien Destugues time_t 140159d1fb6SAdrien Destugues BCertificate::ExpirationDate() 141159d1fb6SAdrien Destugues { 142159d1fb6SAdrien Destugues return B_NOT_SUPPORTED; 143159d1fb6SAdrien Destugues } 144159d1fb6SAdrien Destugues 145159d1fb6SAdrien Destugues 146159d1fb6SAdrien Destugues BString 147159d1fb6SAdrien Destugues BCertificate::Issuer() 148159d1fb6SAdrien Destugues { 149159d1fb6SAdrien Destugues return BString(); 150159d1fb6SAdrien Destugues } 151159d1fb6SAdrien Destugues 152159d1fb6SAdrien Destugues 153159d1fb6SAdrien Destugues BString 154159d1fb6SAdrien Destugues BCertificate::Subject() 155159d1fb6SAdrien Destugues { 156159d1fb6SAdrien Destugues } 157159d1fb6SAdrien Destugues 158159d1fb6SAdrien Destugues 159159d1fb6SAdrien Destugues #endif 160