libdigidocpp
DirectoryX509CertStore.cpp
Go to the documentation of this file.
1 /*
2  * libdigidocpp
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "DirectoryX509CertStore.h"
21 #include "X509CertStore_p.h"
22 
23 #include "../../Conf.h"
24 #include "../../log.h"
25 #include "../../util/File.h"
26 
27 #include <openssl/err.h>
28 
36 {
37  loadCerts(Conf::getInstance()->getCertStorePath());
38 }
39 
47 {
48  loadCerts(path);
49 }
50 
57 void digidoc::DirectoryX509CertStore::loadCerts(const std::string &path) throw(IOException)
58 {
60  THROW_IOEXCEPTION("Directory %s does not exists, can not load cert store.", path.c_str());
61 
62  std::vector<std::string> files = util::File::listFiles(path);
63  for(std::vector<std::string>::const_iterator iter = files.begin(); iter != files.end(); iter++)
64  {
65  try
66  {
67  X509 *c = X509Cert::loadX509(*iter);
68  sk_X509_push(d->stack, c);
69  if(!X509_STORE_add_cert(d->store, c))
70  WARN("Cant add cert %ld to X509_STORE, %s", ASN1_INTEGER_get(X509_get_serialNumber(c)), ERR_reason_error_string(ERR_get_error()));
71  }
72  catch(const IOException& e)
73  {
74  WARN(e.getMsg().c_str());
75  }
76  }
77  INFO("Loaded %d certificates into certificate store.", sk_X509_num(d->stack));
78 }