sync with master
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_PkcsUtility.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSecPkcs_PkcsUtility.h
20  * @brief               This is the header file for the %_PkcsUtility class.
21  *
22  * This header file contains the declarations of the %_PkcsUtility class.
23  */
24
25 #ifndef _FSEC_PKCS_INTERNAL_PKCS_UTILITY_H_
26 #define _FSEC_PKCS_INTERNAL_PKCS_UTILITY_H_
27
28
29 #include <FSecPkcsAlgorithmIdentifier.h>
30 #include <FSecPkcsTypes.h>
31
32 struct X509_algor_st;
33
34 namespace Tizen { namespace Security { namespace Pkcs
35 {
36
37 /**
38  *@class                _PkcsUtility
39  *@brief                This class contains the declarations for FSecurity PKCS Utility Operations.
40  *@since                2.1
41  *
42  */
43
44 /**
45  * @enum _OidType
46  *
47  * Defines the type of Oid.
48  *
49  * @since               2.1
50  */
51
52 enum _OidType
53 {
54         _OID_TYPE_PKCS05,
55         _OID_TYPE_PBKDF2,
56         _OID_TYPE_PBES2,
57         _OID_TYPE_PBMAC1,
58         _OID_TYPE_HMAC_SHA1,
59         _OID_TYPE_HMAC_SHA2_224,
60         _OID_TYPE_HMAC_SHA2_256,
61         _OID_TYPE_HMAC_SHA2_384,
62         _OID_TYPE_HMAC_SHA2_512,
63         _OID_TYPE_DES_CBC,
64         _OID_TYPE_DES_CBC_EDE3,
65         _OID_TYPE_AES_128_CBC,
66         _OID_TYPE_AES_192_CBC,
67         _OID_TYPE_AES_256_CBC,
68         _OID_TYPE_RC2_CBC,
69         _OID_TYPE_PKCS08,
70         _OID_TYPE_RSA_ENCRYPTION,
71         _OID_TYPE_UNKNOWN
72
73 }; //_OidType;
74
75 class _PkcsUtility
76         : public Tizen::Base::Object
77 {
78 public:
79         /**
80          * This function takes the parameters for encryption and decryption of a message.
81          *
82          * @since                       2.1
83          *
84          * @return                      Returns a pointer to ByteBuffer, contains encrypted/decrypted output data,@n
85          *                                      else @c null if an error occurs.
86          * @param [in]          algo                                            An instance of Algorithm Identifier, contains the algorithm.
87          * @param [in]          derivedKey                                      An instance of ByteBuffer, contains derived key generated from _OID_TYPE_PBKDF2.
88          * @param [in]          dataIn                                          An instance of ByteBuffer, contains the input data.
89          * @param [in]          opMode                                          Specifies the Cipher Operation, contains the operation mode(encryption(1)/decryption(0)).
90          * @param [out]         None
91          * @exception           E_SUCCESS                                       The method is successful.
92          * @exception           E_OUT_OF_MEMORY                         The memory is insufficient.
93          * @exception           E_SYSTEM                                        Failed to operate openssl library.
94          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
95          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
96          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
97          */
98         static Tizen::Base::ByteBuffer* EncryptDecryptN(const AlgorithmIdentifier& algo, const Tizen::Base::ByteBuffer& derivedKey, const Tizen::Base::ByteBuffer& dataIn, int opMode);
99
100         /**
101          * This function takes the algorithm and return the _OidType enum value depending on the algorithm.
102          *
103          * @since                       2.1
104          *
105          * @return                      Returns a _OidType enum value.
106          * @param [in]          algorithm                                       String value that contains the algorithm like OID_PBKDF2 etc.
107          * @exception           E_SUCCESS                                       The method is successful.
108          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
109          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
110          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
111          */
112         static _OidType ConvertOidToEnum(Tizen::Base::String algorithm);
113
114         /**
115          * This function takes the algorithm and return the defined standard nid value of algorithm.
116          *
117          * @since                       2.1
118          *
119          * @return                      Returns a defined standard nid value of algorithm.
120          * @param [in]          algorithm                                       String value that contains the algorithm like OID_PBKDF2 etc.
121          * @exception           E_SUCCESS                                       The method is successful.
122          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
123          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
124          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
125          */
126         static int ConvertToNid(Tizen::Base::String algorithm);
127
128         /**
129          * This function takes the standard nid value of algorithm and return the algorithm Oid in String format.
130          *
131          * @since                       3.0
132          *
133          * @return                      Returns a algorithm Oid in String format.
134          * @exception           E_SUCCESS                                       The method is successful.
135          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
136          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
137          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
138          */
139         static Tizen::Base::String ConvertToOid(int nid);
140
141         /**
142          * This function takes the standard asn1 tag value of an attribute and return the Pkcs08 tag value.
143          *
144          * @since                       3.0
145          *
146          * @return                      Returns a enum value contains Pkcs08 tag value.
147          * @exception           E_SUCCESS                                       The method is successful.
148          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
149          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
150          */
151         static Pkcs08TagValue ConvertToTagValue(int ans1Type);
152
153         /**
154          * This function takes the parameters of _OID_TYPE_PBKDF2 and return the X509_ALGOR structure of openssl same as AlgorithmIdentifier for _OID_TYPE_PBKDF2.
155          * It is used for creating the encoded data for _OID_TYPE_PBES2. This structure is used for pbe2->keyfunc. This function is used internally for encoding/decoding.
156          *
157          * @since                       2.1
158          *
159          * @return                      A pointer to the algorithm identifier structure of openssl,@n
160          *                                      else @c null if an error occurs.
161          * @param [in]          iter                                            Integer value contains the iteration count.
162          * @param [in]          pSaltValue                                      An instance of char pointer contains the salt value.
163          * @param [in]          saltLen                                         Integer value contains the salt length.
164          * @param [in]          prfNid                                          Integer value contain the nid value of hmac algorithm.
165          * @param [in]          keyLen                                          Integer value contains the derived key length.
166          * @exception           E_SUCCESS                                       The method is successful.
167          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
168          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
169          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
170          */
171         static X509_algor_st* GenerateKdfParametersN(int iter, unsigned char* pSaltValue, int saltLen, int prfNid, int keyLen);
172
173         /**
174          * This function takes the pointer to the X509_ALGOR structure of openssl and the oid value of the algorithm amd return the
175          * algorithm parameters after parsing this openssl structure depending on the algorithm passed.
176          *
177          * @since                       2.1
178          *
179          * @return                      Returns a pointer to the IALgorithmParamters interface,@n
180          *                                      else @c null if an error occurs.
181          * @param [in]          algoOid                                         Oid value of the algorithm in string format.
182          * @param [in]          pAlgoObj                                        Pointer to the X509_ALGOR structure of openssl.
183          * @exception           E_SUCCESS                                       The method is successful.
184          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
185          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
186          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
187          */
188         static IAlgorithmParameters* GernerateParametersFromOidN(Tizen::Base::String algoOid, X509_algor_st* pAlgoObj);
189
190         /**
191          * This function takes the pointer to the IAlgorithmParameters and the oid value of the algorithm and return the pointer to the
192          * X509_ALGOR  of openssl structure depending on the algorithm passed.
193          *
194          * @since                       2.1
195          *
196          * @return                      A pointer to the algorithm identifier structure of openssl,@n
197          *                                      else @c null if an error occurs.
198          * @param [in]          algoOid                 Oid value of the algorithm in string format.
199          * @param [in]          pAlgoParam              Pointer to the IAlgorithmParameters contains the parameters for any type depending on the algorithm.
200          * @param [out]         None
201          * @exception           E_SUCCESS                                       The method is successful.
202          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
203          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
204          * @exception           E_SYSTEM                                        The method cannot proceed due to a severe system error.
205          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
206          */
207         static X509_algor_st* GenerateAlgorithmIdentifierStructureN(Tizen::Base::String algoOid, IAlgorithmParameters* pAlgoParam);
208
209         /**
210          * This function takes the algorithm oid and check whether the parameters exist for this algorithm or not.
211          *
212          * @since                       2.1
213          *
214          * @return                      Returns true if parameters supported for the input algorithm otherwise false.
215          * @param [in]          algorithm                                       Oid value of the algorithm in string format.
216          * @exception           E_SUCCESS                                       The method is successful.
217          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
218          * @exception           E_UNSUPPORTED_ALGORITHM         The input algorithm is not supported.
219          * @remarks                     The specific error code can be accessed using the GetLastResult() method.
220          */
221         static bool IsParameterSupported(Tizen::Base::String algorithm);
222
223 private:
224         // This default constructor is intentionally declared as private because this class cannot be constructed.
225         //
226         // @since               2.1
227         _PkcsUtility(void);
228
229         // This default destructor is intentionally declared as private because this class cannot be constructed.
230         //
231         // @since               2.1
232         virtual ~_PkcsUtility(void);
233
234         // This copy constructor is intentionally declared as private to prohibit @n
235         // copying of objects by users.
236         //
237         // @since               2.1
238         _PkcsUtility(const _PkcsUtility& rhs);
239
240         // The implementation of this copy assignment operator is intentionally blank and @n
241         // declared as private to prohibit copying of objects.
242         //
243         // @since               2.1
244         _PkcsUtility& operator =(const _PkcsUtility& rhs);
245
246 };
247
248 } } } // Tizen::Security::Pkcs
249
250 #endif //_FSEC_PKCS_INTERNAL_PKCS_UTILITY_H_