• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019, The Android Open Source Project, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package com.google.android.attestation;
17 
18 import org.bouncycastle.asn1.ASN1Boolean;
19 import org.bouncycastle.asn1.ASN1Encodable;
20 import org.bouncycastle.asn1.ASN1Enumerated;
21 import org.bouncycastle.asn1.ASN1Integer;
22 
23 /** Utils to get java representation of ASN1 types. */
24 class ASN1Parsing {
25 
getBooleanFromAsn1(ASN1Encodable asn1Value)26   static boolean getBooleanFromAsn1(ASN1Encodable asn1Value) {
27     if (asn1Value instanceof ASN1Boolean) {
28       return ((ASN1Boolean) asn1Value).isTrue();
29     } else {
30       throw new IllegalArgumentException(
31           "Boolean value expected; found " + asn1Value.getClass().getName() + " instead.");
32     }
33   }
34 
getIntegerFromAsn1(ASN1Encodable asn1Value)35   static int getIntegerFromAsn1(ASN1Encodable asn1Value) {
36     if (asn1Value instanceof ASN1Integer) {
37       return ((ASN1Integer) asn1Value).getValue().intValueExact();
38     } else if (asn1Value instanceof ASN1Enumerated) {
39       return ((ASN1Enumerated) asn1Value).getValue().intValueExact();
40     } else {
41       throw new IllegalArgumentException(
42           "Integer value expected; found " + asn1Value.getClass().getName() + " instead.");
43     }
44   }
45 }
46