X509Context.C
1// -*- C++ -*-
2
3// Copyright (c) 2008-2009, Isode Limited, London, England.
4// All rights reserved.
5//
6// Acquisition and use of this software and related materials for any
7// purpose requires a written licence agreement from Isode Limited,
8// or a written licence from an organisation licenced by Isode Limited
9// to grant such a licence.
10
11//
12//
13// x509_context.C
14//
15// @VERSION@
16
17#include "Event_p.h"
18#include "../include/X509Context.h"
19#include <isode/crypto/x509.h>
20#include <algorithm>
21#include <stdexcept>
22#include <cctype>
23
24namespace {
25 bool
26 lists_equiv(const std::list<std::string>& a, const std::list<std::string>& b)
27 {
28 if (a.size() != b.size())
29 return false;
30
31 if (a.empty())
32 return true;
33
34 return std::equal(a.begin(), a.end(), b.begin());
35 }
36
37}
38
39namespace X509_Context {
40 bool Config::set_ldap_url(const std::string& url) {
41 if (url.compare(0, 7, "ldap://") != 0) {
42 return false;
43 }
44 ldap_host = "";
45 size_t pos = 7;
46 while (pos < url.length() && url[pos] != ':' && url[pos] != '/') {
47 ldap_host += url[pos];
48 ++pos;
49 }
50 if (pos == url.length() || url[pos] == '/') {
51 return true;
52 }
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]))
58 return false;
59 ldap_port = atoi(port.c_str());
60 return true;
61 }
62
63 Identity::Identity(Config& confobj)
64 {
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");
69
70 identity = x509_create_identity_from_file(pkcs12_file.c_str(),
71 pphr_file.c_str(), 1);
72 if (!identity)
73 throw std::runtime_error("unable to create identity");
74 cert_ctx = x509_create_cert_ctx();
75 if (!cert_ctx)
76 throw std::runtime_error("unable to create cert_ctx");
77 x509_cert_ctx_add_identity(cert_ctx, identity);
78
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());
84 if (ta)
85 x509_cert_ctx_add_cert(cert_ctx, ta, 1);
86 X509_free(ta);
87 }
88
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());
93 if (cert)
94 x509_cert_ctx_add_cert(cert_ctx, cert, 0);
95 X509_free(cert);
96 }
97
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);
101
102 X509* OCSPresponder = 0;
103 if (!confobj.OCSPresponder.empty()) {
104 OCSPresponder = x509_read_cert(confobj.OCSPresponder.c_str());
105 }
106 x509_cert_ctx_set_ocsp(cert_ctx, confobj.OCSPuri.c_str(), OCSPresponder,
107 confobj.OCSPnonce, 0, 0);
108
109 x509_cert_ctx_set_lookup_flags(cert_ctx, confobj.lookup_flags);
110 }
111
112 Identity::Identity(PKCS12 * p12, const char * passphrase)
113 {
114 identity = x509_create_identity_from_pkcs12(p12, passphrase);
115
116 if (!identity)
117 throw std::runtime_error("unable to create identity");
118 cert_ctx = x509_create_cert_ctx();
119 if (!cert_ctx)
120 throw std::runtime_error("unable to create cert_ctx");
121 x509_cert_ctx_add_identity(cert_ctx, identity);
122 }
123
124 bool Config::operator!=(const Config&that) const {
125 if (ident_file != that.ident_file)
126 return true;
127 if (ident_pphr_file != that.ident_pphr_file)
128 return true;
129 if (ldap_host != that.ldap_host)
130 return true;
131 if (ldap_port != that.ldap_port)
132 return true;
133 if (check_revocation != that.check_revocation)
134 return true;
135 if (!lists_equiv(certs, that.certs))
136 return true;
137 if (!lists_equiv(trust_anchors, that.trust_anchors))
138 return true;
139
140 return false;
141 }
142}

All rights reserved © 2002 - 2024 Isode Ltd.