Skip to content

Commit b0787e8

Browse files
David WoodhouseDavid Woodhouse
David Woodhouse
authored and
David Woodhouse
committed
Serialize to standard PKCS#11 URIs
Signed-off-by: David Woodhouse <[email protected]>
1 parent 7a97605 commit b0787e8

File tree

1 file changed

+95
-89
lines changed

1 file changed

+95
-89
lines changed

lib/pkcs11h-serialization.c

Lines changed: 95 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,99 @@
6161

6262
#if defined(ENABLE_PKCS11H_TOKEN) || defined(ENABLE_PKCS11H_CERTIFICATE)
6363

64+
#define P11_URL_VERBATIM "abcdefghijklmnopqrstuvwxyz" \
65+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
66+
"0123456789_-."
67+
static char hexchars[16] = "0123456789abcdef";
68+
69+
int token_attr_escaped_len(char *attr, size_t attrlen)
70+
{
71+
int len = 0, i;
72+
73+
for (i = 0; i < attrlen; i++) {
74+
if (strchr(P11_URL_VERBATIM, attr[i]))
75+
len++;
76+
else
77+
len += 3;
78+
}
79+
return len;
80+
}
81+
82+
int token_attr_escape(char *uri, char *attr, size_t attrlen)
83+
{
84+
int len = 0, i;
85+
86+
for (i = 0; i < attrlen; i++) {
87+
if (strchr(P11_URL_VERBATIM, attr[i])) {
88+
len++;
89+
*(uri++) = attr[i];
90+
} else {
91+
*(uri++) = '%';
92+
*(uri++) = hexchars[(unsigned char)attr[i] >> 4];
93+
*(uri++) = hexchars[(unsigned char)attr[i] & 0xf];
94+
len += 3;
95+
}
96+
}
97+
return len;
98+
}
99+
100+
CK_RV
101+
generate_pkcs11_uri (
102+
OUT char * const sz,
103+
IN OUT size_t *max,
104+
IN const pkcs11h_certificate_id_t certificate_id,
105+
IN const pkcs11h_token_id_t token_id
106+
) {
107+
size_t _max;
108+
char *p = sz;
109+
110+
_PKCS11H_ASSERT (max!=NULL);
111+
_PKCS11H_ASSERT (token_id!=NULL);
112+
113+
_max = strlen("pkcs11:");
114+
_max += strlen("model=");
115+
_max += token_attr_escaped_len(token_id->model, strlen(token_id->model));
116+
_max += strlen(";manufacturer=");
117+
_max += token_attr_escaped_len(token_id->manufacturerID, strlen(token_id->manufacturerID));
118+
_max += strlen(";serial=");
119+
_max += token_attr_escaped_len(token_id->serialNumber, strlen(token_id->serialNumber));
120+
_max += strlen(";token=");
121+
_max += token_attr_escaped_len(token_id->label, strlen(token_id->label));
122+
if (certificate_id) {
123+
_max += strlen(";id=");
124+
_max += token_attr_escaped_len(certificate_id->attrCKA_ID,
125+
certificate_id->attrCKA_ID_size);
126+
}
127+
_max++; /* Trailing NUL */
128+
129+
if (!sz) {
130+
*max = _max;
131+
return CKR_OK;
132+
}
133+
134+
if (sz && *max < _max)
135+
return CKR_ATTRIBUTE_VALUE_INVALID;
136+
137+
p += sprintf(p, "pkcs11:model=");
138+
p += token_attr_escape(p, token_id->model, strlen(token_id->model));
139+
p += sprintf(p, ";manufacturer=");
140+
p += token_attr_escape(p, token_id->manufacturerID, strlen(token_id->manufacturerID));
141+
p += sprintf(p, ";serial=");
142+
p += token_attr_escape(p, token_id->serialNumber, strlen(token_id->serialNumber));
143+
p += sprintf(p, ";token=");
144+
p += token_attr_escape(p, token_id->label, strlen(token_id->label));
145+
if (certificate_id) {
146+
p += sprintf(p, ";id=");
147+
p += token_attr_escape(p, certificate_id->attrCKA_ID,
148+
certificate_id->attrCKA_ID_size);
149+
}
150+
*(p++) = 0;
151+
152+
*max = _max;
153+
154+
return CKR_OK;
155+
}
156+
64157
CK_RV
65158
pkcs11h_token_serializeTokenId (
66159
OUT char * const sz,
@@ -76,14 +169,6 @@ pkcs11h_token_serializeTokenId (
76169
_PKCS11H_ASSERT (max!=NULL);
77170
_PKCS11H_ASSERT (token_id!=NULL);
78171

79-
{ /* Must be after assert */
80-
sources[0] = token_id->manufacturerID;
81-
sources[1] = token_id->model;
82-
sources[2] = token_id->serialNumber;
83-
sources[3] = token_id->label;
84-
sources[4] = NULL;
85-
}
86-
87172
_PKCS11H_DEBUG (
88173
PKCS11H_LOG_DEBUG2,
89174
"PKCS#11: pkcs11h_token_serializeTokenId entry sz=%p, *max="P_Z", token_id=%p",
@@ -92,51 +177,7 @@ pkcs11h_token_serializeTokenId (
92177
(void *)token_id
93178
);
94179

95-
n = 0;
96-
for (e=0;sources[e] != NULL;e++) {
97-
size_t t;
98-
if (
99-
(rv = _pkcs11h_util_escapeString (
100-
NULL,
101-
sources[e],
102-
&t,
103-
__PKCS11H_SERIALIZE_INVALID_CHARS
104-
)) != CKR_OK
105-
) {
106-
goto cleanup;
107-
}
108-
n+=t;
109-
}
110-
111-
if (sz != NULL) {
112-
if (*max < n) {
113-
rv = CKR_ATTRIBUTE_VALUE_INVALID;
114-
goto cleanup;
115-
}
116-
117-
n = 0;
118-
for (e=0;sources[e] != NULL;e++) {
119-
size_t t = *max-n;
120-
if (
121-
(rv = _pkcs11h_util_escapeString (
122-
sz+n,
123-
sources[e],
124-
&t,
125-
__PKCS11H_SERIALIZE_INVALID_CHARS
126-
)) != CKR_OK
127-
) {
128-
goto cleanup;
129-
}
130-
n+=t;
131-
sz[n-1] = '/';
132-
}
133-
sz[n-1] = '\x0';
134-
}
135-
136-
*max = n;
137-
rv = CKR_OK;
138-
139-
cleanup:
180+
rv = generate_pkcs11_uri(sz, max, NULL, token_id);
140181

141182
_PKCS11H_DEBUG (
142183
PKCS11H_LOG_DEBUG2,
@@ -510,42 +551,7 @@ pkcs11h_certificate_serializeCertificateId (
510551
(void *)certificate_id
511552
);
512553

513-
if (sz != NULL) {
514-
saved_max = n = *max;
515-
}
516-
*max = 0;
517-
518-
if (
519-
(rv = pkcs11h_token_serializeTokenId (
520-
sz,
521-
&n,
522-
certificate_id->token_id
523-
)) != CKR_OK
524-
) {
525-
goto cleanup;
526-
}
527-
528-
_max = n + certificate_id->attrCKA_ID_size*2 + 1;
529-
530-
if (sz != NULL) {
531-
if (saved_max < _max) {
532-
rv = CKR_ATTRIBUTE_VALUE_INVALID;
533-
goto cleanup;
534-
}
535-
536-
sz[n-1] = '/';
537-
rv = _pkcs11h_util_binaryToHex (
538-
sz+n,
539-
saved_max-n,
540-
certificate_id->attrCKA_ID,
541-
certificate_id->attrCKA_ID_size
542-
);
543-
}
544-
545-
*max = _max;
546-
rv = CKR_OK;
547-
548-
cleanup:
554+
rv = generate_pkcs11_uri(sz, max, certificate_id, certificate_id->token_id);
549555

550556
_PKCS11H_DEBUG (
551557
PKCS11H_LOG_DEBUG2,

0 commit comments

Comments
 (0)