c8abc7241fb052e8135bf82c1562b2f3ba5d5328
[platform/core/security/key-manager.git] / src / manager / crypto / generic-backend / algo-validation.h
1 /*
2  *  Copyright (c) 2000 - 2015 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       algo-validation.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <sstream>
25 #include <string>
26 #include <utility>
27
28 #include <ckm/ckm-type.h>
29
30 #include <exception.h>
31
32 namespace CKM {
33 namespace Crypto {
34
35 template<typename T>
36 T unpack(
37     const CryptoAlgorithm &alg,
38     ParamName paramName)
39 {
40     T result;
41     if (!alg.getParam(paramName, result)) {
42         ThrowErr(Exc::Crypto::InputParam, "Wrong input param");
43     }
44     return result;
45 }
46
47
48 ////////// Validators //////////////
49
50 // Always validates as true. Useful for checking parameter existence only
51 template <typename T>
52 struct DefaultValidator {
53     static bool Check(const T&) { return true; }
54     static void Why(std::ostringstream& os) { os << "is ok"; }
55 };
56
57 // Validates as true if parameter value is equal to one of Args
58 template <typename T>
59 struct Type {
60     template <T ...Args>
61     struct Equals;
62
63     template <T First>
64     struct Equals<First> {
65     public:
66         static bool Check(const T& value) {
67             return First == value;
68         }
69         static void Why(std::ostringstream& os) {
70             os << "doesn't match " << static_cast<int>(First);
71         }
72     };
73
74     template <T First, T ...Args>
75     struct Equals<First, Args...> : public Equals<First>, public Equals<Args...> {
76     public:
77         static bool Check(const T& value) {
78             return Equals<First>::Check(value) || Equals<Args...>::Check(value);
79         }
80         static void Why(std::ostringstream& os) {
81             Equals<First>::Why(os);
82             os << ", ";
83             Equals<Args...>::Why(os);
84         }
85     };
86 };
87
88
89 ////////// Getters //////////////
90
91 // simply returns parameter value
92 template <typename T>
93 struct DefaultGetter {
94     static T Get(const T& value) { return value; }
95     static void What(std::ostringstream& os) { os << "value"; }
96 };
97
98 // returns buffer param size
99 struct BufferSizeGetter {
100     static size_t Get(const RawBuffer& buffer) { return buffer.size(); }
101     static void What(std::ostringstream& os) { os << "buffer size"; }
102 };
103
104
105 ////////// ErrorHandlers //////////////
106
107 struct ThrowingHandler {
108     static void Handle(std::string message) {
109         ThrowErr(Exc::Crypto::InputParam, message);
110     }
111 };
112
113
114 // base class for parameter check
115 struct ParamCheckBase {
116     virtual ~ParamCheckBase() {}
117     virtual void Check(const CryptoAlgorithm& ca) const = 0;
118 };
119
120 typedef std::unique_ptr<const ParamCheckBase> ParamCheckBasePtr;
121
122 typedef std::vector<ParamCheckBasePtr> ValidatorVector;
123
124
125 // ValidatorVector builder. Creates a vector of ParamCheckBasePtr's specified as Args
126 template <typename ...Args>
127 struct VBuilder;
128
129 template <typename First>
130 struct VBuilder<First> {
131 static ValidatorVector Build() {
132         ValidatorVector validators;
133         Add(validators);
134         return validators;
135     }
136 protected:
137     static void Add(ValidatorVector& validators) {
138         validators.emplace_back(new First);
139     }
140 };
141
142 template <typename First, typename ...Args>
143 struct VBuilder<First, Args...> : public VBuilder<First>, public VBuilder<Args...> {
144     static ValidatorVector Build() {
145         ValidatorVector validators;
146         Add(validators);
147         return validators;
148     }
149 protected:
150     static void Add(ValidatorVector& validators) {
151         VBuilder<First>::Add(validators);
152         VBuilder<Args...>::Add(validators);
153     }
154 };
155
156 /*
157  * Generic struct responsible for checking a single constraint on given algorithm parameter
158  *
159  * Name - name of param to check
160  * Type - type of param value
161  * Mandatory - true if param is mandatory
162  * Validator - class providing validation function bool Check(const CryptoAlgorithm&)
163  * Getter - gets the value used for validation (param value itself or a buffer size for example)
164  * ErrorHandler - class providing method for error handling void Handle(std::string)
165  */
166
167 template <ParamName Name,
168           typename Type,
169           bool Mandatory,
170           typename Validator = DefaultValidator<Type>,
171           typename Getter = DefaultGetter<Type>,
172           typename ErrorHandler = ThrowingHandler>
173 struct ParamCheck : public ParamCheckBase {
174     void Check(const CryptoAlgorithm& ca) const {
175         Type value;
176         std::ostringstream os;
177
178         // check existence
179         if(!ca.getParam(Name,value)) {
180             if (Mandatory) {
181                 os << "Mandatory parameter " << static_cast<int>(Name) << " doesn't exist";
182                 ErrorHandler::Handle(os.str());
183             }
184             return;
185         }
186         // validate
187         if(!Validator::Check(Getter::Get(value))) {
188             os << "The ";
189             Getter::What(os);
190             os << " of param '" << static_cast<int>(Name) << "'=" <<
191                   static_cast<int>(Getter::Get(value)) << " ";
192             Validator::Why(os);
193             ErrorHandler::Handle(os.str());
194         }
195     }
196 };
197
198 } // namespace Crypto
199 } // namespace CKM