Refactor SignatureValidator and reduce interface headers
[platform/core/security/cert-svc.git] / vcore / vcore / XmlsecAdapter.h
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 /*
17  * @file        XmlSecAdapter.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #ifndef _VALIDATION_CORE_XMLSECADAPTER_H_
23 #define _VALIDATION_CORE_XMLSECADAPTER_H_
24
25 #include <xmlsec/keysmngr.h>
26
27 #include <dpl/exception.h>
28 #include <dpl/noncopyable.h>
29 #include <dpl/singleton.h>
30
31 #include <vcore/Certificate.h>
32 #include <vcore/SignatureData.h>
33
34 namespace ValidationCore {
35 class XmlSec : public VcoreDPL::Noncopyable
36 {
37   public:
38
39     struct XmlSecContext
40     {
41         /* You _must_ set one of the value: certificatePath or certificate. */
42         XmlSecContext() :
43             validationTime(0),
44             allowBrokenChain(false),
45             errorBrokenChain(false)
46         {
47         }
48
49         /*
50          * Absolute path to signature file.
51          */
52         std::string signatureFile;
53         /*
54          * Direcotory with signed data.
55          * If you leave it empty xmlsec will use directory extracted
56          * from signatureFile.
57          */
58         std::string workingDirectory;
59         /*
60          * Path to trusted certificate.
61          */
62         std::string certificatePath;
63         /*
64          * Trusted certificate. In most cases it should be Root CA certificate.
65          */
66         CertificatePtr certificatePtr;
67         /*
68          * Validation date.
69          * 0 - uses current time.
70          */
71         time_t validationTime;
72         /*
73          * Input parameter.
74          * If true, signature validation will not be interrupted by chain error.
75          * If true and chain is broken then the value errorBrokenChain will be
76          * set to true.
77          */
78         bool allowBrokenChain;
79         /*
80          * Output parameter.
81          * This will be set if chain is incomplete or broken.
82          */
83         bool errorBrokenChain;
84         /*
85          * Output parameter.
86          * Reference checked by xmlsec
87          */
88         ReferenceSet referenceSet;
89     };
90
91     enum Result
92     {
93         NO_ERROR,
94         ERROR_INVALID_SIGNATURE
95     };
96
97     class Exception
98     {
99       public:
100         DECLARE_EXCEPTION_TYPE(VcoreDPL::Exception, Base)
101         DECLARE_EXCEPTION_TYPE(Base, InternalError)
102     };
103
104     /*
105      * Context - input/output param.
106      */
107     Result validate(XmlSecContext *context);
108     Result validateNoHash(XmlSecContext *context);
109     Result validatePartialHash(XmlSecContext *context);
110     Result setPartialHashList(const std::list<std::string>& targetUri);
111  
112  protected:
113     XmlSec();
114     ~XmlSec();
115   private:
116     void deinitialize(void);
117
118     void loadDERCertificateMemory(XmlSecContext *context,
119             xmlSecKeysMngrPtr mngr);
120     void loadPEMCertificateFile(XmlSecContext *context,
121             xmlSecKeysMngrPtr mngr);
122     Result validateFile(XmlSecContext *context,
123             xmlSecKeysMngrPtr mngr);
124
125     bool m_initialized;
126     bool m_noHash;
127     bool m_partialHash;
128     std::list<std::string>* m_pList;
129
130     static std::string s_prefixPath;
131     static int fileMatchCallback(const char *filename);
132     static void* fileOpenCallback(const char *filename);
133     static int fileReadCallback(void *context,
134             char *buffer,
135             int len);
136     static int fileCloseCallback(void *context);
137     static void fileExtractPrefix(XmlSecContext *context);
138 };
139
140 typedef VcoreDPL::Singleton<XmlSec> XmlSecSingleton;
141
142 } // namespace ValidationCore
143
144 #endif // _VALIDATION_CORE_XMLSECVERIFICATOR_H_