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(const BCertificate
& other
)
63 fPrivate
= new(std::nothrow
) BCertificate::Private(other
.fPrivate
->fX509
);
67 BCertificate::~BCertificate()
74 BCertificate::Version() const
76 return X509_get_version(fPrivate
->fX509
) + 1;
81 BCertificate::StartDate() const
83 return parse_ASN1(X509_get_notBefore(fPrivate
->fX509
));
88 BCertificate::ExpirationDate() const
90 return parse_ASN1(X509_get_notAfter(fPrivate
->fX509
));
95 BCertificate::IsValidAuthority() const
97 return X509_check_ca(fPrivate
->fX509
) > 0;
102 BCertificate::IsSelfSigned() const
104 return X509_check_issued(fPrivate
->fX509
, fPrivate
->fX509
) == X509_V_OK
;
109 BCertificate::Issuer() const
111 X509_NAME
* name
= X509_get_issuer_name(fPrivate
->fX509
);
112 return decode_X509_NAME(name
);
117 BCertificate::Subject() const
119 X509_NAME
* name
= X509_get_subject_name(fPrivate
->fX509
);
120 return decode_X509_NAME(name
);
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
);
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
);
145 long length
= BIO_get_mem_data(buffer
, &pointer
);
146 BString
result(pointer
, length
);
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()
178 BCertificate::BCertificate(const BCertificate
& other
)
183 BCertificate::BCertificate(Private
* data
)
188 BCertificate::~BCertificate()
194 BCertificate::StartDate() const
196 return B_NOT_SUPPORTED
;
201 BCertificate::ExpirationDate() const
203 return B_NOT_SUPPORTED
;
208 BCertificate::IsValidAuthority() const
215 BCertificate::Version() const
217 return B_NOT_SUPPORTED
;
222 BCertificate::Issuer() const
229 BCertificate::Subject() const
236 BCertificate::SignatureAlgorithm() const
243 BCertificate::String() const
250 BCertificate::operator==(const BCertificate
& other
) const