1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
|
/*
* Copyright (C) 2011 Collabora Ltd.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Author: Stef Walter <stefw@collabora.co.uk>
*/
#include "config.h"
#define DEBUG_FLAG DEBUG_URI
#include "debug.h"
#include "pkcs11.h"
#include "p11-kit.h"
#include "uri.h"
#include "util.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* SECTION:p11-kit-uri
* @title: URIs
* @short_description: Parsing and formatting PKCS\#11 URIs
*
* PKCS\#11 URIs can be used in configuration files or applications to represent
* PKCS\#11 modules, tokens or objects. An example of a URI might be:
*
* <code><literallayout>
* pkcs11:token=The\%20Software\%20PKCS\#11\%20softtoken;
* manufacturer=Snake\%20Oil,\%20Inc.;serial=;object=my-certificate;
* model=1.0;object-type=cert;id=\%69\%95\%3e\%5c\%f4\%bd\%ec\%91
* </literallayout></code>
*
* You can use p11_kit_uri_parse() to parse such a URI, and p11_kit_uri_format()
* to build one. URIs are represented by the #P11KitUri structure. You can match
* a parsed URI against PKCS\#11 tokens with p11_kit_uri_match_token_info()
* or attributes with p11_kit_uri_match_attributes().
*
* Since URIs can represent different sorts of things, when parsing or formatting
* a URI a 'context' can be used to indicate which sort of URI is expected.
*
* URIs have an <code>unrecognized</code> flag. This flag is set during parsing
* if any parts of the URI are not recognized. This may be because the part is
* from a newer version of the PKCS\#11 spec or because that part was not valid
* inside of the desired context used when parsing.
*/
/**
* P11KitUri:
*
* A structure representing a PKCS\#11 URI. There are no public fields
* visible in this structure. Use the various accessor functions.
*/
/**
* P11KitUriType:
* @P11_KIT_URI_FOR_OBJECT: The URI represents one or more objects
* @P11_KIT_URI_FOR_TOKEN: The URI represents one or more tokens
* @P11_KIT_URI_FOR_MODULE: The URI represents one or more modules
* @P11_KIT_URI_FOR_MODULE_WITH_VERSION: The URI represents a module with
* a specific version.
* @P11_KIT_URI_FOR_OBJECT_ON_TOKEN: The URI represents one or more objects
* that are present on a specific token.
* @P11_KIT_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE: The URI represents one or more
* objects that are present on a specific token, being used with a certain
* module.
* @P11_KIT_URI_FOR_ANY: The URI can represent anything
*
* A PKCS\#11 URI can represent different kinds of things. This flag is used by
* p11_kit_uri_parse() to denote in what context the URI will be used.
*
* The various types can be combined.
*/
/**
* P11KitUriResult:
* @P11_KIT_URI_OK: Success
* @P11_KIT_URI_NO_MEMORY: Memory allocation failed
* @P11_KIT_URI_BAD_SCHEME: The URI had a bad scheme
* @P11_KIT_URI_BAD_ENCODING: The URI had a bad encoding
* @P11_KIT_URI_BAD_SYNTAX: The URI had a bad syntax
* @P11_KIT_URI_BAD_VERSION: The URI contained a bad version number
* @P11_KIT_URI_NOT_FOUND: A requested part of the URI was not found
*
* Error codes returned by various functions. The functions each clearly state
* which error codes they are capable of returning.
*/
/**
* P11_KIT_URI_SCHEME:
*
* String of URI scheme for PKCS\#11 URIs.
*/
/**
* P11_KIT_URI_SCHEME_LEN:
*
* Length of %P11_KIT_URI_SCHEME.
*/
static const CK_ATTRIBUTE_TYPE SUPPORTED_ATTRIBUTE_TYPES[] = {
CKA_CLASS,
CKA_LABEL,
CKA_ID
};
#define NUM_ATTRIBUTE_TYPES \
(sizeof (SUPPORTED_ATTRIBUTE_TYPES) / sizeof (SUPPORTED_ATTRIBUTE_TYPES[0]))
struct p11_kit_uri {
int unrecognized;
CK_INFO module;
CK_TOKEN_INFO token;
CK_ATTRIBUTE attributes[NUM_ATTRIBUTE_TYPES];
CK_ULONG n_attributes;
char *pin_source;
};
const static char HEX_CHARS[] = "0123456789abcdef";
const static char WHITESPACE[] = " \n\r\v";
static int
url_decode (const char *value, const char *end,
unsigned char** output, size_t *length)
{
char *a, *b;
unsigned char *result, *p;
assert (output);
assert (value <= end);
/* String can only get shorter */
result = malloc ((end - value) + 1);
if (!result)
return P11_KIT_URI_NO_MEMORY;
/* Now loop through looking for escapes */
p = result;
while (value != end) {
/*
* A percent sign followed by two hex digits means
* that the digits represent an escaped character.
*/
if (*value == '%') {
value++;
if (value + 2 > end) {
free (result);
return P11_KIT_URI_BAD_ENCODING;
}
a = strchr (HEX_CHARS, tolower (value[0]));
b = strchr (HEX_CHARS, tolower (value[1]));
if (!a || !b) {
free (result);
return P11_KIT_URI_BAD_ENCODING;
}
*p = (a - HEX_CHARS) << 4;
*(p++) |= (b - HEX_CHARS);
value += 2;
/* Ignore whitespace characters */
} else if (strchr (WHITESPACE, *value)) {
value++;
/* A different character */
} else {
*(p++) = *(value++);
}
}
/* Null terminate string, in case its a string */
*p = 0;
if (length)
*length = p - result;
*output = result;
return P11_KIT_URI_OK;
}
static char*
url_encode (const unsigned char *value, const unsigned char *end, size_t *length)
{
char *p;
char *result;
assert (value <= end);
/* Just allocate for worst case */
result = malloc (((end - value) * 3) + 1);
if (!result)
return NULL;
/* Now loop through looking for escapes */
p = result;
while (value != end) {
/* These characters we let through verbatim */
if (*value && (isalnum (*value) || strchr ("_-.", *value) != NULL)) {
*(p++) = *(value++);
/* All others get encoded */
} else {
*(p++) = '%';
*(p++) = HEX_CHARS[((unsigned char)*value) >> 4];
*(p++) = HEX_CHARS[((unsigned char)*value) & 0x0F];
++value;
}
}
*p = 0;
if (length)
*length = p - result;
return result;
}
static char *
key_decode (const char *value, const char *end)
{
size_t length = (end - value);
char *at, *pos;
char *key;
key = malloc (length + 1);
if (key == NULL)
return NULL;
memcpy (key, value, length);
key[length] = '\0';
/* Do we have any whitespace? Strip it out. */
if (strcspn (key, WHITESPACE) != length) {
for (at = key, pos = key; pos != key + length + 1; ++pos) {
if (!strchr (WHITESPACE, *pos))
*(at++) = *pos;
}
*at = '\0';
}
return key;
}
static int
match_struct_string (const unsigned char *inuri, const unsigned char *real,
size_t length)
{
assert (inuri);
assert (real);
assert (length > 0);
/* NULL matches anything */
if (inuri[0] == 0)
return 1;
return memcmp (inuri, real, length) == 0 ? 1 : 0;
}
static int
match_struct_version (CK_VERSION_PTR inuri, CK_VERSION_PTR real)
{
/* This matches anything */
if (inuri->major == (CK_BYTE)-1 && inuri->minor == (CK_BYTE)-1)
return 1;
return memcmp (inuri, real, sizeof (CK_VERSION));
}
/**
* p11_kit_uri_get_module_info:
* @uri: the URI
*
* Get the <code>CK_INFO</code> structure associated with this URI.
*
* If this is a parsed URI, then the fields corresponding to library parts of
* the URI will be filled in. Any library URI parts that were missing will have
* their fields filled with zeros.
*
* If the caller wishes to setup information for building a URI, then relevant
* fields should be filled in. Fields that should not appear as parts in the
* resulting URI should be filled with zeros.
*
* Returns: A pointer to the <code>CK_INFO</code> structure.
*/
CK_INFO_PTR
p11_kit_uri_get_module_info (P11KitUri *uri)
{
assert (uri);
return &uri->module;
}
/**
* p11_kit_uri_match_module_info:
* @uri: the URI
* @info: the structure to match against the URI
*
* Match a <code>CK_INFO</code> structure against the library parts of this URI.
*
* Only the fields of the <code>CK_INFO</code> structure that are valid for use
* in a URI will be matched. A URI part that was not specified in the URI will
* match any value in the structure. If during the URI parsing any unrecognized
* parts were encountered then this match will fail.
*
* Returns: 1 if the URI matches, 0 if not.
*/
int
p11_kit_uri_match_module_info (P11KitUri *uri, CK_INFO_PTR info)
{
assert (uri);
assert (info);
if (uri->unrecognized)
return 0;
return (match_struct_string (uri->module.libraryDescription,
info->libraryDescription,
sizeof (info->libraryDescription)) &&
match_struct_string (uri->module.manufacturerID,
info->manufacturerID,
sizeof (info->manufacturerID)) &&
match_struct_version (&uri->module.libraryVersion,
&info->libraryVersion));
}
/**
* p11_kit_uri_get_token_info:
* @uri: the URI
*
* Get the <code>CK_TOKEN_INFO</code> structure associated with this URI.
*
* If this is a parsed URI, then the fields corresponding to token parts of
* the URI will be filled in. Any token URI parts that were missing will have
* their fields filled with zeros.
*
* If the caller wishes to setup information for building a URI, then relevant
* fields should be filled in. Fields that should not appear as parts in the
* resulting URI should be filled with zeros.
*
* Returns: A pointer to the <code>CK_INFO</code> structure.
*/
CK_TOKEN_INFO_PTR
p11_kit_uri_get_token_info (P11KitUri *uri)
{
assert (uri);
return &uri->token;
}
/**
* p11_kit_uri_match_token_info:
* @uri: the URI
* @token_info: the structure to match against the URI
*
* Match a <code>CK_TOKEN_INFO</code> structure against the token parts of this
* URI.
*
* Only the fields of the <code>CK_TOKEN_INFO</code> structure that are valid
* for use in a URI will be matched. A URI part that was not specified in the
* URI will match any value in the structure. If during the URI parsing any
* unrecognized parts were encountered then this match will fail.
*
* Returns: 1 if the URI matches, 0 if not.
*/
int
p11_kit_uri_match_token_info (P11KitUri *uri, CK_TOKEN_INFO_PTR token_info)
{
assert (uri);
assert (token_info);
if (uri->unrecognized)
return 0;
return (match_struct_string (uri->token.label,
token_info->label,
sizeof (token_info->label)) &&
match_struct_string (uri->token.manufacturerID,
token_info->manufacturerID,
sizeof (token_info->manufacturerID)) &&
match_struct_string (uri->token.model,
token_info->model,
sizeof (token_info->model)) &&
match_struct_string (uri->token.serialNumber,
token_info->serialNumber,
sizeof (token_info->serialNumber)));
}
/**
* p11_kit_uri_get_attribute:
* @uri: The URI
* @attr_type: The attribute type
*
* Get a pointer to an attribute present in this URI.
*
* Returns: A pointer to the attribute, or <code>NULL</code> if not present.
* The attribute is owned by the URI and should not be freed.
*/
CK_ATTRIBUTE_PTR
p11_kit_uri_get_attribute (P11KitUri *uri, CK_ATTRIBUTE_TYPE attr_type)
{
CK_ULONG i;
assert (uri);
for (i = 0; i < uri->n_attributes; i++) {
if (uri->attributes[i].type == attr_type)
return &uri->attributes[i];
}
return NULL;
}
static void
uri_take_attribute (P11KitUri *uri, CK_ATTRIBUTE_PTR attr)
{
CK_ULONG i;
assert (uri);
assert (attr);
/* Replace an attribute already set */
for (i = 0; i < uri->n_attributes; i++) {
if (uri->attributes[i].type == attr->type) {
free (uri->attributes[i].pValue);
memcpy (&uri->attributes[i], attr, sizeof (CK_ATTRIBUTE));
memset (attr, 0, sizeof (CK_ATTRIBUTE));
return;
}
}
/* Add one at the end */
assert (uri->n_attributes < NUM_ATTRIBUTE_TYPES);
memcpy (&uri->attributes[uri->n_attributes], attr, sizeof (CK_ATTRIBUTE));
memset (attr, 0, sizeof (CK_ATTRIBUTE));
uri->n_attributes++;
}
/**
* p11_kit_uri_set_attribute:
* @uri: The URI
* @attr: The attribute to set
*
* Set an attribute on the URI.
*
* Only attributes that map to parts in a PKCS\#11 URI will be accepted.
*
* Returns: %P11_KIT_URI_OK if the attribute was successfully set.
* %P11_KIT_URI_NOT_FOUND if the attribute was not valid for a URI.
* %P11_KIT_URI_NO_MEMORY if allocation failed.
*/
int
p11_kit_uri_set_attribute (P11KitUri *uri, CK_ATTRIBUTE_PTR attr)
{
CK_ATTRIBUTE copy;
CK_ULONG i;
assert (uri);
assert (attr);
/* Make sure the attribute type is valid */
for (i = 0; i < NUM_ATTRIBUTE_TYPES; i++) {
if (SUPPORTED_ATTRIBUTE_TYPES[i] == attr->type)
break;
}
if (i == NUM_ATTRIBUTE_TYPES)
return P11_KIT_URI_NOT_FOUND;
memcpy (©, attr, sizeof (CK_ATTRIBUTE));
/* Duplicate the value */
if (attr->pValue && attr->ulValueLen && attr->ulValueLen != (CK_ULONG)-1) {
copy.pValue = malloc (attr->ulValueLen);
if (!copy.pValue)
return P11_KIT_URI_NO_MEMORY;
memcpy (copy.pValue, attr->pValue, attr->ulValueLen);
}
uri_take_attribute (uri, ©);
return P11_KIT_URI_OK;
}
/**
* p11_kit_uri_clear_attribute:
* @uri: The URI
* @attr_type: The type of the attribute to clear
*
* Clear an attribute on the URI.
*
* Only attributes that map to parts in a PKCS\#11 URI will be accepted.
*
* Returns: %P11_KIT_URI_OK if the attribute was successfully cleared.
* %P11_KIT_URI_NOT_FOUND if the attribute was not valid for a URI.
*/
int
p11_kit_uri_clear_attribute (P11KitUri *uri, CK_ATTRIBUTE_TYPE attr_type)
{
CK_ATTRIBUTE_PTR clear = NULL;
CK_ATTRIBUTE_PTR last;
CK_ULONG i;
assert (uri);
/* Make sure the attribute type is valid */
for (i = 0; i < NUM_ATTRIBUTE_TYPES; i++) {
if (SUPPORTED_ATTRIBUTE_TYPES[i] == attr_type)
break;
}
if (i == NUM_ATTRIBUTE_TYPES)
return P11_KIT_URI_NOT_FOUND;
/* Cleanup the values in the attribute */
for (i = 0; i < uri->n_attributes; i++) {
if (uri->attributes[i].type == attr_type) {
clear = &uri->attributes[i];
free (uri->attributes[i].pValue);
break;
}
}
/* A valid attribute, but not present */
if (clear == NULL)
return P11_KIT_URI_OK;
assert (uri->n_attributes > 0);
uri->n_attributes--;
/* If not the last attribute, then make last take its place */
last = &uri->attributes[uri->n_attributes];
if (clear != last) {
memcpy (clear, last, sizeof (CK_ATTRIBUTE));
clear = last;
}
memset (clear, 0, sizeof (CK_ATTRIBUTE));
return P11_KIT_URI_OK;
}
/**
* p11_kit_uri_get_attribute_types:
* @uri: The URI
* @n_attrs: A location to store the number of attributes returned.
*
* Get the attributes present in this URI. The attributes and values are
* owned by the URI. If the URI is modified, then the attributes that were
* returned from this function will not remain consistent.
*
* Returns: The attributes for this URI. These are owned by the URI.
*/
CK_ATTRIBUTE_PTR
p11_kit_uri_get_attributes (P11KitUri *uri, CK_ULONG_PTR n_attrs)
{
assert (uri);
assert (n_attrs);
*n_attrs = uri->n_attributes;
return uri->attributes;
}
int
p11_kit_uri_set_attributes (P11KitUri *uri, CK_ATTRIBUTE_PTR attrs,
CK_ULONG n_attrs)
{
CK_ULONG i;
int ret;
assert (uri);
p11_kit_uri_clear_attributes (uri);
for (i = 0; i < n_attrs; i++) {
ret = p11_kit_uri_set_attribute (uri, &attrs[i]);
if (ret != P11_KIT_URI_OK && ret != P11_KIT_URI_NOT_FOUND)
return ret;
}
return P11_KIT_URI_OK;
}
void
p11_kit_uri_clear_attributes (P11KitUri *uri)
{
CK_ULONG i;
assert (uri);
for (i = 0; i < uri->n_attributes; i++)
free (uri->attributes[i].pValue);
uri->n_attributes = 0;
}
static int
match_attributes (CK_ATTRIBUTE_PTR one, CK_ATTRIBUTE_PTR two)
{
assert (one);
assert (two);
if (one->type != two->type)
return 0;
if (one->ulValueLen != two->ulValueLen)
return 0;
if (one->pValue == two->pValue)
return 1;
if (!one->pValue || !two->pValue)
return 0;
return memcmp (one->pValue, two->pValue, one->ulValueLen) == 0;
}
/**
* p11_kit_uri_match_attributes:
* @uri: The URI
* @attrs: The attributes to match
* @n_attrs: The number of attributes
*
* Match a attributes against the object parts of this URI.
*
* Only the attributes that are valid for use in a URI will be matched. A URI
* part that was not specified in the URI will match any attribute value. If
* during the URI parsing any unrecognized parts were encountered then this
* match will fail.
*
* Returns: 1 if the URI matches, 0 if not.
*/
int
p11_kit_uri_match_attributes (P11KitUri *uri, CK_ATTRIBUTE_PTR attrs,
CK_ULONG n_attrs)
{
CK_ULONG j;
CK_ULONG i;
assert (uri);
assert (attrs || !n_attrs);
if (uri->unrecognized)
return 0;
for (i = 0; i < uri->n_attributes; i++) {
for (j = 0; j < n_attrs; ++j) {
if (uri->attributes[i].type == attrs[j].type) {
if (!match_attributes (&uri->attributes[i], &attrs[j]))
return 0;
break;
}
}
}
return 1;
}
/**
* p11_kit_uri_set_unrecognized:
* @uri: The URI
* @unrecognized: The new unregognized flag value
*
* Set the unrecognized flag on this URI.
*
* The unrecognized flag is automatically set to 1 when during parsing any part
* of the URI is unrecognized. If the unrecognized flag is set to 1, then
* matching against this URI will always fail.
*/
void
p11_kit_uri_set_unrecognized (P11KitUri *uri, int unrecognized)
{
assert (uri);
uri->unrecognized = unrecognized;
}
/**
* p11_kit_uri_any_unrecognized:
* @uri: The URI
*
* Get the unrecognized flag for this URI.
*
* The unrecognized flag is automatically set to 1 when during parsing any part
* of the URI is unrecognized. If the unrecognized flag is set to 1, then
* matching against this URI will always fail.
*
* Returns: 1 if unrecognized flag is set, 0 otherwise.
*/
int
p11_kit_uri_any_unrecognized (P11KitUri *uri)
{
assert (uri);
return uri->unrecognized;
}
/**
* p11_kit_uri_get_pin_source:
* @uri: The URI
*
* Get the 'pin-source' part of the URI. This is used by some applications to
* lookup a PIN for logging into a PKCS\#11 token.
*
* Returns: The pin-source or %NULL if not present.
*/
const char*
p11_kit_uri_get_pin_source (P11KitUri *uri)
{
assert (uri);
return uri->pin_source;
}
/**
* p11_kit_uri_get_pinfile:
* @uri: The URI
*
* Deprecated: use p11_kit_uri_get_pin_source().
*/
const char*
p11_kit_uri_get_pinfile (P11KitUri *uri)
{
return p11_kit_uri_get_pin_source (uri);
}
/**
* p11_kit_uri_set_pin_source:
* @uri: The URI
* @pin_source: The new pin-source
*
* Set the 'pin-source' part of the URI. This is used by some applications to
* lookup a PIN for logging into a PKCS\#11 token.
*/
void
p11_kit_uri_set_pin_source (P11KitUri *uri, const char *pin_source)
{
assert (uri);
free (uri->pin_source);
uri->pin_source = strdup (pin_source);
}
/**
* p11_kit_uri_set_pinfile:
* @uri: The URI
* @pinfile: The pinfile
*
* Deprecated: use p11_kit_uri_set_pin_source().
*/
void
p11_kit_uri_set_pinfile (P11KitUri *uri, const char *pinfile)
{
p11_kit_uri_set_pin_source (uri, pinfile);
}
/**
* p11_kit_uri_new:
*
* Create a new blank PKCS\#11 URI.
*
* The new URI is in the right state to parse a string into. All relevant fields
* are zeroed out. Formatting this URI will produce a valid but empty URI.
*
* Returns: A newly allocated URI. This should be freed with p11_kit_uri_free().
*/
P11KitUri*
p11_kit_uri_new (void)
{
P11KitUri *uri;
uri = calloc (1, sizeof (P11KitUri));
if (!uri)
return NULL;
/* So that it matches anything */
uri->module.libraryVersion.major = (CK_BYTE)-1;
uri->module.libraryVersion.minor = (CK_BYTE)-1;
return uri;
}
static int
format_raw_string (char **string, size_t *length, int *is_first,
const char *name, const char *value)
{
size_t namelen;
size_t vallen;
/* Not set */
if (!value)
return 1;
namelen = strlen (name);
vallen = strlen (value);
*string = _p11_realloc (*string, *length + namelen + vallen + 3);
if (!*string)
return 0;
if (!*is_first)
(*string)[(*length)++] = ';';
memcpy ((*string) + *length, name, namelen);
*length += namelen;
(*string)[(*length)++] = '=';
memcpy ((*string) + *length, value, vallen);
*length += vallen;
(*string)[*length] = 0;
*is_first = 0;
return 1;
}
static int
format_encode_string (char **string, size_t *length, int *is_first,
const char *name, const unsigned char *value,
size_t n_value)
{
char *encoded;
int ret;
encoded = url_encode (value, value + n_value, NULL);
if (!encoded)
return 0;
ret = format_raw_string (string, length, is_first, name, encoded);
free (encoded);
return ret;
}
static int
format_struct_string (char **string, size_t *length, int *is_first,
const char *name, const unsigned char *value,
size_t value_max)
{
size_t len;
/* Not set */
if (!value[0])
return 1;
len = p11_kit_space_strlen (value, value_max);
return format_encode_string (string, length, is_first, name, value, len);
}
static int
format_attribute_string (char **string, size_t *length, int *is_first,
const char *name, CK_ATTRIBUTE_PTR attr)
{
/* Not set */;
if (attr == NULL)
return 1;
return format_encode_string (string, length, is_first, name,
attr->pValue, attr->ulValueLen);
}
static int
format_attribute_class (char **string, size_t *length, int *is_first,
const char *name, CK_ATTRIBUTE_PTR attr)
{
CK_OBJECT_CLASS klass;
const char *value;
/* Not set */;
if (attr == NULL)
return 1;
klass = *((CK_OBJECT_CLASS*)attr->pValue);
switch (klass) {
case CKO_DATA:
value = "data";
break;
case CKO_SECRET_KEY:
value = "secret-key";
break;
case CKO_CERTIFICATE:
value = "cert";
break;
case CKO_PUBLIC_KEY:
value = "public";
break;
case CKO_PRIVATE_KEY:
value = "private";
break;
default:
return 1;
}
return format_raw_string (string, length, is_first, name, value);
}
static int
format_struct_version (char **string, size_t *length, int *is_first,
const char *name, CK_VERSION_PTR version)
{
char buffer[64];
/* Not set */
if (version->major == (CK_BYTE)-1 && version->minor == (CK_BYTE)-1)
return 1;
snprintf (buffer, sizeof (buffer), "%d.%d",
(int)version->major, (int)version->minor);
return format_raw_string (string, length, is_first, name, buffer);
}
/**
* p11_kit_uri_format:
* @uri: The URI.
* @uri_type: The type of URI that should be produced.
* @string: Location to store a newly allocated string.
*
* Format a PKCS\#11 URI into a string.
*
* Fields which are zeroed out will not be included in the resulting string.
* Attributes which are not present will also not be included.
*
* The uri_type of URI specified limits the different parts of the resulting
* URI. To format a URI containing all possible information use
* %P11_KIT_URI_FOR_ANY
*
* The resulting string should be freed with free().
*
* Returns: %P11_KIT_URI_OK if the URI was formatted successfully.
* %P11_KIT_URI_NO_MEMORY if memory allocation failed.
*/
int
p11_kit_uri_format (P11KitUri *uri, P11KitUriType uri_type, char **string)
{
char *result = NULL;
size_t length = 0;
int is_first = 1;
result = malloc (128);
if (!result)
return P11_KIT_URI_NO_MEMORY;
length = P11_KIT_URI_SCHEME_LEN;
memcpy (result, P11_KIT_URI_SCHEME, length);
result[length] = ':';
result[++length] = 0;
if ((uri_type & P11_KIT_URI_FOR_MODULE) == P11_KIT_URI_FOR_MODULE) {
if (!format_struct_string (&result, &length, &is_first, "library-description",
uri->module.libraryDescription,
sizeof (uri->module.libraryDescription)) ||
!format_struct_string (&result, &length, &is_first, "library-manufacturer",
uri->module.manufacturerID,
sizeof (uri->module.manufacturerID))) {
free (result);
return P11_KIT_URI_NO_MEMORY;
}
}
if ((uri_type & P11_KIT_URI_FOR_MODULE_WITH_VERSION) == P11_KIT_URI_FOR_MODULE_WITH_VERSION) {
if (!format_struct_version (&result, &length, &is_first, "library-version",
&uri->module.libraryVersion)) {
free (result);
return P11_KIT_URI_NO_MEMORY;
}
}
if ((uri_type & P11_KIT_URI_FOR_TOKEN) == P11_KIT_URI_FOR_TOKEN) {
if (!format_struct_string (&result, &length, &is_first, "model",
uri->token.model,
sizeof (uri->token.model)) ||
!format_struct_string (&result, &length, &is_first, "manufacturer",
uri->token.manufacturerID,
sizeof (uri->token.manufacturerID)) ||
!format_struct_string (&result, &length, &is_first, "serial",
uri->token.serialNumber,
sizeof (uri->token.serialNumber)) ||
!format_struct_string (&result, &length, &is_first, "token",
uri->token.label,
sizeof (uri->token.label))) {
free (result);
return P11_KIT_URI_NO_MEMORY;
}
}
if ((uri_type & P11_KIT_URI_FOR_OBJECT) == P11_KIT_URI_FOR_OBJECT) {
if (!format_attribute_string (&result, &length, &is_first, "id",
p11_kit_uri_get_attribute (uri, CKA_ID)) ||
!format_attribute_string (&result, &length, &is_first, "object",
p11_kit_uri_get_attribute (uri, CKA_LABEL))) {
free (result);
return P11_KIT_URI_NO_MEMORY;
}
if (!format_attribute_class (&result, &length, &is_first, "object-type",
p11_kit_uri_get_attribute (uri, CKA_CLASS))) {
free (result);
return P11_KIT_URI_NO_MEMORY;
}
}
if (uri->pin_source) {
format_encode_string (&result, &length, &is_first, "pin-source",
(const unsigned char*)uri->pin_source,
strlen (uri->pin_source));
}
*string = result;
return P11_KIT_URI_OK;
}
static int
parse_string_attribute (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
unsigned char *value;
CK_ATTRIBUTE attr;
size_t length;
int ret;
assert (start <= end);
if (strcmp ("id", name) == 0)
attr.type = CKA_ID;
else if (strcmp ("object", name) == 0)
attr.type = CKA_LABEL;
else
return 0;
ret = url_decode (start, end, &value, &length);
if (ret < 0)
return ret;
attr.pValue = value;
attr.ulValueLen = length;
uri_take_attribute (uri, &attr);
return 1;
}
static int
parse_class_attribute (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
CK_OBJECT_CLASS klass = 0;
CK_ATTRIBUTE attr;
char *value;
assert (start <= end);
if (strcmp ("objecttype", name) != 0 &&
strcmp ("object-type", name) != 0)
return 0;
value = key_decode (start, end);
if (value == NULL)
return P11_KIT_URI_NO_MEMORY;
if (strcmp (value, "cert") == 0)
klass = CKO_CERTIFICATE;
else if (strcmp (value, "public") == 0)
klass = CKO_PUBLIC_KEY;
else if (strcmp (value, "private") == 0)
klass = CKO_PRIVATE_KEY;
else if (strcmp (value, "secretkey") == 0)
klass = CKO_SECRET_KEY;
else if (strcmp (value, "secret-key") == 0)
klass = CKO_SECRET_KEY;
else if (strcmp (value, "data") == 0)
klass = CKO_DATA;
else {
free (value);
uri->unrecognized = 1;
return 1;
}
free (value);
attr.pValue = malloc (sizeof (klass));
if (attr.pValue == NULL)
return P11_KIT_URI_NO_MEMORY;
memcpy (attr.pValue, &klass, sizeof (klass));
attr.ulValueLen = sizeof (klass);
attr.type = CKA_CLASS;
uri_take_attribute (uri, &attr);
return 1;
}
static int
parse_struct_info (unsigned char *where, size_t length, const char *start,
const char *end, P11KitUri *uri)
{
unsigned char *value;
size_t value_length;
int ret;
assert (start <= end);
ret = url_decode (start, end, &value, &value_length);
if (ret < 0)
return ret;
/* Too long, shouldn't match anything */
if (value_length > length) {
free (value);
uri->unrecognized = 1;
return 1;
}
memset (where, ' ', length);
memcpy (where, value, value_length);
free (value);
return 1;
}
static int
parse_token_info (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
unsigned char *where;
size_t length;
assert (start <= end);
if (strcmp (name, "model") == 0) {
where = uri->token.model;
length = sizeof (uri->token.model);
} else if (strcmp (name, "manufacturer") == 0) {
where = uri->token.manufacturerID;
length = sizeof (uri->token.manufacturerID);
} else if (strcmp (name, "serial") == 0) {
where = uri->token.serialNumber;
length = sizeof (uri->token.serialNumber);
} else if (strcmp (name, "token") == 0) {
where = uri->token.label;
length = sizeof (uri->token.label);
} else {
return 0;
}
return parse_struct_info (where, length, start, end, uri);
}
static int
atoin (const char *start, const char *end)
{
int ret = 0;
while (start != end) {
if (strchr (WHITESPACE, *start)) {
start++;
continue;
}
if (*start < '0' || *start > '9')
return -1;
ret *= 10;
ret += (*start - '0');
++start;
}
return ret;
}
static int
parse_struct_version (const char *start, const char *end, CK_VERSION_PTR version)
{
const char *dot;
int val;
assert (start <= end);
dot = memchr (start, '.', end - start);
if (!dot)
dot = end;
if (dot == start)
return P11_KIT_URI_BAD_VERSION;
val = atoin (start, dot);
if (val < 0 || val >= 255)
return P11_KIT_URI_BAD_VERSION;
version->major = (CK_BYTE)val;
version->minor = 0;
if (dot != end) {
if (dot + 1 == end)
return P11_KIT_URI_BAD_VERSION;
val = atoin (dot + 1, end);
if (val < 0 || val >= 255)
return P11_KIT_URI_BAD_VERSION;
version->minor = (CK_BYTE)val;
}
return 1;
}
static int
parse_module_version_info (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
assert (start <= end);
if (strcmp (name, "library-version") == 0)
return parse_struct_version (start, end,
&uri->module.libraryVersion);
return 0;
}
static int
parse_module_info (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
unsigned char *where;
size_t length;
assert (start <= end);
if (strcmp (name, "library-description") == 0) {
where = uri->module.libraryDescription;
length = sizeof (uri->module.libraryDescription);
} else if (strcmp (name, "library-manufacturer") == 0) {
where = uri->module.manufacturerID;
length = sizeof (uri->module.manufacturerID);
} else {
return 0;
}
return parse_struct_info (where, length, start, end, uri);
}
static int
parse_extra_info (const char *name, const char *start, const char *end,
P11KitUri *uri)
{
unsigned char *pin_source;
int ret;
assert (start <= end);
if (strcmp (name, "pinfile") == 0 ||
strcmp (name, "pin-source") == 0) {
ret = url_decode (start, end, &pin_source, NULL);
if (ret < 0)
return ret;
free (uri->pin_source);
uri->pin_source = (char*)pin_source;
return 1;
}
return 0;
}
/**
* p11_kit_uri_parse:
* @string: The string to parse
* @uri_type: The type of URI that is expected
* @uri: The blank URI to parse the values into
*
* Parse a PKCS\#11 URI string.
*
* PKCS\#11 URIs can represent tokens, objects or modules. The uri_type argument
* allows the caller to specify what type of URI is expected and the sorts of
* things the URI should match. %P11_KIT_URI_FOR_ANY can be used to parse a URI
* for any context. It's then up to the caller to make sense of the way that
* it is used.
*
* If the PKCS\#11 URI contains unrecognized URI parts or parts not applicable
* to the specified context, then the unrecognized flag will be set. This will
* prevent the URI from matching using the various match functions.
*
* Returns: %P11_KIT_URI_OK if the URI was parsed successfully.
* %P11_KIT_URI_BAD_SCHEME if this was not a PKCS\#11 URI.
* %P11_KIT_URI_BAD_SYNTAX if the URI syntax was bad.
* %P11_KIT_URI_NO_MEMORY if memory allocation failed.
* %P11_KIT_URI_BAD_VERSION if a version number was bad.
* %P11_KIT_URI_BAD_ENCODING if the URI encoding was invalid.
*/
int
p11_kit_uri_parse (const char *string, P11KitUriType uri_type,
P11KitUri *uri)
{
const char *spos, *epos;
char *key = NULL;
int ret;
int i;
assert (string);
assert (uri);
epos = strchr (string, ':');
if (epos == NULL)
return P11_KIT_URI_BAD_SCHEME;
key = key_decode (string, epos);
ret = strcmp (key, P11_KIT_URI_SCHEME);
free (key);
if (ret != 0)
return P11_KIT_URI_BAD_SCHEME;
string = epos + 1;
ret = -1;
/* Clear everything out */
memset (&uri->module, 0, sizeof (uri->module));
memset (&uri->token, 0, sizeof (uri->token));
for (i = 0; i < uri->n_attributes; ++i) {
free (uri->attributes[i].pValue);
memset (&uri->attributes[i], 0, sizeof (CK_ATTRIBUTE));
}
uri->n_attributes = 0;
uri->module.libraryVersion.major = (CK_BYTE)-1;
uri->module.libraryVersion.minor = (CK_BYTE)-1;
uri->unrecognized = 0;
free (uri->pin_source);
uri->pin_source = NULL;
for (;;) {
spos = strchr (string, ';');
if (spos == NULL) {
spos = string + strlen (string);
assert (*spos == '\0');
if (spos == string)
break;
}
epos = strchr (string, '=');
if (epos == NULL || spos == string || epos == string || epos >= spos)
return P11_KIT_URI_BAD_SYNTAX;
key = key_decode (string, epos);
if (key == NULL)
return P11_KIT_URI_NO_MEMORY;
epos++;
ret = 0;
if ((uri_type & P11_KIT_URI_FOR_OBJECT) == P11_KIT_URI_FOR_OBJECT)
ret = parse_string_attribute (key, epos, spos, uri);
if (ret == 0 && (uri_type & P11_KIT_URI_FOR_OBJECT) == P11_KIT_URI_FOR_OBJECT)
ret = parse_class_attribute (key, epos, spos, uri);
if (ret == 0 && (uri_type & P11_KIT_URI_FOR_TOKEN) == P11_KIT_URI_FOR_TOKEN)
ret = parse_token_info (key, epos, spos, uri);
if (ret == 0 && (uri_type & P11_KIT_URI_FOR_MODULE) == P11_KIT_URI_FOR_MODULE)
ret = parse_module_info (key, epos, spos, uri);
if (ret == 0 && (uri_type & P11_KIT_URI_FOR_MODULE_WITH_VERSION) == P11_KIT_URI_FOR_MODULE_WITH_VERSION)
ret = parse_module_version_info (key, epos, spos, uri);
if (ret == 0)
ret = parse_extra_info (key, epos, spos, uri);
free (key);
if (ret < 0)
return ret;
if (ret == 0)
uri->unrecognized = 1;
if (*spos == '\0')
break;
string = spos + 1;
}
return P11_KIT_URI_OK;
}
/**
* p11_kit_uri_free:
* @uri: The URI
*
* Free a PKCS\#11 URI.
*/
void
p11_kit_uri_free (P11KitUri *uri)
{
int i;
if (!uri)
return;
for (i = 0; i < uri->n_attributes; ++i)
free (uri->attributes[i].pValue);
free (uri->pin_source);
free (uri);
}
/**
* p11_kit_uri_message:
* @code: The error code
*
* Lookup a message for the uri error code. These codes are the P11_KIT_URI_XXX
* error codes that can be returned from p11_kit_uri_parse() or
* p11_kit_uri_format(). As a special case %NULL, will be returned for
* %P11_KIT_URI_OK.
*
* Returns: The message for the error code. This string is owned by the p11-kit
* library.
*/
const char*
p11_kit_uri_message (int code)
{
switch (code) {
case P11_KIT_URI_OK:
return NULL;
case P11_KIT_URI_NO_MEMORY:
return "Out of memory";
case P11_KIT_URI_BAD_SCHEME:
return "URI scheme must be 'pkcs11:'";
case P11_KIT_URI_BAD_ENCODING:
return "URI encoding invalid or corrupted";
case P11_KIT_URI_BAD_SYNTAX:
return "URI syntax is invalid";
case P11_KIT_URI_BAD_VERSION:
return "URI version component is invalid";
case P11_KIT_URI_NOT_FOUND:
return "The URI component was not found";
default:
_p11_debug ("unknown error code: %d", code);
return "Unknown error";
}
}
|