Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.SecureRepository / Tizen.Security.SecureRepository / Pkcs12Manager.cs
1 /*
2  *  Copyright (c) 2016 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 using System;
18 using static Interop;
19
20 namespace Tizen.Security.SecureRepository
21 {
22     /// <summary>
23     /// This class provides the methods storing, retrieving Pkcs12 contents.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class Pkcs12Manager : Manager
27     {
28         /// <summary>
29         /// Gets Pkcs12 contents from secure repository.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <param name="alias">The name of data to retrieve.</param>
33         /// <param name="keyPassword">
34         /// The password used in decrypting a private key value. If password of
35         /// keyPolicy is provided in SavePkcs12(), the same password should be provided
36         /// </param>
37         /// <param name="cerificatePassword">
38         /// The password used in decrypting a certificate value. If password of
39         /// certificatePolicy is provided in SavePkcs12(), the same password should be
40         /// provided
41         /// </param>
42         /// <returns>A Pkcs12 data specified by alias.</returns>
43         /// <exception cref="ArgumentNullException">Alias argument is null.</exception>
44         /// <exception cref="ArgumentException">
45         /// Alias argument is invalid format.
46         /// </exception>
47         /// <exception cref="InvalidOperationException">
48         /// Pkcs12 does not exist with the alias.
49         /// Optional password of key in Pkcs12 isn't matched.
50         /// Optional password of certificate in Pkcs12 isn't matched.
51         /// </exception>
52         static public Pkcs12 Get(
53             string alias, string keyPassword, string cerificatePassword)
54         {
55             if (alias == null)
56                 throw new ArgumentNullException("alias should not be null");
57
58             IntPtr ptr = IntPtr.Zero;
59
60             try
61             {
62                 Interop.CheckNThrowException(
63                     Interop.CkmcManager.GetPkcs12(
64                         alias, keyPassword, cerificatePassword, out ptr),
65                     "Failed to get PKCS12. alias=" + alias);
66                 return new Pkcs12(ptr);
67             }
68             finally
69             {
70                 if (ptr != IntPtr.Zero)
71                     Interop.CkmcTypes.Pkcs12Free(ptr);
72             }
73         }
74
75         /// <summary>
76         /// Stores PKCS12's contents inside key manager based on the provided policies.
77         /// All items from the PKCS12 will use the same alias.
78         /// </summary>
79         /// <since_tizen> 3 </since_tizen>
80         /// <param name="alias">The name of a data to be stored.</param>
81         /// <param name="pkcs12">The pkcs12 data to be stored.</param>
82         /// <param name="keyPolicy">
83         /// The policy about how to store pkcs's private key.
84         /// </param>
85         /// <param name="certificatePolicy">
86         /// The policy about how to store pkcs's certificate.
87         /// </param>
88         /// <exception cref="ArgumentNullException">Any of argument is null.</exception>
89         /// <exception cref="ArgumentException">
90         /// Alias argument is invalid format. Pkcs12 argument is invalid format.
91         /// </exception>
92         /// <exception cref="InvalidOperationException">
93         /// Pkcs12 with alias does already exist.
94         /// </exception>
95         static public void Save(
96             string alias, Pkcs12 pkcs12, Policy keyPolicy, Policy certificatePolicy)
97         {
98             if (alias == null || pkcs12 == null || keyPolicy == null ||
99                 certificatePolicy == null)
100                 throw new ArgumentNullException("any of argument is null");
101
102             IntPtr ptr = IntPtr.Zero;
103             try
104             {
105                 ptr = pkcs12.GetHandle();
106
107                 Interop.CheckNThrowException(
108                     Interop.CkmcManager.SavePkcs12(
109                         alias, ptr, keyPolicy.ToCkmcPolicy(),
110                         certificatePolicy.ToCkmcPolicy()),
111                     "Failed to save PKCS12. alias=" + alias);
112             }
113             finally
114             {
115                 if (ptr != IntPtr.Zero)
116                     Interop.CkmcTypes.Pkcs12Free(ptr);
117             }
118         }
119
120         // to be static class safely
121         internal Pkcs12Manager()
122         {
123         }
124     }
125 }