1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker * Copyright (c) 2021, The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker *
4*e1997b9aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker *
8*e1997b9aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker *
10*e1997b9aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker */
16*e1997b9aSAndroid Build Coastguard Worker
17*e1997b9aSAndroid Build Coastguard Worker #define LOG_TAG "credstore"
18*e1997b9aSAndroid Build Coastguard Worker
19*e1997b9aSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*e1997b9aSAndroid Build Coastguard Worker #include <android/binder_manager.h>
21*e1997b9aSAndroid Build Coastguard Worker #include <android/hardware/identity/support/IdentityCredentialSupport.h>
22*e1997b9aSAndroid Build Coastguard Worker
23*e1997b9aSAndroid Build Coastguard Worker #include <android/security/identity/ICredentialStore.h>
24*e1997b9aSAndroid Build Coastguard Worker #include <android/security/identity/ISession.h>
25*e1997b9aSAndroid Build Coastguard Worker
26*e1997b9aSAndroid Build Coastguard Worker #include "Session.h"
27*e1997b9aSAndroid Build Coastguard Worker #include "Util.h"
28*e1997b9aSAndroid Build Coastguard Worker
29*e1997b9aSAndroid Build Coastguard Worker namespace android {
30*e1997b9aSAndroid Build Coastguard Worker namespace security {
31*e1997b9aSAndroid Build Coastguard Worker namespace identity {
32*e1997b9aSAndroid Build Coastguard Worker
33*e1997b9aSAndroid Build Coastguard Worker using std::optional;
34*e1997b9aSAndroid Build Coastguard Worker
35*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::IPresentationSession;
36*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::IWritableIdentityCredential;
37*e1997b9aSAndroid Build Coastguard Worker
38*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::support::ecKeyPairGetPkcs12;
39*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::support::ecKeyPairGetPrivateKey;
40*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::support::ecKeyPairGetPublicKey;
41*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::support::hexdump;
42*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::support::sha256;
43*e1997b9aSAndroid Build Coastguard Worker
getEphemeralKeyPair(vector<uint8_t> * _aidl_return)44*e1997b9aSAndroid Build Coastguard Worker Status Session::getEphemeralKeyPair(vector<uint8_t>* _aidl_return) {
45*e1997b9aSAndroid Build Coastguard Worker vector<uint8_t> keyPair;
46*e1997b9aSAndroid Build Coastguard Worker Status status = halBinder_->getEphemeralKeyPair(&keyPair);
47*e1997b9aSAndroid Build Coastguard Worker if (!status.isOk()) {
48*e1997b9aSAndroid Build Coastguard Worker return halStatusToGenericError(status);
49*e1997b9aSAndroid Build Coastguard Worker }
50*e1997b9aSAndroid Build Coastguard Worker time_t nowSeconds = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
51*e1997b9aSAndroid Build Coastguard Worker time_t validityNotBefore = nowSeconds;
52*e1997b9aSAndroid Build Coastguard Worker time_t validityNotAfter = nowSeconds + 24 * 60 * 60;
53*e1997b9aSAndroid Build Coastguard Worker optional<vector<uint8_t>> pkcs12Bytes = ecKeyPairGetPkcs12(keyPair,
54*e1997b9aSAndroid Build Coastguard Worker "ephemeralKey", // Alias for key
55*e1997b9aSAndroid Build Coastguard Worker "0", // Serial, as a decimal number
56*e1997b9aSAndroid Build Coastguard Worker "Credstore", // Issuer
57*e1997b9aSAndroid Build Coastguard Worker "Ephemeral Key", // Subject
58*e1997b9aSAndroid Build Coastguard Worker validityNotBefore, validityNotAfter);
59*e1997b9aSAndroid Build Coastguard Worker if (!pkcs12Bytes) {
60*e1997b9aSAndroid Build Coastguard Worker return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
61*e1997b9aSAndroid Build Coastguard Worker "Error creating PKCS#12 structure for key pair");
62*e1997b9aSAndroid Build Coastguard Worker }
63*e1997b9aSAndroid Build Coastguard Worker *_aidl_return = pkcs12Bytes.value();
64*e1997b9aSAndroid Build Coastguard Worker return Status::ok();
65*e1997b9aSAndroid Build Coastguard Worker }
66*e1997b9aSAndroid Build Coastguard Worker
setReaderEphemeralPublicKey(const vector<uint8_t> & publicKey)67*e1997b9aSAndroid Build Coastguard Worker Status Session::setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) {
68*e1997b9aSAndroid Build Coastguard Worker Status status = halBinder_->setReaderEphemeralPublicKey(publicKey);
69*e1997b9aSAndroid Build Coastguard Worker if (!status.isOk()) {
70*e1997b9aSAndroid Build Coastguard Worker return halStatusToGenericError(status);
71*e1997b9aSAndroid Build Coastguard Worker }
72*e1997b9aSAndroid Build Coastguard Worker return Status::ok();
73*e1997b9aSAndroid Build Coastguard Worker }
74*e1997b9aSAndroid Build Coastguard Worker
setSessionTranscript(const vector<uint8_t> & sessionTranscript)75*e1997b9aSAndroid Build Coastguard Worker Status Session::setSessionTranscript(const vector<uint8_t>& sessionTranscript) {
76*e1997b9aSAndroid Build Coastguard Worker Status status = halBinder_->setSessionTranscript(sessionTranscript);
77*e1997b9aSAndroid Build Coastguard Worker if (!status.isOk()) {
78*e1997b9aSAndroid Build Coastguard Worker return halStatusToGenericError(status);
79*e1997b9aSAndroid Build Coastguard Worker }
80*e1997b9aSAndroid Build Coastguard Worker return Status::ok();
81*e1997b9aSAndroid Build Coastguard Worker }
82*e1997b9aSAndroid Build Coastguard Worker
getCredentialForPresentation(const string & credentialName,sp<ICredential> * _aidl_return)83*e1997b9aSAndroid Build Coastguard Worker Status Session::getCredentialForPresentation(const string& credentialName,
84*e1997b9aSAndroid Build Coastguard Worker sp<ICredential>* _aidl_return) {
85*e1997b9aSAndroid Build Coastguard Worker return store_->getCredentialCommon(credentialName, cipherSuite_, halBinder_, _aidl_return);
86*e1997b9aSAndroid Build Coastguard Worker }
87*e1997b9aSAndroid Build Coastguard Worker
getAuthChallenge(int64_t * _aidl_return)88*e1997b9aSAndroid Build Coastguard Worker Status Session::getAuthChallenge(int64_t* _aidl_return) {
89*e1997b9aSAndroid Build Coastguard Worker *_aidl_return = 0;
90*e1997b9aSAndroid Build Coastguard Worker int64_t authChallenge;
91*e1997b9aSAndroid Build Coastguard Worker Status status = halBinder_->getAuthChallenge(&authChallenge);
92*e1997b9aSAndroid Build Coastguard Worker if (!status.isOk()) {
93*e1997b9aSAndroid Build Coastguard Worker return halStatusToGenericError(status);
94*e1997b9aSAndroid Build Coastguard Worker }
95*e1997b9aSAndroid Build Coastguard Worker *_aidl_return = authChallenge;
96*e1997b9aSAndroid Build Coastguard Worker return Status::ok();
97*e1997b9aSAndroid Build Coastguard Worker }
98*e1997b9aSAndroid Build Coastguard Worker
99*e1997b9aSAndroid Build Coastguard Worker } // namespace identity
100*e1997b9aSAndroid Build Coastguard Worker } // namespace security
101*e1997b9aSAndroid Build Coastguard Worker } // namespace android
102