Coverage Report

Created: 2026-01-17 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-sc-hsm.c
Line
Count
Source
1
/*
2
 * pkcs15-sc-hsm.c : Initialize PKCS#15 emulation
3
 *
4
 * Copyright (C) 2012 Andreas Schwier, CardContact, Minden, Germany
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <stdlib.h>
26
#include <string.h>
27
#include <stdio.h>
28
29
#include "internal.h"
30
#include "pkcs15.h"
31
#include "asn1.h"
32
#include "common/compat_strlcpy.h"
33
#include "common/compat_strnlen.h"
34
35
#include "card-sc-hsm.h"
36
37
38
extern struct sc_aid sc_hsm_aid;
39
40
41
void sc_hsm_set_serialnr(sc_card_t *card, char *serial);
42
43
44
45
static struct ec_curve curves[] = {
46
    {
47
        { (unsigned char *) "\x2A\x86\x48\xCE\x3D\x03\x01\x01", 8}, // secp192r1 aka prime192r1
48
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 24},
49
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC", 24},
50
        { (unsigned char *) "\x64\x21\x05\x19\xE5\x9C\x80\xE7\x0F\xA7\xE9\xAB\x72\x24\x30\x49\xFE\xB8\xDE\xEC\xC1\x46\xB9\xB1", 24},
51
        { (unsigned char *) "\x04\x18\x8D\xA8\x0E\xB0\x30\x90\xF6\x7C\xBF\x20\xEB\x43\xA1\x88\x00\xF4\xFF\x0A\xFD\x82\xFF\x10\x12\x07\x19\x2B\x95\xFF\xC8\xDA\x78\x63\x10\x11\xED\x6B\x24\xCD\xD5\x73\xF9\x77\xA1\x1E\x79\x48\x11", 49},
52
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x99\xDE\xF8\x36\x14\x6B\xC9\xB1\xB4\xD2\x28\x31", 24},
53
        { (unsigned char *) "\x01", 1}
54
    },
55
    {
56
        { (unsigned char *) "\x2A\x86\x48\xCE\x3D\x03\x01\x07", 8}, // secp256r1 aka prime256r1
57
        { (unsigned char *) "\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 32},
58
        { (unsigned char *) "\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC", 32},
59
        { (unsigned char *) "\x5A\xC6\x35\xD8\xAA\x3A\x93\xE7\xB3\xEB\xBD\x55\x76\x98\x86\xBC\x65\x1D\x06\xB0\xCC\x53\xB0\xF6\x3B\xCE\x3C\x3E\x27\xD2\x60\x4B", 32},
60
        { (unsigned char *) "\x04\x6B\x17\xD1\xF2\xE1\x2C\x42\x47\xF8\xBC\xE6\xE5\x63\xA4\x40\xF2\x77\x03\x7D\x81\x2D\xEB\x33\xA0\xF4\xA1\x39\x45\xD8\x98\xC2\x96\x4F\xE3\x42\xE2\xFE\x1A\x7F\x9B\x8E\xE7\xEB\x4A\x7C\x0F\x9E\x16\x2B\xCE\x33\x57\x6B\x31\x5E\xCE\xCB\xB6\x40\x68\x37\xBF\x51\xF5", 65},
61
        { (unsigned char *) "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84\xF3\xB9\xCA\xC2\xFC\x63\x25\x51", 32},
62
        { (unsigned char *) "\x01", 1}
63
    },
64
    {
65
        { (unsigned char *) "\x2B\x81\x04\x00\x22", 5}, // secp384r1
66
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF", 48},
67
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFC", 48},
68
        { (unsigned char *) "\xB3\x31\x2F\xA7\xE2\x3E\xE7\xE4\x98\x8E\x05\x6B\xE3\xF8\x2D\x19\x18\x1D\x9C\x6E\xFE\x81\x41\x12\x03\x14\x08\x8F\x50\x13\x87\x5A\xC6\x56\x39\x8D\x8A\x2E\xD1\x9D\x2A\x85\xC8\xED\xD3\xEC\x2A\xEF", 48},
69
        { (unsigned char *) "\x04\xAA\x87\xCA\x22\xBE\x8B\x05\x37\x8E\xB1\xC7\x1E\xF3\x20\xAD\x74\x6E\x1D\x3B\x62\x8B\xA7\x9B\x98\x59\xF7\x41\xE0\x82\x54\x2A\x38\x55\x02\xF2\x5D\xBF\x55\x29\x6C\x3A\x54\x5E\x38\x72\x76\x0A\xB7\x36\x17\xDE\x4A\x96\x26\x2C\x6F\x5D\x9E\x98\xBF\x92\x92\xDC\x29\xF8\xF4\x1D\xBD\x28\x9A\x14\x7C\xE9\xDA\x31\x13\xB5\xF0\xB8\xC0\x0A\x60\xB1\xCE\x1D\x7E\x81\x9D\x7A\x43\x1D\x7C\x90\xEA\x0E\x5F", 97},
70
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC7\x63\x4D\x81\xF4\x37\x2D\xDF\x58\x1A\x0D\xB2\x48\xB0\xA7\x7A\xEC\xEC\x19\x6A\xCC\xC5\x29\x73", 48},
71
        { (unsigned char *) "\x01", 1}
72
    },
73
    {
74
        { (unsigned char *) "\x2B\x81\x04\x00\x23", 5}, // secp521r1
75
        { (unsigned char *) "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 66},
76
        { (unsigned char *) "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC", 66},
77
        { (unsigned char *) "\x00\x51\x95\x3E\xB9\x61\x8E\x1C\x9A\x1F\x92\x9A\x21\xA0\xB6\x85\x40\xEE\xA2\xDA\x72\x5B\x99\xB3\x15\xF3\xB8\xB4\x89\x91\x8E\xF1\x09\xE1\x56\x19\x39\x51\xEC\x7E\x93\x7B\x16\x52\xC0\xBD\x3B\xB1\xBF\x07\x35\x73\xDF\x88\x3D\x2C\x34\xF1\xEF\x45\x1F\xD4\x6B\x50\x3F\x00", 66},
78
        { (unsigned char *) "\x04\x00\xC6\x85\x8E\x06\xB7\x04\x04\xE9\xCD\x9E\x3E\xCB\x66\x23\x95\xB4\x42\x9C\x64\x81\x39\x05\x3F\xB5\x21\xF8\x28\xAF\x60\x6B\x4D\x3D\xBA\xA1\x4B\x5E\x77\xEF\xE7\x59\x28\xFE\x1D\xC1\x27\xA2\xFF\xA8\xDE\x33\x48\xB3\xC1\x85\x6A\x42\x9B\xF9\x7E\x7E\x31\xC2\xE5\xBD\x66\x01\x18\x39\x29\x6A\x78\x9A\x3B\xC0\x04\x5C\x8A\x5F\xB4\x2C\x7D\x1B\xD9\x98\xF5\x44\x49\x57\x9B\x44\x68\x17\xAF\xBD\x17\x27\x3E\x66\x2C\x97\xEE\x72\x99\x5E\xF4\x26\x40\xC5\x50\xB9\x01\x3F\xAD\x07\x61\x35\x3C\x70\x86\xA2\x72\xC2\x40\x88\xBE\x94\x76\x9F\xD1\x66\x50", 133},
79
        { (unsigned char *) "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFA\x51\x86\x87\x83\xBF\x2F\x96\x6B\x7F\xCC\x01\x48\xF7\x09\xA5\xD0\x3B\xB5\xC9\xB8\x89\x9C\x47\xAE\xBB\x6F\xB7\x1E\x91\x38\x64\x09", 66},
80
        { (unsigned char *) "\x01", 1}
81
    },
82
    {
83
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x03", 9}, // brainpoolP192r1
84
        { (unsigned char *) "\xC3\x02\xF4\x1D\x93\x2A\x36\xCD\xA7\xA3\x46\x30\x93\xD1\x8D\xB7\x8F\xCE\x47\x6D\xE1\xA8\x62\x97", 24},
85
        { (unsigned char *) "\x6A\x91\x17\x40\x76\xB1\xE0\xE1\x9C\x39\xC0\x31\xFE\x86\x85\xC1\xCA\xE0\x40\xE5\xC6\x9A\x28\xEF", 24},
86
        { (unsigned char *) "\x46\x9A\x28\xEF\x7C\x28\xCC\xA3\xDC\x72\x1D\x04\x4F\x44\x96\xBC\xCA\x7E\xF4\x14\x6F\xBF\x25\xC9", 24},
87
        { (unsigned char *) "\x04\xC0\xA0\x64\x7E\xAA\xB6\xA4\x87\x53\xB0\x33\xC5\x6C\xB0\xF0\x90\x0A\x2F\x5C\x48\x53\x37\x5F\xD6\x14\xB6\x90\x86\x6A\xBD\x5B\xB8\x8B\x5F\x48\x28\xC1\x49\x00\x02\xE6\x77\x3F\xA2\xFA\x29\x9B\x8F", 49},
88
        { (unsigned char *) "\xC3\x02\xF4\x1D\x93\x2A\x36\xCD\xA7\xA3\x46\x2F\x9E\x9E\x91\x6B\x5B\xE8\xF1\x02\x9A\xC4\xAC\xC1", 24},
89
        { (unsigned char *) "\x01", 1}
90
    },
91
    {
92
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x05", 9}, // brainpoolP224r1
93
        { (unsigned char *) "\xD7\xC1\x34\xAA\x26\x43\x66\x86\x2A\x18\x30\x25\x75\xD1\xD7\x87\xB0\x9F\x07\x57\x97\xDA\x89\xF5\x7E\xC8\xC0\xFF", 28},
94
        { (unsigned char *) "\x68\xA5\xE6\x2C\xA9\xCE\x6C\x1C\x29\x98\x03\xA6\xC1\x53\x0B\x51\x4E\x18\x2A\xD8\xB0\x04\x2A\x59\xCA\xD2\x9F\x43", 28},
95
        { (unsigned char *) "\x25\x80\xF6\x3C\xCF\xE4\x41\x38\x87\x07\x13\xB1\xA9\x23\x69\xE3\x3E\x21\x35\xD2\x66\xDB\xB3\x72\x38\x6C\x40\x0B", 28},
96
        { (unsigned char *) "\x04\x0D\x90\x29\xAD\x2C\x7E\x5C\xF4\x34\x08\x23\xB2\xA8\x7D\xC6\x8C\x9E\x4C\xE3\x17\x4C\x1E\x6E\xFD\xEE\x12\xC0\x7D\x58\xAA\x56\xF7\x72\xC0\x72\x6F\x24\xC6\xB8\x9E\x4E\xCD\xAC\x24\x35\x4B\x9E\x99\xCA\xA3\xF6\xD3\x76\x14\x02\xCD", 57},
97
        { (unsigned char *) "\xD7\xC1\x34\xAA\x26\x43\x66\x86\x2A\x18\x30\x25\x75\xD0\xFB\x98\xD1\x16\xBC\x4B\x6D\xDE\xBC\xA3\xA5\xA7\x93\x9F", 28},
98
        { (unsigned char *) "\x01", 1}
99
    },
100
    {
101
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x07", 9}, // brainpoolP256r1
102
        { (unsigned char *) "\xA9\xFB\x57\xDB\xA1\xEE\xA9\xBC\x3E\x66\x0A\x90\x9D\x83\x8D\x72\x6E\x3B\xF6\x23\xD5\x26\x20\x28\x20\x13\x48\x1D\x1F\x6E\x53\x77", 32},
103
        { (unsigned char *) "\x7D\x5A\x09\x75\xFC\x2C\x30\x57\xEE\xF6\x75\x30\x41\x7A\xFF\xE7\xFB\x80\x55\xC1\x26\xDC\x5C\x6C\xE9\x4A\x4B\x44\xF3\x30\xB5\xD9", 32},
104
        { (unsigned char *) "\x26\xDC\x5C\x6C\xE9\x4A\x4B\x44\xF3\x30\xB5\xD9\xBB\xD7\x7C\xBF\x95\x84\x16\x29\x5C\xF7\xE1\xCE\x6B\xCC\xDC\x18\xFF\x8C\x07\xB6", 32},
105
        { (unsigned char *) "\x04\x8B\xD2\xAE\xB9\xCB\x7E\x57\xCB\x2C\x4B\x48\x2F\xFC\x81\xB7\xAF\xB9\xDE\x27\xE1\xE3\xBD\x23\xC2\x3A\x44\x53\xBD\x9A\xCE\x32\x62\x54\x7E\xF8\x35\xC3\xDA\xC4\xFD\x97\xF8\x46\x1A\x14\x61\x1D\xC9\xC2\x77\x45\x13\x2D\xED\x8E\x54\x5C\x1D\x54\xC7\x2F\x04\x69\x97", 65},
106
        { (unsigned char *) "\xA9\xFB\x57\xDB\xA1\xEE\xA9\xBC\x3E\x66\x0A\x90\x9D\x83\x8D\x71\x8C\x39\x7A\xA3\xB5\x61\xA6\xF7\x90\x1E\x0E\x82\x97\x48\x56\xA7", 32},
107
        { (unsigned char *) "\x01", 1}
108
    },
109
    {
110
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x09", 9}, // brainpoolP320r1
111
        { (unsigned char *) "\xD3\x5E\x47\x20\x36\xBC\x4F\xB7\xE1\x3C\x78\x5E\xD2\x01\xE0\x65\xF9\x8F\xCF\xA6\xF6\xF4\x0D\xEF\x4F\x92\xB9\xEC\x78\x93\xEC\x28\xFC\xD4\x12\xB1\xF1\xB3\x2E\x27", 40},
112
        { (unsigned char *) "\x3E\xE3\x0B\x56\x8F\xBA\xB0\xF8\x83\xCC\xEB\xD4\x6D\x3F\x3B\xB8\xA2\xA7\x35\x13\xF5\xEB\x79\xDA\x66\x19\x0E\xB0\x85\xFF\xA9\xF4\x92\xF3\x75\xA9\x7D\x86\x0E\xB4", 40},
113
        { (unsigned char *) "\x52\x08\x83\x94\x9D\xFD\xBC\x42\xD3\xAD\x19\x86\x40\x68\x8A\x6F\xE1\x3F\x41\x34\x95\x54\xB4\x9A\xCC\x31\xDC\xCD\x88\x45\x39\x81\x6F\x5E\xB4\xAC\x8F\xB1\xF1\xA6", 40},
114
        { (unsigned char *) "\x04\x43\xBD\x7E\x9A\xFB\x53\xD8\xB8\x52\x89\xBC\xC4\x8E\xE5\xBF\xE6\xF2\x01\x37\xD1\x0A\x08\x7E\xB6\xE7\x87\x1E\x2A\x10\xA5\x99\xC7\x10\xAF\x8D\x0D\x39\xE2\x06\x11\x14\xFD\xD0\x55\x45\xEC\x1C\xC8\xAB\x40\x93\x24\x7F\x77\x27\x5E\x07\x43\xFF\xED\x11\x71\x82\xEA\xA9\xC7\x78\x77\xAA\xAC\x6A\xC7\xD3\x52\x45\xD1\x69\x2E\x8E\xE1", 81},
115
        { (unsigned char *) "\xD3\x5E\x47\x20\x36\xBC\x4F\xB7\xE1\x3C\x78\x5E\xD2\x01\xE0\x65\xF9\x8F\xCF\xA5\xB6\x8F\x12\xA3\x2D\x48\x2E\xC7\xEE\x86\x58\xE9\x86\x91\x55\x5B\x44\xC5\x93\x11", 40},
116
        { (unsigned char *) "\x01", 1}
117
    },
118
    {
119
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x0B", 9}, // brainpoolP384r1
120
        { (unsigned char *) "\x8C\xB9\x1E\x82\xA3\x38\x6D\x28\x0F\x5D\x6F\x7E\x50\xE6\x41\xDF\x15\x2F\x71\x09\xED\x54\x56\xB4\x12\xB1\xDA\x19\x7F\xB7\x11\x23\xAC\xD3\xA7\x29\x90\x1D\x1A\x71\x87\x47\x00\x13\x31\x07\xEC\x53", 48},
121
        { (unsigned char *) "\x7B\xC3\x82\xC6\x3D\x8C\x15\x0C\x3C\x72\x08\x0A\xCE\x05\xAF\xA0\xC2\xBE\xA2\x8E\x4F\xB2\x27\x87\x13\x91\x65\xEF\xBA\x91\xF9\x0F\x8A\xA5\x81\x4A\x50\x3A\xD4\xEB\x04\xA8\xC7\xDD\x22\xCE\x28\x26", 48},
122
        { (unsigned char *) "\x04\xA8\xC7\xDD\x22\xCE\x28\x26\x8B\x39\xB5\x54\x16\xF0\x44\x7C\x2F\xB7\x7D\xE1\x07\xDC\xD2\xA6\x2E\x88\x0E\xA5\x3E\xEB\x62\xD5\x7C\xB4\x39\x02\x95\xDB\xC9\x94\x3A\xB7\x86\x96\xFA\x50\x4C\x11", 48},
123
        { (unsigned char *) "\x04\x1D\x1C\x64\xF0\x68\xCF\x45\xFF\xA2\xA6\x3A\x81\xB7\xC1\x3F\x6B\x88\x47\xA3\xE7\x7E\xF1\x4F\xE3\xDB\x7F\xCA\xFE\x0C\xBD\x10\xE8\xE8\x26\xE0\x34\x36\xD6\x46\xAA\xEF\x87\xB2\xE2\x47\xD4\xAF\x1E\x8A\xBE\x1D\x75\x20\xF9\xC2\xA4\x5C\xB1\xEB\x8E\x95\xCF\xD5\x52\x62\xB7\x0B\x29\xFE\xEC\x58\x64\xE1\x9C\x05\x4F\xF9\x91\x29\x28\x0E\x46\x46\x21\x77\x91\x81\x11\x42\x82\x03\x41\x26\x3C\x53\x15", 97},
124
        { (unsigned char *) "\x8C\xB9\x1E\x82\xA3\x38\x6D\x28\x0F\x5D\x6F\x7E\x50\xE6\x41\xDF\x15\x2F\x71\x09\xED\x54\x56\xB3\x1F\x16\x6E\x6C\xAC\x04\x25\xA7\xCF\x3A\xB6\xAF\x6B\x7F\xC3\x10\x3B\x88\x32\x02\xE9\x04\x65\x65", 48},
125
        { (unsigned char *) "\x01", 1}
126
    },
127
    {
128
        { (unsigned char *) "\x2B\x24\x03\x03\x02\x08\x01\x01\x0D", 9}, // brainpoolP512r1
129
        { (unsigned char *) "\xAA\xDD\x9D\xB8\xDB\xE9\xC4\x8B\x3F\xD4\xE6\xAE\x33\xC9\xFC\x07\xCB\x30\x8D\xB3\xB3\xC9\xD2\x0E\xD6\x63\x9C\xCA\x70\x33\x08\x71\x7D\x4D\x9B\x00\x9B\xC6\x68\x42\xAE\xCD\xA1\x2A\xE6\xA3\x80\xE6\x28\x81\xFF\x2F\x2D\x82\xC6\x85\x28\xAA\x60\x56\x58\x3A\x48\xF3", 64},
130
        { (unsigned char *) "\x78\x30\xA3\x31\x8B\x60\x3B\x89\xE2\x32\x71\x45\xAC\x23\x4C\xC5\x94\xCB\xDD\x8D\x3D\xF9\x16\x10\xA8\x34\x41\xCA\xEA\x98\x63\xBC\x2D\xED\x5D\x5A\xA8\x25\x3A\xA1\x0A\x2E\xF1\xC9\x8B\x9A\xC8\xB5\x7F\x11\x17\xA7\x2B\xF2\xC7\xB9\xE7\xC1\xAC\x4D\x77\xFC\x94\xCA", 64},
131
        { (unsigned char *) "\x3D\xF9\x16\x10\xA8\x34\x41\xCA\xEA\x98\x63\xBC\x2D\xED\x5D\x5A\xA8\x25\x3A\xA1\x0A\x2E\xF1\xC9\x8B\x9A\xC8\xB5\x7F\x11\x17\xA7\x2B\xF2\xC7\xB9\xE7\xC1\xAC\x4D\x77\xFC\x94\xCA\xDC\x08\x3E\x67\x98\x40\x50\xB7\x5E\xBA\xE5\xDD\x28\x09\xBD\x63\x80\x16\xF7\x23", 64},
132
        { (unsigned char *) "\x04\x81\xAE\xE4\xBD\xD8\x2E\xD9\x64\x5A\x21\x32\x2E\x9C\x4C\x6A\x93\x85\xED\x9F\x70\xB5\xD9\x16\xC1\xB4\x3B\x62\xEE\xF4\xD0\x09\x8E\xFF\x3B\x1F\x78\xE2\xD0\xD4\x8D\x50\xD1\x68\x7B\x93\xB9\x7D\x5F\x7C\x6D\x50\x47\x40\x6A\x5E\x68\x8B\x35\x22\x09\xBC\xB9\xF8\x22\x7D\xDE\x38\x5D\x56\x63\x32\xEC\xC0\xEA\xBF\xA9\xCF\x78\x22\xFD\xF2\x09\xF7\x00\x24\xA5\x7B\x1A\xA0\x00\xC5\x5B\x88\x1F\x81\x11\xB2\xDC\xDE\x49\x4A\x5F\x48\x5E\x5B\xCA\x4B\xD8\x8A\x27\x63\xAE\xD1\xCA\x2B\x2F\xA8\xF0\x54\x06\x78\xCD\x1E\x0F\x3A\xD8\x08\x92", 129},
133
        { (unsigned char *) "\xAA\xDD\x9D\xB8\xDB\xE9\xC4\x8B\x3F\xD4\xE6\xAE\x33\xC9\xFC\x07\xCB\x30\x8D\xB3\xB3\xC9\xD2\x0E\xD6\x63\x9C\xCA\x70\x33\x08\x70\x55\x3E\x5C\x41\x4C\xA9\x26\x19\x41\x86\x61\x19\x7F\xAC\x10\x47\x1D\xB1\xD3\x81\x08\x5D\xDA\xDD\xB5\x87\x96\x82\x9C\xA9\x00\x69", 64},
134
        { (unsigned char *) "\x01", 1}
135
    },
136
    {
137
        { (unsigned char *) "\x2B\x81\x04\x00\x1F", 5}, // secp192k1
138
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xEE\x37", 24},
139
        { (unsigned char *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 24},
140
        { (unsigned char *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03", 24},
141
        { (unsigned char *) "\x04\xDB\x4F\xF1\x0E\xC0\x57\xE9\xAE\x26\xB0\x7D\x02\x80\xB7\xF4\x34\x1D\xA5\xD1\xB1\xEA\xE0\x6C\x7D\x9B\x2F\x2F\x6D\x9C\x56\x28\xA7\x84\x41\x63\xD0\x15\xBE\x86\x34\x40\x82\xAA\x88\xD9\x5E\x2F\x9D", 49},
142
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\x26\xF2\xFC\x17\x0F\x69\x46\x6A\x74\xDE\xFD\x8D", 24},
143
        { (unsigned char *) "\x01", 1}
144
    },
145
    {
146
        { (unsigned char *) "\x2B\x81\x04\x00\x0A", 5}, // secp256k1
147
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFC\x2F", 32},
148
        { (unsigned char *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32},
149
        { (unsigned char *) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07", 32},
150
        { (unsigned char *) "\x04\x79\xBE\x66\x7E\xF9\xDC\xBB\xAC\x55\xA0\x62\x95\xCE\x87\x0B\x07\x02\x9B\xFC\xDB\x2D\xCE\x28\xD9\x59\xF2\x81\x5B\x16\xF8\x17\x98\x48\x3A\xDA\x77\x26\xA3\xC4\x65\x5D\xA4\xFB\xFC\x0E\x11\x08\xA8\xFD\x17\xB4\x48\xA6\x85\x54\x19\x9C\x47\xD0\x8F\xFB\x10\xD4\xB8", 65},
151
        { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xBA\xAE\xDC\xE6\xAF\x48\xA0\x3B\xBF\xD2\x5E\x8C\xD0\x36\x41\x41", 32},
152
        { (unsigned char *) "\x01", 1}
153
    },
154
    {
155
        { NULL, 0},
156
        { NULL, 0},
157
        { NULL, 0},
158
        { NULL, 0},
159
        { NULL, 0},
160
        { NULL, 0},
161
        { NULL, 0}
162
    }
163
};
164
165
166
167
144
#define C_ASN1_CVC_PUBKEY_SIZE 10
168
static const struct sc_asn1_entry c_asn1_cvc_pubkey[C_ASN1_CVC_PUBKEY_SIZE] = {
169
  { "publicKeyOID", SC_ASN1_OBJECT, SC_ASN1_UNI | SC_ASN1_OBJECT, 0, NULL, NULL },
170
  { "primeOrModulus", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 1, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
171
  { "coefficientAorExponent", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 2,  SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
172
  { "coefficientB", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 3, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
173
  { "basePointG", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 4, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
174
  { "order", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 5, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
175
  { "publicPoint", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 6, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
176
  { "cofactor", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 7, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
177
  { "modulusSize", SC_ASN1_INTEGER, SC_ASN1_UNI | SC_ASN1_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL },
178
  { NULL, 0, 0, 0, NULL, NULL }
179
};
180
181
144
#define C_ASN1_CVC_BODY_SIZE 5
182
static const struct sc_asn1_entry c_asn1_cvc_body[C_ASN1_CVC_BODY_SIZE] = {
183
  { "certificateProfileIdentifier", SC_ASN1_INTEGER, SC_ASN1_APP | 0x1F29, 0, NULL, NULL },
184
  { "certificationAuthorityReference", SC_ASN1_PRINTABLESTRING, SC_ASN1_APP | 2, SC_ASN1_OPTIONAL, NULL, NULL },
185
  { "publicKey", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F49, 0, NULL, NULL },
186
  { "certificateHolderReference", SC_ASN1_PRINTABLESTRING, SC_ASN1_APP | 0x1F20, 0, NULL, NULL },
187
  { NULL, 0, 0, 0, NULL, NULL }
188
};
189
190
144
#define C_ASN1_CVCERT_SIZE 3
191
static const struct sc_asn1_entry c_asn1_cvcert[C_ASN1_CVCERT_SIZE] = {
192
  { "certificateBody", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F4E, 0, NULL, NULL },
193
  { "signature", SC_ASN1_OCTET_STRING, SC_ASN1_APP | 0x1F37, SC_ASN1_ALLOC, NULL, NULL },
194
  { NULL, 0, 0, 0, NULL, NULL }
195
};
196
197
#define C_ASN1_CVC_SIZE 2
198
static const struct sc_asn1_entry c_asn1_cvc[C_ASN1_CVC_SIZE] = {
199
  { "certificate", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F21, 0, NULL, NULL },
200
  { NULL, 0, 0, 0, NULL, NULL }
201
};
202
203
144
#define C_ASN1_AUTHREQ_SIZE 4
204
static const struct sc_asn1_entry c_asn1_authreq[C_ASN1_AUTHREQ_SIZE] = {
205
  { "certificate", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F21, 0, NULL, NULL },
206
  { "outerCAR", SC_ASN1_PRINTABLESTRING, SC_ASN1_APP | 2, 0, NULL, NULL },
207
  { "signature", SC_ASN1_OCTET_STRING, SC_ASN1_APP | 0x1F37, SC_ASN1_ALLOC, NULL, NULL },
208
  { NULL, 0, 0, 0, NULL, NULL }
209
};
210
211
#define C_ASN1_REQ_SIZE 2
212
static const struct sc_asn1_entry c_asn1_req[C_ASN1_REQ_SIZE] = {
213
  { "authenticatedrequest", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 7, 0, NULL, NULL },
214
  { NULL, 0, 0, 0, NULL, NULL }
215
};
216
217
struct sc_object_id sc_hsm_public_key_oid = {
218
    {1, 3, 6, 1, 4, 1, 24991, 4, 3, 1, -1}
219
};
220
221
#define C_ASN1_SC_HSM_PKA_NEW_SIZE 5
222
static const struct sc_asn1_entry c_asn1_sc_hsm_pka_new_format[C_ASN1_SC_HSM_PKA_NEW_SIZE] = {
223
  { "oid", SC_ASN1_OBJECT, SC_ASN1_TAG_UNIVERSAL | SC_ASN1_TAG_OBJECT, 0, NULL, NULL },
224
  { "dicaCVC", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 1, 0, NULL, NULL },
225
  { "deviceCVC", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 2, 0, NULL, NULL },
226
  { "publicKey", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 3, 0, NULL, NULL },
227
  { NULL, 0, 0, 0, NULL, NULL }
228
};
229
230
#define C_ASN1_SC_HSM_PKA_OLD_SIZE 4
231
static const struct sc_asn1_entry c_asn1_sc_hsm_pka_old_format[C_ASN1_SC_HSM_PKA_OLD_SIZE] = {
232
  { "publicKey", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 7, 0, NULL, NULL },
233
  { "deviceCVCert", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F21, 0, NULL, NULL },
234
  { "dicaCVCert", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F21, 0, NULL, NULL },
235
  { NULL, 0, 0, 0, NULL, NULL }
236
};
237
238
239
static int read_file(sc_pkcs15_card_t * p15card, u8 fid[2],
240
    u8 *efbin, size_t *len, int optional)
241
71
{
242
71
  sc_path_t path;
243
71
  int r;
244
245
71
  sc_path_set(&path, SC_PATH_TYPE_FILE_ID, fid, 2, 0, 0);
246
  /* look this up with our AID */
