1 /*
2  * Copyright 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 #pragma once
18 
19 #include <cppbor.h>
20 
21 #include <aidl/android/hardware/security/keymint/BnRemotelyProvisionedComponent.h>
22 #include <aidl/android/hardware/security/keymint/RpcHardwareInfo.h>
23 #include <aidl/android/hardware/security/keymint/SecurityLevel.h>
24 
25 #include <keymaster/UniquePtr.h>
26 #include <keymaster/android_keymaster.h>
27 
28 #include "CborConverter.h"
29 #include "JavacardSecureElement.h"
30 
31 namespace aidl::android::hardware::security::keymint {
32 using ::keymint::javacard::CborConverter;
33 using ::keymint::javacard::JavacardSecureElement;
34 using ndk::ScopedAStatus;
35 using std::shared_ptr;
36 
37 class JavacardRemotelyProvisionedComponentDevice : public BnRemotelyProvisionedComponent {
38   public:
JavacardRemotelyProvisionedComponentDevice(shared_ptr<JavacardSecureElement> card)39     explicit JavacardRemotelyProvisionedComponentDevice(shared_ptr<JavacardSecureElement> card)
40         : card_(card) {}
41 
42     virtual ~JavacardRemotelyProvisionedComponentDevice() = default;
43 
44     ScopedAStatus getHardwareInfo(RpcHardwareInfo* info) override;
45 
46     ScopedAStatus generateEcdsaP256KeyPair(bool testMode, MacedPublicKey* macedPublicKey,
47                                            std::vector<uint8_t>* privateKeyHandle) override;
48 
49     ScopedAStatus generateCertificateRequest(bool testMode,
50                                              const std::vector<MacedPublicKey>& keysToSign,
51                                              const std::vector<uint8_t>& endpointEncCertChain,
52                                              const std::vector<uint8_t>& challenge,
53                                              DeviceInfo* deviceInfo, ProtectedData* protectedData,
54                                              std::vector<uint8_t>* keysToSignMac) override;
55 
56     ScopedAStatus generateCertificateRequestV2(const std::vector<MacedPublicKey>& keysToSign,
57                                                const std::vector<uint8_t>& challenge,
58                                                std::vector<uint8_t>* csr) override;
59 
60   private:
61     ScopedAStatus beginSendData(const std::vector<MacedPublicKey>& keysToSign,
62                                 const std::vector<uint8_t>& challenge, DeviceInfo* deviceInfo,
63                                 uint32_t* version, std::string* certificateType);
64 
65     ScopedAStatus updateMacedKey(const std::vector<MacedPublicKey>& keysToSign,
66                                  cppbor::Array& coseKeys);
67 
68     ScopedAStatus finishSendData(std::vector<uint8_t>& coseEncryptProtectedHeader,
69                                  std::vector<uint8_t>& signature, uint32_t& version,
70                                  uint32_t& respFlag);
71 
72     ScopedAStatus getResponse(std::vector<uint8_t>& partialCipheredData,
73                               cppbor::Array& recepientStructure, uint32_t& respFlag);
74     ScopedAStatus getDiceCertChain(std::vector<uint8_t>& diceCertChain);
75     ScopedAStatus getUdsCertsChain(std::vector<uint8_t>& udsCertsChain);
76     std::shared_ptr<JavacardSecureElement> card_;
77     CborConverter cbor_;
78 };
79 
80 }  // namespace aidl::android::hardware::security::keymint
81