Coverage Report

Created: 2025-11-13 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
48
#define GLOBAL_PLATFORM_CLASS   0x80
32
33
/* ISO 71816 INS values */
34
48
#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
287
{
51
287
  int rv;
52
287
  LOG_FUNC_CALLED(card->ctx);
53
287
  rv = iso7816_select_aid(card, aid->value, aid->len, NULL, NULL);
54
287
  LOG_FUNC_RETURN(card->ctx, rv);
55
287
}
56
57
/* Select the Open Platform Card Manager */
58
int
59
gp_select_card_manager(struct sc_card *card)
60
253
{
61
253
  int rv;
62
63
253
  LOG_FUNC_CALLED(card->ctx);
64
253
  rv = gp_select_aid(card, &gp_card_manager);
65
253
  LOG_FUNC_RETURN(card->ctx, rv);
66
253
}
67
68
/* Select Global Platform Card Manager */
69
int
70
gp_select_isd_rid(struct sc_card *card)
71
34
{
72
34
  int rv;
73
74
34
  LOG_FUNC_CALLED(card->ctx);
75
34
  rv = gp_select_aid(card, &gp_isd_rid);
76
34
  LOG_FUNC_RETURN(card->ctx, rv);
77
34
}
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
48
{
83
48
  size_t len = sizeof(global_platform_cplc_data_t);
84
48
  u8 *receive_buf = (u8 *)cplc_data;
85
48
  struct sc_apdu apdu;
86
48
  int rc;
87
88
48
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, ISO7816_INS_GET_DATA, 0x9f, 0x7f);
89
48
  apdu.cla = GLOBAL_PLATFORM_CLASS;
90
48
  apdu.resp = receive_buf;
91
48
  apdu.resplen = len;
92
48
  apdu.le = len;
93
94
48
  rc = sc_transmit_apdu(card, &apdu);
95
48
  if (rc < 0)
96
48
    LOG_FUNC_RETURN(card->ctx, rc);
97
98
42
  rc = sc_check_sw(card, apdu.sw1, apdu.sw2);
99
42
  if (rc < 0)
100
42
    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
34
  if ((size_t)apdu.resplen < sizeof(global_platform_cplc_data_t)) {
105
4
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_CORRUPTED_DATA);
106
4
  }
107
30
  LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
108
30
}