247
71
  path.aid = sc_hsm_aid;
248
  /* we don't have a pre-known size of the file */
249
71
  path.count = -1;
250
71
  if (!p15card->opts.use_file_cache || !efbin
251
71
      || SC_SUCCESS != sc_pkcs15_read_cached_file(p15card, &path, &efbin, len)) {
252
    /* avoid re-selection of SC-HSM */
253
71
    path.aid.len = 0;
254
71
    r = sc_select_file(p15card->card, &path, NULL);
255
71
    if (r < 0) {
256
17
      sc_log(p15card->card->ctx, "Could not select EF");
257
54
    } else {
258
54
      r = sc_read_binary(p15card->card, 0, efbin, *len, 0);
259
54
    }
260
261
71
    if (r < 0) {
262
22
      sc_log(p15card->card->ctx, "Could not read EF");
263
22
      if (!optional) {
264
0
        return r;
265
0
      }
266
      /* optional files are saved as empty files to avoid card
267
       * transactions. Parsing the file's data will reveal that they were
268
       * missing. */
269
22
      *len = 0;
270
49
    } else {
271
49
      *len = r;
272
49
    }
273
274
71
    if (p15card->opts.use_file_cache) {
275
      /* save this with our AID */
276
0
      path.aid = sc_hsm_aid;
277
0
      sc_pkcs15_cache_file(p15card, &path, efbin, *len);
278
0
    }
279
71
  }
