summaryrefslogtreecommitdiffstats
path: root/examples/certtest/certtest.cpp
blob: db82993a5eb3cbfabb2a58a44c492cec65174dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<tqdom.h>
#include<tqfile.h>
#include"base64.h"
#include"qca.h"

TQCA::Cert readCertXml(const TQDomElement &e)
{
	TQCA::Cert cert;
	// there should be one child data tag
	TQDomElement data = e.elementsByTagName("data").item(0).toElement();
	if(!data.isNull())
		cert.fromDER(Base64::stringToArray(data.text()));
	return cert;
}

void showCertInfo(const TQCA::Cert &cert)
{
	printf(" CN: %s\n", cert.subject()["CN"].latin1());
	printf(" Valid from: %s, until %s\n",
		cert.notBefore().toString().latin1(),
		cert.notAfter().toString().latin1());
	printf(" PEM:\n%s\n", cert.toPEM().latin1());
}

int main()
{
	if(!TQCA::isSupported(TQCA::CAP_X509)) {
		printf("X509 not supported!\n");
		return 1;
	}

	// open the Psi rootcerts file
	TQFile f("/usr/local/share/psi/certs/rootcert.xml");
	if(!f.open(IO_ReadOnly)) {
		printf("unable to open %s\n", f.name().latin1());
		return 1;
	}
	TQDomDocument doc;
	doc.setContent(&f);
	f.close();

	TQDomElement base = doc.documentElement();
	if(base.tagName() != "store") {
		printf("wrong format of %s\n", f.name().latin1());
		return 1;
	}
	TQDomNodeList cl = base.elementsByTagName("certificate");
	if(cl.count() == 0) {
		printf("no certs found in %s\n", f.name().latin1());
		return 1;
	}

	for(int n = 0; n < (int)cl.count(); ++n) {
		printf("-- Cert %d --\n", n);
		TQCA::Cert cert = readCertXml(cl.item(n).toElement());
		if(cert.isNull()) {
			printf("error reading cert\n");
			continue;
		}
		showCertInfo(cert);
	}

	return 0;
}