1 /*
2  * Copyright (C) 2020 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 android.keystore.cts;
18 
19 import co.nstant.in.cbor.model.Array;
20 import co.nstant.in.cbor.model.ByteString;
21 import co.nstant.in.cbor.model.DataItem;
22 import co.nstant.in.cbor.model.Map;
23 import co.nstant.in.cbor.model.NegativeInteger;
24 import co.nstant.in.cbor.model.Number;
25 import co.nstant.in.cbor.model.SimpleValue;
26 import co.nstant.in.cbor.model.SimpleValueType;
27 import co.nstant.in.cbor.model.UnsignedInteger;
28 
29 import java.nio.charset.StandardCharsets;
30 
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36 
37 
38 class CborUtils {
toNumber(long l)39     public static Number toNumber(long l) {
40         return l >= 0 ? new UnsignedInteger(l) : new NegativeInteger(l);
41     }
42 
getInt(Map map, long index)43     public static int getInt(Map map, long index) {
44         DataItem item = map.get(CborUtils.toNumber(index));
45         return ((Number) item).getValue().intValue();
46     }
47 
getInt(Map map, DataItem index)48     public static int getInt(Map map, DataItem index) {
49         DataItem item = map.get(index);
50         return ((Number) item).getValue().intValue();
51     }
52 
getLong(Map map, DataItem index)53     public static long getLong(Map map, DataItem index) {
54         DataItem item = map.get(index);
55         return ((Number) item).getValue().longValue();
56     }
57 
getIntSet(Map map, DataItem index)58     public static Set<Integer> getIntSet(Map map, DataItem index) {
59         Array array = (Array) map.get(index);
60         Set<Integer> result = new HashSet();
61         for (DataItem item : array.getDataItems()) {
62             result.add(((Number) item).getValue().intValue());
63         }
64         return result;
65     }
66 
getBoolean(Map map, DataItem index)67     public static Boolean getBoolean(Map map, DataItem index) {
68         SimpleValueType value = ((SimpleValue) map.get(index)).getSimpleValueType();
69         if (value != SimpleValueType.TRUE && value != SimpleValueType.FALSE) {
70             throw new RuntimeException("Only expecting boolean values for " + index);
71         }
72         return (value == SimpleValueType.TRUE);
73     }
74 
getBooleanList(Map map, DataItem index)75     public static List<Boolean> getBooleanList(Map map, DataItem index) {
76         Array array = (Array) map.get(index);
77         List<Boolean> result = new ArrayList();
78         for (DataItem item : array.getDataItems()) {
79             SimpleValueType value = ((SimpleValue) item).getSimpleValueType();
80             if (value == SimpleValueType.FALSE) {
81                 result.add(false);
82             } else if (value == SimpleValueType.TRUE) {
83                 result.add(true);
84             } else {
85                 throw new RuntimeException("Map contains more than booleans: " + map);
86             }
87         }
88         return result;
89     }
90 
getDate(Map map, DataItem index)91     public static Date getDate(Map map, DataItem index) {
92         DataItem item = map.get(index);
93         long epochMillis = ((Number) item).getValue().longValue();
94         return new Date(epochMillis);
95     }
96 
getBytes(Map map, DataItem index)97     public static byte[] getBytes(Map map, DataItem index) {
98         DataItem item = map.get(index);
99         return ((ByteString) item).getBytes();
100     }
101 
getString(Map map, DataItem index)102     public static String getString(Map map, DataItem index) {
103         byte[] bytes = getBytes(map, index);
104         return new String(bytes, StandardCharsets.UTF_8);
105     }
106 }
107