4104c886c08f500195b6d53b51d7903cdf982c6f
[framework/web/wrt-commons.git] / modules / vcore / src / 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 _XMLSECADAPTER_H_
23 #define _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 "Certificate.h"
32 #include "ValidatorCommon.h"
33
34 namespace ValidationCore {
35 class XmlSec : public DPL::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(DPL::Exception, Base)
101         DECLARE_EXCEPTION_TYPE(Base, InternalError)
102     };
103
104     /*
105      * Context - input/output param.
106      */
107     Result validate(XmlSecContext *context);
108   protected:
109     XmlSec();
110     ~XmlSec();
111   private:
112     void deinitialize(void);
113
114     void loadDERCertificateMemory(XmlSecContext *context,
115             xmlSecKeysMngrPtr mngr);
116     void loadPEMCertificateFile(XmlSecContext *context,
117             xmlSecKeysMngrPtr mngr);
118     Result validateFile(XmlSecContext *context,
119             xmlSecKeysMngrPtr mngr);
120
121     bool m_initialized;
122
123     static std::string s_prefixPath;
124     static int fileMatchCallback(const char *filename);
125     static void* fileOpenCallback(const char *filename);
126     static int fileReadCallback(void *context,
127             char *buffer,
128             int len);
129     static int fileCloseCallback(void *context);
130     static void fileExtractPrefix(XmlSecContext *context);
131 };
132
133 typedef DPL::Singleton<XmlSec> XmlSecSingleton;
134 } // namespace ValidationCore
135 #endif // _XMLSECVERIFICATOR_H_