18#include "../include/X509Context.h"
19#include <isode/crypto/x509.h>
26 lists_equiv(
const std::list<std::string>& a,
const std::list<std::string>& b)
28 if (a.size() != b.size())
34 return std::equal(a.begin(), a.end(), b.begin());
39namespace X509_Context {
40 bool Config::set_ldap_url(
const std::string& url) {
41 if (url.compare(0, 7,
"ldap://") != 0) {
46 while (pos < url.length() && url[pos] !=
':' && url[pos] !=
'/') {
47 ldap_host += url[pos];
50 if (pos == url.length() || url[pos] ==
'/') {
53 std::string port(url.substr(pos+1));
54 if (port[port.length()-1] ==
'/')
55 port = port.substr(0, port.length()-1);
56 for (pos=0; pos < port.length(); ++pos)
57 if (!std::isdigit(port[pos]))
59 ldap_port = atoi(port.c_str());
63 Identity::Identity(Config& confobj)
65 const std::string pkcs12_file(confobj.ident_file);
66 const std::string pphr_file(confobj.ident_pphr_file);
67 if (pkcs12_file.empty() || pphr_file.empty())
68 throw std::invalid_argument(
"no private key info");
70 identity = x509_create_identity_from_file(pkcs12_file.c_str(),
71 pphr_file.c_str(), 1);
73 throw std::runtime_error(
"unable to create identity");
74 cert_ctx = x509_create_cert_ctx();
76 throw std::runtime_error(
"unable to create cert_ctx");
77 x509_cert_ctx_add_identity(cert_ctx, identity);
79 for (std::list<std::string>::const_iterator
80 i = confobj.trust_anchors.begin();
81 i != confobj.trust_anchors.end(); ++i) {
82 const std::string& name = *i;
83 X509 *ta = x509_read_cert(name.c_str());
85 x509_cert_ctx_add_cert(cert_ctx, ta, 1);
89 for (std::list<std::string>::const_iterator i = confobj.certs.begin();
90 i != confobj.certs.end(); ++i) {
91 const std::string& name = *i;
92 X509 *cert = x509_read_cert(name.c_str());
94 x509_cert_ctx_add_cert(cert_ctx, cert, 0);
98 x509_cert_ctx_set_ldap(cert_ctx, confobj.ldap_host.empty() ?
99 0:confobj.ldap_host.c_str(), confobj.ldap_port);
100 x509_cert_ctx_check_revocation(cert_ctx, confobj.check_revocation);
102 X509* OCSPresponder = 0;
103 if (!confobj.OCSPresponder.empty()) {
104 OCSPresponder = x509_read_cert(confobj.OCSPresponder.c_str());
106 x509_cert_ctx_set_ocsp(cert_ctx, confobj.OCSPuri.c_str(), OCSPresponder,
107 confobj.OCSPnonce, 0, 0);
109 x509_cert_ctx_set_lookup_flags(cert_ctx, confobj.lookup_flags);
112 Identity::Identity(PKCS12 * p12,
const char * passphrase)
114 identity = x509_create_identity_from_pkcs12(p12, passphrase);
117 throw std::runtime_error(
"unable to create identity");
118 cert_ctx = x509_create_cert_ctx();
120 throw std::runtime_error(
"unable to create cert_ctx");
121 x509_cert_ctx_add_identity(cert_ctx, identity);
124 bool Config::operator!=(
const Config&that)
const {
125 if (ident_file != that.ident_file)
127 if (ident_pphr_file != that.ident_pphr_file)
129 if (ldap_host != that.ldap_host)
131 if (ldap_port != that.ldap_port)
133 if (check_revocation != that.check_revocation)
135 if (!lists_equiv(certs, that.certs))
137 if (!lists_equiv(trust_anchors, that.trust_anchors))