/* vi: set sw=4 ts=4: * * Copyright (C) 2001 - 2007 Christian Hohnstaedt. * * All rights reserved. */ #include "x509v3ext.h" #include #include #include #include "base.h" x509v3ext::x509v3ext() { ext = X509_EXTENSION_new(); } x509v3ext::x509v3ext(const X509_EXTENSION *n) { ext = X509_EXTENSION_dup((X509_EXTENSION *)n); } x509v3ext::x509v3ext(const x509v3ext &n) { ext = NULL; set(n.ext); } x509v3ext::~x509v3ext() { X509_EXTENSION_free(ext); } x509v3ext &x509v3ext::set(const X509_EXTENSION *n) { if (ext != NULL) X509_EXTENSION_free(ext); ext = X509_EXTENSION_dup((X509_EXTENSION *)n); return *this; } x509v3ext &x509v3ext::create(int nid, const QString &et, X509V3_CTX *ctx) { if (ext) { X509_EXTENSION_free(ext); ext = NULL; } if (!et.isEmpty()) { ext = X509V3_EXT_conf_nid(NULL, ctx, nid, (char*)CCHAR(et)); } if (!ext) ext = X509_EXTENSION_new(); return *this; } int x509v3ext::nid() const { ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext); return OBJ_obj2nid(obj); } void *x509v3ext::d2i() { return X509V3_EXT_d2i(ext); } /* bool x509v3ext::operator == (const x509v3ext &x) const { return (X509_EXTENSION_cmp(ext, x.ext) == 0); } */ x509v3ext &x509v3ext::operator = (const x509v3ext &x) { set(x.ext); return *this; } QString x509v3ext::getObject() const { QString a = OBJ_nid2ln(nid()); return a; } int x509v3ext::getCritical() const { return X509_EXTENSION_get_critical(ext); } QString x509v3ext::getValue() const { #define V3_BUF 100 QString text = ""; int len; char buffer[V3_BUF+1]; BIO *bio = BIO_new(BIO_s_mem()); if(!X509V3_EXT_print(bio, ext, X509V3_EXT_PARSE_UNKNOWN, 0)) return text; do { len = BIO_read(bio, buffer, V3_BUF); if (len < 0) break; buffer[len] = '\0'; text+=buffer; } while (len == V3_BUF); #undef V3_BUF return text; } QString x509v3ext::getHtml() const { QString html; html = "" + getObject(); if (getCritical() != 0) html += " critical"; html += ":
" + getValue() + ""; return html; } X509_EXTENSION *x509v3ext::get() const { return X509_EXTENSION_dup(ext); } bool x509v3ext::isValid() const { return ext->value->length > 0; } /*************************************************************/ void extList::setStack(STACK_OF(X509_EXTENSION) *st) { clear(); int cnt = sk_X509_EXTENSION_num(st); x509v3ext e; for (int i=0; i