Fix description (#3619)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Cion / Tizen.Applications.Cion / SecurityInfo.cs
1 /*
2  * Copyright (c) 2021 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
19 namespace Tizen.Applications.Cion
20 {
21     /// <summary>
22     /// A class to represent security info.
23     /// </summary>
24     /// <since_tizen> 9 </since_tizen>
25     public class SecurityInfo : IDisposable
26     {
27         private readonly string LogTag = "Tizen.Cion";
28         internal SecuritySafeHandle _handle = null;
29
30         /// <summary>
31         /// The constructor of SecurityInfo class.
32         /// </summary>
33         /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception> 
34         /// <since_tizen> 9 </since_tizen>
35         public SecurityInfo()
36         {
37             Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecurityCreate(out _handle);
38             if (ret != Interop.Cion.ErrorCode.None)
39             {
40                 throw CionErrorFactory.GetException(ret, "Failed to create security info.");
41             }
42         }
43
44         /// <summary>
45         /// Gets the CA cert path.
46         /// </summary>
47         /// <remarks>
48         /// http://tizen.org/privilege/mediastorage is needed if the file path is relevant to media storage.
49         /// http://tizen.org/privilege/externalstorage is needed if the file path is relevant to external storage.
50         /// </remarks>
51         /// <exception cref="ArgumentException">Thrown when the CA path is invalid.</exception>
52         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
53         /// <since_tizen> 9 </since_tizen>
54         public string CaPath
55         {
56             get
57             {
58                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecurityGetCaPath(_handle, out string caPath);
59                 if (ret != Interop.Cion.ErrorCode.None)
60                 {
61                     Log.Error(LogTag, "Failed to get CA path.");
62                     return "";
63                 }
64                 return caPath;
65             }
66
67             set
68             {
69                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecuritySetCaPath(_handle, value);
70                 if (ret != Interop.Cion.ErrorCode.None)
71                 {
72                     throw CionErrorFactory.GetException(ret, "Failed to set CA path.");
73                 }
74             }
75         }
76
77         /// <summary>
78         /// Gets the cert path.
79         /// </summary>
80         /// <remarks>
81         /// http://tizen.org/privilege/mediastorage is needed if the file path is relevant to media storage.
82         /// http://tizen.org/privilege/externalstorage is needed if the file path is relevant to external storage.
83         /// </remarks>
84         /// <exception cref="ArgumentException">Thrown when the cert path is invalid.</exception>
85         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
86         /// <since_tizen> 9 </since_tizen>
87         public string CertPath
88         {
89             get
90             {
91                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecurityGetCertPath(_handle, out string certPath);
92                 if (ret != Interop.Cion.ErrorCode.None)
93                 {
94                     Log.Error(LogTag, "Failed to get cert path.");
95                     return "";
96                 }
97                 return certPath;
98             }
99
100             set
101             {
102                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecuritySetCertPath(_handle, value);
103                 if (ret != Interop.Cion.ErrorCode.None)
104                 {
105                     throw CionErrorFactory.GetException(ret, "Failed to set cert path.");
106                 }
107             }
108         }
109
110         /// <summary>
111         /// Gets the private key path.
112         /// </summary>
113         /// <remarks>
114         /// http://tizen.org/privilege/mediastorage is needed if the file path is relevant to media storage.
115         /// http://tizen.org/privilege/externalstorage is needed if the file path is relevant to external storage.
116         /// </remarks>
117         /// <exception cref="ArgumentException">Thrown when the private key path is invalid.</exception>
118         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
119         /// <since_tizen> 9 </since_tizen>
120         public string PrivateKeyPath
121         {
122             get
123             {
124                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecurityGetPrivateKeyPath(_handle, out string privateKeyPath);
125                 if (ret != Interop.Cion.ErrorCode.None)
126                 {
127                     Log.Error(LogTag, "Failed to get private key path.");
128                     return "";
129                 }
130                 return privateKeyPath;
131             }
132
133             set
134             {
135                 Interop.Cion.ErrorCode ret = Interop.CionSecurity.CionSecuritySetPrivateKeyPath(_handle, value);
136                 if (ret != Interop.Cion.ErrorCode.None)
137                 {
138                     throw CionErrorFactory.GetException(ret, "Failed to set private key path.");
139                 }
140             }
141         }
142
143         #region IDisposable Support
144         private bool disposedValue = false;
145
146         /// <summary>
147         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
148         /// </summary>
149         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
150         /// <since_tizen> 9 </since_tizen>
151         protected virtual void Dispose(bool disposing)
152         {
153             if (!disposedValue)
154             {
155                 if (disposing)
156                 {
157                     _handle.Dispose();
158                 }
159                 disposedValue = true;
160             }
161         }
162
163         /// <summary>
164         /// Releases all resources used by the SecurityInfo class.
165         /// </summary>
166         /// <since_tizen> 9 </since_tizen>
167         public void Dispose()
168         {
169             Dispose(true);
170             GC.SuppressFinalize(this);
171         }
172         #endregion
173     }
174 }