280
281
71
  return SC_SUCCESS;
282
71
}
283
284
static void fixup_cvc_printable_string_lengths(sc_cvc_t *cvc)
285
0
{
286
  /* SC_ASN1_PRINTABLESTRING adds 1 for the null-terminator */
287
0
  if (cvc->chrLen > 0) {
288
0
    cvc->chrLen--;
289
0
  }
290
0
  if (cvc->carLen > 0) {
291
0
    cvc->carLen--;
292
0
  }
293
0
  if (cvc->outerCARLen > 0) {
294
0
    cvc->outerCARLen--;
295
0
  }
296
0
}
297
298
/*
299
 * Sets up asn1_cvcert to point to asn1_cvc_body, asn1_cvc_pubkey, and
300
 * cvc. When sc_asn1_decode is called on asn1_cvcert, it will populate fields
301
 * in cvc.
302
 *
303
 * @param asn1_cvcert: unpopulated array with len matching c_asn1_cvcert
304
 * @param asn1_cvc_cert: unpopulated array with len matching c_asn1_cvc_body
305
 * @param asn1_cvc_pubkey: unpopulated array matching c_asn1_cvc_pubkey
306
 * @param cvc: non NULL cvc struct
307
 */
308
static int sc_pkcs15emu_sc_hsm_format_asn1_cvcert(
309
    struct sc_asn1_entry *asn1_cvcert, size_t asn1_cvcert_len,
310
    struct sc_asn1_entry *asn1_cvc_body, size_t asn1_cvc_body_len,
311
    struct sc_asn1_entry *asn1_cvc_pubkey, size_t asn1_cvc_pubkey_len,
312
    sc_cvc_t *cvc)
313
72
{
314
72
  if ((asn1_cvc_pubkey_len < C_ASN1_CVC_PUBKEY_SIZE) ||
315
72
    (asn1_cvc_body_len < C_ASN1_CVC_BODY_SIZE) ||
316
72
    (asn1_cvcert_len < C_ASN1_CVCERT_SIZE)) {
317
0
    return SC_ERROR_BUFFER_TOO_SMALL;
318
0
  }
319
320
72
  sc_copy_asn1_entry(c_asn1_cvc_pubkey, asn1_cvc_pubkey);
321
72
  sc_copy_asn1_entry(c_asn1_cvc_body, asn1_cvc_body);
322
72
  sc_copy_asn1_entry(c_asn1_cvcert, asn1_cvcert);
323
324
72
  sc_format_asn1_entry(asn1_cvc_pubkey    , &cvc->pukoid, NULL, 0);
325
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 1, &cvc->primeOrModulus, &cvc->primeOrModuluslen, 0);
326
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 2, &cvc->coefficientAorExponent, &cvc->coefficientAorExponentlen, 0);
327
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 3, &cvc->coefficientB, &cvc->coefficientBlen, 0);
328
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 4, &cvc->basePointG, &cvc->basePointGlen, 0);
329
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 5, &cvc->order, &cvc->orderlen, 0);
330
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 6, &cvc->publicPoint, &cvc->publicPointlen, 0);
331
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 7, &cvc->cofactor, &cvc->cofactorlen, 0);
332
72
  sc_format_asn1_entry(asn1_cvc_pubkey + 8, &cvc->modulusSize, NULL, 0);
333
334
72
  sc_format_asn1_entry(asn1_cvc_body    , &cvc->cpi, NULL, 0);
335
72
  cvc->carLen = sizeof(cvc->car);
336
72
  sc_format_asn1_entry(asn1_cvc_body + 1, &cvc->car, &cvc->carLen, 0);
337
72
  sc_format_asn1_entry(asn1_cvc_body + 2, asn1_cvc_pubkey, NULL, 0);
338
72
  cvc->chrLen = sizeof(cvc->chr);
339
72
  sc_format_asn1_entry(asn1_cvc_body + 3, &cvc->chr, &cvc->chrLen, 0);
340
341
72
  sc_format_asn1_entry(asn1_cvcert    , asn1_cvc_body, NULL, 0);
342
72
  sc_format_asn1_entry(asn1_cvcert + 1, &cvc->signature, &cvc->signatureLen, 0);
343
72
  return SC_SUCCESS;
344
72
}
345
346
/*
347
 * Sets up asn1_req to point to asn1_authreq, which points to asn1_cvcert and
348
 * cvc
349
 * When sc_asn1_decode is called on asn1_authreq, it will populate fields
350
 * in cvc and asn1_cvcert
351
 *
352
 * @param asn1_authreq: unpopulated array with len matching c_asn1_req
353
 * @param asn1_authreq: unpopulated array with len matching c_asn1_authreq
354
 * @param asn1_cvcert: already-initialized array matching c_asn1_cvcert
355
 *
356
 */
357
static int sc_pkcs15emu_sc_hsm_format_asn1_req(
358
    struct sc_asn1_entry *asn1_authreq, size_t asn1_authreq_len,
359
    struct sc_asn1_entry *asn1_cvcert,
360
    sc_cvc_t *cvc)
