Debian Codegraph @

usr/lib/ep11_stdll/ep11adm.h

raw
1 /*
2 * (C) Copyright IBM Corp. 2012, 2025
3 *
4 * This program is provided under the terms of the Common Public License,
5 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7 * found in the file LICENSE file or at
8 * https://opensource.org/licenses/cpl1.0.php
9 *
10 *----------------------------------------------------------------------
11 * EP11 service mail address: [email protected]
12 *
13 * Use this mail address for Bugs and Comments with the EP11 product.
14 *----------------------------------------------------------------------
15 */
16
17 #if ! defined(__xcpadm_h__)
18 #define __xcpadm_h__
19
20 #if !defined(INT64_MIN)
21 #error "We need 32/64-bit <stdint.h> types, please include before this file."
22 #endif
23
24 #if !defined(XCP_SERIALNR_CHARS)
25 #error "We need <ep11.h> types, please include before this file."
26 #endif
27
28
29 //-------------------------------------
30 // flags common to all functions that have a flag parameter
31 //
32 #define XCP_ADMFL_DOMAIN 1 // query/command targets domain not full card
33 #define XCP_ADMFL_FORCE 0x80 // force an action otherwise canceled
34
35 //-------------------------------------
36 // error codes returned by administrative functions
37 // for common error codes see the XCP_E* defines in ep11.h
38 // if not noted otherwise a function returns XCP_OK for successfully completion
39 // see the header of the functions for more details about the errors
40 //
41 // generic administrative errors
42 #define XCP_ADMERR_MIN ((long)(1UL<<(sizeof(long)*8-1)))
43 /* all error codes > XCP_ADMERR_MIN. */
44 #define XCP_ADMERR_QUERY_FAILED (XCP_ADMERR_MIN+0x101)
45 #define XCP_ADMERR_NOT_ENOUGH_SIGNATURES (XCP_ADMERR_MIN+0x102)
46 #define XCP_ADMERR_SIGNING_CB_FAILED (XCP_ADMERR_MIN+0x103)
47 #define XCP_ADMERR_COMMAND_FAILED (XCP_ADMERR_MIN+0x104)
48 #define XCP_ADMERR_CARD_CPS_QUERY (XCP_ADMERR_MIN+0x105)
49 #define XCP_ADMERR_CMD_UNEXPECTED_RV (XCP_ADMERR_MIN+0x106)
50 #define XCP_ADMERR_COMPLIANCE_MODE_CONFLICT (XCP_ADMERR_MIN+0x107)
51 #define XCP_ADMERR_NEEDS_FORCE (XCP_ADMERR_MIN+0x108)
52 #define XCP_ADMERR_CTR_SIZE_INVALID (XCP_ADMERR_MIN+0x109)
53 // signerinfo related errors
54 #define XCP_ADMERR_SI_HSH_MECH_UNSUPPORTED (XCP_ADMERR_MIN+0x151)
55 #define XCP_ADMERR_SI_SIG_MECH_UNSUPPORTED (XCP_ADMERR_MIN+0x152)
56 #define XCP_ADMERR_SI_OID_MECH_MISMATCH (XCP_ADMERR_MIN+0x153)
57 #define XCP_ADMERR_SI_SIG_EMPTY (XCP_ADMERR_MIN+0x154)
58 #define XCP_ADMERR_SI_SIZE (XCP_ADMERR_MIN+0x155)
59 // recipientinfo related errors
60 #define XCP_ADMERR_RI_ENC_EMPTY (XCP_ADMERR_MIN+0x181)
61 #define XCP_ADMERR_RI_UKM_INVALID (XCP_ADMERR_MIN+0x182)
62 #define XCP_ADMERR_RI_IMPR_INVALID (XCP_ADMERR_MIN+0x183)
63 #define XCP_ADMERR_RI_RSA_ALG_INVALID (XCP_ADMERR_MIN+0x184)
64 #define XCP_ADMERR_RI_KWRAPSIZE_INVALID (XCP_ADMERR_MIN+0x185)
65 #define XCP_ADMERR_RI_ECPUB_INVALID (XCP_ADMERR_MIN+0x186)
66 #define XCP_ADMERR_RI_SKI_INVALID (XCP_ADMERR_MIN+0x187)
67 #define XCP_ADMERR_RI_RSA_OID_INVALID (XCP_ADMERR_MIN+0x188)
68 #define XCP_ADMERR_RI_EC_OID_INVALID (XCP_ADMERR_MIN+0x189)
69 #define XCP_ADMERR_RI_VERSION_INVALID (XCP_ADMERR_MIN+0x18a)
70 #define XCP_ADMERR_RI_KWRAPALG_INVALID (XCP_ADMERR_MIN+0x18b)
71
72
73 #define DOMAIN_MASK_LENGTH XCP_DOMAINS/8 // space for 256 domains
74
75 //-------------------------------------
76 // Key-Part-Holder template
77 // contain credentials of a key-part holder. Those credentials
78 // can be file based and/or smart card based references.
79 struct KPH {
80 const unsigned char *cert; // certificate
81 size_t clen; // certificate length
82 const char *id; // private key
83 const char *pw; // private key passphrase
84 const char *kpfname; // filename of the key-part
85 char scard; // indicates a smart card user
86 char ski_id; // subject key identifier ID
87 int rdr_id; // smart card reader number
88 char kp_id; // key-part ID
89 uint64_t sigmech; // signature mechenism
90 const char *padmode; // padding mode
91 } ;
92
93
94 //-------------------------------------
95 // Export WK Request template (including preamble for import state command)
96 //
97 // requesting 2-of-n keyparts
98 //
99 // append n certificate sections and load with XCP_ADM_IMPORT_STATE command
100 // certificate sections are ASN.1 octet strings starting with a 2-Byte tag
101 // value of 0x1d followed by the four Byte certificate index followed by the
102 // certificate:
103 // 0x04,0x82,0x02,0x32,
104 // 0x00,0x1d,
105 // 0x00,0x00,0x00,0x01,
106 // ...cert data...
107 //
108 static const unsigned char expreqhdr[] = {
109 0x00,0x00,0x00,XCP_FILEID_EXPREQUEST, /* export file id */
110 0x00,0x00,0x00,0x00, /* offset, 0 for full file */
111 0x00,0x00,0x00,0x00, /* length of part following */
112 0x30,0x82,0x00,0x00,
113 } ;
114
115
116 //-------------------------------------
117 // admin response structure
118 typedef struct XCPadmresp {
119 uint32_t fn;
120 uint32_t domain;
121 uint32_t domainInst;
122
123 /* module ID || module instance */
124 unsigned char module[ XCP_SERIALNR_CHARS + XCP_SERIALNR_CHARS ];
125 unsigned char modNr[ XCP_SERIALNR_CHARS ];
126 unsigned char modInst[ XCP_SERIALNR_CHARS ];
127
128 unsigned char tctr[ XCP_ADMCTR_BYTES ]; /* transaction counter */
129
130 CK_RV rv;
131 uint32_t reason;
132
133 // points to original response; NULL if no payload
134 // make sure it's copied if used after releasing response block
135 //
136 const unsigned char *payload;
137 size_t pllen;
138 } *XCPadmresp_t;
139 //
140 #define XCP_ADMRESP_INIT0 { 0,0,0, {0},{0},{0}, {0}, CKR_OK, 0, NULL,0, }
141
142
143 //-------------------------------------
144 // listing of CP modes with their respective sets of control points that are
145 // either required or prohibited
146 //
147 // the CPs that can never be turned on are not included in any compliance
148 // modes prohibited list (see XCP_CPB_ALG_RAW_RSA, XCP_CPB_KEYSZ_BELOW80BIT,
149 // XCP_CPB_KEYSZ_HMAC_ANY, XCP_CPB_SKIP_KEYTESTS and XCP_CPB_ALG_NBSI2009)
150 //
151 static const struct {
152 uint32_t mode;
153 const char *name;
154 unsigned int num_prohibited;
155 XCP_CPbit_t prohibited[ XCP_CPCOUNT ];
156 unsigned int num_required;
157 XCP_CPbit_t required[ XCP_CPCOUNT ];
158 } ep11_cpt_modes[] = {
159 { XCP_ADMS_FIPS2009, "fips2009", // default mode, can't be left
160 0,
161 { },
162 0,
163 { },
164 },
165 { XCP_ADMS_FIPS2011, "fips2011",
166 3,
167 { XCP_CPB_ALG_NFIPS2011, XCP_CPB_KEYSZ_80BIT,
168 XCP_CPB_KEYSZ_RSA65536, },
169 0,
170 { },
171 },
172 { XCP_ADMS_BSI2009, "bsi2009",
173 2,
174 { XCP_CPB_ALG_NBSI2009, XCP_CPB_NON_ATTRBOUND },
175 0,
176 { },
177 },
178 { XCP_ADMS_BSI2011, "bsi2011",
179 3,
180 { XCP_CPB_ALG_NBSI2011, XCP_CPB_NON_ATTRBOUND,
181 XCP_CPB_KEYSZ_80BIT },
182 0,
183 { },
184 },
185 { XCP_ADMS_BSICC2017, "bsicc2017",
186 12,
187 { XCP_CPB_NON_ATTRBOUND, XCP_CPB_RNG_SEED,
188 XCP_CPB_KEYSZ_RSA65536, XCP_CPB_USER_SET_TRUSTED,
189 XCP_CPB_ALG_SKIP_CROSSCHK, XCP_CPB_WRAP_CRYPT_KEYS,
190 XCP_CPB_SIGN_CRYPT_KEYS, XCP_CPB_WRAP_SIGN_KEYS,
191 XCP_CPB_USER_SET_ATTRBOUND, XCP_CPB_ALLOW_PASSPHRASE,
192 XCP_CPB_WRAP_STRONGER_KEY, XCP_CPB_WRAP_WITH_RAW_SPKI },
193 27,
194 { XCP_CPB_SIGN_ASYMM, XCP_CPB_ENCRYPT_SYMM,
195 XCP_CPB_DECRYPT_ASYMM, XCP_CPB_DECRYPT_SYMM,
196 XCP_CPB_WRAP_ASYMM, XCP_CPB_WRAP_SYMM,
197 XCP_CPB_UNWRAP_ASYMM, XCP_CPB_UNWRAP_SYMM,
198 XCP_CPB_KEYGEN_ASYMM, XCP_CPB_KEYGEN_SYMM,
199 XCP_CPB_RETAINKEYS, XCP_CPB_MODIFY_OBJECTS,
200 XCP_CPB_ALG_NBSI2009, XCP_CPB_KEYSZ_80BIT,
201 XCP_CPB_KEYSZ_112BIT, XCP_CPB_KEYSZ_128BIT,
202 XCP_CPB_KEYSZ_192BIT, XCP_CPB_KEYSZ_256BIT,
203 XCP_CPB_ALG_RSA, XCP_CPB_ALG_DSA,
204 XCP_CPB_ALG_EC, XCP_CPB_ALG_EC_BPOOLCRV,
205 XCP_CPB_ALG_EC_NISTCRV, XCP_CPB_ALG_NFIPS2011,
206 XCP_CPB_ALG_NBSI2011, XCP_CPB_ALG_DH,
207 XCP_CPB_DERIVE },
208 },
209 { XCP_ADMS_FIPS2021, "fips2021",
210 17,
211 { XCP_CPB_ALG_NFIPS2011, XCP_CPB_KEYSZ_80BIT,
212 XCP_CPB_KEYSZ_RSA65536,
213 XCP_CPB_ALG_NFIPS2021, XCP_CPB_ALG_EC_25519,
214 XCP_CPB_ALG_PQC, XCP_CPB_BTC,
215 XCP_CPB_ECDSA_OTHER, XCP_CPB_ALLOW_NONSESSION,
216 XCP_CPB_ALG_EC_SECGCRV, XCP_CPB_ALG_EC_BPOOLCRV,
217 XCP_CPB_COMPAT_LEGACY_SHA3, XCP_CPB_DSA_PARAMETER_GEN,
218 XCP_CPB_WRAP_ASYMM, XCP_CPB_UNWRAP_ASYMM,
219 XCP_CPB_ALLOW_LOGIN_PRE_F2021,
220 XCP_CPB_ALG_RSA_OAEP
221 },
222 0,
223 { },
224 },
225 { XCP_ADMS_FIPS2024, "fips2024",
226 18,
227 { XCP_CPB_ALG_NFIPS2011, XCP_CPB_KEYSZ_80BIT,
228 XCP_CPB_KEYSZ_RSA65536,
229 XCP_CPB_ALG_NFIPS2021, XCP_CPB_ALG_EC_25519,
230 XCP_CPB_ALG_PQC, XCP_CPB_BTC,
231 XCP_CPB_ECDSA_OTHER, XCP_CPB_ALLOW_NONSESSION,
232 XCP_CPB_ALG_EC_SECGCRV, XCP_CPB_ALG_EC_BPOOLCRV,
233 XCP_CPB_ALG_NFIPS2024, XCP_CPB_COMPAT_LEGACY_SHA3,
234 XCP_CPB_DSA_PARAMETER_GEN, XCP_CPB_WRAP_ASYMM,
235 XCP_CPB_UNWRAP_ASYMM, XCP_CPB_ALLOW_LOGIN_PRE_F2021,
236 XCP_CPB_ALG_RSA_OAEP
237 },
238 0,
239 { },
240 // XCP_ADMC_ADM_FIPS2021 is not reported here as it is not set with
241 // control points
242 }
243 } ;
244
245 //-------------------------------------
246 // Structure to collect all relevant data for state export/import
247 //
248 // statefname: file name of the serialized state data
249 // restrict_mode: restriction mode
250 // set to 0 -> no restriction (complete state export)
251 // set to 1 -> XCP_STDATA_DOMAIN (domain data only)
252 // set to 2 -> XCP_STDATA_NONSENSITIVE (non sensitive data)
253 // domainmask: mask of domains to export/import
254 //
255 struct STATESAVE {
256 const char *statefname;
257 unsigned int restrict_mode;
258 unsigned char domainmask[DOMAIN_MASK_LENGTH];
259 } ;
260
261
262 //-------------------------------------
263 // callback function prototype definition for library functions
264 // that have to send sign commands but are not fixed on how
265 // to generate the signatures
266 // they take two parameters:
267 // 1. a function pointer to the callback function
268 // 2. a void pointer to an arbitrary data structure that gets directly
269 // pass through to the call back function in the signopts parm
270 //
271 // should return >0 if requested signatures generated successfully
272 // =0 not enough signatures could be generated
273 // <0 anything else fails
274 //
275 typedef long (*xcpa_admin_signs_cb_t)(unsigned char *sigs, size_t slen,
276 const unsigned char *data, size_t dlen,
277 const void *signopts) ;
278
279
280 //-------------------------------------
281 // build a query block to (blk,blen), querying 'fn'
282 // (payload,plen) copied to query block if non-NULL
283 //
284 // *minf used for module ID and transaction counter
285 // ignored for commands where those fields are ignored
286 //
287 // returns written bytecount; size query if blk is NULL
288 // <0 if anything fails
289 //
290 // Possible error return codes:
291 // XCP_ETARGET: a group target is supplied
292 // XCP_ESIZE: the payload is too big, the output buffer is too small or the
293 // size of the query block can not be represented in a single
294 // byte
295 // This restriction will be lifted in the future if a need arises
296 //
297 long xcpa_queryblock(unsigned char *blk, size_t blen,
298 unsigned int fn, target_t domain,
299 const unsigned char *payload, size_t plen) ;
300
301
302 //-------------------------------------
303 // build a query block to (blk,blen), querying 'fn'
304 // (payload,plen) copied to query block if non-NULL
305 //
306 // *minf used for module ID and transaction counter
307 // ignored for commands where those fields are ignored
308 //
309 // returns written bytecount; size query if blk is NULL
310 // <0 if anything fails
311 //
312 // Possible error return codes:
313 // XCP_EARG: an argument is missing
314 // XCP_ESIZE: the output buffer is too small
315 // XCP_ADMERR_CTR_SIZE_INVALID: the transaction counter overflows
316 //
317 long xcpa_cmdblock(unsigned char *blk, size_t blen,
318 unsigned int fn,
319 const struct XCPadmresp *minf,
320 const unsigned char *tctr, /* XCP_ADMCTR_BYTES */
321 const unsigned char *payload, size_t plen) ;
322
323
324 //-------------------------------------
325 // construct the DER encapsulation of cert replacement as follows (blk):
326 // SEQUENCE {
327 // OCTET STRING ski
328 // OCTET STRING cert
329 // }
330 //
331 // returns size of constructed sequence in success case.
332 // <0 if anything fails
333 //
334 // Possible error return codes:
335 // XCP_EARG: an argument is missing
336 // XCP_ESIZE: blen is not big enough
337 //
338 long xcpa_certreplace(unsigned char *blk, size_t blen,
339 const unsigned char *ski, size_t slen,
340 const unsigned char *cert, size_t clen) ;
341
342
343 //-------------------------------------
344 // Queries the current/next WK for the given target
345 //
346 // WK Hash is returned in (*wk, wlen) on success if wk is not NULL
347 //
348 // returns >0 (bytecount) if present
349 // 0 if valid but no current WK
350 // <0 if anything failed
351 //
352 // Possible error return codes:
353 // XCP_ERESPONSE: the domain info request failed
354 // XCP_ADMERR_QUERY_FAILED: the WK query failed
355 // XCP_ESIZE: the output buffer is too small
356 //
357 // Uses xcpa_queryblock() - See function header for possible return codes
358 //
359 long xcpa_query_wk(unsigned char *wk, size_t wlen, int type, target_t target) ;
360
361
362 //-------------------------------------
363 // get current module ID, transaction counter to *blk
364 // performs card or module query, depending on 'is_card'
365 //
366 // copies all fields except 'payload', which will be left NULL
367 //
368 // basic sanity check on 'target' with NULL blk (SNH if target is present)
369 //
370 // flags e.g. domain or card, see XCP_ADMFL_... constants
371 //
372 // returns <0 if anything fails
373 // 0 otherwise
374 //
375 // Possible error return codes:
376 // XCP_ERESPONSE: outer return value is not CKR_OK.
377 // XCP_ADMERR_QUERY_FAILED: inner return value indicated an error. See the
378 // reason code for more information.
379 //
380 // Uses xcpa_queryblock() - See function header for possible return codes
381 //
382 long xcpa_state(struct XCPadmresp *blk, target_t target, unsigned int flags) ;
383
384
385 //-------------------------------------
386 // gets current instance id for domain
387 //
388 // returns <0 if query failed
389 // 0 otherwise, setting *inst if non-NULL
390 //
391 // see xcpa_state() for possible error return values
392 //
393 long xcpa_dom_instance(uint32_t *inst, target_t target) ;
394
395
396 //-------------------------------------
397 // submit query/command block from (req,reqlen), optionally with signature/s
398 // verify that return value (internal or external) matches exprv
399 //
400 // returns >0 if (rsp1,rsp1len) has been written. returns response size
401 // 0 if size query (rsp1 is NULL) which was successful
402 // (setting *rsp1len) if expecting CKR_OK OR
403 // if encountered expected error, and not returning results
404 // <0 otherwise
405 //
406 // points non-NULL rb to response, within (rsp1, rsp1len) if successful
407 // non-NULL rv is updated to retrieved PKCS#11 result
408 //
409 // msg, rb, rv, (rsp2,rsp2len), (sigs,slen) are optional
410 // non-NULL msg is used for annotating debug messages (not viewable by
411 // customers)
412 // (rsp2,rsp2len) contains reason code if provided by backend
413 //
414 // Possible error return codes:
415 // XCP_EARG: missing arguments
416 // XCP_ADMERR_CMD_UNEXPECTED_RV: the outer/inner rv does not match the
417 // expected rv. see *rv for more information
418 // XCP_EINVALID: transport error or invalid formated response
419 // (unexpected)
420 //
421 long xcpa_admin_call(unsigned char *rsp1, size_t *rsp1len,
422 unsigned char *rsp2, size_t *rsp2len,
423 const unsigned char *req, size_t reqlen,
424 const unsigned char *sigs, size_t slen,
425 target_t target,
426 const char *msg,
427 struct XCPadmresp *rb,
428 CK_RV *rv,
429 CK_RV exprv) ;
430
431
432 //-------------------------------------
433 // flags >0 domain, otherwise card
434 //
435 // (*val) set to the requested integer attribute value
436 //
437 // returns 0 with the request int attr for the given target (card/domain)
438 // <0 if something fails
439 //
440 // Possible error return codes:
441 // XCP_EARG: bad arguments
442 //
443 // Uses xcpa_query_admin_attrs() - See function header for possible return codes
444 //
445 long xcpa_admin_attr(target_t target, unsigned int flags,
446 XCP_AdmAttr_t attr, uint32_t *val) ;
447
448
449 //-------------------------------------
450 // flags e.g. domain or card, see XCP_ADMFL_... constants
451 //
452 // sets *mode to the operating mode of card/domain
453 //
454 // returns <0 if query failed
455 // 0 if still imprinting, setting *mode to full word
456 // >0 if out of imprint mode, setting *mode to full word
457 //
458 // See for possible error return codes xcpa_query_admin_attrs()
459 //
460 long xcpa_admin_attr_mode (target_t target, unsigned int flags,
461 uint32_t *mode) ;
462
463
464 //-------------------------------------
465 // flags >0 domain, otherwise card
466 //
467 // Sets *perms to the permissions of card/domain
468 //
469 // returns <0 if query failed
470 // 0 if 1-sign not allowed, setting *perms to full word
471 // >0 if 1-sign allowed, setting *perms to full word
472 //
473 // See for possible error return codes xcpa_query_admin_attrs()
474 //
475 long xcpa_admin_attr_perms (target_t target, unsigned int flags,
476 uint32_t *perms) ;
477
478
479 //-------------------------------------
480 // if *val is non-NULL, and index 'idx' is found, *val is set to its value
481 //
482 // updates a32[] entries which are within bounds, ignoring others
483 //
484 // returns 0 with val and a32 set when non-NULL
485 // <0 if something fails
486 //
487 // Possible error return codes:
488 // XCP_EARG: attrs is NULL or alen is too small
489 //
490 long xcpa_report_attrs_field (const unsigned char *attrs, size_t alen,
491 uint32_t *a32, unsigned int acount,
492 uint32_t idx, uint32_t *val) ;
493
494
495 //-------------------------------------
496 // returns bytecount written to (attrs,alen), which is 8*attribute count
497 // size query with NULL attrs, ignoring alen
498 //
499 // a32[] is updated if non-NULL and has sufficient entries;
500 // fails if a32 is non-NULL but acount is too low
501 // indexes interpreted as one-based (attr indexes intentionally skip 0),
502 // a32[] transformed back to zero-based representation
503 //
504 // *rb updated to current state, if non-NULL
505 //
506 // flags e.g. domain or card, see XCP_ADMFL_... constants
507 //
508 // Possible error return codes:
509 // XCP_ADMERR_QUERY_FAILED: query failed (with reason code in *rb if non NULL)
510 // XCP_EINVALID: response from card could not be correctly parsed
511 //
512 // Uses xcpa_queryblock() - See function header for possible return codes
513 //
514 long xcpa_query_admin_attrs(target_t target,
515 unsigned char *attrs, size_t alen,
516 uint32_t *a32, unsigned int acount,
517 struct XCPadmresp *rb,
518 unsigned int flags) ;
519
520
521 //-------------------------------------
522 // set attributes from pre-formated set-attr field in (attrs,alen)
523 //
524 // (*attrs, attributes to set
525 // alen)
526 // (sign_cb, provide the callback for generating signatures,
527 // signopts) may be NULL if no signatures required,
528 // see xcpl_admin_signs
529 // flags e.g. domain or card, see XCP_ADMFL_... constants
530 // exprv the expected return value to check for
531 //
532 // updates *rb if not NULL
533 //
534 // returns 0 on success (expected RV met)
535 // <0 on failure
536 //
537 // Other possible return codes
538 // XCP_ADMERR_SIGNING_CB_FAILED: signing callback failed
539 // XCP_ADMERR_NOT_ENOUGH_SIGNATURES: not enough signatures to fulfill
540 // threshold value
541 // XCP_ADMERR_CMD_UNEXPECTED_RV: see reason code in *rb if non NULL
542 //
543 // uses xcpa_cmdblock() - see function header for more return codes
544 // uses xcpa_state() if rb is NULL, see the function for possible return codes
545 // uses xcpa_admin_call() - see function header for more return codes
546 //
547 // See rb->reason for the reason code in an error case.
548 //
549 long xcpa_set_admin_attrs(target_t target,
550 unsigned char *attrs, size_t alen,
551 struct XCPadmresp *rb,
552 unsigned int flags,
553 xcpa_admin_signs_cb_t sign_cb, const void *signopts,
554 CK_RV exprv) ;
555
556
557 //-------------------------------------
558 // set single attribute
559 //
560 // (idx, val) attribute index and value to set
561 // (sign_cb,
562 // signopts) provide the callback for generating signatures,
563 // may be NULL if no signatures required,
564 // see xcpl_admin_signs
565 // flags e.g. domain or card, see XCP_ADMFL_... constants
566 // exprv the expected return value to check for
567 //
568 // updates *rb if not NULL
569 //
570 // returns 0 on success (expected RV met)
571 // <0 on failure
572 //
573 // Other possible return codes
574 // XCP_ADMERR_SIGNING_CB_FAILED: signing callback failed
575 // XCP_ADMERR_NOT_ENOUGH_SIGNATURES: not enough signatures to fulfill
576 // threshold value
577 // XCP_ADMERR_CMD_UNEXPECTED_RV: see reason code in *rb if non NULL
578 //
579 // uses xcpa_cmdblock() - see function header for more return codes
580 // uses xcpa_state() if rb is NULL, see the function for possible return codes
581 // uses xcpa_admin_call() - see function header for more return codes
582 //
583 // See rb->reason for the reason code in an error case.
584 //
585 long xcpa_set_admin_attr(target_t target, uint32_t idx, uint32_t val,
586 struct XCPadmresp *rb,
587 unsigned int flags,
588 xcpa_admin_signs_cb_t sign_cb, const void *signopts,
589 CK_RV exprv) ;
590
591
592 //-------------------------------------
593 // returns bytecount written to (cps,cplen)
594 //
595 // *cps is updated if non-NULL and has sufficient size, see XCP_CP_BYTES
596 //
597 // *rb updated to current state, if non-NULL
598 //
599 // flags, must have domain, see XCP_ADMFL_... constants
600 //
601 // returns size of control points, if query was successful
602 // <0 on failure
603 //
604 // uses xcpa_state() if rb is NULL, see the function for possible rc's
605 //
606 // Other possible return codes
607 // XCP_ADMERR_CARD_CPS_QUERY: trying to get card control points which are
608 // not supported
609 // XCP_ADMERR_QUERY_FAILED: the CP query failed (see the reason code)
610 // XCP_ESIZE: cplen is not large enough (cplen <XCP_CP_BYTES)
611 //
612 long xcpa_query_cps(target_t target,
613 unsigned char *cps, size_t cplen,
614 struct XCPadmresp *rb,
615 unsigned int flags) ;
616
617
618 //-------------------------------------
619 // set control points (currently domain only)
620 //
621 // (sign_cb, provide the callback for generating signatures,
622 // signopts) may be NULL if no signatures required,
623 // see xcpl_admin_signs
624 // flags e.g. domain or card, see XCP_ADMFL_... constants
625 // exprv the expected return value to check for
626 //
627 // updates *rb if not NULL (updates reason code in error case)
628 //
629 // returns 0 on success (expected RV met)
630 // <0 on failure
631 //
632 // Other possible return codes
633 // XCP_ADMERR_CARD_CPS_QUERY: trying to get card control points which
634 // are not supported
635 // XCP_ESIZE: cplen is not large enough (cplen
636 // <XCP_CP_BYTES)
637 // XCP_ADMERR_SIGNING_CB_FAILED: signing callback failed
638 // XCP_ADMERR_NOT_ENOUGH_SIGNATURES: not enough signatures to fulfill threshold
639 // value
640 //
641 // uses xcpa_cmdblock() - see function header for more return codes
642 // uses xcpa_state() if rb is NULL, see the function for possible return codes
643 // uses xcpa_admin_call() - see function header for more return codes
644 //
645 // See rb->reason for the reason code in an error case.
646 //
647 long xcpa_set_cps(target_t target,
648 const unsigned char *cps, /* XCP_CP_BYTES */
649 struct XCPadmresp *rb,
650 unsigned int flags,
651 xcpa_admin_signs_cb_t sign_cb, const void *signopts,
652 CK_RV exprv) ;
653
654
655 //-------------------------------------
656 // get compliance mode from CP set (see ep11_cpt_modes[] for possible compliance
657 // modes)
658 // can not check for administrative compliance modes
659 //
660 // cps CP set of XCP_CP_BYTES length, see xcpa_query_cps
661 //
662 // returns >0 compliance mode (see XCP_ADMS_...)
663 //
664 // does not verify CP set
665 //
666 uint32_t xcpa_cps2compliance(const unsigned char *cps /* XCP_CP_BYTES */) ;
667
668
669 //-------------------------------------
670 // set compliance mode (domain only)
671 //
672 // mode compliance mode(s) to set (see XCP_ADMS_...)
673 // (sign_cb,
674 // signopts) provide the callback for generating signatures,
675 // may be NULL if no signatures required,
676 // see xcpl_admin_signs
677 // flags e.g. domain or card, see XCP_ADMFL_... constants
678 // exprv the expected return value to check for
679 //
680 // returns 0 on success
681 // <0 on failure
682 //
683 // See xcpa_query_cps() and xcpa_set_cps() for possible error return codes
684 //
685 // updates *rb if not NULL
686 //
687 long xcpa_set_compliance(target_t target, uint64_t mode,
688 struct XCPadmresp *rb,
689 unsigned int flags,
690 xcpa_admin_signs_cb_t sign_cb, const void *signopts) ;
691
692
693 //--------------------------------------
694 // is bit number bitidx set in big-endian bitmask?
695 // returns 0/1, not in-bitmask power-of-two value
696 //
697 // bitidx is zero-based; 0 -> 0x80 of first byte, 1 -> 0x40 of first byte etc.
698 //
699 // tolerates missing/too-small bitmask, which does not contain any set bits
700 //
701 static inline
702 int xcpa_bitmask_has_bit(const unsigned char *bm, size_t bmbytes,
703 const unsigned int bitidx)
704 {
705 return (bm && bmbytes && (bmbytes > bitidx / 8))
706 ? !!(bm[ bitidx /8 ] & ((unsigned char) 0x80 >> (bitidx %8)))
707 : 0;
708 }
709
710
711 //--------------------------------------
712 // set bit number bitidx in big-endian bitmask to 0 (set==0) or 1 (set!=0)
713 // returns 0/1, not in-bitmask/bit set
714 //
715 // bitidx is zero-based; 0 -> 0x80 of first byte, 1 -> 0x40 of first byte etc.
716 //
717 // tolerates missing/too-small bitmask
718 //
719 static inline
720 int xcpa_bitmask_set_bit(unsigned char *bm, size_t bmbytes,
721 const unsigned int bitidx,
722 const unsigned int set)
723 {
724 if (!bm || !bmbytes || (bmbytes <= bitidx / 8))
725 return 0;
726
727 if (set)
728 bm[ bitidx /8 ] |= ((unsigned char) 0x80 >> (bitidx %8));
729 else
730 bm[ bitidx /8 ] &= ~((unsigned char) 0x80 >> (bitidx %8));
731
732 return 1;
733 }
734
735
736 //-------------------------------------
737 // test if specified CPbit is set
738 //
739 // returns 0/1, not in-bitmask power-of-two value
740 // bitidx is zero-based; 0 -> 0x80 of first byte, 1 -> 0x40 of first byte etc.
741 //
742 // tolerates missing/too-small bitmask, which does not contain any set bits
743 //
744 static inline int xcpa_cpb_is_set(const unsigned char cps[ XCP_CP_BYTES ],
745 const unsigned int cpbit)
746 {
747 return xcpa_bitmask_has_bit(cps, XCP_CP_BYTES, cpbit);
748 }
749
750
751 //-------------------------------------
752 // enables a specific CP for the given CPs field,
753 // do nothing if cps is NULL or cpbit greater than XCP_CPBITS_MAX
754 //
755 static inline void xcpa_cpb_add(unsigned char cps[ XCP_CP_BYTES ],
756 const unsigned int cpbit)
757 {
758 if ((NULL != cps) && (XCP_CPBITS_MAX >= cpbit))
759 cps[ cpbit /8 ] |= (unsigned char) (0x80 >> (cpbit %8));
760 }
761
762
763 //-------------------------------------
764 // enable a specific CP for the given CPs field,
765 // do nothing if cps is NULL or cpbit greater than XCP_CPBITS_MAX
766 //
767 static inline void xcpa_cpb_del(unsigned char cps[ XCP_CP_BYTES ],
768 const unsigned int cpbit)
769 {
770 if ((NULL != cps) && (XCP_CPBITS_MAX >= cpbit))
771 cps[ cpbit /8 ] &= ~((unsigned char) (0x80 >> (cpbit %8)));
772 }
773
774
775 //-------------------------------------
776 // call with full-sized CP bitmask (XCP_CP_BYTES)
777 // returns 0/1, not bit position
778 //
779 #define CPB_IS_SET(cps, cpbit) xcpa_cpb_is_set((cps), (cpbit))
780
781 // call with full-sized CP bitmask (XCP_CP_BYTES)
782 // returns 0/1
783 //
784 #define CPB_ADD(cps, cpbit) xcpa_cpb_add((cps), (cpbit))
785
786 // call with full-sized CP bitmask (XCP_CP_BYTES)
787 // returns 0/1
788 //
789 #define CPB_DEL(cps, cpbit) xcpa_cpb_del((cps), (cpbit))
790
791
792 //-------------------------------------
793 // supported size for user key material
794 #define XCP_RI_UKM_BYTES 40
795 //
796 // key import
797 typedef struct Encrdkey {
798 // EC only: RSA recipients must keep these lengths 0
799 //
800 // largest supported curve: P-521
801 unsigned char srcprivate[ 66 ]; /* private key (PKCS#8) */
802 size_t sprivlen; /* priv. key byte count */
803 unsigned char *oid; /* EC curve OID */
804 size_t olen; /* EC curve OID length */
805 unsigned char srcpublic[ 1+66+66 ]; /* originator public point */
806 size_t splen; /* pub. point bytecount */
807
808 unsigned char ukm[ XCP_RI_UKM_BYTES ]; /* user keymaterial */
809 size_t ulen;
810 //
811 // /EC-only parameters
812
813 // importer information
814 const unsigned char *spki;
815 size_t spkilen;
816 unsigned char ski[ XCP_CERTHASH_BYTES ];
817
818 int ktype; /* one of the wire-specified types */
819
820 CK_MECHANISM *alg; /* currently, ignored */
821 unsigned char wrap_alg[25]; /* AES Key Wrap algorithm OID */
822 // largest supported importer type: 4096-bit RSA
823 unsigned char raw[ 4096/8 ]; /* actual encrypted bytes */
824 size_t rlen;
825 } *Encrdkey_t;
826
827
828 //-------------------------------------
829 // Recipient info used for encrypted key part transport
830 //
831 typedef struct Recipient_info {
832 uint32_t version; /* struct version */
833 unsigned char data[ 1024 ]; /* ASN.1 encoded recipient info */
834 size_t dlen; /* length of recipient info */
835 } *Recipient_info_t;
836
837 //-------------------------------------
838 // turn user key material (UKM), target key bitcount, wrapping alg into
839 // RFC 3278 SharedInfo structure
840 //
841 // We currently only support UKMs of size where the compound has a single-byte
842 // size (i.e., <=0x7f bytes). This is an arbitrary limitation, and may be
843 // removed in the future.
844 //
845 // kbits,wrapalg currently only take default zero (0->256, 0->AES256/wrap)
846 // size restriction on ulen, see above
847 //
848 // returns written size, or failure if anything is invalid
849 // size query (sinfo == NULL) returns exact size, not a conservative estimate
850 //
851 // Other possible return codes
852 // XCP_ADMERR_RI_KWRAPSIZE_INVALID: invalid key wrap size (not zero or 256)
853 // XCP_ADMERR_RI_KWRAPALG_INVALID: invalid key wrap algorithm (not zero)
854 // XCP_ADMERR_RI_UKM_INVALID: invalid user key material (size)
855 // XCP_ESIZE: too small output buffer
856 //
857 long xcp_rcptinfo_sharedinfo(unsigned char *sinfo, size_t slen,
858 const unsigned char *ukm, size_t ulen,
859 unsigned int kbits, int wrapalg) ;
860
861
862 //-------------------------------------
863 // creates RecipientInfo ASN.1 sequence (asn) from encr structure following RFC
864 // 3852 for RSA and RFC 5753 for EC
865 //
866 // verifies if a known importer key is used and if the SPKI does match
867 // the importer key type
868 //
869 // returns size of created rcptinfo if RecipientInfo could be created
870 // <0 if an error occurred
871 //
872 // Other possible return codes
873 // XCP_EARG: if encr is missing
874 // XCP_ADMERR_RI_IMPR_INVALID: if the importer type or the key import structure
875 // encr is not supported / invalid
876 //
877 long xcp_rcptinfo (unsigned char *asn, size_t alen,
878 const struct Encrdkey *encr,
879 const CK_MECHANISM *encrmech) ;
880
881
882 //-------------------------------------
883 // reads ASN.1 formatted RecipientInfo (asn) and turns it into rinfo structure
884 //
885 // returns size of RecipientInfo if asn could be read and rinfo (if non NULL)
886 // could be filled. Otherwise return failure.
887 //
888 // Note: Depending on the ASN.1 information xcp_rcptinfo_read() may not
889 // necessarily update all available fields of the Encrdkey structure.
890 // It will not wipe the Encrdkey structure before, hence it may contain
891 // unfilled sections. It's recommended to provide a zeroized Encrdkey struct.
892 //
893 // possible error return codes:
894 // XCP_EARG: missing RecipientInfo
895 // XCP_EINVALD: invalid ASN.1 formated sequence
896 // XCP_ADMERR_RI_VERSION_INVALID: invalid rcptinfo version
897 // XCP_ADMERR_RI_SKI_INVALID: SKI (size) invalid
898 // XCP_ADMERR_RI_RSA_OID_INVALID: No valid RSA OID (unsupported or invalid)
899 // XCP_ADMERR_RI_EC_OID_INVALID: No valid EC OID (unsupported or invalid)
900 // XCP_ADMERR_RI_IMPR_INVALID: Public key size does not match supporte
901 // importer key type
902 // XCP_ADMERR_RI_ECPUB_INVALID: invalid public key bit string
903 // XCP_ESIZE: EC public key is bigger then the Encrdkey
904 // structure can encompass
905 //
906 long xcp_rcptinfo_read (struct Encrdkey *rinfo,
907 const unsigned char *asn, size_t alen) ;
908
909
910 //-------------------------------------
911 // construct administrative request for key part import:
912 //
913 // xcpAdminReq ::= SEQUENCE
914 // functionId OCTET STRING, -- m_admin()
915 // domain OCTET STRING, -- raw domain (wire 6.2) (4 bytes)
916 // administrative OCTET STRING encapsulates {
917 // command xcpAdminBlk
918 // }
919 // signatures OCTET STRING {
920 // -- signerInfo/s, without encapsulating SET OF
921 // }
922 //
923 // returns size of created request if successfully
924 // <0 if an error occurred
925 //
926 // Other possible return codes
927 // XCP_EARG: arguments are missing
928 // XCP_ESIZE: output buffer is too small or olen/slen are too big
929 //
930 long xcpa_import_keypart (unsigned char *out, size_t olen,
931 const unsigned char *cmdblk, size_t clen,
932 const unsigned char *sig, size_t slen,
933 const struct XCPadmresp *minf,
934 const target_t target) ;
935
936
937 //-------------------------------------
938 // construct single import piece: one command block with one recipientInfo
939 // to be signed by one admin
940 // xcpa_import_keypart() turns this block and its signature into final form
941 //
942 // restrictions: RSA keys must have 2048, 3072 or 4096-bit modulus with exponent
943 // 0x10001. EC must be one of the supported importers
944 //
945 // SPKI used only to find out importer key type; SKI identifies recipient
946 // both must be already imported into *key
947 //
948 // module info (minf) must be identical to all simultaneously imported parts
949 //
950 // tctr is transaction counter; if non-NULL, must be XCP_ADMCTR_BYTES
951 // otherwise derived from minf
952 //
953 // returns length of ASN.1 formated command block if successful, otherwise error
954 //
955 // possible error return codes:
956 // XCP_EARG: missing parameters
957 // XCP_ADMERR_RI_ENC_EMPTY: spki not supplied within Encrdkey struture
958 // XCP_ADMERR_RI_IMPR_INVALID: importer key type invalid / unsupported or does
959 // not match SPKI
960 //
961 // uses xcpa_cmdblock() - see function header for more return codes
962 //
963 long xcpa_import_cmdblock (unsigned char *out, size_t olen,
964 const struct Encrdkey *key,
965 const struct XCPadmresp *minf,
966 const unsigned char *tctr) ;
967
968
969 //-------------------------------------
970 // Validate if EC OID does match any supported EC importer type
971 // Sets importer type in type if not NULL
972 //
973 // Return 0 if no match and 1 for match and XCP_EARG if an OID is not supplied
974 int xcp_valid_ec_oid2imprtype(XCP_IMPRKEY_t *type,
975 const unsigned char *oid, size_t olen) ;
976
977
978 //-------------------------------------
979 // Matches importer type to EC OID
980 // Sets EC OID in oid if not NULL
981 //
982 // Returns 0 if no match and 1 for match
983 int xcp_valid_ec_imprtype2oid(const unsigned char **oid, size_t *olen,
984 XCP_IMPRKEY_t type) ;
985
986
987 //-------------------------------------
988 // parse embedded return value from response, writes to *rv if non-NULL
989 // (outside envelope always reports CKR_OK, unless infrastructure failed)
990 //
991 // possible error return codes:
992 // XCP_EINVALID: response is malformed or contents invalid
993 // XCP_EARG: arguments are missing
994 //
995 long xcpa_internal_rv(const unsigned char *rsp, size_t rlen,
996 struct XCPadmresp *rspblk, CK_RV *rv) ;
997
998
999 //-------------------------------------
1000 // returns net bytecount (full T+L+V) if start of (asn,alen) is full tag
1001 // 0 if invalid
1002 //
1003 // sets *voffs to T+L bytecount if non-NULL
1004 //
1005 size_t xcpa_asn_tag(size_t *voffs, const unsigned char *asn, size_t alen);
1006
1007
1008 //-------------------------------------
1009 // return raw bytecount of ASN.1 SEQ
1010 // return XCP_EINVALID if malformed and XCP_ESIZE if ASN.1 lengths are not
1011 // correct
1012 //
1013 long xcpa_asn_bytes(const void *asn, size_t alen);
1014
1015
1016 //-------------------------------------
1017 // return offset of value in ASN.1 SEQ
1018 // return XCP_EINVALID if malformed and XCP_ESIZE if ASN.1 lengths are not
1019 // correct
1020 //
1021 long xcpa_asn_value_offset(const void *asn, size_t alen) ;
1022
1023
1024 //-------------------------------------
1025 // nonzero tag is compared to that of (asn,alen)
1026 // returns T+L+V bytecount in case of success
1027 // XCP_EINVALID if asn is malformed
1028 // XCP_EARG if asn or alen arguments are invalid
1029 // XCP_ESIZE if ASN.1 lengths are not correct
1030 //
1031 long xcpa_asn_tlv (const void *asn, size_t alen,
1032 unsigned int tag, size_t *voffset,
1033 size_t *vbytes);
1034
1035
1036 //-------------------------------------
1037 // DH ASN.1 SEQ's need to be split up, since we only need public value (X)
1038 // full input is (potentially MACed) DH SPKI
1039 //
1040 // SEQUENCE {
1041 // SEQUENCE {
1042 // ...OID, P, G /* not verified */
1043 // }
1044 // BIT STRING { /* 0 unused bits -> single-byte 00 before INT
1045 // INTEGER {
1046 // Y /* reminder: possibly with leading zero/es */
1047 // }
1048 // }
1049 // }
1050 //
1051 // bits is derived from SEQUENCE if 0 (not currently used/supported!)
1052 // must be full bytes, otherwise (no partial-byte moduluses supported)
1053 //
1054 // returns >0 if recognized; X is at (pub+offs, length)
1055 // 0 if input appears to be invalid
1056 //
1057 size_t xcpa_dh_asn2y(const unsigned char *pub, size_t plen,
1058 size_t *offs);
1059
1060
1061 //-------------------------------------
1062 // parses public value (Y) from Diffie-Hellman public key (ASN)
1063 // accepts MACed public keys
1064 //
1065 // sets *offs to offset of Y within (asn,alen) if non-NULL
1066 //
1067 // returns >0 if parsing successful (raw bytecount of public value)
1068 // 0 if anything failed, logging reason
1069 //
1070 size_t xcpa_dh_pub2y(size_t *offs, const unsigned char *asn, size_t alen);
1071
1072
1073 //-------------------------------------
1074 // EC ASN.1 SEQ's need to be split up, since we only need public value (X)
1075 // full input is (potentially MACed) DH SPKI
1076 //
1077 // SEQUENCE {
1078 // SEQUENCE {
1079 // OID(EC)
1080 // OID(curve) /* not verified */
1081 // }
1082 // BIT STRING { /* 0 unused bits -> single-byte 00 before INT
1083 // Y /* raw value */
1084 // }
1085 // }
1086 //
1087 // bits is derived from SEQUENCE if 0 (not currently used/supported!)
1088 // must be full bytes, otherwise (no partial-byte moduluses supported)
1089 //
1090 // returns >0 if recognized; X is at (pub+offs, length)
1091 // 0 if input appears to be invalid
1092 //
1093 size_t xcpa_ec_asn2y(const unsigned char *pub, size_t plen,
1094 size_t *offs);
1095
1096
1097 //-------------------------------------
1098 // parses public value (Y) from EC public key (ASN)
1099 // accepts MACed public keys
1100 //
1101 // sets *offs to offset of Y within (asn,alen) if non-NULL
1102 //
1103 // returns >0 if parsing successful (raw bytecount of public value)
1104 // 0 if anything failed, logging reason
1105 //
1106 size_t xcpa_ec_pub2y(size_t *offs, const unsigned char *asn, size_t alen);
1107
1108
1109 //-------------------------------------
1110 // generates the export request in an ASN.1 structure
1111 // reuired for export wk or export state
1112 //
1113 // asn pointer to the resulting ASN.1 buffer
1114 // alen max. ASN.1 buffer len
1115 // certs pointer to the KPH certificates
1116 // ccnt number of KPHs
1117 // exportstate set to 0 if exportwk is requested
1118 // set to 1 if exportstate is requested
1119 // statesave contains properties for exportstate
1120 // restriction_mask mask of exportwk restrictions
1121 long xcpa_fill_export_req(unsigned char *asn, size_t alen,
1122 const struct KPH *certs, unsigned int ccnt,
1123 int exportstate,
1124 struct STATESAVE *statesave, uint32_t restriction_mask);
1125
1126
1127 //-------------------------------------
1128 // Constructs key part file with ASN.1 envelope
1129 // writes output to (*reqprep, reqpreplen)
1130 //
1131 // domainmask target domain mask
1132 // kphs keypart holder certificates
1133 // kcnt number of kphs
1134 // ekps contains re-encrypted keyparts
1135 // reqprep output buffer
1136 // reqpreplen output length
1137 // headerinfo set to 0 if no header info requested
1138 // set to 1 if header info requested
1139 //
1140 // returns 0 if successful
1141 // <0 if something fails
1142 long xcpa_construct_keypart_file(unsigned char *domainmask,
1143 const struct KPH *kphs,
1144 const struct Encrdkey *ekps,
1145 unsigned int kcnt,
1146 unsigned char *reqprep,
1147 size_t *reqpreplen,
1148 unsigned int headerinfo);
1149
1150
1151 //-------------------------------------
1152 // Enable export WK permission
1153 //
1154 // target target domain
1155 // sign_cb provide the callback for generating signatures
1156 // may be NULL if no signatures required
1157 // signopts number of signatures requested
1158 long xcpa_enable_exportwk(target_t target,
1159 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1160
1161
1162 //-------------------------------------
1163 // Enable export state permission
1164 //
1165 // target target module/domain
1166 // sign_cb provide the callback for generating signatures
1167 // may be NULL if no signatures required
1168 // signopts number of signatures requested
1169 //
1170 long xcpa_enable_export_state(target_t target,
1171 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1172
1173
1174 //-------------------------------------
1175 // Enable import WK permission
1176 //
1177 // target target domain
1178 // sign_cb provide the callback for generating signatures
1179 // may be NULL if no signatures required
1180 // signopts number of signatures requested
1181 long xcpa_enable_importwk(target_t target,
1182 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1183
1184
1185 //-------------------------------------
1186 // Enable import state permission
1187 //
1188 // target target module/domain
1189 // sign_cb provide the callback for generating signatures
1190 // may be NULL if no signatures required
1191 // signopts number of signatures requested
1192 //
1193 long xcpa_enable_import_state(target_t target,
1194 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1195
1196
1197 //-------------------------------------
1198 // Export the domain WK of the given target
1199 // writes output to (*resp, resplen)
1200 //
1201 // target addresses target module/domain
1202 // wktype indicates either current or next WK
1203 // keyparts pointer to the encrypted keyparts
1204 // keypartlen length of encrypted keyparts
1205 // request pointer to the export request data
1206 // requestlen length of request data
1207 // sign_cb provide the callback for generating signatures
1208 // may be NULL if no signatures required
1209 // signopts number of signatures requested
1210 long xcpa_export_wk(target_t target, int wktype,
1211 unsigned char *keyparts, size_t *keypartlen,
1212 const unsigned char *request, size_t requestlen,
1213 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1214
1215
1216 //-------------------------------------
1217 // Export the state of the given target
1218 // writes output to (*state/statelen, tkps/tkpslen)
1219 //
1220 // target addresses target module/domain
1221 // state pointer to exported state data
1222 // statelen length of exported state data
1223 // keyparts pointer to transport keyparts
1224 // keypartlen length of transport keyparts
1225 // request pointer to the export request
1226 // requestlen length of export request
1227 // sign_cb provide the callback for generating signatures
1228 // may be NULL if no signatures required
1229 // signopts number of signatures requested
1230 //
1231 long xcpa_export_state(target_t target,
1232 unsigned char *state, size_t *statelen,
1233 unsigned char *tkps, size_t *tkplen,
1234 const unsigned char *request, size_t requestlen,
1235 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1236
1237
1238 //-------------------------------------
1239 // Import a domain WK (from recipient info) to the given target
1240 //
1241 // target addresses target module/domain
1242 // rinfo contains recipient infos
1243 // ricnt number of recipient infos
1244 // wkvp WK verification pattern
1245 // sign_cb provide the callback for generating signatures
1246 // may be NULL if no signatures required
1247 // signopts number of signatures requested
1248 long xcpa_import_wk_rcptinfo(target_t target,
1249 struct Recipient_info *rinfo, unsigned int ricnt,
1250 const unsigned char *wkvp,
1251 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1252
1253
1254 //-------------------------------------
1255 // Import a domain WK (from ekps struct) to the given target
1256 //
1257 // target addresses target module/domain
1258 // ekps contains re-encrypted keyparts
1259 // kcnt number of keyparts
1260 // wkvp WK verification pattern
1261 // sign_cb provide the callback for generating signatures
1262 // may be NULL if no signatures required
1263 // signopts number of signatures requested
1264 long xcpa_import_wk(target_t target, const struct Encrdkey *ekps,
1265 unsigned int kcnt, const unsigned char *wkvp,
1266 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1267
1268
1269
1270 //-------------------------------------
1271 // Import module state data to a target
1272 //
1273 // target addresses target module
1274 // domainmask list of affected domains
1275 // ekps contains re-encrypted keyparts
1276 // kpcnt number of keyparts
1277 // state module state data
1278 // statelen state data len
1279 // sign_cb provide the callback for generating signatures
1280 // may be NULL if no signatures required
1281 // signopts number of signatures requested
1282 //
1283 long xcpa_import_state(target_t target, unsigned char *domainmask,
1284 unsigned char *state, size_t statelen,
1285 struct Encrdkey *ekps, unsigned int kpcnt,
1286 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1287
1288
1289 //-------------------------------------
1290 // Commit a domain WK of the given target
1291 //
1292 // target addresses target module/domain
1293 // wkvp WK verification pattern
1294 // sign_cb provide the callback for generating signatures
1295 // may be NULL if no signatures required
1296 // signopts number of signatures requested
1297 long xcpa_commit_wk(target_t target, const unsigned char *wkvp,
1298 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1299
1300
1301 //-------------------------------------
1302 // Finalize a domain WK of the given target
1303 //
1304 // target addresses target module/domain
1305 // wkvp WK verification pattern
1306 long xcpa_finalize_wk(target_t target, const unsigned char *wkvp);
1307
1308
1309 //-------------------------------------
1310 // Generate a random WK for the given target
1311 //
1312 // target addresses target module/domain
1313 // wkvp WK verification pattern
1314 // sign_cb provide the callback for generating signatures
1315 // may be NULL if no signatures required
1316 // signopts number of signatures requested
1317 long xcpa_gen_random_wk(target_t target, unsigned char *wkvp,
1318 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1319
1320
1321 //-------------------------------------
1322 // SKI-based SignerInfo form
1323 //
1324 // 30(02(03) // v3: signer identified by SKI
1325 // 80(...SKI...)
1326 // 30(...digest OID...)
1327 // 30(...signature_alg OID...)
1328 // 04(...signature...)
1329 // )
1330 //
1331 // we construct ASN.1 following these rules:
1332 // 1. lengths, except that of the signature, are single-byte (<127)
1333 // - this is true for all forms we support
1334 // 2. signature and whole SEQUENCE use two-byte length fields (82 xx yy)
1335 // 3. version is minimal-encoded
1336 // 4. OIDs always contain trailing NULL (05 00)
1337 // - we tolerate the lack of this, see si_read below
1338
1339 // sigmech may be a combined hash+sign mechanism, then hashmech is ignored
1340 //
1341 // placeholder 'signature' is added if sig is NULL: siglen is bytecount then
1342 //
1343 // if encoding a generic 'rsaEncryption' mode, use CKM_RSA_X_509 and
1344 // separate hash in hashmech
1345 //
1346 // returns length of ASN.1 formated SignerInfo if successful, otherwise failure
1347 //
1348 // possible error return codes:
1349 // XCP_EARG: missing arguments or bad ski length
1350 // XCP_ADMERR_SI_SIZE: bad signature length
1351 // XCP_ESIZE: other lengths are over the 127 limit
1352 // or output buffer too small
1353 // XCP_ADMERR_SI_SIG_MECH_UNSUPPORTED: unsupported signature mechanism
1354 // XCP_ADMERR_SI_HSH_MECH_UNSUPPORTED: unsupported hash mechanism
1355 // XCP_ADMERR_SI_OID_MECH_MISMATCH: mismatch between signature and hash
1356 // mechanism
1357 //
1358 long xcp_signerinfo (unsigned char *asn, size_t alen,
1359 const unsigned char *ski, size_t skilen, /* signer */
1360 const unsigned char *sig, size_t siglen,
1361 const CK_MECHANISM *sigmech,
1362 const CK_MECHANISM *hashmech) ;
1363
1364
1365 //-------------------------------------
1366 // checks if valid and parses signer info into components
1367 //
1368 // (ski,skilen) and (sig,siglen) are within (sinfo,silen), if set
1369 //
1370 // returns signerinfo struct bytecount if successful
1371 //
1372 // possible error return codes:
1373 // XCP_EARG: missing arguments
1374 // XCP_EINVALID: could not read signerinfo
1375 // XCP_ADMERR_SI_SIG_EMPTY: no signature present
1376 // XCP_ADMERR_SI_SIG_MECH_UNSUPPORTED: unsupported signature mechanism
1377 // XCP_ADMERR_SI_HSH_MECH_UNSUPPORTED: unsupported hash mechanism
1378 // XCP_ADMERR_SI_OID_MECH_MISMATCH: mismatch between signature and hash
1379 // mechanism
1380 //
1381 // no length checks on signature or SKI, other than checking both for non-empty
1382 //
1383 long xcp_signerinfo_read (const unsigned char *sinfo, size_t silen,
1384 const unsigned char **ski, size_t *skilen,
1385 const unsigned char **sig, size_t *siglen,
1386 const unsigned char **hoid, size_t *hoidlen,
1387 const unsigned char **soid, size_t *soidlen,
1388 CK_MECHANISM *signmech,
1389 CK_MECHANISM *hashmech) ;
1390
1391
1392 //-------------------------------------
1393 // given an SPKI, return its raw public key (which hashes into the SKI):
1394 //
1395 // SEQUENCE {
1396 // SEQUENCE ... -- type information (which we do not parse)
1397 // BIT STRING { -- SKI base is net content of this BIT STRING
1398 // -- without leading byte (==unused bits' count)
1399 // SEQUENCE { -- capture this T+L+V (or raw public value for EC key)
1400 // ...
1401 // }
1402 // }
1403 //
1404 // returns SKI-base bytecount, setting *ski to start of SKI base, if non-NULL
1405 // XCP_EINVALID if input does not look like an SPKI
1406 // XCP_EARG if missing arguments
1407 //
1408 // note: we do not verify other details of SPKI; caller must do so
1409 //
1410 long xcp_spki2pubkey (const unsigned char **bitstr,
1411 const unsigned char *spki, size_t slen) ;
1412
1413
1414
1415
1416 /*
1417 * Parse the list of indices and ranges and construct corresponding bitmask
1418 * args pointer to a comma separated list of indices
1419 * (ranges of indices are also allowed)
1420 * mask pointer to an 32 byte array that represents our domain mask
1421 * masksize bit-length of the mask
1422 */
1423 int xcp_args2mask(char *args, unsigned char *mask, int masksize);
1424
1425
1426 /*
1427 * Fills in 'file-ID', 'offset' and 'length' parameter into the file header
1428 * hdr pointer to the file header
1429 * hlen length of the header
1430 * fileid fileid to refer the file type
1431 * offset offset of data pointer
1432 * bytes number of bytes to transfer
1433 */
1434 int xcpa_write_filepart_hdr(unsigned char *hdr, size_t hlen,
1435 uint32_t fileid, uint32_t offset,
1436 uint32_t bytes);
1437
1438
1439 /* reads partial data from target/fileid
1440 * returning payload to res, rlen
1441 *
1442 * returns >0 on success (bytecount)
1443 * 0 if file access failed (not available, etc.)
1444 * <0 if anything unexpected failed
1445 */
1446 long xcpa_query_filepart(unsigned char *res, size_t rlen,
1447 const unsigned char *hdr, size_t hlen,
1448 target_t target,
1449 CK_RV exprv);
1450
1451
1452 /* reads complete data from target/fileid
1453 * returning payload to res, rlen
1454 *
1455 * returns >0 on success (bytecount)
1456 * 0 if file access failed (not available, etc.)
1457 * <0 if anything unexpected failed
1458 */
1459 long xcpa_query_full_file(unsigned char *res, size_t rlen,
1460 target_t target,
1461 unsigned int fileid,
1462 unsigned int block);
1463
1464
1465 /* writes data to internal files
1466 * takes data, dlen to write to target/fileid
1467 *
1468 * returns >0 if results have been all written
1469 * <0 if anything unexpected failed
1470 */
1471 long xcpa_write_full_file(target_t target,
1472 xcpa_admin_signs_cb_t sign_cb, const void *signopts,
1473 const unsigned char *data, size_t dlen,
1474 unsigned int fileid, unsigned int block);
1475
1476
1477 long xcpa_remove_file(target_t target, unsigned int fileid,
1478 xcpa_admin_signs_cb_t sign_cb, const void *signopts);
1479
1480
1481 /* brute-force section parser: enumerate all encrypted-KP sections
1482 *
1483 * returns >0 offset of full OCTET STRING T+L+V section
1484 * 0 when there are no more sections
1485 * <0 if encoding is invalid (which SNH)
1486 *
1487 * sets *kpidx to index (sub-type) of keypart found if not NULL
1488 *
1489 * since external compound is SEQUENCE, it can't match KP at offset 0
1490 * therefore, comparing <current offset> > <idx> is correct
1491 */
1492 long xcpa_kp_next_section(const unsigned char *kps, size_t kplen,
1493 unsigned long idx, uint32_t *kpidx);
1494
1495
1496 /* retrieve recipient infos from keypart sections
1497 *
1498 * returns >0 number of recipient infos found
1499 * <0 if anything failed
1500 */
1501 long xcpa_kps_retrieve_rcptinfo(struct Recipient_info *rcpti,
1502 unsigned int rimax,
1503 const unsigned char *kpexport,
1504 size_t kplen);
1505
1506
1507 /*
1508 * report domain compliance
1509 *
1510 * returns compliance bitmask if successful and 0 if anything failed
1511 * (as zero is invalid as we always have a default compliance active)
1512 *
1513 */
1514 uint64_t get_dom_compl(target_t target);
1515
1516 #endif /* !defined(__xcpadm_h__) */
1517