1 /*
2  * Copyright (C) 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 package com.android.rkpdapp.database;
18 
19 import java.time.Instant;
20 
21 import co.nstant.in.cbor.model.DataItem;
22 
23 /**
24  * In-memory key representation for Remotely Provisioned Keys.
25  */
26 public final class RkpKey {
27     private final byte[] mMacedPublicKey;
28     private final DataItem mCoseKey;
29     private final byte[] mKeyBlob;
30     private final String mIrpcHal;
31     private final byte[] mPublicKey;
32 
RkpKey(byte[] keyBlob, byte[] macedPublicKey, DataItem coseKey, String irpcHal, byte[] publicKey)33     public RkpKey(byte[] keyBlob, byte[] macedPublicKey, DataItem coseKey, String irpcHal,
34             byte[] publicKey) {
35         this.mKeyBlob = keyBlob;
36         this.mMacedPublicKey = macedPublicKey;
37         this.mCoseKey = coseKey;
38         this.mIrpcHal = irpcHal;
39         this.mPublicKey = publicKey;
40     }
41 
getMacedPublicKey()42     public byte[] getMacedPublicKey() {
43         return mMacedPublicKey;
44     }
45 
getCoseKey()46     public DataItem getCoseKey() {
47         return mCoseKey;
48     }
49 
getPublicKey()50     public byte[] getPublicKey() {
51         return mPublicKey;
52     }
53 
54     /**
55      * Creates the provisioned key with the information present in this data object as well as the
56      * provided expiration time and certificate chain.
57      *
58      * This function is helpful to generate the provisioned key only when required instead of
59      * generating and storing it separately.
60      */
generateProvisionedKey(byte[] certificateChain, Instant expirationTime)61     public ProvisionedKey generateProvisionedKey(byte[] certificateChain, Instant expirationTime) {
62         return new ProvisionedKey(mKeyBlob, mIrpcHal, mPublicKey, certificateChain, expirationTime);
63     }
64 }
65