361
72
{
362
72
  if (asn1_authreq_len < C_ASN1_AUTHREQ_SIZE) {
363
0
    return SC_ERROR_BUFFER_TOO_SMALL;
364
0
  }
365
366
72
  sc_copy_asn1_entry(c_asn1_authreq, asn1_authreq);
367
368
72
  sc_format_asn1_entry(asn1_authreq    , asn1_cvcert, NULL, 0);
369
72
  cvc->outerCARLen = sizeof(cvc->outer_car);
370
72
  sc_format_asn1_entry(asn1_authreq + 1, &cvc->outer_car, &cvc->outerCARLen, 0);
371
72
  sc_format_asn1_entry(asn1_authreq + 2, &cvc->outerSignature, &cvc->outerSignatureLen, 0);
372
72
  return SC_SUCCESS;
373
72
}
374
375
/*
376
 * Decode a card verifiable certificate as defined in TR-03110.
377
 */
378
int sc_pkcs15emu_sc_hsm_decode_cvc(sc_pkcs15_card_t * p15card,
379
  const u8 ** buf, size_t *buflen,
380
  sc_cvc_t *cvc)
381
72
{
382
72
  sc_card_t *card = p15card->card;
383
72
  struct sc_asn1_entry asn1_req[C_ASN1_REQ_SIZE];
384
72
  struct sc_asn1_entry asn1_authreq[C_ASN1_AUTHREQ_SIZE];
385
72
  struct sc_asn1_entry asn1_cvc[C_ASN1_CVC_SIZE];
386
72
  struct sc_asn1_entry asn1_cvcert[C_ASN1_CVCERT_SIZE];
387
72
  struct sc_asn1_entry asn1_cvc_body[C_ASN1_CVC_BODY_SIZE];
388
72
  struct sc_asn1_entry asn1_cvc_pubkey[C_ASN1_CVC_PUBKEY_SIZE];
389
72
  unsigned int cla = 0, tag = 0;
390
72
  size_t taglen;
391
72
  const u8 *tbuf;
392
72
  int r;
393
394
72
  memset(cvc, 0, sizeof(*cvc));
395
72
  sc_copy_asn1_entry(c_asn1_req, asn1_req);
396
72
  sc_copy_asn1_entry(c_asn1_cvc, asn1_cvc);
397
398
72
  r = sc_pkcs15emu_sc_hsm_format_asn1_cvcert(
399
72
      asn1_cvcert, C_ASN1_CVCERT_SIZE,
400
72
      asn1_cvc_body, C_ASN1_CVC_BODY_SIZE,
401
72
      asn1_cvc_pubkey, C_ASN1_CVC_PUBKEY_SIZE,
402
72
      cvc);
403
72
  LOG_TEST_RET(card->ctx, r, "sc_asn1_entry array too small");
404
405
72
  sc_format_asn1_entry(asn1_cvc, asn1_cvcert, NULL, 0);
406
407
72
  r = sc_pkcs15emu_sc_hsm_format_asn1_req(
408
72
      asn1_authreq, C_ASN1_AUTHREQ_SIZE,
409
72
      asn1_cvcert, cvc);
410
72
  LOG_TEST_RET(card->ctx, r, "sc_asn1_entry array too small");
411
412
72
  sc_format_asn1_entry(asn1_req, asn1_authreq, NULL, 0);
413
414
/*  sc_asn1_print_tags(*buf, *buflen); */
415
416
72
  tbuf = *buf;
417
72
  r = sc_asn1_read_tag(&tbuf, *buflen, &cla, &tag, &taglen);
418
72
  LOG_TEST_RET(card->ctx, r, "Could not decode card verifiable certificate");
419
420
  /*  Determine if we deal with an authenticated request, plain request or certificate */
421
38
  if ((cla == (SC_ASN1_TAG_APPLICATION|SC_ASN1_TAG_CONSTRUCTED)) && (tag == 7)) {
422
3
    r = sc_asn1_decode(card->ctx, asn1_req, *buf, *buflen, buf, buflen);
423
35
  } else {
424
35
    r = sc_asn1_decode(card->ctx, asn1_cvc, *buf, *buflen, buf, buflen);
425
35
  }
426
427
38
  LOG_TEST_RET(card->ctx, r, "Could not decode card verifiable certificate");
428
429
0
  fixup_cvc_printable_string_lengths(cvc);
430
431
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
432
0
}
433
434
struct sc_asn1_cvc_format {
435
  struct sc_asn1_entry asn1_cvc[C_ASN1_CVC_SIZE];
436
  struct sc_asn1_entry asn1_cvcert[C_ASN1_CVCERT_SIZE];
437
  struct sc_asn1_entry asn1_cvc_body[C_ASN1_CVC_BODY_SIZE];
438
  struct sc_asn1_entry asn1_cvc_pubkey[C_ASN1_CVC_PUBKEY_SIZE];
439
};
440
441
struct sc_asn1_sc_hsm_pka_callback_arg {
442
  sc_context_t *ctx;
443
  struct sc_asn1_entry *next_entry;
444
  sc_cvc_pka_component_t *component;
445
};
446
447
448
struct sc_asn1_sc_hsm_pka_data {
449
  struct sc_asn1_entry asn1_public_key_req[C_ASN1_REQ_SIZE];
450
  struct sc_asn1_entry asn1_public_key_authreq[C_ASN1_AUTHREQ_SIZE];
451
  struct sc_asn1_cvc_format asn1_public_key_cvc;
452
  struct sc_asn1_cvc_format asn1_device_cvc;
453
  struct sc_asn1_cvc_format asn1_dica_cvc;
454
  struct sc_asn1_sc_hsm_pka_callback_arg public_key_req_arg;
455
  struct sc_asn1_sc_hsm_pka_callback_arg device_arg;
456
  struct sc_asn1_sc_hsm_pka_callback_arg dica_arg;
457
};
458
459
struct sc_asn1_sc_hsm_pka_new_format {
460
  struct sc_asn1_entry seq[C_ASN1_SC_HSM_PKA_NEW_SIZE];
461
  struct sc_asn1_sc_hsm_pka_data data;
462
  struct sc_object_id oid;
463
};
464
465
struct sc_asn1_sc_hsm_pka_old_format {
466
  struct sc_asn1_entry seq[C_ASN1_SC_HSM_PKA_OLD_SIZE];
467
  struct sc_asn1_sc_hsm_pka_data data;
468
};
469
470
/*
471
 * Saves the current pointer then continues to decode
472
 */
473
static int sc_asn1_sc_hsm_pka_set_ptr_callback(
474
    sc_context_t *nctx, void *arg, const u8 *obj,
475
    size_t objlen, int depth)
476
0
{
477
0
  struct sc_asn1_sc_hsm_pka_callback_arg *carg = arg;
478
479
0
  carg->component->ptr = obj;
480
0
  carg->component->len = objlen;
481
482
0
  return sc_asn1_decode(carg->ctx, carg->next_entry, obj, objlen, NULL, NULL);
483
0
}
484
485
static int sc_asn1_sc_hsm_pka_data_init(sc_context_t *ctx,
486
  struct sc_asn1_sc_hsm_pka_data *data,
487
  sc_cvc_pka_t *pka)
488
0
{
489
0
  int r;
490
491
0
  data->public_key_req_arg.ctx = ctx;
492
0
  data->device_arg.ctx = ctx;
493
0
  data->dica_arg.ctx = ctx;
494
495
  /* public key info is in an authenticatedrequest (0x67) */
496
0
  r = sc_pkcs15emu_sc_hsm_format_asn1_cvcert(
497
0
      data->asn1_public_key_cvc.asn1_cvcert, C_ASN1_CVCERT_SIZE,
498
0
      data->asn1_public_key_cvc.asn1_cvc_body, C_ASN1_CVC_BODY_SIZE,
499
0
      data->asn1_public_key_cvc.asn1_cvc_pubkey, C_ASN1_CVC_PUBKEY_SIZE,
500
0
      &pka->public_key_req.cvc);
501
0
  LOG_TEST_RET(ctx, r, "sc_asn1_entry too small");
502
503
0
  r = sc_pkcs15emu_sc_hsm_format_asn1_req(
504
0
      data->asn1_public_key_authreq, C_ASN1_AUTHREQ_SIZE,
505
0
      data->asn1_public_key_cvc.asn1_cvcert,
506
0
      &pka->public_key_req.cvc);
507
0
  LOG_TEST_RET(ctx, r, "sc_asn1_entry too small");
508
509
  /*
510
   * insert a callback between req and authreq
511
   * the HSM expects the contents of the 0x67 authenticatedrequest tag (not
512
   * including the 0x67 tag itself)
513
   */
514
0
  sc_copy_asn1_entry(c_asn1_req, data->asn1_public_key_req);
515
0
  data->asn1_public_key_req[0].type = SC_ASN1_CALLBACK;
516
0
  data->public_key_req_arg.component = &pka->public_key_req;
517
0
  data->public_key_req_arg.next_entry = data->asn1_public_key_authreq;
518
0
  sc_format_asn1_entry(data->asn1_public_key_req,
519
0
      sc_asn1_sc_hsm_pka_set_ptr_callback,
520
0
      &data->public_key_req_arg, 0);
521
522
  /* device CVC is a certificate (0x7F21) */
523
0
  r = sc_pkcs15emu_sc_hsm_format_asn1_cvcert(
524
0
      data->asn1_device_cvc.asn1_cvcert, C_ASN1_CVCERT_SIZE,
525
0
      data->asn1_device_cvc.asn1_cvc_body, C_ASN1_CVC_BODY_SIZE,
526
0
      data->asn1_device_cvc.asn1_cvc_pubkey, C_ASN1_CVC_PUBKEY_SIZE,
527
0
      &pka->device.cvc);
528
0
  LOG_TEST_RET(ctx, r, "sc_asn1_entry too small");
529
530
  /*
531
   * insert a callback between asn1_cvc and asn1_cvcert
532
   * the HSM expects the contents of the 0x7F21 CVC tag (not including the
533
   * 0x7F21 tag itself)
534
   */
535
0
  sc_copy_asn1_entry(c_asn1_cvc, data->asn1_device_cvc.asn1_cvc);
536
0
  data->asn1_device_cvc.asn1_cvc[0].type = SC_ASN1_CALLBACK;
537
0
  data->device_arg.component = &pka->device;
538
0
  data->device_arg.next_entry = data->asn1_device_cvc.asn1_cvcert;
539
0
  sc_format_asn1_entry(data->asn1_device_cvc.asn1_cvc,
540
0
      sc_asn1_sc_hsm_pka_set_ptr_callback,
541
0
      &data->device_arg, 0);
542
543
  /* device issuer CA CVC is a certificate (0x7F21) */
544
0
  r = sc_pkcs15emu_sc_hsm_format_asn1_cvcert(
545
0
      data->asn1_dica_cvc.asn1_cvcert, C_ASN1_CVCERT_SIZE,
546
0
      data->asn1_dica_cvc.asn1_cvc_body, C_ASN1_CVC_BODY_SIZE,
547
0
      data->asn1_dica_cvc.asn1_cvc_pubkey, C_ASN1_CVC_PUBKEY_SIZE,
548
0
      &pka->dica.cvc);
549
0
  LOG_TEST_RET(ctx, r, "sc_asn1_entry too small");
550
551
  /*
552
   * insert a callback between asn1_cvc and asn1_cvcert
553
   * the HSM expects the contents of the 0x7F21 CVC tag (not including the
554
   * 0x7F21 tag itself)
555
   */
556
0
  sc_copy_asn1_entry(c_asn1_cvc, data->asn1_dica_cvc.asn1_cvc);
557
0
  data->asn1_dica_cvc.asn1_cvc[0].type = SC_ASN1_CALLBACK;
558
0
  data->dica_arg.component = &pka->dica;
559
0
  data->dica_arg.next_entry = data->asn1_dica_cvc.asn1_cvcert;
560
0
  sc_format_asn1_entry(data->asn1_dica_cvc.asn1_cvc,
561
0
      sc_asn1_sc_hsm_pka_set_ptr_callback,
562
0
      &data->dica_arg, 0);
563
0
  return SC_SUCCESS;
564
0
}
565
566
/*
567
 * For SmartCard HSMs, this is the older format for registering a public key
568
 * for public key authentication.
569
 *
570
 * @param buf: *buf should point to the first tag in the sequence
571
 *
572
 * SEQUENCE (0x30)
573
 *   authenticatedrequest for public key details (0x67)
574
 *   device CVC (0x7F21)
575
 *   device issuer CA CVC (0x7F21)
576
 */
