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