2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
20 namespace Tizen.Security.SecureRepository
23 /// This class provides the methods storing, retrieving Pkcs12 contents.
25 /// <since_tizen> 3 </since_tizen>
26 public class Pkcs12Manager : Manager
29 /// Gets Pkcs12 contents from secure repository.
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
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
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.
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.
52 static public Pkcs12 Get(
53 string alias, string keyPassword, string cerificatePassword)
56 throw new ArgumentNullException("alias should not be null");
58 IntPtr ptr = IntPtr.Zero;
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);
70 if (ptr != IntPtr.Zero)
71 Interop.CkmcTypes.Pkcs12Free(ptr);
76 /// Stores PKCS12's contents inside key manager based on the provided policies.
77 /// All items from the PKCS12 will use the same alias.
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.
85 /// <param name="certificatePolicy">
86 /// The policy about how to store pkcs's certificate.
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.
92 /// <exception cref="InvalidOperationException">
93 /// Pkcs12 with alias does already exist.
95 static public void Save(
96 string alias, Pkcs12 pkcs12, Policy keyPolicy, Policy certificatePolicy)
98 if (alias == null || pkcs12 == null || keyPolicy == null ||
99 certificatePolicy == null)
100 throw new ArgumentNullException("any of argument is null");
102 IntPtr ptr = IntPtr.Zero;
105 ptr = pkcs12.GetHandle();
107 Interop.CheckNThrowException(
108 Interop.CkmcManager.SavePkcs12(
109 alias, ptr, keyPolicy.ToCkmcPolicy(),
110 certificatePolicy.ToCkmcPolicy()),
111 "Failed to save PKCS12. alias=" + alias);
115 if (ptr != IntPtr.Zero)
116 Interop.CkmcTypes.Pkcs12Free(ptr);
120 // to be static class safely
121 internal Pkcs12Manager()