Fix the Prevent problems
[platform/core/connectivity/smartcard-service.git] / common / PKCS15.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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 /* standard library header */
18
19 /* SLP library header */
20
21 /* local header */
22 #include "Debug.h"
23 #include "APDUHelper.h"
24 #include "EFDIR.h"
25 #include "PKCS15.h"
26
27 namespace smartcard_service_api
28 {
29         static unsigned char aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x63, 0x50,
30                 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
31         ByteArray PKCS15::PKCS15_AID(ARRAY_AND_SIZE(aid));
32
33         PKCS15::PKCS15(Channel *channel) :
34                 PKCS15Object(channel), odf(NULL)
35         {
36         }
37
38         PKCS15::PKCS15(Channel *channel, const ByteArray &selectResponse) :
39                 PKCS15Object(channel, selectResponse), odf(NULL)
40         {
41         }
42
43         PKCS15::~PKCS15()
44         {
45                 if (odf != NULL)
46                 {
47                         delete odf;
48                         odf = NULL;
49                 }
50         }
51
52         int PKCS15::select()
53         {
54                 int ret;
55
56                 ret = PKCS15Object::select(PKCS15::PKCS15_AID);
57                 if (ret >= SCARD_ERROR_OK)
58                 {
59                         _DBG("response : %s", selectResponse.toString().c_str());
60                 }
61                 else if (ret == ResponseHelper::ERROR_FILE_NOT_FOUND)
62                 {
63                         _ERR("PKCS15 AID not found, search in EF DIR");
64
65                         ret = selectFromEFDIR();
66                         if (ret >= SCARD_ERROR_OK)
67                         {
68                                 _DBG("response : %s", selectResponse.toString().c_str());
69                         }
70                         else
71                         {
72                                 _ERR("PKCS15 select failed, [%d]", ret);
73                         }
74                 }
75                 else
76                 {
77                         _ERR("PKCS15 select failed, [%d]", ret);
78                 }
79
80                 return ret;
81         }
82
83         int PKCS15::selectFromEFDIR()
84         {
85                 int ret;
86                 ByteArray path;
87                 EFDIR dir(channel);
88
89                 ret = dir.select();
90                 if (ret >= SCARD_ERROR_OK)
91                 {
92                         path = dir.getPathByAID(PKCS15_AID);
93                         if (path.size() > 0)
94                         {
95                                 ret = PKCS15Object::select(path, false);
96                                 if (ret < SCARD_ERROR_OK)
97                                 {
98                                         _ERR("PKCS15 select failed, [%d]", ret);
99                                 }
100                         }
101                         else
102                         {
103                                 _ERR("PKCS15 path is not found");
104                                 ret = SCARD_ERROR_NOT_SUPPORTED;
105                         }
106                 }
107                 else
108                 {
109                         _ERR("select EFDIR failed, [%x]", -ret);
110                 }
111
112                 return ret;
113         }
114
115         PKCS15ODF *PKCS15::getODF()
116         {
117                 if (odf == NULL)
118                 {
119                         odf = new PKCS15ODF(channel);
120
121                         if (odf != NULL && odf->isClosed() == true)
122                         {
123                                 _ERR("failed to open ODF");
124
125                                 delete odf;
126                                 odf = NULL;
127                         }
128                 }
129
130                 _DBG("odf [%p]", odf);
131
132                 return odf;
133         }
134 } /* namespace smartcard_service_api */