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 #if HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 
35 #include "internal.h"
36 #include "asn1.h"
37 #include "pkcs15.h"
38 #include "common/compat_strnlen.h"
39 
40 
41 int
sc_pkcs15_read_data_object(struct sc_pkcs15_card * p15card,const struct sc_pkcs15_data_info * info,struct sc_pkcs15_data ** data_object_out)42 sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
43 		const struct sc_pkcs15_data_info *info,
44 		struct sc_pkcs15_data **data_object_out)
45 {
46 	struct sc_context *ctx = p15card->card->ctx;
47 	struct sc_pkcs15_data *data_object;
48 	struct sc_pkcs15_der der;
49 	int r;
50 
51 	LOG_FUNC_CALLED(ctx);
52 	if (!info || !data_object_out)
53 		LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
54 
55 	if (!info->data.value)   {
56 		r = sc_pkcs15_read_file(p15card, &info->path, (unsigned char **) &info->data.value, (size_t *) &info->data.len);
57 		LOG_TEST_RET(ctx, r, "Cannot get DATA object data");
58 	}
59 
60 	r = sc_der_copy(&der, &info->data);
61 	LOG_TEST_RET(ctx, r, "Cannot allocate memory for der value");
62 
63 	data_object = calloc(sizeof(struct sc_pkcs15_data), 1);
64 	if (!data_object)   {
65 		free(der.value);
66 		LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate memory for data object");
67 	}
68 
69 	data_object->data = der.value;
70 	data_object->data_len = der.len;
71 	*data_object_out = data_object;
72 
73 	LOG_FUNC_RETURN(ctx,SC_SUCCESS);
74 }
75 
76 
77 static const struct sc_asn1_entry c_asn1_data[] = {
78 	{ "data", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
79 	{ NULL, 0, 0, 0, NULL, NULL }
80 };
81 static const struct sc_asn1_entry c_asn1_com_data_attr[] = {
82 	{ "appName", SC_ASN1_UTF8STRING, SC_ASN1_TAG_UTF8STRING, SC_ASN1_OPTIONAL, NULL, NULL },
83 	{ "appOID", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL },
84 	{ NULL, 0, 0, 0, NULL, NULL }
85 };
86 static const struct sc_asn1_entry c_asn1_type_data_attr[] = {
87 	{ "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
88 	{ NULL, 0, 0, 0, NULL, NULL }
89 };
90 
sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card * p15card,struct sc_pkcs15_object * obj,const u8 ** buf,size_t * buflen)91 int sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card *p15card,
92 			       struct sc_pkcs15_object *obj,
93 			       const u8 ** buf, size_t *buflen)
94 {
95         sc_context_t *ctx = p15card->card->ctx;
96 	struct sc_pkcs15_data_info info;
97 	struct sc_asn1_entry	asn1_com_data_attr[3],
98 				asn1_type_data_attr[2],
99 				asn1_data[2];
100 	struct sc_asn1_pkcs15_object data_obj = { obj, asn1_com_data_attr, NULL,
101 					     asn1_type_data_attr };
102 	size_t label_len = sizeof(info.app_label) - 1;
103 	int r;
104 
105 	memset(info.app_label, 0, sizeof(info.app_label));
106 
107 	sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
108 	sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
109 	sc_copy_asn1_entry(c_asn1_data, asn1_data);
110 
111 	sc_format_asn1_entry(asn1_com_data_attr + 0, &info.app_label, &label_len, 0);
112 	sc_format_asn1_entry(asn1_com_data_attr + 1, &info.app_oid, NULL, 0);
113 	sc_format_asn1_entry(asn1_type_data_attr + 0, &info.path, NULL, 0);
114 	sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 0);
115 
116 	/* Fill in defaults */
117 	memset(&info, 0, sizeof(info));
118 	sc_init_oid(&info.app_oid);
119 
120 	r = sc_asn1_decode(ctx, asn1_data, *buf, *buflen, buf, buflen);
121 	if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
122 		return r;
123 	LOG_TEST_RET(ctx, r, "ASN.1 decoding failed");
124 
125 	if (!p15card->app || !p15card->app->ddo.aid.len) {
126 		if (!p15card->file_app) {
127 			return SC_ERROR_INTERNAL;
128 		}
129 		r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path);
130 		if (r < 0)
131 			return r;
132 	}
133 	else   {
134 		info.path.aid = p15card->app->ddo.aid;
135 	}
136 
137 	obj->type = SC_PKCS15_TYPE_DATA_OBJECT;
138 	obj->data = malloc(sizeof(info));
139 	if (obj->data == NULL)
140 		LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
141 	memcpy(obj->data, &info, sizeof(info));
142 
143 	return SC_SUCCESS;
144 }
145 
sc_pkcs15_encode_dodf_entry(sc_context_t * ctx,const struct sc_pkcs15_object * obj,u8 ** buf,size_t * bufsize)146 int sc_pkcs15_encode_dodf_entry(sc_context_t *ctx,
147 			       const struct sc_pkcs15_object *obj,
148 			       u8 **buf, size_t *bufsize)
149 {
150 	struct sc_asn1_entry	asn1_com_data_attr[4],
151 				asn1_type_data_attr[2],
152 				asn1_data[2];
153 	struct sc_pkcs15_data_info *info;
154 	struct sc_asn1_pkcs15_object data_obj = { (struct sc_pkcs15_object *) obj,
155 							asn1_com_data_attr, NULL,
156 							asn1_type_data_attr };
157 	size_t label_len;
158 
159 	info = (struct sc_pkcs15_data_info *) obj->data;
160 	label_len = strnlen(info->app_label, sizeof info->app_label);
161 
162 	sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
163 	sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
164 	sc_copy_asn1_entry(c_asn1_data, asn1_data);
165 
166 	if (label_len)
167 		sc_format_asn1_entry(asn1_com_data_attr + 0, &info->app_label, &label_len, 1);
168 
169 	if (sc_valid_oid(&info->app_oid))
170 		sc_format_asn1_entry(asn1_com_data_attr + 1, &info->app_oid, NULL, 1);
171 
172 	sc_format_asn1_entry(asn1_type_data_attr + 0, &info->path, NULL, 1);
173 	sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 1);
174 
175 	return sc_asn1_encode(ctx, asn1_data, buf, bufsize);
176 }
177 
sc_pkcs15_free_data_object(struct sc_pkcs15_data * data_object)178 void sc_pkcs15_free_data_object(struct sc_pkcs15_data *data_object)
179 {
180 	if (data_object == NULL)
181 		return;
182 
183 	free(data_object->data);
184 	free(data_object);
185 }
186 
sc_pkcs15_free_data_info(struct sc_pkcs15_data_info * info)187 void sc_pkcs15_free_data_info(struct sc_pkcs15_data_info *info)
188 {
189 	if (info && info->data.value && info->data.len)
190 		free(info->data.value);
191 
192 	free(info);
193 }
194