Coverage Report

Created: 2026-01-10 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-cac-common.c
Line
Count
Source
1
/*
2
 * card-cac-common.c: Code shared among CAC1 and CAC2 drivers
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 <ctype.h>
28
#include <string.h>
29
#include <stdlib.h>
30
31
#ifdef _WIN32
32
#include <io.h>
33
#else
34
#include <unistd.h>
35
#endif
36
37
#include "internal.h"
38
#include "iso7816.h"
39
#include "card-cac-common.h"
40
41
/* default certificate labels for the CAC card */
42
const char *cac_labels[MAX_CAC_SLOTS] = {
43
  "CAC ID Certificate",
44
  "CAC Email Signature Certificate",
45
  "CAC Email Encryption Certificate",
46
  "CAC Cert 4",
47
  "CAC Cert 5",
48
  "CAC Cert 6",
49
  "CAC Cert 7",
50
  "CAC Cert 8",
51
  "CAC Cert 9",
52
  "CAC Cert 10",
53
  "CAC Cert 11",
54
  "CAC Cert 12",
55
  "CAC Cert 13",
56
  "CAC Cert 14",
57
  "CAC Cert 15",
58
  "CAC Cert 16"
59
};
60
61
const char *get_cac_label(int index)
62
1.56k
{
63
1.56k
  if (index < 0 || index >= MAX_CAC_SLOTS)
64
0
    return NULL;
65
66
1.56k
  return cac_labels[index];
67
1.56k
}
68
69
static int cac_list_compare_path(const void *a, const void *b)
70
11.8k
{
71
11.8k
  if (a == NULL || b == NULL)
72
0
    return 1;
73
11.8k
  return memcmp( &((cac_object_t *) a)->path,
74
11.8k
    &((cac_object_t *) b)->path, sizeof(sc_path_t));
75
11.8k
}
76
77
/* For SimCList autocopy, we need to know the size of the data elements */
78
1.66k
static size_t cac_list_meter(const void *el) {
79
1.66k
  return sizeof(cac_object_t);
80
1.66k
}
81
82
cac_private_data_t *cac_new_private_data(void)
83
820
{
84
820
  cac_private_data_t *priv;
85
86
820
  priv = calloc(1, sizeof(cac_private_data_t));
87
820
  if (priv == NULL)
88
0
    return NULL;
89
90
  /* Initialize PKI Applets list */
91
820
  if (list_init(&priv->pki_list) != 0 ||
92
820
      list_attributes_comparator(&priv->pki_list, cac_list_compare_path) != 0 ||
93
820
      list_attributes_copy(&priv->pki_list, cac_list_meter, 1) != 0) {
94
0
    cac_free_private_data(priv);
95
0
    return NULL;
96
0
  }
97
98
  /* Initialize General Applets List */
99
820
  if (list_init(&priv->general_list) != 0 ||
100
820
      list_attributes_comparator(&priv->general_list, cac_list_compare_path) != 0 ||
101
820
      list_attributes_copy(&priv->general_list, cac_list_meter, 1) != 0) {
102
0
    cac_free_private_data(priv);
103
0
    return NULL;
104
0
  }
105
106
820
  return priv;
107
820
}
108
109
void cac_free_private_data(cac_private_data_t *priv)
110
820
{
111
820
  free(priv->cac_id);
112
820
  free(priv->cache_buf);
113
820
  free(priv->aca_path);
114
820
  list_destroy(&priv->pki_list);
115
820
  list_destroy(&priv->general_list);
116
820
  free(priv);
117
820
  return;
118
820
}
119
120
int cac_add_object_to_list(list_t *list, const cac_object_t *object)
121
1.66k
{
122
1.66k
  if (list_append(list, object) < 0)
123
0
    return SC_ERROR_UNKNOWN;
124
1.66k
  return SC_SUCCESS;
125
1.66k
}
126