577
static int decode_pka_old_format(sc_pkcs15_card_t *p15card,
578
  const u8 **buf, size_t *buflen,
579
  sc_cvc_pka_t *pka)
580
0
{
581
0
  int r;
582
0
  sc_card_t *card;
583
0
  struct sc_asn1_sc_hsm_pka_old_format *format = NULL;
584
585
0
  card = p15card->card;
586
587
0
  format = calloc(1, sizeof(*format));
588
0
  if (format == NULL) {
589
0
    r = SC_ERROR_OUT_OF_MEMORY;
590
0
    goto err;
591
0
  }
592
593
0
  r = sc_asn1_sc_hsm_pka_data_init(card->ctx, &format->data, pka);
594
0
  LOG_TEST_GOTO_ERR(card->ctx, r, "sc_asn1_entry array too small");
595
596
0
  sc_copy_asn1_entry(c_asn1_sc_hsm_pka_old_format, format->seq);
597
0
  format->seq[0] = format->data.asn1_public_key_req[0];
598
0
  format->seq[1] = format->data.asn1_device_cvc.asn1_cvc[0];
599
0
  format->seq[2] = format->data.asn1_dica_cvc.asn1_cvc[0];
600
601
0
  r = sc_asn1_decode(p15card->card->ctx, format->seq, *buf, *buflen,
602
0
      buf, buflen);
603
0
  LOG_TEST_GOTO_ERR(card->ctx, r,
604
0
    "Could not decode ASN.1 for public key file's old format");
605
606
0
  r = SC_SUCCESS;
607
  /* fall-through */
608
609
0
err:
610
0
  free(format);
611
0
  format = NULL;
612
0
  return r;
613
0
}
614
615
/*
616
 * For SmartCard HSMs, this is the newer format for registering a public key
617
 * for public key authentication.
618
 *
619
 * @param buf: *buf should point to the first tag after the sequence tag
620
 *
621
 * 1.3.6.1.4.1.24991 is the CardContact organization
622
 * The 4.3.1 part is from inspecting their exported public key, but it doesn't
623
 * seem to be publicly registered.
624
 *
625
 * SEQUENCE (0x30)
626
 *   OID (0x6) 1.3.6.1.4.1.24991.4.3.1
627
 *   Application 1 (0x61)
628
 *      device CVC (0x7F21)
629
 *   Application 2 (0x62)
630
 *      device issuer CA CVC (0x7F21)
631
 *   Application 3 (0x63)
632
 *      authenticatedrequest for public key details (0x67)
633
 */
634
static int decode_pka_new_format(sc_pkcs15_card_t *p15card,
635
  const u8 **buf, size_t *buflen,
636
  sc_cvc_pka_t *pka)
637
0
{
638
0
  int r;
639
0
  sc_card_t *card;
640
0
  struct sc_asn1_sc_hsm_pka_new_format *format = NULL;
641
642
0
  card = p15card->card;
643
644
0
  format = calloc(1, sizeof(*format));
645
0
  if (format == NULL) {
646
0
    r = SC_ERROR_OUT_OF_MEMORY;
647
0
    goto err;
648
0
  }
649
650
0
  r = sc_asn1_sc_hsm_pka_data_init(card->ctx, &format->data, pka);
651
0
  LOG_TEST_GOTO_ERR(card->ctx, r, "sc_asn1_entry array too small");
652
653
0
  sc_copy_asn1_entry(c_asn1_sc_hsm_pka_new_format, format->seq);
654
0
  sc_format_asn1_entry(&format->seq[0], &format->oid, NULL, 0);
655
0
  sc_format_asn1_entry(&format->seq[1],
656
0
      format->data.asn1_dica_cvc.asn1_cvc, NULL, 0);
657
0
  sc_format_asn1_entry(&format->seq[2],
658
0
      format->data.asn1_device_cvc.asn1_cvc, NULL, 0);
659
0
  sc_format_asn1_entry(&format->seq[3],
660
0
      format->data.asn1_public_key_req, NULL, 0);
661
662
0
  r = sc_asn1_decode(p15card->card->ctx, format->seq, *buf, *buflen,
663
0
      buf, buflen);
664
0
  LOG_TEST_GOTO_ERR(card->ctx, r,
665
0
    "Could not decode ASN.1 for public key file's new format");
666
667
0
  if (sc_compare_oid(&format->oid, &sc_hsm_public_key_oid) == 0) {
668
    /* sc_dump_oid uses static memory */
669
0
    sc_log(p15card->card->ctx, "OID %s did not match expected value",
670
0
         sc_dump_oid(&format->oid));
671
0
    r = -1;
672
0
    goto err;
673
0
  }
674
675
0
  r = SC_SUCCESS;
676
  /* fall-through */
677
678
0
err:
679
0
  free(format);
680
0
  format = NULL;
681
0
  return r;
682
0
}
683
684
/*
685
 * @param pka: will be overwritten, should be uninitialized or memset to 0
686
 */
687
int sc_pkcs15emu_sc_hsm_decode_pka(sc_pkcs15_card_t *p15card,
688
  const u8 **buf, size_t *buflen,
689
  sc_cvc_pka_t *pka)
690
0
{
691
0
  int r;
692
0
  const u8 *curr;
693
0
  const u8 *peek;
694
0
  unsigned int cla, tag;
695
0
  size_t taglen;
696
0
  size_t currlen;
697
0
  sc_card_t *card;
698
699
0
  memset(pka, 0, sizeof(*pka));
700
701
0
  card = p15card->card;
702
0
  curr = *buf;
703
0
  currlen = *buflen;
704
705
  /* first tag should be sequence */
706
0
  r = sc_asn1_read_tag(&curr, currlen, &cla, &tag, &taglen);
707
0
  LOG_TEST_GOTO_ERR(card->ctx, r,
708
0
      "Could not decode first sequence tag for public key file");
709
0
  currlen = *buflen - (curr - *buf);
710
711
0
  if ((cla != (SC_ASN1_TAG_UNIVERSAL|SC_ASN1_TAG_CONSTRUCTED)) ||
712
0
      (tag != SC_ASN1_TAG_SEQUENCE)) {
713
0
    sc_log(card->ctx,
714
0
         "Expected sequence tag, but got tag %u class 0x%x", tag, cla);
715
0
    r = SC_ERROR_INVALID_ASN1_OBJECT;
716
0
    goto err;
717
0
  }
718
719
  /* next tag is either OID (new format) or 0x67 (old format) */
720
0
  peek = curr;
721
0
  r = sc_asn1_read_tag(&peek, currlen, &cla, &tag, &taglen);
722
0
  LOG_TEST_GOTO_ERR(card->ctx, r,
723
0
      "Could not decode first sequence element tag for public key file");
724
725
0
  if (tag == SC_ASN1_TAG_OBJECT) {
726
    /* OID means it's the new format */
727
0
    r = decode_pka_new_format(p15card, &curr, &currlen, pka);
728
0
    LOG_TEST_GOTO_ERR(card->ctx, r,
729
0
      "Could not decode public key file new format");
730
0
  } else if ((cla == (SC_ASN1_TAG_APPLICATION|SC_ASN1_TAG_CONSTRUCTED)) &&
731
0
        (tag == 7)) {
732
    /*
733
     * if it's authenticatedrequest (Application 7 / 0x67), then attempt
734
     * to parse the old format
735
     */
736
0
    r = decode_pka_old_format(p15card, &curr, &currlen, pka);
737
0
    LOG_TEST_GOTO_ERR(card->ctx, r,
738
0
      "Could not decode authenticatedrequest for public key file");
739
0
  } else {
740
0
    sc_log(card->ctx,
741
0
        "Unexpected tag %u class 0x%x for first element of sequence",
742
0
        tag, cla);
743
0
    r = SC_ERROR_INVALID_ASN1_OBJECT;
744
0
    goto err;
745
0
  }
746
747
0
  fixup_cvc_printable_string_lengths(&pka->public_key_req.cvc);
748
0
  fixup_cvc_printable_string_lengths(&pka->device.cvc);
749
0
  fixup_cvc_printable_string_lengths(&pka->dica.cvc);
750
751
0
  *buf = curr;
752
0
  *buflen = *buflen - (curr - *buf);
753
754
0
  return SC_SUCCESS;
755
756
0
err:
757
0
  sc_pkcs15emu_sc_hsm_free_cvc_pka(pka);
758
0
  return r;
759
0
}
760
761
/*
762
 * Encode a card verifiable certificate as defined in TR-03110.
763
 */
764
int sc_pkcs15emu_sc_hsm_encode_cvc(sc_pkcs15_card_t * p15card,
765
    sc_cvc_t *cvc,
766
    u8 ** buf, size_t *buflen)
767
0
{
768
0
  sc_card_t *card = p15card->card;
769
0
  struct sc_asn1_entry asn1_cvc[C_ASN1_CVC_SIZE];
770
0
  struct sc_asn1_entry asn1_cvcert[C_ASN1_CVCERT_SIZE];
771
0
  struct sc_asn1_entry asn1_cvc_body[C_ASN1_CVC_BODY_SIZE];
772
0
  struct sc_asn1_entry asn1_cvc_pubkey[C_ASN1_CVC_PUBKEY_SIZE];
773
0
  int r;
774
775
0
  sc_copy_asn1_entry(c_asn1_cvc, asn1_cvc);
776
0
  sc_copy_asn1_entry(c_asn1_cvcert, asn1_cvcert);
777
0
  sc_copy_asn1_entry(c_asn1_cvc_body, asn1_cvc_body);
778
0
  sc_copy_asn1_entry(c_asn1_cvc_pubkey, asn1_cvc_pubkey);
779
780
0
  asn1_cvc_pubkey[1].flags = SC_ASN1_OPTIONAL;
781
0
  asn1_cvcert[1].flags = SC_ASN1_OPTIONAL;
782
783
0
  sc_format_asn1_entry(asn1_cvc_pubkey    , &cvc->pukoid, NULL, 1);
784
0
  if (cvc->primeOrModulus && (cvc->primeOrModuluslen > 0)) {
785
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 1, cvc->primeOrModulus, &cvc->primeOrModuluslen, 1);
786
0
  }
787
0
  sc_format_asn1_entry(asn1_cvc_pubkey + 2, cvc->coefficientAorExponent, &cvc->coefficientAorExponentlen, 1);
788
0
  if (cvc->coefficientB && (cvc->coefficientBlen > 0)) {
789
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 3, cvc->coefficientB, &cvc->coefficientBlen, 1);
790
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 4, cvc->basePointG, &cvc->basePointGlen, 1);
791
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 5, cvc->order, &cvc->orderlen, 1);
792
0
    if (cvc->publicPoint && (cvc->publicPointlen > 0)) {
793
0
      sc_format_asn1_entry(asn1_cvc_pubkey + 6, cvc->publicPoint, &cvc->publicPointlen, 1);
794
0
    }
795
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 7, cvc->cofactor, &cvc->cofactorlen, 1);
796
0
  }
797
0
  if (cvc->modulusSize > 0) {
798
0
    sc_format_asn1_entry(asn1_cvc_pubkey + 8, &cvc->modulusSize, NULL, 1);
799
0
  }
800
801
0
  sc_format_asn1_entry(asn1_cvc_body    , &cvc->cpi, NULL, 1);
802
0
  sc_format_asn1_entry(asn1_cvc_body + 1, &cvc->car, &cvc->carLen, 1);
803
0
  sc_format_asn1_entry(asn1_cvc_body + 2, &asn1_cvc_pubkey, NULL, 1);
804
0
  sc_format_asn1_entry(asn1_cvc_body + 3, &cvc->chr, &cvc->chrLen, 1);
805
806
0
  sc_format_asn1_entry(asn1_cvcert    , &asn1_cvc_body, NULL, 1);
807
0
  if (cvc->signature && (cvc->signatureLen > 0)) {
808
0
    sc_format_asn1_entry(asn1_cvcert + 1, cvc->signature, &cvc->signatureLen, 1);
809
0
  }
