BPicture: Fix archive constructor.
[haiku.git] / src / kits / network / libnetapi / Certificate.cpp
blob290a9bb07c3f9a5a29f0e781d246eb6149e15f62
1 /*
2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <Certificate.h>
9 #include <String.h>
11 #include "CertificatePrivate.h"
14 #ifdef OPENSSL_ENABLED
17 #include <openssl/x509v3.h>
20 static time_t
21 parse_ASN1(ASN1_GENERALIZEDTIME *asn1)
23 // Get the raw string data out of the ASN1 container. It looks like this:
24 // "YYMMDDHHMMSSZ"
25 struct tm time;
27 if (sscanf((char*)asn1->data, "%2d%2d%2d%2d%2d%2d", &time.tm_year,
28 &time.tm_mon, &time.tm_mday, &time.tm_hour, &time.tm_min,
29 &time.tm_sec) == 6) {
31 // Month is 0 based, and year is 1900-based for mktime.
32 time.tm_year += 100;
33 time.tm_mon -= 1;
35 return mktime(&time);
37 return B_BAD_DATA;
41 static BString
42 decode_X509_NAME(X509_NAME* name)
44 char* buffer = X509_NAME_oneline(name, NULL, 0);
46 BString result(buffer);
47 OPENSSL_free(buffer);
48 return result;
52 // #pragma mark - BCertificate
55 BCertificate::BCertificate(Private* data)
57 fPrivate = data;
61 BCertificate::~BCertificate()
63 delete fPrivate;
67 int
68 BCertificate::Version()
70 return X509_get_version(fPrivate->fX509) + 1;
74 time_t
75 BCertificate::StartDate()
77 return parse_ASN1(X509_get_notBefore(fPrivate->fX509));
81 time_t
82 BCertificate::ExpirationDate()
84 return parse_ASN1(X509_get_notAfter(fPrivate->fX509));
88 bool
89 BCertificate::IsValidAuthority()
91 return X509_check_ca(fPrivate->fX509) > 0;
95 bool
96 BCertificate::IsSelfSigned()
98 return X509_check_issued(fPrivate->fX509, fPrivate->fX509) == X509_V_OK;
102 BString
103 BCertificate::Issuer()
105 X509_NAME* name = X509_get_issuer_name(fPrivate->fX509);
106 return decode_X509_NAME(name);
110 BString
111 BCertificate::Subject()
113 X509_NAME* name = X509_get_subject_name(fPrivate->fX509);
114 return decode_X509_NAME(name);
118 BString
119 BCertificate::SignatureAlgorithm()
121 int algorithmIdentifier = OBJ_obj2nid(
122 fPrivate->fX509->cert_info->key->algor->algorithm);
124 if (algorithmIdentifier == NID_undef)
125 return BString("undefined");
127 const char* buffer = OBJ_nid2ln(algorithmIdentifier);
128 return BString(buffer);
132 BString
133 BCertificate::String()
135 BIO *buffer = BIO_new(BIO_s_mem());
136 X509_print_ex(buffer, fPrivate->fX509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
138 char* pointer;
139 long length = BIO_get_mem_data(buffer, &pointer);
140 BString result(pointer, length);
142 BIO_free(buffer);
143 return result;
147 // #pragma mark - BCertificate::Private
150 BCertificate::Private::Private(X509* data)
151 : fX509(data)
156 #else
159 BCertificate::BCertificate(Private* data)
164 BCertificate::~BCertificate()
169 time_t
170 BCertificate::StartDate()
172 return B_NOT_SUPPORTED;
176 time_t
177 BCertificate::ExpirationDate()
179 return B_NOT_SUPPORTED;
183 bool
184 BCertificate::IsValidAuthority()
186 return false;
191 BCertificate::Version()
193 return B_NOT_SUPPORTED;
197 BString
198 BCertificate::Issuer()
200 return BString();
204 BString
205 BCertificate::Subject()
207 return BString();
211 BString
212 BCertificate::SignatureAlgorithm()
214 return BString();
218 BString
219 BCertificate::String()
221 return BString();
225 #endif