2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
7 #include <Certificate.h>
11 #include "CertificatePrivate.h"
14 #ifdef OPENSSL_ENABLED
17 #include <openssl/x509v3.h>
21 parse_ASN1(ASN1_GENERALIZEDTIME
*asn1
)
23 // Get the raw string data out of the ASN1 container. It looks like this:
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
,
31 // Month is 0 based, and year is 1900-based for mktime.
42 decode_X509_NAME(X509_NAME
* name
)
44 char* buffer
= X509_NAME_oneline(name
, NULL
, 0);
46 BString
result(buffer
);
52 // #pragma mark - BCertificate
55 BCertificate::BCertificate(Private
* data
)
61 BCertificate::~BCertificate()
68 BCertificate::Version()
70 return X509_get_version(fPrivate
->fX509
) + 1;
75 BCertificate::StartDate()
77 return parse_ASN1(X509_get_notBefore(fPrivate
->fX509
));
82 BCertificate::ExpirationDate()
84 return parse_ASN1(X509_get_notAfter(fPrivate
->fX509
));
89 BCertificate::IsValidAuthority()
91 return X509_check_ca(fPrivate
->fX509
) > 0;
96 BCertificate::IsSelfSigned()
98 return X509_check_issued(fPrivate
->fX509
, fPrivate
->fX509
) == X509_V_OK
;
103 BCertificate::Issuer()
105 X509_NAME
* name
= X509_get_issuer_name(fPrivate
->fX509
);
106 return decode_X509_NAME(name
);
111 BCertificate::Subject()
113 X509_NAME
* name
= X509_get_subject_name(fPrivate
->fX509
);
114 return decode_X509_NAME(name
);
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
);
133 BCertificate::String()
135 BIO
*buffer
= BIO_new(BIO_s_mem());
136 X509_print_ex(buffer
, fPrivate
->fX509
, XN_FLAG_COMPAT
, X509_FLAG_COMPAT
);
139 long length
= BIO_get_mem_data(buffer
, &pointer
);
140 BString
result(pointer
, length
);
147 // #pragma mark - BCertificate::Private
150 BCertificate::Private::Private(X509
* data
)
159 BCertificate::BCertificate(Private
* data
)
164 BCertificate::~BCertificate()
170 BCertificate::StartDate()
172 return B_NOT_SUPPORTED
;
177 BCertificate::ExpirationDate()
179 return B_NOT_SUPPORTED
;
184 BCertificate::IsValidAuthority()
191 BCertificate::Version()
193 return B_NOT_SUPPORTED
;
198 BCertificate::Issuer()
205 BCertificate::Subject()
212 BCertificate::SignatureAlgorithm()
219 BCertificate::String()