Merge "[Security] Fixed memory leaks through valgrind tool" into tizen_2.1
[platform/framework/native/appfw.git] / inc / FSecRsaKeyConverter.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                        FSecRsaKeyConverter.h
20  * @brief               This is the header file for the %RsaKeyConverter class.
21  *
22  * This header file contains the declarations of the %RsaKeyConverter class.
23  */
24
25 #ifndef _FSEC_RSA_KEY_CONVERTER_H_
26 #define _FSEC_RSA_KEY_CONVERTER_H_
27
28 #include <FSecIPublicKey.h>
29 #include <FSecIPrivateKey.h>
30
31 namespace Tizen { namespace Security
32 {
33
34 /**
35  * @enum    RsaKeyFormat
36  *
37  * Defines the type of rsa key format.
38  *
39  * @since               2.1
40  */
41 enum RsaKeyFormat
42 {
43         RSA_KEY_FORMAT_PKCS01_PRIVATE_KEY = 0,     /**< % rsa key format is pkcs1 private key. */
44         RSA_KEY_FORMAT_PKCS01_PUBLIC_KEY = 1,      /**< % rsa key format is pkcs1 public key. */
45         RSA_KEY_FORMAT_PKCS08_PRIVATE_KEY = 2,     /**< % rsa key format is pkcs8 private key. */
46         RSA_KEY_FORMAT_X509_PUBLIC_KEY = 3,      /**< % rsa key format is X509 public key. */
47         RSA_KEY_FORMAT_UNKNOWN = 0xffffffff       /**< % rsa key format is unknown. */
48 }; //RsaKeyFormat;
49
50 /**
51  *  @class              RsaKeyConverter
52  *  @brief              This class provides support for converting the RSA public/private key in multiple key formats and multiple encoding formats.
53  *                              Supported RSA key formats are PKCS1/PKCS8/X509.
54  *                              Supported encoding formats are DER/PEM.
55  *  @since      2.1
56  */
57
58 class _OSP_EXPORT_ RsaKeyConverter
59 {
60
61 public:
62         /**
63          *      Generates a RSA encoded private key in the passed input RSA key format.
64          *      If the input private key is in PEM encoded format then the output RSA private key will be PEM encoded and if it is in DER encoded format
65          *      then the output RSA private key will be DER encoded.
66          *
67          *      @since          2.1
68          *
69          *      @return         A pointer to the encoded %ByteBuffer instance, @n
70          *                          else @c null if the method fails to convert the private key in the passed input format.
71          *      @param[in]      format                          An enum value contains the RSA private key format.
72          *                                                                      Valid values for the supported output RSA private key formats are only RSA_KEY_FORMAT_PKCS01_PRIVATE_KEY, RSA_KEY_FORMAT_PKCS08_PRIVATE_KEY.
73          *      @param[in]      key                                     An instance of IPrivateKey.
74          *      @exception      E_SUCCESS                                       The method is successful.
75          *      @exception      E_INVALID_ARG           The specified input parameter is invalid.
76          *  @remarks    The specific error code can be accessed using the GetLastResult() method.
77          */
78         static Tizen::Base::ByteBuffer* ConvertPrivateKeyFormatN(RsaKeyFormat format, const IPrivateKey& key);
79
80         /**
81          *      Generates a RSA encoded public key in the passed input RSA key format.
82          *      If the input public key is in PEM encoded format then the output RSA public key will be PEM encoded and if it is in DER encoded format
83          *      then the output RSA public key will be DER encoded.
84          *
85          *      @since          2.1
86          *
87          *      @return         A pointer to the encoded %ByteBuffer instance, @n
88          *                              else @c null if the method fails to convert the public key in the passed input format.
89          *      @param[in]      format                          An enum value contains the RSA public key format.
90          *                                                                      Valid values for the supported output RSA public key formats are only RSA_KEY_FORMAT_PKCS01_PUBLIC_KEY, RSA_KEY_FORMAT_X509_PUBLIC_KEY.
91          *      @param[in]      key                                     An instance of IPublicKey.
92          *      @exception      E_SUCCESS                                       The method is successful.
93          *      @exception      E_INVALID_ARG           The specified input parameter is invalid.
94          *      @remarks    The specific error code can be accessed using the GetLastResult() method.
95          */
96         static Tizen::Base::ByteBuffer* ConvertPublicKeyFormatN(RsaKeyFormat format, const IPublicKey& key);
97
98         /**
99          *      Converts a DER encoded RSA key to the PEM encoded format.
100          *
101          *      @since          2.1
102          *
103          *      @return         A pointer to the PEM encoded %ByteBuffer instance, @n
104                                         else @c null if the method fails to convert the key in the pem encoded format.
105          *      @param[in]      format                          An enum value contains the RSA key format of the input key.
106          *      @param[in]      key                                     An instance of IKey.
107          *      @exception      E_SUCCESS                                       The method is successful.
108          *      @exception      E_INVALID_ARG           The specified input parameter is invalid.
109          *      @remarks    The specific error code can be accessed using the GetLastResult() method.
110          */
111         static Tizen::Base::ByteBuffer* ConvertDerToPemN(RsaKeyFormat format, const IKey& key);
112
113         /**
114          *      Converts a PEM encoded RSA key to the DER encoded format.
115          *
116          *      @since          2.1
117          *
118          *      @return         A pointer to the DER encoded %ByteBuffer instance, @n
119                                             else @c null if the method fails to convert the key in the der encoded format.
120          *      @param[in]      format                          An enum value contains the RSA key format of the input key.
121          *      @param[in]      key                                     An instance of IKey,
122          *      @exception      E_SUCCESS                                       The method is successful.
123          *      @exception      E_INVALID_ARG           The specified input parameter is invalid.
124          *  @remarks    The specific error code can be accessed using the GetLastResult() method.
125          */
126         static Tizen::Base::ByteBuffer* ConvertPemToDerN(RsaKeyFormat format, const IKey& key);
127
128 private:
129         //
130         // This default constructor is intentionally declared as private because this class cannot be constructed.
131         //
132         // @since       2.1
133         //
134         RsaKeyConverter(void);
135
136         //
137         // This destructor is intentionally declared as private because this class cannot be constructed.
138         //
139         // @since       2.1
140         //
141         ~RsaKeyConverter(void);
142
143         //
144         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
145         //
146         // @since 2.1
147         //
148         RsaKeyConverter(const RsaKeyConverter& rhs);
149
150         //
151         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
152         //
153         // @since 2.1
154         //
155         RsaKeyConverter& operator =(const RsaKeyConverter& rhs);
156
157 private:
158         class _RsaKeyConverterImpl* __pRsaKeyConverterImpl;
159         friend class _RsaKeyConverterImpl;
160
161 };     //RsaKeyConverter
162
163 } } //Tizen::Security
164
165 #endif // _FSEC_RSA_KEY_CONVERTER_H_