aboutsummaryrefslogtreecommitdiff
path: root/ready_se/google/keymint/KM300/Applet/src/com/android/javacard/keymaster/KMEnumTag.java
blob: a7bcbe683744df93d51f6fddc8f234749ae5c04c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright(C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.javacard.keymaster;

import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

/**
 * KMEnumTag represents ENUM Tag type specified in android keymaster hal specifications. struct{byte
 * TAG_TYPE; short length; struct{short ENUM_TAG; short tagKey; byte value}}
 */
public class KMEnumTag extends KMTag {

  private static KMEnumTag prototype;

  // The allowed tag keys of type enum tag.
  private static short[] tags = {
    ALGORITHM, ECCURVE, BLOB_USAGE_REQ, USER_AUTH_TYPE, ORIGIN, HARDWARE_TYPE
  };

  private static Object[] enums = null;

  private KMEnumTag() {}

  private static KMEnumTag proto(short ptr) {
    if (prototype == null) {
      prototype = new KMEnumTag();
    }
    KMType.instanceTable[KM_ENUM_TAG_OFFSET] = ptr;
    return prototype;
  }

  // pointer to an empty instance used as expression
  public static short exp() {
    short ptr = instance(TAG_TYPE, (short) 2);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), ENUM_TAG);
    return ptr;
  }

  public static short instance(short key) {
    if (!validateEnum(key, NO_VALUE)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short ptr = KMType.instance(TAG_TYPE, (short) 4);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), ENUM_TAG);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), key);
    return ptr;
  }

  public static short instance(short key, byte val) {
    if (!validateEnum(key, val)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short ptr = instance(TAG_TYPE, (short) 5);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), ENUM_TAG);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), key);
    heap[(short) (ptr + TLV_HEADER_SIZE + 4)] = val;
    return ptr;
  }

  public static KMEnumTag cast(short ptr) {
    if (heap[ptr] != TAG_TYPE) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    if (Util.getShort(heap, (short) (ptr + TLV_HEADER_SIZE)) != ENUM_TAG) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    return proto(ptr);
  }

  public static void create() {
    if (enums == null) {
      // enum tag values.
      enums =
          new Object[] {
            new byte[] {RSA, DES, EC, AES, HMAC},
            new byte[] {P_224, P_256, P_384, P_521, CURVE_25519},
            new byte[] {STANDALONE, REQUIRES_FILE_SYSTEM},
            new byte[] {USER_AUTH_NONE, PASSWORD, FINGERPRINT, BOTH, ANY},
            new byte[] {GENERATED, DERIVED, IMPORTED, UNKNOWN, SECURELY_IMPORTED},
            new byte[] {SOFTWARE, TRUSTED_ENVIRONMENT, STRONGBOX}
          };
    }
  }

  // isValidTag enumeration keys and values.
  private static boolean validateEnum(short key, byte value) {
    create();
    byte[] vals;
    short enumInd;
    // check if key exists
    short index = (short) tags.length;
    while (--index >= 0) {
      if (tags[index] == key) {
        // check if value given
        if (value != NO_VALUE) {
          // check if the value exist
          vals = (byte[]) enums[index];
          enumInd = (short) vals.length;
          while (--enumInd >= 0) {
            if (vals[enumInd] == value) {
              // return true if value exist
              return true;
            }
          }
          // return false if value does not exist
          return false;
        }
        // return true if key exist and value not given
        return true;
      }
    }
    // return false if key does not exist
    return false;
  }

  public static short getValue(short tagKey, short keyParameters) {
    short tagPtr = KMKeyParameters.findTag(KMType.ENUM_TAG, tagKey, keyParameters);
    if (tagPtr != KMType.INVALID_VALUE) {
      return heap[(short) (tagPtr + TLV_HEADER_SIZE + 4)];
    }
    return KMType.INVALID_VALUE;
  }

  public short getKey() {
    return Util.getShort(
        heap, (short) (KMType.instanceTable[KM_ENUM_TAG_OFFSET] + TLV_HEADER_SIZE + 2));
  }

  public short getTagType() {
    return KMType.ENUM_TAG;
  }

  public byte getValue() {
    return heap[(short) (KMType.instanceTable[KM_ENUM_TAG_OFFSET] + TLV_HEADER_SIZE + 4)];
  }
}