| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include "internal.h" |
|---|
| 22 | #include "pkcs15.h" |
|---|
| 23 | #include "asn1.h" |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <stdio.h> |
|---|
| 27 | #include <sys/stat.h> |
|---|
| 28 | #ifdef HAVE_UNISTD_H |
|---|
| 29 | #include <unistd.h> |
|---|
| 30 | #endif |
|---|
| 31 | #include <assert.h> |
|---|
| 32 | |
|---|
| 33 | static int parse_x509_cert(sc_context_t *ctx, const u8 *buf, size_t buflen, struct sc_pkcs15_cert *cert) |
|---|
| 34 | { |
|---|
| 35 | int r; |
|---|
| 36 | struct sc_algorithm_id pk_alg, sig_alg; |
|---|
| 37 | sc_pkcs15_der_t pk = { NULL, 0 }; |
|---|
| 38 | struct sc_asn1_entry asn1_version[] = { |
|---|
| 39 | { "version", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, &cert->version, NULL }, |
|---|
| 40 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 41 | }; |
|---|
| 42 | struct sc_asn1_entry asn1_pkinfo[] = { |
|---|
| 43 | { "algorithm", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, &pk_alg, NULL }, |
|---|
| 44 | { "subjectPublicKey", SC_ASN1_BIT_STRING_NI, SC_ASN1_TAG_BIT_STRING, SC_ASN1_ALLOC, &pk.value, &pk.len }, |
|---|
| 45 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 46 | }; |
|---|
| 47 | struct sc_asn1_entry asn1_x509v3[] = { |
|---|
| 48 | { "certificatePolicies", SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 49 | { "subjectKeyIdentifier", SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 50 | { "crlDistributionPoints", SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, &cert->crl, &cert->crl_len }, |
|---|
| 51 | { "authorityKeyIdentifier", SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 52 | { "keyUsage", SC_ASN1_BOOLEAN, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 53 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 54 | }; |
|---|
| 55 | struct sc_asn1_entry asn1_extensions[] = { |
|---|
| 56 | { "x509v3", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_x509v3, NULL }, |
|---|
| 57 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 58 | }; |
|---|
| 59 | struct sc_asn1_entry asn1_tbscert[] = { |
|---|
| 60 | { "version", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_version, NULL }, |
|---|
| 61 | { "serialNumber", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_INTEGER, SC_ASN1_ALLOC, &cert->serial, &cert->serial_len }, |
|---|
| 62 | { "signature", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
|---|
| 63 | { "issuer", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, &cert->issuer, &cert->issuer_len }, |
|---|
| 64 | { "validity", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
|---|
| 65 | { "subject", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, &cert->subject, &cert->subject_len }, |
|---|
| 66 | { "subjectPublicKeyInfo",SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, asn1_pkinfo, NULL }, |
|---|
| 67 | { "extensions", SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_extensions, NULL }, |
|---|
| 68 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 69 | }; |
|---|
| 70 | struct sc_asn1_entry asn1_cert[] = { |
|---|
| 71 | { "tbsCertificate", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, asn1_tbscert, NULL }, |
|---|
| 72 | { "signatureAlgorithm", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, &sig_alg, NULL }, |
|---|
| 73 | { "signatureValue", SC_ASN1_BIT_STRING, SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL }, |
|---|
| 74 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 75 | }; |
|---|
| 76 | const u8 *obj; |
|---|
| 77 | size_t objlen; |
|---|
| 78 | |
|---|
| 79 | memset(cert, 0, sizeof(*cert)); |
|---|
| 80 | obj = sc_asn1_verify_tag(ctx, buf, buflen, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, |
|---|
| 81 | &objlen); |
|---|
| 82 | if (obj == NULL) { |
|---|
| 83 | sc_error(ctx, "X.509 certificate not found\n"); |
|---|
| 84 | return SC_ERROR_INVALID_ASN1_OBJECT; |
|---|
| 85 | } |
|---|
| 86 | cert->data_len = objlen + (obj - buf); |
|---|
| 87 | r = sc_asn1_decode(ctx, asn1_cert, obj, objlen, NULL, NULL); |
|---|
| 88 | SC_TEST_RET(ctx, r, "ASN.1 parsing of certificate failed"); |
|---|
| 89 | |
|---|
| 90 | cert->version++; |
|---|
| 91 | |
|---|
| 92 | cert->key.algorithm = pk_alg.algorithm; |
|---|
| 93 | pk.len >>= 3; |
|---|
| 94 | cert->key.data = pk; |
|---|
| 95 | |
|---|
| 96 | r = sc_pkcs15_decode_pubkey(ctx, &cert->key, pk.value, pk.len); |
|---|
| 97 | if (r < 0) |
|---|
| 98 | free(pk.value); |
|---|
| 99 | sc_asn1_clear_algorithm_id(&pk_alg); |
|---|
| 100 | sc_asn1_clear_algorithm_id(&sig_alg); |
|---|
| 101 | |
|---|
| 102 | return r; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, |
|---|
| 106 | const struct sc_pkcs15_cert_info *info, |
|---|
| 107 | struct sc_pkcs15_cert **cert_out) |
|---|
| 108 | { |
|---|
| 109 | int r; |
|---|
| 110 | struct sc_pkcs15_cert *cert; |
|---|
| 111 | u8 *data = NULL; |
|---|
| 112 | size_t len; |
|---|
| 113 | |
|---|
| 114 | assert(p15card != NULL && info != NULL && cert_out != NULL); |
|---|
| 115 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
|---|
| 116 | |
|---|
| 117 | if (info->path.len) { |
|---|
| 118 | r = sc_pkcs15_read_file(p15card, &info->path, &data, &len, NULL); |
|---|
| 119 | if (r) |
|---|
| 120 | return r; |
|---|
| 121 | } else { |
|---|
| 122 | sc_pkcs15_der_t copy; |
|---|
| 123 | |
|---|
| 124 | sc_der_copy(©, &info->value); |
|---|
| 125 | data = copy.value; |
|---|
| 126 | len = copy.len; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | cert = (struct sc_pkcs15_cert *) malloc(sizeof(struct sc_pkcs15_cert)); |
|---|
| 130 | if (cert == NULL) { |
|---|
| 131 | free(data); |
|---|
| 132 | return SC_ERROR_OUT_OF_MEMORY; |
|---|
| 133 | } |
|---|
| 134 | memset(cert, 0, sizeof(struct sc_pkcs15_cert)); |
|---|
| 135 | if (parse_x509_cert(p15card->card->ctx, data, len, cert)) { |
|---|
| 136 | free(data); |
|---|
| 137 | free(cert); |
|---|
| 138 | return SC_ERROR_INVALID_ASN1_OBJECT; |
|---|
| 139 | } |
|---|
| 140 | cert->data = data; |
|---|
| 141 | *cert_out = cert; |
|---|
| 142 | return 0; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | static const struct sc_asn1_entry c_asn1_cred_ident[] = { |
|---|
| 146 | { "idType", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL }, |
|---|
| 147 | { "idValue", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
|---|
| 148 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 149 | }; |
|---|
| 150 | static const struct sc_asn1_entry c_asn1_com_cert_attr[] = { |
|---|
| 151 | { "iD", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
|---|
| 152 | { "authority", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 153 | { "identifier", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 154 | |
|---|
| 155 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 156 | }; |
|---|
| 157 | static const struct sc_asn1_entry c_asn1_x509_cert_value_choice[] = { |
|---|
| 158 | { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
|---|
| 159 | { "direct", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL }, |
|---|
| 160 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 161 | }; |
|---|
| 162 | static const struct sc_asn1_entry c_asn1_x509_cert_attr[] = { |
|---|
| 163 | { "value", SC_ASN1_CHOICE, 0, 0, NULL, NULL }, |
|---|
| 164 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 165 | }; |
|---|
| 166 | static const struct sc_asn1_entry c_asn1_type_cert_attr[] = { |
|---|
| 167 | { "x509CertificateAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
|---|
| 168 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 169 | }; |
|---|
| 170 | static const struct sc_asn1_entry c_asn1_cert[] = { |
|---|
| 171 | { "x509Certificate", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
|---|
| 172 | { NULL, 0, 0, 0, NULL, NULL } |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | int sc_pkcs15_decode_cdf_entry(struct sc_pkcs15_card *p15card, |
|---|
| 176 | struct sc_pkcs15_object *obj, |
|---|
| 177 | const u8 ** buf, size_t *buflen) |
|---|
| 178 | { |
|---|
| 179 | sc_context_t *ctx = p15card->card->ctx; |
|---|
| 180 | struct sc_pkcs15_cert_info info; |
|---|
| 181 | struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4], |
|---|
| 182 | asn1_x509_cert_attr[2], asn1_type_cert_attr[2], |
|---|
| 183 | asn1_cert[2], asn1_x509_cert_value_choice[3]; |
|---|
| 184 | struct sc_asn1_pkcs15_object cert_obj = { obj, asn1_com_cert_attr, NULL, |
|---|
| 185 | asn1_type_cert_attr }; |
|---|
| 186 | sc_pkcs15_der_t *der = &info.value; |
|---|
| 187 | u8 id_value[128]; |
|---|
| 188 | int id_type; |
|---|
| 189 | size_t id_value_len = sizeof(id_value); |
|---|
| 190 | int r; |
|---|
| 191 | |
|---|
| 192 | sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident); |
|---|
| 193 | sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr); |
|---|
| 194 | sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr); |
|---|
| 195 | sc_copy_asn1_entry(c_asn1_x509_cert_value_choice, asn1_x509_cert_value_choice); |
|---|
| 196 | sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr); |
|---|
| 197 | sc_copy_asn1_entry(c_asn1_cert, asn1_cert); |
|---|
| 198 | |
|---|
| 199 | sc_format_asn1_entry(asn1_cred_ident + 0, &id_type, NULL, 0); |
|---|
| 200 | sc_format_asn1_entry(asn1_cred_ident + 1, &id_value, &id_value_len, 0); |
|---|
| 201 | sc_format_asn1_entry(asn1_com_cert_attr + 0, &info.id, NULL, 0); |
|---|
| 202 | sc_format_asn1_entry(asn1_com_cert_attr + 1, &info.authority, NULL, 0); |
|---|
| 203 | sc_format_asn1_entry(asn1_com_cert_attr + 2, asn1_cred_ident, NULL, 0); |
|---|
| 204 | sc_format_asn1_entry(asn1_x509_cert_attr + 0, asn1_x509_cert_value_choice, NULL, 0); |
|---|
| 205 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 0, &info.path, NULL, 0); |
|---|
| 206 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 1, &der->value, &der->len, 0); |
|---|
| 207 | sc_format_asn1_entry(asn1_type_cert_attr + 0, asn1_x509_cert_attr, NULL, 0); |
|---|
| 208 | sc_format_asn1_entry(asn1_cert + 0, &cert_obj, NULL, 0); |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | memset(&info, 0, sizeof(info)); |
|---|
| 212 | info.authority = 0; |
|---|
| 213 | |
|---|
| 214 | r = sc_asn1_decode(ctx, asn1_cert, *buf, *buflen, buf, buflen); |
|---|
| 215 | |
|---|
| 216 | if (r < 0 && der->value) |
|---|
| 217 | free(der->value); |
|---|
| 218 | if (r == SC_ERROR_ASN1_END_OF_CONTENTS) |
|---|
| 219 | return r; |
|---|
| 220 | SC_TEST_RET(ctx, r, "ASN.1 decoding failed"); |
|---|
| 221 | r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path); |
|---|
| 222 | if (r < 0) |
|---|
| 223 | return r; |
|---|
| 224 | obj->type = SC_PKCS15_TYPE_CERT_X509; |
|---|
| 225 | obj->data = malloc(sizeof(info)); |
|---|
| 226 | if (obj->data == NULL) |
|---|
| 227 | SC_FUNC_RETURN(ctx, 0, SC_ERROR_OUT_OF_MEMORY); |
|---|
| 228 | memcpy(obj->data, &info, sizeof(info)); |
|---|
| 229 | |
|---|
| 230 | return 0; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | int sc_pkcs15_encode_cdf_entry(sc_context_t *ctx, |
|---|
| 234 | const struct sc_pkcs15_object *obj, |
|---|
| 235 | u8 **buf, size_t *bufsize) |
|---|
| 236 | { |
|---|
| 237 | struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4], |
|---|
| 238 | asn1_x509_cert_attr[2], asn1_type_cert_attr[2], |
|---|
| 239 | asn1_cert[2], asn1_x509_cert_value_choice[3]; |
|---|
| 240 | struct sc_pkcs15_cert_info *infop = (sc_pkcs15_cert_info_t *) obj->data; |
|---|
| 241 | sc_pkcs15_der_t *der = &infop->value; |
|---|
| 242 | struct sc_asn1_pkcs15_object cert_obj = { (struct sc_pkcs15_object *) obj, |
|---|
| 243 | asn1_com_cert_attr, NULL, |
|---|
| 244 | asn1_type_cert_attr }; |
|---|
| 245 | int r; |
|---|
| 246 | |
|---|
| 247 | sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident); |
|---|
| 248 | sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr); |
|---|
| 249 | sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr); |
|---|
| 250 | sc_copy_asn1_entry(c_asn1_x509_cert_value_choice, asn1_x509_cert_value_choice); |
|---|
| 251 | sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr); |
|---|
| 252 | sc_copy_asn1_entry(c_asn1_cert, asn1_cert); |
|---|
| 253 | |
|---|
| 254 | sc_format_asn1_entry(asn1_com_cert_attr + 0, (void *) &infop->id, NULL, 1); |
|---|
| 255 | if (infop->authority) |
|---|
| 256 | sc_format_asn1_entry(asn1_com_cert_attr + 1, (void *) &infop->authority, NULL, 1); |
|---|
| 257 | if (infop->path.len || !der->value) { |
|---|
| 258 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 0, &infop->path, NULL, 1); |
|---|
| 259 | } else { |
|---|
| 260 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 1, der->value, &der->len, 1); |
|---|
| 261 | } |
|---|
| 262 | sc_format_asn1_entry(asn1_type_cert_attr + 0, &asn1_x509_cert_value_choice, NULL, 1); |
|---|
| 263 | sc_format_asn1_entry(asn1_cert + 0, (void *) &cert_obj, NULL, 1); |
|---|
| 264 | |
|---|
| 265 | r = sc_asn1_encode(ctx, asn1_cert, buf, bufsize); |
|---|
| 266 | |
|---|
| 267 | return r; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | void sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert) |
|---|
| 271 | { |
|---|
| 272 | assert(cert != NULL); |
|---|
| 273 | |
|---|
| 274 | sc_pkcs15_erase_pubkey(&cert->key); |
|---|
| 275 | free(cert->subject); |
|---|
| 276 | free(cert->issuer); |
|---|
| 277 | free(cert->serial); |
|---|
| 278 | free(cert->data); |
|---|
| 279 | free(cert->crl); |
|---|
| 280 | free(cert); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | void sc_pkcs15_free_cert_info(sc_pkcs15_cert_info_t *cert) |
|---|
| 284 | { |
|---|
| 285 | if (!cert) |
|---|
| 286 | return; |
|---|
| 287 | if (cert->value.value) |
|---|
| 288 | free(cert->value.value); |
|---|
| 289 | free(cert); |
|---|
| 290 | } |
|---|