vfs: check userland buffers before reading them.
[haiku.git] / src / kits / network / libnetapi / Certificate.cpp
blob957790ddb98e0f63cc159a08b06ad313b7b19b60
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(const BCertificate& other)
63 fPrivate = new(std::nothrow) BCertificate::Private(other.fPrivate->fX509);
67 BCertificate::~BCertificate()
69 delete fPrivate;
73 int
74 BCertificate::Version() const
76 return X509_get_version(fPrivate->fX509) + 1;
80 time_t
81 BCertificate::StartDate() const
83 return parse_ASN1(X509_get_notBefore(fPrivate->fX509));
87 time_t
88 BCertificate::ExpirationDate() const
90 return parse_ASN1(X509_get_notAfter(fPrivate->fX509));
94 bool
95 BCertificate::IsValidAuthority() const
97 return X509_check_ca(fPrivate->fX509) > 0;
101 bool
102 BCertificate::IsSelfSigned() const
104 return X509_check_issued(fPrivate->fX509, fPrivate->fX509) == X509_V_OK;
108 BString
109 BCertificate::Issuer() const
111 X509_NAME* name = X509_get_issuer_name(fPrivate->fX509);
112 return decode_X509_NAME(name);
116 BString
117 BCertificate::Subject() const
119 X509_NAME* name = X509_get_subject_name(fPrivate->fX509);
120 return decode_X509_NAME(name);
124 BString
125 BCertificate::SignatureAlgorithm() const
127 int algorithmIdentifier = OBJ_obj2nid(
128 fPrivate->fX509->cert_info->key->algor->algorithm);
130 if (algorithmIdentifier == NID_undef)
131 return BString("undefined");
133 const char* buffer = OBJ_nid2ln(algorithmIdentifier);
134 return BString(buffer);
138 BString
139 BCertificate::String() const
141 BIO *buffer = BIO_new(BIO_s_mem());
142 X509_print_ex(buffer, fPrivate->fX509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
144 char* pointer;
145 long length = BIO_get_mem_data(buffer, &pointer);
146 BString result(pointer, length);
148 BIO_free(buffer);
149 return result;
153 bool
154 BCertificate::operator==(const BCertificate& other) const
156 return X509_cmp(fPrivate->fX509, other.fPrivate->fX509) == 0;
160 // #pragma mark - BCertificate::Private
163 BCertificate::Private::Private(X509* data)
164 : fX509(X509_dup(data))
169 BCertificate::Private::~Private()
171 X509_free(fX509);
175 #else
178 BCertificate::BCertificate(const BCertificate& other)
183 BCertificate::BCertificate(Private* data)
188 BCertificate::~BCertificate()
193 time_t
194 BCertificate::StartDate() const
196 return B_NOT_SUPPORTED;
200 time_t
201 BCertificate::ExpirationDate() const
203 return B_NOT_SUPPORTED;
207 bool
208 BCertificate::IsValidAuthority() const
210 return false;
215 BCertificate::Version() const
217 return B_NOT_SUPPORTED;
221 BString
222 BCertificate::Issuer() const
224 return BString();
228 BString
229 BCertificate::Subject() const
231 return BString();
235 BString
236 BCertificate::SignatureAlgorithm() const
238 return BString();
242 BString
243 BCertificate::String() const
245 return BString();
249 bool
250 BCertificate::operator==(const BCertificate& other) const
252 return false;
255 #endif