/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 | 0 | { |
63 | 0 | if (index < 0 || index >= MAX_CAC_SLOTS) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | return cac_labels[index]; |
67 | 0 | } |
68 | | |
69 | | static int cac_list_compare_path(const void *a, const void *b) |
70 | 0 | { |
71 | 0 | if (a == NULL || b == NULL) |
72 | 0 | return 1; |
73 | 0 | return memcmp( &((cac_object_t *) a)->path, |
74 | 0 | &((cac_object_t *) b)->path, sizeof(sc_path_t)); |
75 | 0 | } |
76 | | |
77 | | /* For SimCList autocopy, we need to know the size of the data elements */ |
78 | 0 | static size_t cac_list_meter(const void *el) { |
79 | 0 | return sizeof(cac_object_t); |
80 | 0 | } |
81 | | |
82 | | cac_private_data_t *cac_new_private_data(void) |
83 | 0 | { |
84 | 0 | cac_private_data_t *priv; |
85 | |
|
86 | 0 | priv = calloc(1, sizeof(cac_private_data_t)); |
87 | 0 | if (priv == NULL) |
88 | 0 | return NULL; |
89 | | |
90 | | /* Initialize PKI Applets list */ |
91 | 0 | if (list_init(&priv->pki_list) != 0 || |
92 | 0 | list_attributes_comparator(&priv->pki_list, cac_list_compare_path) != 0 || |
93 | 0 | 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 | 0 | if (list_init(&priv->general_list) != 0 || |
100 | 0 | list_attributes_comparator(&priv->general_list, cac_list_compare_path) != 0 || |
101 | 0 | 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 | 0 | return priv; |
107 | 0 | } |
108 | | |
109 | | void cac_free_private_data(cac_private_data_t *priv) |
110 | 0 | { |
111 | 0 | free(priv->cac_id); |
112 | 0 | free(priv->cache_buf); |
113 | 0 | free(priv->aca_path); |
114 | 0 | list_destroy(&priv->pki_list); |
115 | 0 | list_destroy(&priv->general_list); |
116 | 0 | free(priv); |
117 | 0 | return; |
118 | 0 | } |
119 | | |
120 | | int cac_add_object_to_list(list_t *list, const cac_object_t *object) |
121 | 0 | { |
122 | 0 | if (list_append(list, object) < 0) |
123 | 0 | return SC_ERROR_UNKNOWN; |
124 | 0 | return SC_SUCCESS; |
125 | 0 | } |
126 | | |