xref: /aosp_15_r20/system/security/identity/Session.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1 /*
2  * Copyright (c) 2021, 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 "credstore"
18 
19 #include <android-base/logging.h>
20 #include <android/binder_manager.h>
21 #include <android/hardware/identity/support/IdentityCredentialSupport.h>
22 
23 #include <android/security/identity/ICredentialStore.h>
24 #include <android/security/identity/ISession.h>
25 
26 #include "Session.h"
27 #include "Util.h"
28 
29 namespace android {
30 namespace security {
31 namespace identity {
32 
33 using std::optional;
34 
35 using ::android::hardware::identity::IPresentationSession;
36 using ::android::hardware::identity::IWritableIdentityCredential;
37 
38 using ::android::hardware::identity::support::ecKeyPairGetPkcs12;
39 using ::android::hardware::identity::support::ecKeyPairGetPrivateKey;
40 using ::android::hardware::identity::support::ecKeyPairGetPublicKey;
41 using ::android::hardware::identity::support::hexdump;
42 using ::android::hardware::identity::support::sha256;
43 
getEphemeralKeyPair(vector<uint8_t> * _aidl_return)44 Status Session::getEphemeralKeyPair(vector<uint8_t>* _aidl_return) {
45     vector<uint8_t> keyPair;
46     Status status = halBinder_->getEphemeralKeyPair(&keyPair);
47     if (!status.isOk()) {
48         return halStatusToGenericError(status);
49     }
50     time_t nowSeconds = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
51     time_t validityNotBefore = nowSeconds;
52     time_t validityNotAfter = nowSeconds + 24 * 60 * 60;
53     optional<vector<uint8_t>> pkcs12Bytes = ecKeyPairGetPkcs12(keyPair,
54                                                                "ephemeralKey",  // Alias for key
55                                                                "0",  // Serial, as a decimal number
56                                                                "Credstore",      // Issuer
57                                                                "Ephemeral Key",  // Subject
58                                                                validityNotBefore, validityNotAfter);
59     if (!pkcs12Bytes) {
60         return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
61                                                 "Error creating PKCS#12 structure for key pair");
62     }
63     *_aidl_return = pkcs12Bytes.value();
64     return Status::ok();
65 }
66 
setReaderEphemeralPublicKey(const vector<uint8_t> & publicKey)67 Status Session::setReaderEphemeralPublicKey(const vector<uint8_t>& publicKey) {
68     Status status = halBinder_->setReaderEphemeralPublicKey(publicKey);
69     if (!status.isOk()) {
70         return halStatusToGenericError(status);
71     }
72     return Status::ok();
73 }
74 
setSessionTranscript(const vector<uint8_t> & sessionTranscript)75 Status Session::setSessionTranscript(const vector<uint8_t>& sessionTranscript) {
76     Status status = halBinder_->setSessionTranscript(sessionTranscript);
77     if (!status.isOk()) {
78         return halStatusToGenericError(status);
79     }
80     return Status::ok();
81 }
82 
getCredentialForPresentation(const string & credentialName,sp<ICredential> * _aidl_return)83 Status Session::getCredentialForPresentation(const string& credentialName,
84                                              sp<ICredential>* _aidl_return) {
85     return store_->getCredentialCommon(credentialName, cipherSuite_, halBinder_, _aidl_return);
86 }
87 
getAuthChallenge(int64_t * _aidl_return)88 Status Session::getAuthChallenge(int64_t* _aidl_return) {
89     *_aidl_return = 0;
90     int64_t authChallenge;
91     Status status = halBinder_->getAuthChallenge(&authChallenge);
92     if (!status.isOk()) {
93         return halStatusToGenericError(status);
94     }
95     *_aidl_return = authChallenge;
96     return Status::ok();
97 }
98 
99 }  // namespace identity
100 }  // namespace security
101 }  // namespace android
102