| Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2022 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 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> | 
 | 18 | #include <android/binder_manager.h> | 
 | 19 | #include <cppbor.h> | 
 | 20 | #include <keymaster/cppcose/cppcose.h> | 
 | 21 |  | 
 | 22 | #include <cstdint> | 
 | 23 | #include <memory> | 
| Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 24 | #include <string> | 
| Seth Moore | b84a1fb | 2022-09-13 12:02:49 -0700 | [diff] [blame] | 25 | #include <string_view> | 
| Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 26 | #include <vector> | 
 | 27 |  | 
| Tri Vo | ee773a2 | 2022-10-26 16:07:52 -0700 | [diff] [blame] | 28 | // Challenge size must be between 32 and 64 bytes inclusive. | 
 | 29 | constexpr size_t kChallengeSize = 64; | 
| Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 30 |  | 
| Seth Moore | b84a1fb | 2022-09-13 12:02:49 -0700 | [diff] [blame] | 31 | // Contains a the result of an operation that should return cborData on success. | 
 | 32 | // Returns an an error message and null cborData on error. | 
 | 33 | template <typename T> struct CborResult { | 
 | 34 |     std::unique_ptr<T> cborData; | 
 | 35 |     std::string errMsg; | 
| Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 36 | }; | 
 | 37 |  | 
 | 38 | // Return `buffer` encoded as a base64 string. | 
 | 39 | std::string toBase64(const std::vector<uint8_t>& buffer); | 
 | 40 |  | 
 | 41 | // Generate a random challenge containing `kChallengeSize` bytes. | 
 | 42 | std::vector<uint8_t> generateChallenge(); | 
 | 43 |  | 
 | 44 | // Get a certificate signing request for the given IRemotelyProvisionedComponent. | 
 | 45 | // On error, the csr Array is null, and the string field contains a description of | 
 | 46 | // what went wrong. | 
| Seth Moore | b84a1fb | 2022-09-13 12:02:49 -0700 | [diff] [blame] | 47 | CborResult<cppbor::Array> | 
 | 48 | getCsr(std::string_view componentName, | 
| Seth Moore | 0475678 | 2022-09-13 16:09:15 -0700 | [diff] [blame] | 49 |        aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent* irpc); | 
 | 50 |  | 
 | 51 | // Generates a test certificate chain and validates it, exiting the process on error. | 
 | 52 | void selfTestGetCsr( | 
 | 53 |     std::string_view componentName, | 
| Tri Vo | ee773a2 | 2022-10-26 16:07:52 -0700 | [diff] [blame] | 54 |     aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent* irpc); |