static int anchor_store (int argc, char *argv[], bool *changed) { CK_ATTRIBUTE *attrs; CK_FUNCTION_LIST *module = NULL; CK_SESSION_HANDLE session; CK_OBJECT_HANDLE object; p11_array *anchors; int ret; int i; anchors = files_to_attrs (argc, argv); if (anchors == NULL) return 1; if (anchors->num == 0) { p11_message ("specify at least one anchor input file"); p11_array_free (anchors); return 2; } session = session_for_store (&module); if (session == 0UL) { p11_array_free (anchors); return 1; } for (i = 0, ret = 0; i < anchors->num; i++) { attrs = anchors->elem[i]; anchors->elem[i] = NULL; object = find_anchor (module, session, attrs); if (object == 0) { p11_debug ("don't yet have this anchor"); if (create_anchor (module, session, attrs)) { *changed = true; } else { ret = 1; break; } } else { p11_debug ("already have this anchor"); if (modify_anchor (module, session, object, attrs)) { *changed = true; } else { ret = 1; break; } } } p11_array_free (anchors); p11_kit_module_finalize (module); p11_kit_module_release (module); return ret; }
static bool remove_all (p11_kit_iter *iter, bool *changed) { const char *desc; CK_RV rv; while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { desc = description_for_object_at_iter (iter); p11_debug ("removing %s: %lu", desc, p11_kit_iter_get_object (iter)); rv = p11_kit_iter_destroy_object (iter); switch (rv) { case CKR_OK: *changed = true; /* fall through */ case CKR_OBJECT_HANDLE_INVALID: continue; case CKR_TOKEN_WRITE_PROTECTED: case CKR_SESSION_READ_ONLY: case CKR_ATTRIBUTE_READ_ONLY: p11_message ("couldn't remove read-only %s", desc); continue; default: p11_message ("couldn't remove %s: %s", desc, p11_kit_strerror (rv)); break; } } return (rv == CKR_CANCEL); }
static bool create_anchor (CK_FUNCTION_LIST *module, CK_SESSION_HANDLE session, CK_ATTRIBUTE *attrs) { CK_BBOOL truev = CK_TRUE; CK_OBJECT_HANDLE object; char *string; CK_RV rv; CK_ULONG klass; CK_ATTRIBUTE basics_certificate[] = { { CKA_TOKEN, &truev, sizeof (truev) }, { CKA_TRUSTED, &truev, sizeof (truev) }, { CKA_INVALID, }, }; CK_ATTRIBUTE basics_extension[] = { { CKA_TOKEN, &truev, sizeof (truev) }, { CKA_INVALID, }, }; CK_ATTRIBUTE basics_empty[] = { { CKA_INVALID, }, }; CK_ATTRIBUTE *basics = basics_empty; if (p11_attrs_find_ulong (attrs, CKA_CLASS, &klass)) { switch (klass) { case CKO_CERTIFICATE: basics = basics_certificate; break; case CKO_X_CERTIFICATE_EXTENSION: basics = basics_extension; break; } } attrs = p11_attrs_merge (attrs, p11_attrs_dup (basics), true); p11_attrs_remove (attrs, CKA_MODIFIABLE); if (p11_debugging) { string = p11_attrs_to_string (attrs, -1); p11_debug ("storing: %s", string); free (string); } rv = (module->C_CreateObject) (session, attrs, p11_attrs_count (attrs), &object); p11_attrs_free (attrs); if (rv != CKR_OK) { p11_message ("couldn't create object: %s", p11_kit_strerror (rv)); return false; } return true; }
static CK_SESSION_HANDLE session_for_store_on_module (const char *name, CK_FUNCTION_LIST *module, bool *found_read_only) { CK_SESSION_HANDLE session = 0; CK_SLOT_ID *slots = NULL; CK_TOKEN_INFO info; CK_ULONG count; CK_ULONG i; CK_RV rv; rv = p11_kit_module_initialize (module); if (rv != CKR_OK) { p11_message ("%s: couldn't initialize: %s", name, p11_kit_message ()); return 0UL; } rv = (module->C_GetSlotList) (CK_TRUE, NULL, &count); if (rv == CKR_OK) { slots = calloc (count, sizeof (CK_ULONG)); return_val_if_fail (slots != NULL, 0UL); rv = (module->C_GetSlotList) (CK_TRUE, slots, &count); } if (rv != CKR_OK) { p11_message ("%s: couldn't enumerate slots: %s", name, p11_kit_strerror (rv)); free (slots); return 0UL; } for (i = 0; session == 0 && i < count; i++) { rv = (module->C_GetTokenInfo) (slots[i], &info); if (rv != CKR_OK) { p11_message ("%s: couldn't get token info: %s", name, p11_kit_strerror (rv)); continue; } if (info.flags & CKF_WRITE_PROTECTED) { *found_read_only = true; continue; } rv = (module->C_OpenSession) (slots[i], CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL, NULL, &session); if (rv != CKR_OK) { p11_message ("%s: couldn't open session: %s", name, p11_kit_strerror (rv)); session = 0; } p11_debug ("opened writable session on: %s", name); } free (slots); if (session == 0UL) p11_kit_module_finalize (module); return session; }
static p11_array * files_to_attrs (int argc, char *argv[]) { p11_parser *parser; p11_array *parsed; p11_array *array; int ret = P11_PARSE_SUCCESS; int i, j; array = p11_array_new (p11_attrs_free); return_val_if_fail (array != NULL, NULL); parser = create_arg_file_parser (); return_val_if_fail (parser != NULL, NULL); for (i = 0; i < argc; i++) { ret = p11_parse_file (parser, argv[i], NULL, P11_PARSE_FLAG_ANCHOR); switch (ret) { case P11_PARSE_SUCCESS: p11_debug ("parsed file: %s", argv[i]); break; case P11_PARSE_UNRECOGNIZED: p11_message ("unrecognized file format: %s", argv[i]); break; default: p11_message ("failed to parse file: %s", argv[i]); break; } if (ret != P11_PARSE_SUCCESS) break; parsed = p11_parser_parsed (parser); for (j = 0; j < parsed->num; j++) { if (!p11_array_push (array, parsed->elem[j])) return_val_if_reached (NULL); parsed->elem[j] = NULL; } } p11_parser_free (parser); if (ret == P11_PARSE_SUCCESS) return array; p11_array_free (array); return NULL; }
static bool modify_anchor (CK_FUNCTION_LIST *module, CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object, CK_ATTRIBUTE *attrs) { CK_BBOOL truev = CK_TRUE; CK_ATTRIBUTE *changes; CK_ATTRIBUTE *label; CK_ULONG klass; char *string; CK_RV rv; CK_ATTRIBUTE trusted = { CKA_TRUSTED, &truev, sizeof (truev) }; label = p11_attrs_find_valid (attrs, CKA_LABEL); if (p11_attrs_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_CERTIFICATE) changes = p11_attrs_build (NULL, &trusted, label, NULL); else changes = p11_attrs_build (NULL, label, NULL); return_val_if_fail (attrs != NULL, FALSE); /* Don't need the attributes anymore */ p11_attrs_free (attrs); if (p11_debugging) { string = p11_attrs_to_string (changes, -1); p11_debug ("setting: %s", string); free (string); } rv = (module->C_SetAttributeValue) (session, object, changes, p11_attrs_count (changes)); p11_attrs_free (changes); if (rv != CKR_OK) { p11_message ("couldn't create object: %s", p11_kit_strerror (rv)); return false; } return true; }
static bool load_attached_extension (p11_dict *attached, p11_dict *asn1_defs, const unsigned char *der, size_t len) { char message[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; node_asn *ext; char *oid; int length; int start; int end; int ret; ext = p11_asn1_decode (asn1_defs, "PKIX1.Extension", der, len, message); if (ext == NULL) { p11_message ("couldn't parse attached certificate extension: %s", message); return false; } ret = asn1_der_decoding_startEnd (ext, der, len, "extnID", &start, &end); return_val_if_fail (ret == ASN1_SUCCESS, false); /* Make sure it's a straightforward oid with certain assumptions */ length = (end - start) + 1; if (!p11_oid_simple (der + start, length)) { p11_debug ("strange complex certificate extension object id"); return false; } oid = memdup (der + start, length); return_val_if_fail (oid != NULL, false); if (!p11_dict_set (attached, oid, ext)) return_val_if_reached (false); return true; }
static p11_array * uris_or_files_to_iters (int argc, char *argv[], int behavior) { int flags = P11_KIT_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE; p11_parser *parser = NULL; p11_array *iters; p11_array *parsed; p11_kit_uri *uri; p11_kit_iter *iter; int ret; int i, j; iters = p11_array_new ((p11_destroyer)p11_kit_iter_free); return_val_if_fail (iters != NULL, NULL); for (i = 0; i < argc; i++) { /* A PKCS#11 URI */ if (strncmp (argv[i], "pkcs11:", 7) == 0) { uri = p11_kit_uri_new (); if (p11_kit_uri_parse (argv[i], flags, uri) != P11_KIT_URI_OK) { p11_message ("invalid PKCS#11 uri: %s", argv[i]); p11_kit_uri_free (uri); break; } iter = p11_kit_iter_new (uri, behavior); return_val_if_fail (iter != NULL, NULL); p11_kit_uri_free (uri); if (!p11_array_push (iters, iter)) return_val_if_reached (NULL); } else { if (parser == NULL) parser = create_arg_file_parser (); ret = p11_parse_file (parser, argv[i], NULL, P11_PARSE_FLAG_ANCHOR); switch (ret) { case P11_PARSE_SUCCESS: p11_debug ("parsed file: %s", argv[i]); break; case P11_PARSE_UNRECOGNIZED: p11_message ("unrecognized file format: %s", argv[i]); break; default: p11_message ("failed to parse file: %s", argv[i]); break; } if (ret != P11_PARSE_SUCCESS) break; parsed = p11_parser_parsed (parser); for (j = 0; j < parsed->num; j++) { iter = p11_kit_iter_new (NULL, behavior); return_val_if_fail (iter != NULL, NULL); iter_match_anchor (iter, parsed->elem[j]); if (!p11_array_push (iters, iter)) return_val_if_reached (NULL); } } } if (parser) p11_parser_free (parser); if (argc != i) { p11_array_free (iters); return NULL; } return iters; }