6d76978caae00a34177f9dd754da31c34630a3bf
[platform/core/security/cert-svc.git] / tests / vcore / TestCRL.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <algorithm>
17 #include <cstring>
18 #include <openssl/x509v3.h>
19 #include <file_input_mapping.h>
20 #include <dpl/log/log.h>
21 #include "TestCRL.h"
22
23 using namespace ValidationCore;
24 using namespace std;
25
26
27 namespace {
28 const char *CRL_LOOKUP_DIR = "/opt/etc/ssl/certs/";
29 const char *beginCertificate = "-----BEGIN CERTIFICATE-----";
30 const char *endCertificate = "-----END CERTIFICATE-----";
31 const char *beginTrustedCertificate = "-----BEGIN TRUSTED CERTIFICATE-----";
32 const char *endTrustedCertificate = "-----END TRUSTED CERTIFICATE-----";
33
34
35 bool whiteCharacter(char a){
36     return a == '\n';
37 }
38
39 }
40
41 TestCRL::TestCRL()
42   : CRLImpl (new CRLCacheDAO)
43 {
44     //Add additional lookup dir
45     int rv = X509_LOOKUP_add_dir(m_lookup, CRL_LOOKUP_DIR, X509_FILETYPE_PEM);
46     if (!rv) {
47         LogError("Failed to add lookup dir for PEM files.");
48         ThrowMsg(CRLException::StorageError,
49                 "Failed to add lookup dir for PEM files.");
50     }
51     LogInfo("CRL storage initialization complete.");
52 }
53
54 std::string TestCRL::getFileContent(const std::string &filename)
55 {
56     //Only PEM formatted files allowed
57     LogInfo("Read file: " << filename);
58     FileInputMapping file(filename);
59     string content(reinterpret_cast<const char*>(file.GetAddress()),
60             file.GetSize());
61
62     size_t posBegin = content.find(beginCertificate);
63     size_t posEnd = content.find(endCertificate);
64     if (posBegin != string::npos &&
65         posEnd != string::npos) {
66         posBegin += strlen(beginCertificate);
67     } else {
68         posBegin = content.find(beginTrustedCertificate);
69         posEnd = content.find(endTrustedCertificate);
70         if (posBegin != string::npos &&
71             posEnd != string::npos) {
72             posBegin += strlen(beginTrustedCertificate);
73         } else {
74             LogError("Failed to parse PEM file");
75             return string();
76         }
77     }
78     //Remove whitespaces
79     string cert(content, posBegin, posEnd - posBegin);
80     cert.erase(std::remove_if(cert.begin(), cert.end(), whiteCharacter),
81             cert.end());
82
83     return cert;
84 }
85
86 void TestCRL::addCRLToStore(const string &filename, const string &uri)
87 {
88     LogInfo("Read file: " << filename);
89     //Only PEM formatted files allowed
90     FileInputMapping file(filename);
91     char *buffer = new char[file.GetSize()];
92     memcpy(buffer, file.GetAddress(), file.GetSize());
93     CRLDataPtr crl(new  CRLData(buffer, file.GetSize(), uri));
94     updateCRL(crl);
95 }