xref: /aosp_15_r20/system/security/ondevice-signing/include/CertUtils.h (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (C) 2020 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 #pragma once
18*e1997b9aSAndroid Build Coastguard Worker 
19*e1997b9aSAndroid Build Coastguard Worker #include <functional>
20*e1997b9aSAndroid Build Coastguard Worker #include <string>
21*e1997b9aSAndroid Build Coastguard Worker #include <vector>
22*e1997b9aSAndroid Build Coastguard Worker 
23*e1997b9aSAndroid Build Coastguard Worker #include <android-base/result.h>
24*e1997b9aSAndroid Build Coastguard Worker 
25*e1997b9aSAndroid Build Coastguard Worker // Information extracted from a certificate.
26*e1997b9aSAndroid Build Coastguard Worker struct CertInfo {
27*e1997b9aSAndroid Build Coastguard Worker     std::string subjectCn;
28*e1997b9aSAndroid Build Coastguard Worker     std::vector<uint8_t> subjectRsaPublicKey;
29*e1997b9aSAndroid Build Coastguard Worker };
30*e1997b9aSAndroid Build Coastguard Worker 
31*e1997b9aSAndroid Build Coastguard Worker // Subjects of certificates we issue.
32*e1997b9aSAndroid Build Coastguard Worker struct CertSubject {
33*e1997b9aSAndroid Build Coastguard Worker     const char* commonName;
34*e1997b9aSAndroid Build Coastguard Worker     unsigned serialNumber;
35*e1997b9aSAndroid Build Coastguard Worker };
36*e1997b9aSAndroid Build Coastguard Worker 
37*e1997b9aSAndroid Build Coastguard Worker // This is our self-signed cert.
38*e1997b9aSAndroid Build Coastguard Worker inline const CertSubject kRootSubject{"ODS", 1};
39*e1997b9aSAndroid Build Coastguard Worker 
40*e1997b9aSAndroid Build Coastguard Worker android::base::Result<void> createSelfSignedCertificate(
41*e1997b9aSAndroid Build Coastguard Worker     const std::vector<uint8_t>& publicKey,
42*e1997b9aSAndroid Build Coastguard Worker     const std::function<android::base::Result<std::string>(const std::string&)>& signFunction,
43*e1997b9aSAndroid Build Coastguard Worker     const std::string& path);
44*e1997b9aSAndroid Build Coastguard Worker 
45*e1997b9aSAndroid Build Coastguard Worker android::base::Result<std::vector<uint8_t>>
46*e1997b9aSAndroid Build Coastguard Worker extractPublicKeyFromX509(const std::vector<uint8_t>& x509);
47*e1997b9aSAndroid Build Coastguard Worker android::base::Result<std::vector<uint8_t>> extractPublicKeyFromX509(const std::string& path);
48*e1997b9aSAndroid Build Coastguard Worker 
49*e1997b9aSAndroid Build Coastguard Worker android::base::Result<void> verifySignature(const std::string& message,
50*e1997b9aSAndroid Build Coastguard Worker                                             const std::string& signature,
51*e1997b9aSAndroid Build Coastguard Worker                                             const std::vector<uint8_t>& publicKey);
52