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.Runtime.InteropServices;
21 namespace Tizen.Security.SecureRepository
24 /// Class that represents a certificate.
26 public class Certificate : SafeHandle
29 /// A constructor of Certificate that takes the binary and its format.
31 /// <param name="binary">The binary data of a certificate.</param>
32 /// <param name="format">The format of the binary data.</param>
33 public Certificate(byte[] binary, DataFormat format) : base(IntPtr.Zero, true)
35 this.SetHandle(IntPtr.Zero);
40 internal Certificate(IntPtr ptrCkmcCert, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle)
42 base.SetHandle(ptrCkmcCert);
44 CkmcCert ckmcCert = (CkmcCert)Marshal.PtrToStructure(ptrCkmcCert, typeof(CkmcCert));
45 Binary = new byte[ckmcCert.size];
46 Marshal.Copy(ckmcCert.rawCert, Binary, 0, Binary.Length);
47 Format = (DataFormat)ckmcCert.dataFormat;
51 /// The binary value of a certificate.
59 /// The format of the binary value.
61 public DataFormat Format
66 internal CkmcCert ToCkmcCert()
68 return new Interop.CkmcCert(new PinnedObject(Binary), Binary.Length, (int)Format);
72 /// Gets a value that indicates whether the handle is invalid.
74 public override bool IsInvalid
76 get { return handle == IntPtr.Zero; }
80 /// When overridden in a derived class, executes the code required to free the handle.
82 /// <returns>true if the handle is released successfully</returns>
83 protected override bool ReleaseHandle()
85 if (IsInvalid) // do not release
87 Interop.CkmcTypes.CertFree(handle);
88 this.SetHandle(IntPtr.Zero);