1 /*
2 * Copyright 2020, The Android Open Source Project
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 #define LOG_TAG "javacard.keymint.device.strongbox-impl"
18 #include "JavacardSecureElement.h"
19
20 #include <algorithm>
21 #include <iostream>
22 #include <iterator>
23 #include <memory>
24 #include <regex.h>
25 #include <string>
26 #include <vector>
27
28 #include <android-base/logging.h>
29 #include <android-base/properties.h>
30 #include <keymaster/android_keymaster_messages.h>
31
32 #include "keymint_utils.h"
33
34 namespace keymint::javacard {
35
initializeJavacard()36 keymaster_error_t JavacardSecureElement::initializeJavacard() {
37 Array request;
38 request.add(Uint(getOsVersion()));
39 request.add(Uint(getOsPatchlevel()));
40 request.add(Uint(getVendorPatchlevel()));
41 auto [item, err] = sendRequest(Instruction::INS_INIT_STRONGBOX_CMD, request);
42 return err;
43 }
44
sendEarlyBootEndedEvent(bool eventTriggered)45 keymaster_error_t JavacardSecureElement::sendEarlyBootEndedEvent(bool eventTriggered) {
46 isEarlyBootEventPending |= eventTriggered;
47 if (!isEarlyBootEventPending) {
48 return KM_ERROR_OK;
49 }
50 auto [item, err] = sendRequest(Instruction::INS_EARLY_BOOT_ENDED_CMD);
51 if (err != KM_ERROR_OK) {
52 // Incase of failure cache the event and send in the next immediate request to Applet.
53 isEarlyBootEventPending = true;
54 return err;
55 }
56 isEarlyBootEventPending = false;
57 return KM_ERROR_OK;
58 }
59
constructApduMessage(Instruction & ins,std::vector<uint8_t> & inputData,std::vector<uint8_t> & apduOut)60 keymaster_error_t JavacardSecureElement::constructApduMessage(Instruction& ins,
61 std::vector<uint8_t>& inputData,
62 std::vector<uint8_t>& apduOut) {
63 apduOut.push_back(static_cast<uint8_t>(APDU_CLS)); // CLS
64 apduOut.push_back(static_cast<uint8_t>(ins)); // INS
65 apduOut.push_back(static_cast<uint8_t>(APDU_P1)); // P1
66 apduOut.push_back(static_cast<uint8_t>(APDU_P2)); // P2
67
68 if (USHRT_MAX >= inputData.size()) {
69 // Send extended length APDU always as response size is not known to HAL.
70 // Case 1: Lc > 0 CLS | INS | P1 | P2 | 00 | 2 bytes of Lc | CommandData | 2 bytes of Le
71 // all set to 00. Case 2: Lc = 0 CLS | INS | P1 | P2 | 3 bytes of Le all set to 00.
72 // Extended length 3 bytes, starts with 0x00
73 apduOut.push_back(static_cast<uint8_t>(0x00));
74 if (inputData.size() > 0) {
75 apduOut.push_back(static_cast<uint8_t>(inputData.size() >> 8));
76 apduOut.push_back(static_cast<uint8_t>(inputData.size() & 0xFF));
77 // Data
78 apduOut.insert(apduOut.end(), inputData.begin(), inputData.end());
79 }
80 // Expected length of output.
81 // Accepting complete length of output every time.
82 apduOut.push_back(static_cast<uint8_t>(0x00));
83 apduOut.push_back(static_cast<uint8_t>(0x00));
84 } else {
85 LOG(ERROR) << "Error in constructApduMessage.";
86 return (KM_ERROR_INVALID_INPUT_LENGTH);
87 }
88 return (KM_ERROR_OK); // success
89 }
90
sendData(Instruction ins,std::vector<uint8_t> & inData,std::vector<uint8_t> & response)91 keymaster_error_t JavacardSecureElement::sendData(Instruction ins, std::vector<uint8_t>& inData,
92 std::vector<uint8_t>& response) {
93 keymaster_error_t ret = KM_ERROR_UNKNOWN_ERROR;
94 std::vector<uint8_t> apdu;
95
96 ret = constructApduMessage(ins, inData, apdu);
97
98 if (ret != KM_ERROR_OK) {
99 return ret;
100 }
101
102 ret = transport_->sendData(apdu, response);
103 if (ret != KM_ERROR_OK) {
104 LOG(ERROR) << "Error in sending data in sendData. " << static_cast<int>(ret);
105 return ret;
106 }
107
108 // Response size should be greater than 2. Cbor output data followed by two bytes of APDU
109 // status.
110 if ((response.size() <= 2) || (getApduStatus(response) != APDU_RESP_STATUS_OK)) {
111 LOG(ERROR) << "Response of the sendData is wrong: response size = " << response.size()
112 << " apdu status = " << getApduStatus(response);
113 return (KM_ERROR_UNKNOWN_ERROR);
114 }
115 // remove the status bytes
116 response.pop_back();
117 response.pop_back();
118 return (KM_ERROR_OK); // success
119 }
120
121 std::tuple<std::unique_ptr<Item>, keymaster_error_t>
sendRequest(Instruction ins,Array & request)122 JavacardSecureElement::sendRequest(Instruction ins, Array& request) {
123 vector<uint8_t> response;
124 // encode request
125 std::vector<uint8_t> command = request.encode();
126 auto sendError = sendData(ins, command, response);
127 if (sendError != KM_ERROR_OK) {
128 return {unique_ptr<Item>(nullptr), sendError};
129 }
130 // decode the response and send that back
131 return cbor_.decodeData(response);
132 }
133
134 std::tuple<std::unique_ptr<Item>, keymaster_error_t>
sendRequest(Instruction ins,std::vector<uint8_t> & command)135 JavacardSecureElement::sendRequest(Instruction ins, std::vector<uint8_t>& command) {
136 vector<uint8_t> response;
137 auto sendError = sendData(ins, command, response);
138 if (sendError != KM_ERROR_OK) {
139 return {unique_ptr<Item>(nullptr), sendError};
140 }
141 // decode the response and send that back
142 return cbor_.decodeData(response);
143 }
144
145 std::tuple<std::unique_ptr<Item>, keymaster_error_t>
sendRequest(Instruction ins)146 JavacardSecureElement::sendRequest(Instruction ins) {
147 vector<uint8_t> response;
148 vector<uint8_t> emptyRequest;
149 auto sendError = sendData(ins, emptyRequest, response);
150 if (sendError != KM_ERROR_OK) {
151 return {unique_ptr<Item>(nullptr), sendError};
152 }
153 // decode the response and send that back
154 return cbor_.decodeData(response);
155 }
156
157 } // namespace keymint::javacard
158