Coverage Report

Created: 2025-07-11 06:59

/src/opensc/src/libopensc/gp.c
Line
Count
Source
1
/*
2
 * gp.c: Global Platform Related functions
3
 *
4
 * Copyright (C) 2018 Red Hat, Inc.
5
 *
6
 * Author: Jakub Jelen <jjelen@redhat.com>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
27
#include "internal.h"
28
#include "gp.h"
29
30
/* ISO 7816 CLA values */
31
562
#define GLOBAL_PLATFORM_CLASS   0x80
32
33
/* ISO 71816 INS values */
34
562
#define ISO7816_INS_GET_DATA    0xca
35
36
/* The AID of the Card Manager defined by Open Platform 2.0.1 specification */
37
static const struct sc_aid gp_card_manager = {
38
  {0xA0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00}, 7
39
};
40
41
/* The AID of the Issuer Security Domain defined by GlobalPlatform 2.3.1 specification. */
42
static const struct sc_aid gp_isd_rid = {
43
  {0xA0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00}, 7
44
};
45
46
47
/* Select AID */
48
int
49
gp_select_aid(struct sc_card *card, const struct sc_aid *aid)
50
1.42k
{
51
1.42k
  int rv;
52
1.42k
  LOG_FUNC_CALLED(card->ctx);
53
1.42k
  rv = iso7816_select_aid(card, aid->value, aid->len, NULL, NULL);
54
1.42k
  LOG_FUNC_RETURN(card->ctx, rv);
55
1.42k
}
56
57
/* Select the Open Platform Card Manager */
58
int
59
gp_select_card_manager(struct sc_card *card)
60
1.32k
{
61
1.32k
  int rv;
62
63
1.32k
  LOG_FUNC_CALLED(card->ctx);
64
1.32k
  rv = gp_select_aid(card, &gp_card_manager);
65
1.32k
  LOG_FUNC_RETURN(card->ctx, rv);
66
1.32k
}
67
68
/* Select Global Platform Card Manager */
69
int
70
gp_select_isd_rid(struct sc_card *card)
71
42
{
72
42
  int rv;
73
74
42
  LOG_FUNC_CALLED(card->ctx);
75
42
  rv = gp_select_aid(card, &gp_isd_rid);
76
42
  LOG_FUNC_RETURN(card->ctx, rv);
77
42
}
78
79
/* Get Card Production Life-Cycle information */
80
int
81
gp_get_cplc_data(struct sc_card *card, global_platform_cplc_data_t *cplc_data)
82
562
{
83
562
  size_t len = sizeof(global_platform_cplc_data_t);
84
562
  u8 *receive_buf = (u8 *)cplc_data;
85
562
  struct sc_apdu apdu;
86
562
  int rc;
87
88
562
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, ISO7816_INS_GET_DATA, 0x9f, 0x7f);
89
562
  apdu.cla = GLOBAL_PLATFORM_CLASS;
90
562
  apdu.resp = receive_buf;
91
562
  apdu.resplen = len;
92
562
  apdu.le = len;
93
94
562
  rc = sc_transmit_apdu(card, &apdu);
95
562
  if (rc < 0)
96
562
    LOG_FUNC_RETURN(card->ctx, rc);
97
98
559
  rc = sc_check_sw(card, apdu.sw1, apdu.sw2);
99
559
  if (rc < 0)
100
559
    LOG_FUNC_RETURN(card->ctx, rc);
101
102
  /* We expect this will fill the whole structure in the argument.
103
   * If we got something else, report error */
104
552
  if ((size_t)apdu.resplen < sizeof(global_platform_cplc_data_t)) {
105
5
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_CORRUPTED_DATA);
106
5
  }
107
547
  LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
108
547
}