handle exception case of epoll_wait
[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                 int ret;
37
38                 ret = select(PKCS15::PKCS15_AID);
39                 if (ret == FileObject::SUCCESS)
40                 {
41                         SCARD_DEBUG("response : %s", selectResponse.toString());
42                 }
43                 else if (ret == ResponseHelper::ERROR_FILE_NOT_FOUND)
44                 {
45                         SCARD_DEBUG_ERR("PKCS15 AID not found, search in EF DIR");
46
47                         if (selectFromEFDIR() == true)
48                         {
49                                 SCARD_DEBUG("response : %s", selectResponse.toString());
50                         }
51                         else
52                         {
53                                 SCARD_DEBUG_ERR("PKCS15 select failed, [%d]", ret);
54                         }
55                 }
56                 else
57                 {
58                         SCARD_DEBUG_ERR("PKCS15 select failed, [%d]", ret);
59                 }
60         }
61
62         PKCS15::PKCS15(Channel *channel, ByteArray selectResponse)
63                 : PKCS15Object(channel, selectResponse), odf(NULL)
64         {
65         }
66
67         PKCS15::~PKCS15()
68         {
69                 if (odf != NULL)
70                 {
71                         delete odf;
72                         odf = NULL;
73                 }
74         }
75
76         bool PKCS15::selectFromEFDIR()
77         {
78                 bool result = false;
79                 ByteArray path;
80                 EFDIR dir(channel);
81
82                 path = dir.getPathByAID(PKCS15_AID);
83                 if (path.getLength() > 0)
84                 {
85                         int ret;
86
87                         ret = select(path, false);
88                         if (ret == FileObject::SUCCESS)
89                         {
90                                 result = true;
91                         }
92                         else
93                         {
94                                 SCARD_DEBUG_ERR("path select failed, [%d]", ret);
95                         }
96                 }
97                 else
98                 {
99                         SCARD_DEBUG_ERR("PKCS15 not found");
100                 }
101
102                 return result;
103         }
104
105         PKCS15ODF *PKCS15::getODF()
106         {
107                 if (odf == NULL)
108                 {
109                         odf = new PKCS15ODF(channel);
110
111                         if (odf != NULL && odf->isClosed() == true)
112                         {
113                                 SCARD_DEBUG_ERR("failed to open ODF");
114
115                                 delete odf;
116                                 odf = NULL;
117                         }
118                 }
119
120                 SCARD_DEBUG("odf [%p]", odf);
121
122                 return odf;
123         }
124 } /* namespace smartcard_service_api */