810
811
0
  sc_format_asn1_entry(asn1_cvc , &asn1_cvcert, NULL, 1);
812
813
0
  r = sc_asn1_encode(card->ctx, asn1_cvc, buf, buflen);
814
0
  LOG_TEST_RET(card->ctx, r, "Could not encode card verifiable certificate");
815
816
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
817
0
}
818
819
820
821
int sc_pkcs15emu_sc_hsm_get_curve(struct ec_curve **curve, u8 *oid, size_t oidlen)
822
0
{
823
0
  int i;
824
825
0
  for (i = 0; curves[i].oid.value; i++) {
826
0
    if ((curves[i].oid.len == oidlen) && !memcmp(curves[i].oid.value, oid, oidlen)) {
827
0
      *curve = &curves[i];
828
0
      return SC_SUCCESS;
829
0
    }
830
0
  }
831
0
  return SC_ERROR_INVALID_DATA;
832
0
}
833
834
835
836
int sc_pkcs15emu_sc_hsm_get_curve_oid(sc_cvc_t *cvc, const struct sc_lv_data **oid)
837
0
{
838
0
  int i;
839
840
0
  for (i = 0; curves[i].oid.value; i++) {
841
0
    if ((curves[i].prime.len == cvc->primeOrModuluslen) && !memcmp(curves[i].prime.value, cvc->primeOrModulus, cvc->primeOrModuluslen)) {
842
0
      *oid = &curves[i].oid;
843
0
      return SC_SUCCESS;
844
0
    }
845
0
  }
846
0
  return SC_ERROR_INVALID_DATA;
847
0
}
848
849
850
851
static int sc_pkcs15emu_sc_hsm_get_rsa_public_key(struct sc_context *ctx, sc_cvc_t *cvc, struct sc_pkcs15_pubkey *pubkey)
852
0
{
853
0
  pubkey->algorithm = SC_ALGORITHM_RSA;
854
855
0
  pubkey->alg_id = (struct sc_algorithm_id *)calloc(1, sizeof(struct sc_algorithm_id));
856
0
  if (!pubkey->alg_id)
857
0
    return SC_ERROR_OUT_OF_MEMORY;
858
859
0
  pubkey->alg_id->algorithm = SC_ALGORITHM_RSA;
860
861
0
  pubkey->u.rsa.modulus.len = cvc->primeOrModuluslen;
862
0
  pubkey->u.rsa.modulus.data  = malloc(pubkey->u.rsa.modulus.len);
863
0
  pubkey->u.rsa.exponent.len  = cvc->coefficientAorExponentlen;
864
0
  pubkey->u.rsa.exponent.data = malloc(pubkey->u.rsa.exponent.len);
865
0
  if (!pubkey->u.rsa.modulus.data || !pubkey->u.rsa.exponent.data) {
866
0
    free(pubkey->u.rsa.modulus.data);
867
0
    free(pubkey->u.rsa.exponent.data);
868
0
    free(pubkey->alg_id);
869
0
    return SC_ERROR_OUT_OF_MEMORY;
870
0
  }
871
872
0
  memcpy(pubkey->u.rsa.exponent.data, cvc->coefficientAorExponent, pubkey->u.rsa.exponent.len);
873
0
  memcpy(pubkey->u.rsa.modulus.data, cvc->primeOrModulus, pubkey->u.rsa.modulus.len);
874
875
0
  return SC_SUCCESS;
876
0
}
877
878
879
880
static int sc_pkcs15emu_sc_hsm_get_ec_public_key(struct sc_context *ctx, sc_cvc_t *cvc, struct sc_pkcs15_pubkey *pubkey)
881
0
{
882
0
  struct sc_ec_parameters *ecp;
883
0
  const struct sc_lv_data *oid;
884
0
  int r;
885
886
0
  pubkey->algorithm = SC_ALGORITHM_EC;
887
888
0
  r = sc_pkcs15emu_sc_hsm_get_curve_oid(cvc, &oid);
889
0
  if (r != SC_SUCCESS)
890
0
    return r;
891
892
0
  ecp = calloc(1, sizeof(struct sc_ec_parameters));
893
0
  if (!ecp)
894
0
    return SC_ERROR_OUT_OF_MEMORY;
895
896
0
  ecp->der.len = oid->len + 2;
897
0
  ecp->der.value = calloc(1, ecp->der.len);
898
0
  if (!ecp->der.value) {
899
0
    free(ecp);
900
0
    return SC_ERROR_OUT_OF_MEMORY;
901
0
  }
902
903
0
  *(ecp->der.value + 0) = 0x06;
904
0
  *(ecp->der.value + 1) = (u8)oid->len;
905
0
  memcpy(ecp->der.value + 2, oid->value, oid->len);
906
0
  ecp->type = 1;    // Named curve
907
908
0
  pubkey->alg_id = (struct sc_algorithm_id *)calloc(1, sizeof(struct sc_algorithm_id));
909
0
  if (!pubkey->alg_id) {
910
0
    free(ecp->der.value);
911
0
    free(ecp);
912
0
    return SC_ERROR_OUT_OF_MEMORY;
913
0
  }
914
915
0
  pubkey->alg_id->algorithm = SC_ALGORITHM_EC;
916
0
  pubkey->alg_id->params = ecp;
917
918
0
  pubkey->u.ec.ecpointQ.value = malloc(cvc->publicPointlen);
919
0
  if (!pubkey->u.ec.ecpointQ.value) {
920
0
    free(pubkey->alg_id);
921
0
    free(ecp->der.value);
922
0
    free(ecp);
923
0
    return SC_ERROR_OUT_OF_MEMORY;
924
0
  }
925
0
  memcpy(pubkey->u.ec.ecpointQ.value, cvc->publicPoint, cvc->publicPointlen);
926
0
  pubkey->u.ec.ecpointQ.len = cvc->publicPointlen;
927
928
0
  pubkey->u.ec.params.der.value = malloc(ecp->der.len);
929
0
  if (!pubkey->u.ec.params.der.value) {
930
0
    free(pubkey->u.ec.ecpointQ.value);
931
0
    free(pubkey->alg_id);
932
0
    free(ecp->der.value);
933
0
    free(ecp);
934
0
    return SC_ERROR_OUT_OF_MEMORY;
935
0
  }
936
0
  memcpy(pubkey->u.ec.params.der.value, ecp->der.value, ecp->der.len);
937
0
  pubkey->u.ec.params.der.len = ecp->der.len;
938
939
  /* FIXME: check return value? */
940
0
  sc_pkcs15_fix_ec_parameters(ctx, &pubkey->u.ec.params);
941
942
0
  return SC_SUCCESS;
943
0
}
944
945
946
947
int sc_pkcs15emu_sc_hsm_get_public_key(struct sc_context *ctx, sc_cvc_t *cvc, struct sc_pkcs15_pubkey *pubkey)
948
0
{
949
0
  if (cvc->publicPoint && cvc->publicPointlen) {
950
0
    return sc_pkcs15emu_sc_hsm_get_ec_public_key(ctx, cvc, pubkey);
951
0
  } else {
952
0
    return sc_pkcs15emu_sc_hsm_get_rsa_public_key(ctx, cvc, pubkey);
953
0
  }
954
0
}
955
956
957
958
void sc_pkcs15emu_sc_hsm_free_cvc(sc_cvc_t *cvc)
959
8
{
960
8
  if (cvc->outerSignature) {
961
0
    free(cvc->outerSignature);
962
0
    cvc->outerSignature = NULL;
963
0
  }
964
8
  if (cvc->signature) {
965
0
    free(cvc->signature);
966
0
    cvc->signature = NULL;
967
0
  }
968
8
  if (cvc->primeOrModulus) {
969
0
    free(cvc->primeOrModulus);
970
0
    cvc->primeOrModulus = NULL;
971
0
  }
972
8
  if (cvc->coefficientAorExponent) {
973
0
    free(cvc->coefficientAorExponent);
974
0
    cvc->coefficientAorExponent = NULL;
975
0
  }
976
8
  if (cvc->coefficientB) {
977
0
    free(cvc->coefficientB);
978
0
    cvc->coefficientB = NULL;
979
0
  }
980
8
  if (cvc->basePointG) {
981
0
    free(cvc->basePointG);
982
0
    cvc->basePointG = NULL;
983
0
  }
984
8
  if (cvc->order) {
985
0
    free(cvc->order);
986
0
    cvc->order = NULL;
987
0
  }
988
8
  if (cvc->publicPoint) {
989
0
    free(cvc->publicPoint);
990
0
    cvc->publicPoint = NULL;
991
0
  }
992
8
  if (cvc->cofactor) {
993
0
    free(cvc->cofactor);
994
0
    cvc->cofactor = NULL;
995
0
  }
996
8
}
997
998
void sc_pkcs15emu_sc_hsm_free_cvc_pka(sc_cvc_pka_t *pka)
999
0
{
1000
0
  sc_pkcs15emu_sc_hsm_free_cvc(&pka->public_key_req.cvc);
1001
0
  sc_pkcs15emu_sc_hsm_free_cvc(&pka->device.cvc);
1002
0
  sc_pkcs15emu_sc_hsm_free_cvc(&pka->dica.cvc);
1003
0
  memset(pka, 0, sizeof(*pka));
1004
0
}
1005
1006
static int sc_pkcs15emu_sc_hsm_add_pubkey(sc_pkcs15_card_t *p15card, u8 *efbin, size_t len, sc_pkcs15_prkey_info_t *key_info, char *label)
1007
0
{
1008
0
  struct sc_context *ctx = p15card->card->ctx;
1009
0
  sc_card_t *card = p15card->card;
1010
0
  sc_pkcs15_pubkey_info_t pubkey_info;
1011
0
  sc_pkcs15_object_t pubkey_obj;
1012
0
  struct sc_pkcs15_pubkey pubkey;
1013
0
  sc_cvc_t cvc;
1014
0
  u8 *cvcpo;
1015
0
  int r;
1016
1017
0
  cvcpo = efbin;
1018
1019
0
  memset(&cvc, 0, sizeof(cvc));
1020
0
  r = sc_pkcs15emu_sc_hsm_decode_cvc(p15card, (const u8 **)&cvcpo, &len, &cvc);
1021
0
  LOG_TEST_RET(ctx, r, "Could decode certificate signing request");
1022
1023
0
  memset(&pubkey, 0, sizeof(pubkey));
1024
0
  r = sc_pkcs15emu_sc_hsm_get_public_key(ctx, &cvc, &pubkey);
1025
0
  LOG_TEST_RET(card->ctx, r, "Could not extract public key");
1026
1027
0
  memset(&pubkey_info, 0, sizeof(pubkey_info));
1028
0
  memset(&pubkey_obj, 0, sizeof(pubkey_obj));
1029
1030
0
  r = sc_pkcs15_encode_pubkey(ctx, &pubkey, &pubkey_obj.content.value, &pubkey_obj.content.len);
1031
0
  if (r != SC_SUCCESS) {
1032
0
    sc_pkcs15_erase_pubkey(&pubkey);
1033
0
    LOG_TEST_RET(ctx, r, "Could not encode public key");
1034
0
  }
1035
0
  r = sc_pkcs15_encode_pubkey(ctx, &pubkey, &pubkey_info.direct.raw.value, &pubkey_info.direct.raw.len);
1036
0
  if (r != SC_SUCCESS) {
1037
0
    sc_pkcs15_erase_pubkey(&pubkey);
1038
0
    LOG_TEST_RET(ctx, r, "Could not encode public key");
1039
0
  }
1040
0
  r = sc_pkcs15_encode_pubkey_as_spki(ctx, &pubkey, &pubkey_info.direct.spki.value, &pubkey_info.direct.spki.len);
1041
0
  if (r != SC_SUCCESS) {
1042
0
    sc_pkcs15_erase_pubkey(&pubkey);
1043
0
    LOG_TEST_RET(ctx, r, "Could not encode public key");
1044
0
  }
1045
1046
0
  pubkey_info.id = key_info->id;
1047
0
  strlcpy(pubkey_obj.label, label, sizeof(pubkey_obj.label));
1048
1049
0
  if (pubkey.algorithm == SC_ALGORITHM_RSA) {
1050
0
    pubkey_info.modulus_length = pubkey.u.rsa.modulus.len << 3;
1051
0
    pubkey_info.usage = SC_PKCS15_PRKEY_USAGE_ENCRYPT|SC_PKCS15_PRKEY_USAGE_VERIFY|SC_PKCS15_PRKEY_USAGE_WRAP;
1052
0
    r = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj, &pubkey_info);
1053
0
  } else {
1054
    /* TODO fix if support of non multiple of 8 curves are added */
1055
0
    pubkey_info.field_length = cvc.primeOrModuluslen << 3;
1056
0
    pubkey_info.usage = SC_PKCS15_PRKEY_USAGE_VERIFY|SC_PKCS15_PRKEY_USAGE_DERIVE;
1057
0
    r = sc_pkcs15emu_add_ec_pubkey(p15card, &pubkey_obj, &pubkey_info);
1058
0
  }
