root/trunk/src/libopensc/pkcs15-data.c

Revision 3028, 5.4 KB (checked in by henryk, 2 years ago)

Make absolute paths from all paths read from the PKCS#15 directories by prepending the DF(PKCS#15) path if necessary.
Fixes compatibility with Siemens HiPath? SIcurity formatted cards which use relative paths.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * pkcs15-data.c: PKCS #15 data object functions
3 *
4 * Copyright (C) 2002  Danny De Cock <daniel.decock@postbox.be>
5 *
6 * This source file was inspired by pkcs15-cert.c.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23#include "internal.h"
24#include "pkcs15.h"
25#include "asn1.h"
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <sys/stat.h>
30#ifdef HAVE_UNISTD_H
31#include <unistd.h>
32#endif
33
34static const struct sc_asn1_entry     c_asn1_data_object[] = {
35        { "dataObject", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
36        { NULL, 0, 0, 0, NULL, NULL }
37};
38
39int sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
40                               const struct sc_pkcs15_data_info *info,
41                               struct sc_pkcs15_data **data_object_out)
42{
43        int r;
44        struct sc_pkcs15_data *data_object;
45        u8 *data = NULL;
46        size_t len;
47       
48        if (p15card == NULL || info == NULL || data_object_out == NULL)
49                return SC_ERROR_INVALID_ARGUMENTS;
50        SC_FUNC_CALLED(p15card->card->ctx, 1);
51
52        r = sc_pkcs15_read_file(p15card, &info->path, &data, &len, NULL);
53        if (r)
54                return r;
55        data_object = (struct sc_pkcs15_data *) malloc(sizeof(struct sc_pkcs15_data));
56        if (data_object == NULL) {
57                free(data);
58                return SC_ERROR_OUT_OF_MEMORY;
59        }
60        memset(data_object, 0, sizeof(struct sc_pkcs15_data));
61       
62        data_object->data = data;
63        data_object->data_len = len;
64        *data_object_out = data_object;
65        return 0;
66}
67
68static const struct sc_asn1_entry c_asn1_data[] = {
69        { "data", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
70        { NULL, 0, 0, 0, NULL, NULL }
71};
72static const struct sc_asn1_entry c_asn1_com_data_attr[] = {
73        { "appName", SC_ASN1_UTF8STRING, SC_ASN1_TAG_UTF8STRING, SC_ASN1_OPTIONAL, NULL, NULL },
74        { "appOID", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL },
75        { NULL, 0, 0, 0, NULL, NULL }
76};
77static const struct sc_asn1_entry c_asn1_type_data_attr[] = {
78        { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
79        { NULL, 0, 0, 0, NULL, NULL }
80};
81
82int sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card *p15card,
83                               struct sc_pkcs15_object *obj,
84                               const u8 ** buf, size_t *buflen)
85{
86        sc_context_t *ctx = p15card->card->ctx;
87        struct sc_pkcs15_data_info info;
88        struct sc_asn1_entry    asn1_com_data_attr[3],
89                                asn1_type_data_attr[2],
90                                asn1_data[2];
91        struct sc_asn1_pkcs15_object data_obj = { obj, asn1_com_data_attr, NULL,
92                                             asn1_type_data_attr };
93        size_t label_len = sizeof(info.app_label);
94        int r;
95
96        sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
97        sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
98        sc_copy_asn1_entry(c_asn1_data, asn1_data);
99       
100        sc_format_asn1_entry(asn1_com_data_attr + 0, &info.app_label, &label_len, 0);
101        sc_format_asn1_entry(asn1_com_data_attr + 1, &info.app_oid, NULL, 0);
102        sc_format_asn1_entry(asn1_type_data_attr + 0, &info.path, NULL, 0);
103        sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 0);
104
105        /* Fill in defaults */
106        memset(&info, 0, sizeof(info));
107        info.app_oid.value[0] = -1;
108
109        r = sc_asn1_decode(ctx, asn1_data, *buf, *buflen, buf, buflen);
110        if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
111                return r;
112        SC_TEST_RET(ctx, r, "ASN.1 decoding failed");
113        r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path);
114        if (r < 0)
115                return r;
116        obj->type = SC_PKCS15_TYPE_DATA_OBJECT;
117        obj->data = malloc(sizeof(info));
118        if (obj->data == NULL)
119                SC_FUNC_RETURN(ctx, 0, SC_ERROR_OUT_OF_MEMORY);
120        memcpy(obj->data, &info, sizeof(info));
121
122        return 0;
123}
124
125int sc_pkcs15_encode_dodf_entry(sc_context_t *ctx,
126                               const struct sc_pkcs15_object *obj,
127                               u8 **buf, size_t *bufsize)
128{
129        struct sc_asn1_entry    asn1_com_data_attr[4],
130                                asn1_type_data_attr[2],
131                                asn1_data[2];
132        struct sc_pkcs15_data_info *info;
133        struct sc_asn1_pkcs15_object data_obj = { (struct sc_pkcs15_object *) obj,
134                                                        asn1_com_data_attr, NULL,
135                                                        asn1_type_data_attr };
136        size_t label_len;
137
138        info = (struct sc_pkcs15_data_info *) obj->data;
139        label_len = strlen(info->app_label);
140
141        sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
142        sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
143        sc_copy_asn1_entry(c_asn1_data, asn1_data);
144       
145        if (label_len) {
146                sc_format_asn1_entry(asn1_com_data_attr + 0,
147                                &info->app_label, &label_len, 1);
148        }
149        if (info->app_oid.value[0] != -1) {
150                sc_format_asn1_entry(asn1_com_data_attr + 1,
151                                &info->app_oid, NULL, 1);
152        }
153        sc_format_asn1_entry(asn1_type_data_attr + 0, &info->path, NULL, 1);
154        sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 1);
155
156        return sc_asn1_encode(ctx, asn1_data, buf, bufsize);
157}
158
159void sc_pkcs15_free_data_object(struct sc_pkcs15_data *data_object)
160{
161        if (data_object == NULL)
162                return;
163
164        free(data_object->data);
165        free(data_object);
166}
167
168void sc_pkcs15_free_data_info(sc_pkcs15_data_info_t *data)
169{
170        free(data);
171}
Note: See TracBrowser for help on using the browser.