aboutsummaryrefslogtreecommitdiff
path: root/ready_se/google/keymint/KM300/Applet/src/com/android/javacard/keymaster/KMByteTag.java
blob: 5f7231f3d61d9011c1da7f64974d790328c6eea2 (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
/*
 * 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;

/**
 * KMByteTag represents BYTES Tag Type from android keymaster hal specifications. The tag value of
 * this tag is the KMByteBlob pointer i.e. offset of KMByteBlob in memory heap. struct{byte
 * TAG_TYPE; short length; struct{short BYTES_TAG; short tagKey; short blobPtr}}
 */
public class KMByteTag extends KMTag {

  private static KMByteTag prototype;

  private KMByteTag() {}

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

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

  public static short instance(short key, short byteBlob) {
    if (!validateKey(key, byteBlob)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    if (heap[byteBlob] != BYTE_BLOB_TYPE) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short ptr = instance(TAG_TYPE, (short) 6);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), BYTES_TAG);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), key);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 4), byteBlob);
    return ptr;
  }

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

  private static boolean validateKey(short key, short byteBlob) {
    short valueLen = KMByteBlob.cast(byteBlob).length();
    switch (key) {
      case ATTESTATION_APPLICATION_ID:
        if (valueLen > MAX_ATTESTATION_APP_ID_SIZE) {
          return false;
        }
        break;
      case CERTIFICATE_SUBJECT_NAME:
        {
          if (valueLen > KMConfigurations.MAX_SUBJECT_DER_LEN) {
            return false;
          }
          KMAsn1Parser asn1Decoder = KMAsn1Parser.instance();
          asn1Decoder.validateDerSubject(byteBlob);
        }
        break;
      case APPLICATION_ID:
      case APPLICATION_DATA:
        if (valueLen > MAX_APP_ID_APP_DATA_SIZE) {
          return false;
        }
        break;
      case ATTESTATION_CHALLENGE:
        if (valueLen > MAX_ATTESTATION_CHALLENGE_SIZE) {
          return false;
        }
        break;
      case ATTESTATION_ID_BRAND:
      case ATTESTATION_ID_DEVICE:
      case ATTESTATION_ID_PRODUCT:
      case ATTESTATION_ID_SERIAL:
      case ATTESTATION_ID_IMEI:
      case ATTESTATION_ID_SECOND_IMEI:
      case ATTESTATION_ID_MEID:
      case ATTESTATION_ID_MANUFACTURER:
      case ATTESTATION_ID_MODEL:
        if (valueLen > KMConfigurations.MAX_ATTESTATION_IDS_SIZE) {
          return false;
        }
        break;
      case ROOT_OF_TRUST:
      case NONCE:
        break;
      default:
        return false;
    }
    return true;
  }

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

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

  public short getValue() {
    return Util.getShort(
        heap, (short) (KMType.instanceTable[KM_BYTE_TAG_OFFSET] + TLV_HEADER_SIZE + 4));
  }

  public short length() {
    short blobPtr =
        Util.getShort(
            heap, (short) (KMType.instanceTable[KM_BYTE_TAG_OFFSET] + TLV_HEADER_SIZE + 4));
    return KMByteBlob.cast(blobPtr).length();
  }
}