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
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
22 namespace Tizen.Security.SecureRepository
25 /// The class that represents a PKCS#12 contents.
26 /// It has a private key or its certificate or all the members of a chain of trust.
28 /// <since_tizen> 3 </since_tizen>
31 private SafeCertificateListHandle _certChainHandle = null;
34 /// Loads the Pkcs12 from the given PKCS#12 file path.
36 /// <since_tizen> 3 </since_tizen>
37 /// <param name="filePath">The path of the PKCS12 file to be loaded.</param>
38 /// <param name="filePassword">The passphrase used to decrypt the PCKS12 file.
39 /// If the PKCS12 file is not encrypted, passphrase can be null.</param>
40 /// <exception cref="ArgumentNullException">The filePath is null.</exception>
41 /// <exception cref="InvalidOperationException">
42 /// No file on filePath.
43 /// No permission to access file.
44 /// File is in the invalid PKCS12 format.
45 /// File cannot be extracted with provided filePassword.
47 static public Pkcs12 Load(string filePath, string filePassword)
50 throw new ArgumentNullException("filePath should not be null");
52 IntPtr ptr = IntPtr.Zero;
56 Interop.CheckNThrowException(
57 Interop.CkmcTypes.Pkcs12Load(filePath, filePassword, out ptr),
58 "Failed to load PKCS12. file=" + filePath);
59 return new Pkcs12(ptr);
63 if (ptr != IntPtr.Zero)
64 Interop.CkmcTypes.Pkcs12Free(ptr);
69 /// A constructor of Key that takes a private key.
71 /// <since_tizen> 3 </since_tizen>
72 /// <param name="privateKey">A private key.</param>
73 public Pkcs12(Key privateKey)
75 this.PrivateKey = privateKey;
76 this.Certificate = null;
81 /// A constructor of Key that takes a private key, its corresponding certicate,
82 /// and CA's certificate chain.
84 /// <since_tizen> 3 </since_tizen>
85 /// <param name="privateKey">A private key.</param>
86 /// <param name="certificate">A certificate corresponding the private key.</param>
87 /// <param name="caChain">
88 /// A certificate chain of CA(Certificate Authority) that issued the certificate.
90 public Pkcs12(Key privateKey,
91 Certificate certificate,
92 IEnumerable<Certificate> caChain)
94 this.PrivateKey = privateKey;
95 this.Certificate = certificate;
96 this.CaChain = caChain;
99 internal Pkcs12(IntPtr ptr)
101 var ckmcPkcs12 = Marshal.PtrToStructure<Interop.CkmcPkcs12>(ptr);
103 this.PrivateKey = new Key(ckmcPkcs12.privateKey);
104 if (ckmcPkcs12.certificate != IntPtr.Zero)
105 this.Certificate = new Certificate(ckmcPkcs12.certificate);
106 if (ckmcPkcs12.caChain != IntPtr.Zero)
107 this._certChainHandle = new SafeCertificateListHandle(ckmcPkcs12.caChain);
110 internal IntPtr GetHandle()
112 IntPtr keyPtr = IntPtr.Zero;
113 IntPtr certPtr = IntPtr.Zero;
114 IntPtr cacertPtr = IntPtr.Zero;
115 IntPtr p12Ptr = IntPtr.Zero;
118 keyPtr = this.PrivateKey.GetHandle();
120 if (this.Certificate != null)
121 certPtr = this.Certificate.GetHandle();
123 if (this._certChainHandle != null)
124 cacertPtr = this._certChainHandle.GetHandle();
126 Interop.CheckNThrowException(
127 Interop.CkmcTypes.Pkcs12New(keyPtr, certPtr, cacertPtr, out p12Ptr),
128 "Failed to create pkcs12");
134 if (p12Ptr != IntPtr.Zero)
136 Interop.CkmcTypes.Pkcs12Free(p12Ptr);
140 if (keyPtr != IntPtr.Zero)
141 Interop.CkmcTypes.KeyFree(keyPtr);
142 if (certPtr != IntPtr.Zero)
143 Interop.CkmcTypes.CertFree(certPtr);
144 if (cacertPtr != IntPtr.Zero)
145 Interop.CkmcTypes.CertListAllFree(cacertPtr);
155 /// <since_tizen> 3 </since_tizen>
156 public Key PrivateKey
162 /// A certificate corresponding to the private key.
164 /// <since_tizen> 3 </since_tizen>
165 public Certificate Certificate
171 /// A certificate chain of CA(Certificate Authority) that issued the certificate.
173 /// <since_tizen> 3 </since_tizen>
174 public IEnumerable<Certificate> CaChain
178 if (this._certChainHandle == null)
181 return this._certChainHandle.Certificates;
186 this._certChainHandle = null;
188 this._certChainHandle = new SafeCertificateListHandle(value);