1059
0
  if (r < 0)
1060
0
    free(pubkey_info.direct.spki.value);
1061
0
  LOG_TEST_RET(ctx, r, "Could not add public key");
1062
1063
0
  sc_pkcs15emu_sc_hsm_free_cvc(&cvc);
1064
0
  sc_pkcs15_erase_pubkey(&pubkey);
1065
1066
0
  return SC_SUCCESS;
1067
0
}
1068
1069
1070
1071
/*
1072
 * Add a key and the key description in PKCS#15 format to the framework
1073
 */
1074
0
static int sc_pkcs15emu_sc_hsm_add_prkd(sc_pkcs15_card_t * p15card, u8 keyid) {
1075
1076
0
  sc_card_t *card = p15card->card;
1077
0
  sc_pkcs15_cert_info_t cert_info;
1078
0
  sc_pkcs15_object_t cert_obj;
1079
0
  struct sc_pkcs15_object prkd;
1080
0
  sc_pkcs15_prkey_info_t *key_info;
1081
0
  u8 fid[2];
1082
  /* enough to hold a complete certificate */
1083
0
  u8 efbin[4096];
1084
0
  u8 *ptr;
1085
0
  size_t len;
1086
0
  int r;
1087
1088
0
  if (keyid == 0) {
1089
    // Device authentication key does not have PKCS#15 meta data
1090
0
    return SC_SUCCESS;
1091
0
  }
1092
1093
0
  fid[0] = PRKD_PREFIX;
1094
0
  fid[1] = keyid;
1095
1096
  /* Try to select a related EF containing the PKCS#15 description of the key */
1097
0
  len = sizeof efbin;
1098
0
  r = read_file(p15card, fid, efbin, &len, 1);
1099
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.PRKD");
1100
1101
0
  ptr = efbin;
1102
1103
0
  memset(&prkd, 0, sizeof(prkd));
1104
0
  r = sc_pkcs15_decode_prkdf_entry(p15card, &prkd, (const u8 **)&ptr, &len);
1105
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.PRKD");
1106
1107
  /* All keys require user PIN authentication */
1108
0
  prkd.auth_id.len = 1;
1109
0
  prkd.auth_id.value[0] = 1;
1110
1111
  /*
1112
   * Set private key flag as all keys are private anyway
1113
   */
1114
0
  prkd.flags |= SC_PKCS15_CO_FLAG_PRIVATE;
1115
1116
0
  key_info = (sc_pkcs15_prkey_info_t *)prkd.data;
1117
0
  key_info->key_reference = keyid;
1118
0
  key_info->path.aid.len = 0;
1119
1120
0
  if (prkd.type == SC_PKCS15_TYPE_PRKEY_RSA) {
1121
0
    r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkd, key_info);
1122
0
  } else {
1123
0
    if (key_info->field_length == 528) {
1124
      // Fix a bug for secp521 key generated with OpenSCDP
1125
0
      key_info->field_length = 521;
1126
0
    }
1127
0
    r = sc_pkcs15emu_add_ec_prkey(p15card, &prkd, key_info);
1128
0
  }
1129
1130
0
  LOG_TEST_RET(card->ctx, r, "Could not add private key to framework");
1131
1132
  /* Check if we also have a certificate for the private key */
1133
0
  fid[0] = EE_CERTIFICATE_PREFIX;
1134
1135
0
  len = sizeof efbin;
1136
0
  r = read_file(p15card, fid, efbin, &len, 0);
1137
0
  LOG_TEST_RET(card->ctx, r, "Could not read EF");
1138
1139
0
  if (efbin[0] == 0x67) {   /* Decode CSR and create public key object */
1140
0
    sc_pkcs15emu_sc_hsm_add_pubkey(p15card, efbin, len, key_info, prkd.label);
1141
0
    free(key_info);
1142
0
    return SC_SUCCESS;   /* Ignore any errors */
1143
0
  }
1144
1145
0
  if (efbin[0] != 0x30) {
1146
0
    free(key_info);
1147
0
    return SC_SUCCESS;
1148
0
  }
1149
1150
0
  memset(&cert_info, 0, sizeof(cert_info));
1151
0
  memset(&cert_obj, 0, sizeof(cert_obj));
1152
1153
0
  cert_info.id = key_info->id;
1154
0
  sc_path_set(&cert_info.path, SC_PATH_TYPE_FILE_ID, fid, 2, 0, 0);
1155
0
  cert_info.path.count = -1;
1156
0
  if (p15card->opts.use_file_cache) {
1157
    /* look this up with our AID, which should already be cached from the
1158
     * call to `read_file`. This may have the side effect that OpenSC's
1159
     * caching layer re-selects our applet *if the cached file cannot be
1160
     * found/used* and we may loose the authentication status. We assume
1161
     * that caching works perfectly without this side effect. */
1162
0
    cert_info.path.aid = sc_hsm_aid;
1163
0
  }
1164
1165
0
  strlcpy(cert_obj.label, prkd.label, sizeof(cert_obj.label));
1166
0
  r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
1167
1168
0
  free(key_info);
1169
1170
0
  LOG_TEST_RET(card->ctx, r, "Could not add certificate");
1171
1172
0
  return SC_SUCCESS;
1173
0
}
1174
1175
1176
1177
/*
1178
 * Add a data object and description in PKCS#15 format to the framework
1179
 */
1180
0
static int sc_pkcs15emu_sc_hsm_add_dcod(sc_pkcs15_card_t * p15card, u8 id) {
1181
1182
0
  sc_card_t *card = p15card->card;
1183
0
  sc_pkcs15_data_info_t *data_info;
1184
0
  sc_pkcs15_object_t data_obj;
1185
0
  u8 fid[2];
1186
0
  u8 efbin[512];
1187
0
  const u8 *ptr;
1188
0
  size_t len;
1189
0
  int r;
1190
1191
0
  fid[0] = DCOD_PREFIX;
1192
0
  fid[1] = id;
1193
1194
  /* Try to select a related EF containing the PKCS#15 description of the data */
1195
0
  len = sizeof efbin;
1196
0
  r = read_file(p15card, fid, efbin, &len, 1);
1197
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.DCOD");
1198
1199
0
  ptr = efbin;
1200
1201
0
  memset(&data_obj, 0, sizeof(data_obj));
1202
0
  r = sc_pkcs15_decode_dodf_entry(p15card, &data_obj, &ptr, &len);
1203
0
  LOG_TEST_RET(card->ctx, r, "Could not decode optional EF.DCOD");
1204
1205
0
  data_info = (sc_pkcs15_data_info_t *)data_obj.data;
1206
1207
0
  r = sc_pkcs15emu_add_data_object(p15card, &data_obj, data_info);
1208
1209
0
  LOG_TEST_RET(card->ctx, r, "Could not add data object to framework");
1210
1211
0
  return SC_SUCCESS;
1212
0
}
1213
1214
1215
1216
/*
1217
 * Add a unrelated certificate object and description in PKCS#15 format to the framework
1218
 */
1219
0
static int sc_pkcs15emu_sc_hsm_add_cd(sc_pkcs15_card_t * p15card, u8 id) {
1220
1221
0
  sc_card_t *card = p15card->card;
1222
0
  sc_pkcs15_cert_info_t *cert_info;
1223
0
  sc_pkcs15_object_t obj;
1224
0
  u8 fid[2];
1225
0
  u8 efbin[512];
1226
0
  const u8 *ptr;
1227
0
  size_t len;
1228
0
  int r;
1229
1230
0
  fid[0] = CD_PREFIX;
1231
0
  fid[1] = id;
1232
1233
  /* Try to select a related EF containing the PKCS#15 description of the data */
1234
0
  len = sizeof efbin;
1235
0
  r = read_file(p15card, fid, efbin, &len, 1);
1236
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.CDF");
1237
1238
0
  ptr = efbin;
1239
1240
0
  memset(&obj, 0, sizeof(obj));
1241
0
  r = sc_pkcs15_decode_cdf_entry(p15card, &obj, &ptr, &len);
1242
0
  if (obj.data == NULL && r >= SC_SUCCESS)
1243
0
    r = SC_ERROR_OBJECT_NOT_FOUND;
1244
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.CDF");
1245
1246
0
  cert_info = (sc_pkcs15_cert_info_t *)obj.data;
1247
1248
0
  r = sc_pkcs15emu_add_x509_cert(p15card, &obj, cert_info);
1249
1250
0
  LOG_TEST_RET(card->ctx, r, "Could not add data object to framework");
1251
1252
0
  return SC_SUCCESS;
1253
0
}
1254
1255
1256
1257
static int sc_pkcs15emu_sc_hsm_read_tokeninfo (sc_pkcs15_card_t * p15card)
1258
0
{
1259
0
  sc_card_t *card = p15card->card;
1260
0
  int r;
1261
0
  u8 efbin[512];
1262
0
  size_t len;
1263
1264
0
  LOG_FUNC_CALLED(card->ctx);
1265
1266
  /* Read token info */
1267
0
  len = sizeof efbin;
1268
0
  r = read_file(p15card, (u8 *) "\x2F\x03", efbin, &len, 1);
1269
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.TokenInfo");
1270
1271
0
  r = sc_pkcs15_parse_tokeninfo(card->ctx, p15card->tokeninfo, efbin, len);
1272
0
  LOG_TEST_RET(card->ctx, r, "Skipping optional EF.TokenInfo");
1273
1274
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1275
0
}
1276
1277
1278
1279
/*
1280
 * Initialize PKCS#15 emulation with user PIN, private keys, certificate and data objects
1281
 *
1282
 */
1283
static int sc_pkcs15emu_sc_hsm_init (sc_pkcs15_card_t * p15card)
1284
121
{
1285
121
  sc_card_t *card = p15card->card;
1286
121
  sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
1287
121
  sc_file_t *file = NULL;
1288
121
  sc_path_t path;
1289
121
  u8 filelist[MAX_EXT_APDU_LENGTH];
1290
121
  int filelistlength;
1291
121
  int r, i;
1292
121
  sc_cvc_t devcert;
1293
121
  struct sc_app_info *appinfo;
1294
121
  struct sc_pkcs15_auth_info pin_info;
1295
121
  struct sc_pkcs15_object pin_obj;
1296
121
  struct sc_pin_cmd_data pindata;
1297
121
  u8 efbin[1024];
1298
121
  u8 *ptr;
1299
121
  size_t len;
1300
1301
121
  LOG_FUNC_CALLED(card->ctx);
1302
1303
121
  appinfo = calloc(1, sizeof(struct sc_app_info));
1304
1305
121
  if (appinfo == NULL) {
1306
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
1307
0
  }
1308
1309
121
  appinfo->aid = sc_hsm_aid;
1310
1311
121
  appinfo->ddo.aid = sc_hsm_aid;
1312
121
  p15card->app = appinfo;
1313
1314
121
  sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
1315
121
  r = sc_select_file(card, &path, &file);
1316
121
  LOG_TEST_RET(card->ctx, r, "Could not select SmartCard-HSM application");
1317
1318
71
  p15card->card->version.hw_major = 24; /* JCOP 2.4.1r3 */
1319
71
  p15card->card->version.hw_minor = 13;
1320
71
  if (file && file->prop_attr && file->prop_attr_len >= 2) {
1321
5
    p15card->card->version.fw_major = file->prop_attr[file->prop_attr_len - 2];
1322
5
    p15card->card->version.fw_minor = file->prop_attr[file->prop_attr_len - 1];
1323
5
  }
1324
1325
71
  sc_file_free(file);
1326
1327
  /* Read device certificate to determine serial number */
1328
71
  if (priv->EF_C_DevAut && priv->EF_C_DevAut_len) {
1329
0
    ptr = priv->EF_C_DevAut;
1330
0
    len = priv->EF_C_DevAut_len;
1331
71
  } else {
1332
71
    len = sizeof efbin;
1333
71
    r = read_file(p15card, (u8 *) "\x2F\x02", efbin, &len, 1);
1334
71
    LOG_TEST_RET(card->ctx, r, "Skipping optional EF.C_DevAut");
1335
1336
71
    if (len > 0) {
1337
      /* save EF_C_DevAut for further use */
1338
44
      ptr = realloc(priv->EF_C_DevAut, len);
1339
44
      if (ptr) {
1340
44
        memcpy(ptr, efbin, len);
1341
44
        priv->EF_C_DevAut = ptr;
1342
44
        priv->EF_C_DevAut_len = len;
1343
44
      }
1344
44
    }
1345
1346
71
    ptr = efbin;
1347
71
  }
1348
1349
71
  memset(&devcert, 0 ,sizeof(devcert));
1350
71
  r = sc_pkcs15emu_sc_hsm_decode_cvc(p15card, (const u8 **)&ptr, &len, &devcert);
1351
71
  LOG_TEST_RET(card->ctx, r, "Could not decode EF.C_DevAut");
1352
1353
0
  sc_pkcs15emu_sc_hsm_read_tokeninfo(p15card);
1354
1355
0
  if (p15card->tokeninfo->label == NULL) {
1356
0
    if (p15card->card->type == SC_CARD_TYPE_SC_HSM_GOID
1357
0
        || p15card->card->type == SC_CARD_TYPE_SC_HSM_SOC) {
1358
0
      p15card->tokeninfo->label = strdup("GoID");
1359
0
    } else {
1360
0
      p15card->tokeninfo->label = strdup("SmartCard-HSM");
1361
0
    }
1362
0
    if (p15card->tokeninfo->label == NULL)
1363
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
1364
0
  }
1365
1366
0
  if ((p15card->tokeninfo->manufacturer_id != NULL) && !strcmp("(unknown)", p15card->tokeninfo->manufacturer_id)) {
1367
0
    free(p15card->tokeninfo->manufacturer_id);
1368
0
    p15card->tokeninfo->manufacturer_id = NULL;
1369
0
  }
1370
1371
0
  if (p15card->tokeninfo->manufacturer_id == NULL) {
1372
0
    if (p15card->card->type == SC_CARD_TYPE_SC_HSM_GOID
1373
0
        || p15card->card->type == SC_CARD_TYPE_SC_HSM_SOC) {
1374
0
      p15card->tokeninfo->manufacturer_id = strdup("Bundesdruckerei GmbH");
1375
0
    } else {
1376
0
      p15card->tokeninfo->manufacturer_id = strdup("www.CardContact.de");
1377
0
    }
1378
0
    if (p15card->tokeninfo->manufacturer_id == NULL) {
1379
0
      sc_pkcs15_card_clear(p15card);
1380
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
1381
0
    }
1382
0
  }
1383
1384
0
  appinfo->label = strdup(p15card->tokeninfo->label);
1385
0
  if (appinfo->label == NULL) {
1386
0
    sc_pkcs15_card_clear(p15card);
1387
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
1388
0
  }
1389
1390
0
  len = strnlen(devcert.chr, sizeof devcert.chr);   /* Strip last 5 digit sequence number from CHR */
1391
0
  assert(len >= 8);
1392
0
  len -= 5;
1393
1394
0
  free(p15card->tokeninfo->serial_number);
1395
0
  p15card->tokeninfo->serial_number = calloc(1, len + 1);
1396
0
  if (p15card->tokeninfo->serial_number == NULL) {
1397
0
    sc_pkcs15_card_clear(p15card);
1398
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
1399
0
  }
1400
1401
0
  memcpy(p15card->tokeninfo->serial_number, devcert.chr, len);
1402
0
  *(p15card->tokeninfo->serial_number + len) = 0;
1403
1404
0
  sc_hsm_set_serialnr(card, p15card->tokeninfo->serial_number);
1405
1406
0
  sc_pkcs15emu_sc_hsm_free_cvc(&devcert);
1407
1408
0
  memset(&pin_info, 0, sizeof(pin_info));
1409
0
  memset(&pin_obj, 0, sizeof(pin_obj));
1410
1411
0
  pin_info.auth_id.len = 1;
1412
0
  pin_info.auth_id.value[0] = 1;
1413
0
  pin_info.path.aid = sc_hsm_aid;
1414
0
  pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
1415
0
  pin_info.auth_method = SC_AC_CHV;
1416
0
  pin_info.attrs.pin.reference = 0x81;
1417
0
  pin_info.attrs.pin.flags = SC_PKCS15_PIN_FLAG_LOCAL|SC_PKCS15_PIN_FLAG_INITIALIZED|SC_PKCS15_PIN_FLAG_EXCHANGE_REF_DATA;
1418
0
  pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
1419
0
  pin_info.attrs.pin.min_length = 6;
1420
0
  pin_info.attrs.pin.stored_length = 0;
1421
0
  pin_info.attrs.pin.max_length = 15;
1422
0
  pin_info.attrs.pin.pad_char = '\0';
1423
0
  pin_info.tries_left = 3;
1424
0
  pin_info.max_tries = 3;
1425
1426
0
  pin_obj.auth_id.len = 1;
1427
0
  pin_obj.auth_id.value[0] = 2;
1428
0
  strlcpy(pin_obj.label, "UserPIN", sizeof(pin_obj.label));
1429
0
  pin_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE|SC_PKCS15_CO_FLAG_MODIFIABLE;
1430
1431
0
  pin_obj.data = &pin_info;
1432
1433
0
  r = sc_pkcs15_get_pin_info(p15card, &pin_obj);
1434
1435
0
  if (r != SC_ERROR_DATA_OBJECT_NOT_FOUND) {
1436
0
    if (r < 0) {
1437
0
      sc_pkcs15_card_clear(p15card);
1438
0
      LOG_FUNC_RETURN(card->ctx, r);
1439
0
    }
1440
1441
0
    r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
1442
0
    if (r < 0) {
1443
0
      sc_pkcs15_card_clear(p15card);
1444
0
      LOG_FUNC_RETURN(card->ctx, r);
1445
0
    }
1446
0
  }
1447
1448
0
  memset(&pin_info, 0, sizeof(pin_info));
1449
0
  memset(&pin_obj, 0, sizeof(pin_obj));
1450
1451
0
  pin_info.auth_id.len = 1;
1452
0
  pin_info.auth_id.value[0] = 2;
1453
0
  pin_info.path.aid = sc_hsm_aid;
1454
0
  pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
1455
0
  pin_info.auth_method = SC_AC_CHV;
1456
0
  pin_info.attrs.pin.reference = 0x88;
1457
0
  pin_info.attrs.pin.flags = SC_PKCS15_PIN_FLAG_LOCAL|SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED|SC_PKCS15_PIN_FLAG_SO_PIN;
1458
0
  pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_BCD;
1459
0
  pin_info.attrs.pin.min_length = 16;
1460
0
  pin_info.attrs.pin.stored_length = 0;
1461
0
  pin_info.attrs.pin.max_length = 16;
1462
0
  pin_info.attrs.pin.pad_char = '\0';
1463
0
  pin_info.tries_left = 15;
1464
0
  pin_info.max_tries = 15;
1465
1466
0
  strlcpy(pin_obj.label, "SOPIN", sizeof(pin_obj.label));
1467
0
  pin_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
1468
1469
0
  pin_obj.data = &pin_info;
1470
1471
0
  r = sc_pkcs15_get_pin_info(p15card, &pin_obj);
1472
1473
0
  if (r != SC_ERROR_DATA_OBJECT_NOT_FOUND) {
1474
0
    pin_info.attrs.pin.flags |= SC_PKCS15_PIN_FLAG_INITIALIZED;
1475
0
  } else {
1476
0
    r = SC_SUCCESS;
1477
0
  }
1478
1479
0
  if (r < 0) {
1480
0
    sc_pkcs15_card_clear(p15card);
1481
0
    LOG_FUNC_RETURN(card->ctx, r);
1482
0
  }
1483
1484
0
  r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
1485
0
  if (r < 0) {
1486
0
    sc_pkcs15_card_clear(p15card);
1487
0
    LOG_FUNC_RETURN(card->ctx, r);
1488
0
  }
1489
1490
1491
0
  if (card->type == SC_CARD_TYPE_SC_HSM_SOC
1492
0
      || card->type == SC_CARD_TYPE_SC_HSM_GOID) {
1493
    /* SC-HSM of this type always has a PIN-Pad */
1494
0
    r = SC_SUCCESS;
1495
0
  } else {
1496
0
    memset(&pindata, 0, sizeof(pindata));
1497
0
    pindata.cmd = SC_PIN_CMD_GET_INFO;
1498
0
    pindata.pin_type = SC_AC_CHV;
1499
0
    pindata.pin_reference = 0x85;
1500
1501
0
    r = sc_pin_cmd(card, &pindata, NULL);
1502
0
  }
1503
0
  if (r == SC_ERROR_DATA_OBJECT_NOT_FOUND) {
1504
0
    memset(&pindata, 0, sizeof(pindata));
1505
0
    pindata.cmd = SC_PIN_CMD_GET_INFO;
1506
0
    pindata.pin_type = SC_AC_CHV;
1507
0
    pindata.pin_reference = 0x86;
1508
1509
0
    r = sc_pin_cmd(card, &pindata, NULL);
1510
0
  }
1511
1512
0
  if ((r != SC_ERROR_DATA_OBJECT_NOT_FOUND) && (r != SC_ERROR_INCORRECT_PARAMETERS) && (r != SC_ERROR_REF_DATA_NOT_USABLE))
1513
0
    card->caps |= SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH;
1514
1515
1516
0
  filelistlength = sc_list_files(card, filelist, sizeof(filelist));
1517
0
  if (filelistlength < 0)
1518
0
    sc_pkcs15_card_clear(p15card);
1519
0
  LOG_TEST_RET(card->ctx, filelistlength, "Could not enumerate file and key identifier");
1520
1521
0
  for (i = 0; i < filelistlength; i += 2) {
1522
0
    switch(filelist[i]) {
1523
0
    case KEY_PREFIX:
1524
0
      r = sc_pkcs15emu_sc_hsm_add_prkd(p15card, filelist[i + 1]);
1525
0
      break;
1526
0
    case DCOD_PREFIX:
1527
0
      r = sc_pkcs15emu_sc_hsm_add_dcod(p15card, filelist[i + 1]);
1528
0
      break;
1529
0
    case CD_PREFIX:
1530
0
      r = sc_pkcs15emu_sc_hsm_add_cd(p15card, filelist[i + 1]);
1531
0
      break;
1532
0
    }
1533
0
    if (r != SC_SUCCESS) {
1534
0
      sc_log(card->ctx, "Error %d adding elements to framework", r);
1535
0
    }
1536
0
  }
1537
1538
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1539
0
}
1540
1541
1542
1543
int sc_pkcs15emu_sc_hsm_init_ex(sc_pkcs15_card_t *p15card,
1544
        struct sc_aid *aid)
1545
9.32k
{
1546
9.32k
  if (p15card->card->type != SC_CARD_TYPE_SC_HSM
1547
9.32k
      && p15card->card->type != SC_CARD_TYPE_SC_HSM_SOC
1548
9.32k
      && p15card->card->type != SC_CARD_TYPE_SC_HSM_GOID) {
1549
9.20k
    return SC_ERROR_WRONG_CARD;
1550
9.20k
  }
1551
121
  return sc_pkcs15emu_sc_hsm_init(p15card);
1552
9.32k
}