1N/A/*
1N/A * Common Public License Version 0.5
1N/A *
1N/A * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
1N/A * THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
1N/A * REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
1N/A * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
1N/A *
1N/A * 1. DEFINITIONS
1N/A *
1N/A * "Contribution" means:
1N/A * a) in the case of the initial Contributor, the
1N/A * initial code and documentation distributed under
1N/A * this Agreement, and
1N/A *
1N/A * b) in the case of each subsequent Contributor:
1N/A * i) changes to the Program, and
1N/A * ii) additions to the Program;
1N/A *
1N/A * where such changes and/or additions to the Program
1N/A * originate from and are distributed by that
1N/A * particular Contributor. A Contribution 'originates'
1N/A * from a Contributor if it was added to the Program
1N/A * by such Contributor itself or anyone acting on such
1N/A * Contributor's behalf. Contributions do not include
1N/A * additions to the Program which: (i) are separate
1N/A * modules of software distributed in conjunction with
1N/A * the Program under their own license agreement, and
1N/A * (ii) are not derivative works of the Program.
1N/A *
1N/A *
1N/A * "Contributor" means any person or entity that distributes
1N/A * the Program.
1N/A *
1N/A * "Licensed Patents " mean patent claims licensable by a
1N/A * Contributor which are necessarily infringed by the use or
1N/A * sale of its Contribution alone or when combined with the
1N/A * Program.
1N/A *
1N/A * "Program" means the Contributions distributed in
1N/A * accordance with this Agreement.
1N/A *
1N/A * "Recipient" means anyone who receives the Program under
1N/A * this Agreement, including all Contributors.
1N/A *
1N/A * 2. GRANT OF RIGHTS
1N/A *
1N/A * a) Subject to the terms of this Agreement, each
1N/A * Contributor hereby grants Recipient a
1N/A * non-exclusive, worldwide, royalty-free copyright
1N/A * license to reproduce, prepare derivative works of,
1N/A * publicly display, publicly perform, distribute and
1N/A * sublicense the Contribution of such Contributor, if
1N/A * any, and such derivative works, in source code and
1N/A * object code form.
1N/A *
1N/A * b) Subject to the terms of this Agreement, each
1N/A * Contributor hereby grants Recipient a
1N/A * non-exclusive, worldwide, royalty-free patent
1N/A * license under Licensed Patents to make, use, sell,
1N/A * offer to sell, import and otherwise transfer the
1N/A * Contribution of such Contributor, if any, in source
1N/A * code and object code form. This patent license
1N/A * shall apply to the combination of the Contribution
1N/A * and the Program if, at the time the Contribution is
1N/A * added by the Contributor, such addition of the
1N/A * Contribution causes such combination to be covered
1N/A * by the Licensed Patents. The patent license shall
1N/A * not apply to any other combinations which include
1N/A * the Contribution. No hardware per se is licensed
1N/A * hereunder.
1N/A *
1N/A * c) Recipient understands that although each
1N/A * Contributor grants the licenses to its
1N/A * Contributions set forth herein, no assurances are
1N/A * provided by any Contributor that the Program does
1N/A * not infringe the patent or other intellectual
1N/A * property rights of any other entity. Each
1N/A * Contributor disclaims any liability to Recipient
1N/A * for claims brought by any other entity based on
1N/A * infringement of intellectual property rights or
1N/A * otherwise. As a condition to exercising the rights
1N/A * and licenses granted hereunder, each Recipient
1N/A * hereby assumes sole responsibility to secure any
1N/A * other intellectual property rights needed, if any.
1N/A *
1N/A * For example, if a third party patent license is
1N/A * required to allow Recipient to distribute the
1N/A * Program, it is Recipient's responsibility to
1N/A * acquire that license before distributing the
1N/A * Program.
1N/A *
1N/A * d) Each Contributor represents that to its
1N/A * knowledge it has sufficient copyright rights in its
1N/A * Contribution, if any, to grant the copyright
1N/A * license set forth in this Agreement.
1N/A *
1N/A * 3. REQUIREMENTS
1N/A *
1N/A * A Contributor may choose to distribute the Program in
1N/A * object code form under its own license agreement, provided
1N/A * that:
1N/A * a) it complies with the terms and conditions of
1N/A * this Agreement; and
1N/A *
1N/A * b) its license agreement:
1N/A * i) effectively disclaims on behalf of all
1N/A * Contributors all warranties and conditions, express
1N/A * and implied, including warranties or conditions of
1N/A * title and non-infringement, and implied warranties
1N/A * or conditions of merchantability and fitness for a
1N/A * particular purpose;
1N/A *
1N/A * ii) effectively excludes on behalf of all
1N/A * Contributors all liability for damages, including
1N/A * direct, indirect, special, incidental and
1N/A * consequential damages, such as lost profits;
1N/A *
1N/A * iii) states that any provisions which differ from
1N/A * this Agreement are offered by that Contributor
1N/A * alone and not by any other party; and
1N/A *
1N/A * iv) states that source code for the Program is
1N/A * available from such Contributor, and informs
1N/A * licensees how to obtain it in a reasonable manner
1N/A * on or through a medium customarily used for
1N/A * software exchange.
1N/A *
1N/A * When the Program is made available in source code form:
1N/A * a) it must be made available under this Agreement;
1N/A * and
1N/A * b) a copy of this Agreement must be included with
1N/A * each copy of the Program.
1N/A *
1N/A * Contributors may not remove or alter any copyright notices
1N/A * contained within the Program.
1N/A *
1N/A * Each Contributor must identify itself as the originator of
1N/A * its Contribution, if any, in a manner that reasonably
1N/A * allows subsequent Recipients to identify the originator of
1N/A * the Contribution.
1N/A *
1N/A *
1N/A * 4. COMMERCIAL DISTRIBUTION
1N/A *
1N/A * Commercial distributors of software may accept certain
1N/A * responsibilities with respect to end users, business
1N/A * partners and the like. While this license is intended to
1N/A * facilitate the commercial use of the Program, the
1N/A * Contributor who includes the Program in a commercial
1N/A * product offering should do so in a manner which does not
1N/A * create potential liability for other Contributors.
1N/A * Therefore, if a Contributor includes the Program in a
1N/A * commercial product offering, such Contributor ("Commercial
1N/A * Contributor") hereby agrees to defend and indemnify every
1N/A * other Contributor ("Indemnified Contributor") against any
1N/A * losses, damages and costs (collectively "Losses") arising
1N/A * from claims, lawsuits and other legal actions brought by a
1N/A * third party against the Indemnified Contributor to the
1N/A * extent caused by the acts or omissions of such Commercial
1N/A * Contributor in connection with its distribution of the
1N/A * Program in a commercial product offering. The obligations
1N/A * in this section do not apply to any claims or Losses
1N/A * relating to any actual or alleged intellectual property
1N/A * infringement. In order to qualify, an Indemnified
1N/A * Contributor must: a) promptly notify the Commercial
1N/A * Contributor in writing of such claim, and b) allow the
1N/A * Commercial Contributor to control, and cooperate with the
1N/A * Commercial Contributor in, the defense and any related
1N/A * settlement negotiations. The Indemnified Contributor may
1N/A * participate in any such claim at its own expense.
1N/A *
1N/A *
1N/A * For example, a Contributor might include the Program in a
1N/A * commercial product offering, Product X. That Contributor
1N/A * is then a Commercial Contributor. If that Commercial
1N/A * Contributor then makes performance claims, or offers
1N/A * warranties related to Product X, those performance claims
1N/A * and warranties are such Commercial Contributor's
1N/A * responsibility alone. Under this section, the Commercial
1N/A * Contributor would have to defend claims against the other
1N/A * Contributors related to those performance claims and
1N/A * warranties, and if a court requires any other Contributor
1N/A * to pay any damages as a result, the Commercial Contributor
1N/A * must pay those damages.
1N/A *
1N/A *
1N/A * 5. NO WARRANTY
1N/A *
1N/A * EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
1N/A * PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
1N/A * WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
1N/A * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
1N/A * CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
1N/A * FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
1N/A * responsible for determining the appropriateness of using
1N/A * and distributing the Program and assumes all risks
1N/A * associated with its exercise of rights under this
1N/A * Agreement, including but not limited to the risks and
1N/A * costs of program errors, compliance with applicable laws,
1N/A * damage to or loss of data, programs or equipment, and
1N/A * unavailability or interruption of operations.
1N/A *
1N/A * 6. DISCLAIMER OF LIABILITY
1N/A * EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
1N/A * RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
1N/A * OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
1N/A * LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
1N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1N/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
1N/A * OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
1N/A * OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
1N/A * POSSIBILITY OF SUCH DAMAGES.
1N/A *
1N/A * 7. GENERAL
1N/A *
1N/A * If any provision of this Agreement is invalid or
1N/A * unenforceable under applicable law, it shall not affect
1N/A * the validity or enforceability of the remainder of the
1N/A * terms of this Agreement, and without further action by the
1N/A * parties hereto, such provision shall be reformed to the
1N/A * minimum extent necessary to make such provision valid and
1N/A * enforceable.
1N/A *
1N/A *
1N/A * If Recipient institutes patent litigation against a
1N/A * Contributor with respect to a patent applicable to
1N/A * software (including a cross-claim or counterclaim in a
1N/A * lawsuit), then any patent licenses granted by that
1N/A * Contributor to such Recipient under this Agreement shall
1N/A * terminate as of the date such litigation is filed. In
1N/A * addition, If Recipient institutes patent litigation
1N/A * against any entity (including a cross-claim or
1N/A * counterclaim in a lawsuit) alleging that the Program
1N/A * itself (excluding combinations of the Program with other
1N/A * software or hardware) infringes such Recipient's
1N/A * patent(s), then such Recipient's rights granted under
1N/A * Section 2(b) shall terminate as of the date such
1N/A * litigation is filed.
1N/A *
1N/A * All Recipient's rights under this Agreement shall
1N/A * terminate if it fails to comply with any of the material
1N/A * terms or conditions of this Agreement and does not cure
1N/A * such failure in a reasonable period of time after becoming
1N/A * aware of such noncompliance. If all Recipient's rights
1N/A * under this Agreement terminate, Recipient agrees to cease
1N/A * use and distribution of the Program as soon as reasonably
1N/A * practicable. However, Recipient's obligations under this
1N/A * Agreement and any licenses granted by Recipient relating
1N/A * to the Program shall continue and survive.
1N/A *
1N/A * Everyone is permitted to copy and distribute copies of
1N/A * this Agreement, but in order to avoid inconsistency the
1N/A * Agreement is copyrighted and may only be modified in the
1N/A * following manner. The Agreement Steward reserves the right
1N/A * to publish new versions (including revisions) of this
1N/A * Agreement from time to time. No one other than the
1N/A * Agreement Steward has the right to modify this Agreement.
1N/A *
1N/A * IBM is the initial Agreement Steward. IBM may assign the
1N/A * responsibility to serve as the Agreement Steward to a
1N/A * suitable separate entity. Each new version of the
1N/A * Agreement will be given a distinguishing version number.
1N/A * The Program (including Contributions) may always be
1N/A * distributed subject to the version of the Agreement under
1N/A * which it was received. In addition, after a new version of
1N/A * the Agreement is published, Contributor may elect to
1N/A * distribute the Program (including its Contributions) under
1N/A * the new version. Except as expressly stated in Sections
1N/A * 2(a) and 2(b) above, Recipient receives no rights or
1N/A * licenses to the intellectual property of any Contributor
1N/A * under this Agreement, whether expressly, by implication,
1N/A * estoppel or otherwise. All rights in the Program not
1N/A * expressly granted under this Agreement are reserved.
1N/A *
1N/A *
1N/A * This Agreement is governed by the laws of the State of New
1N/A * York and the intellectual property laws of the United
1N/A * States of America. No party to this Agreement will bring a
1N/A * legal action under this Agreement more than one year after
1N/A * the cause of action arose. Each party waives its rights to
1N/A * a jury trial in any resulting litigation.
1N/A *
1N/A *
1N/A *
1N/A * (C) COPYRIGHT International Business Machines Corp. 2001,2002
1N/A */
1N/A/*
2N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A#include "tpmtok_int.h"
1N/A#include <pwd.h>
1N/A#include <grp.h>
1N/A#include <fcntl.h>
1N/A
1N/A#define ALTERNATE_KEYSTORE_PATH "PKCS11_TPM_DIR"
1N/A#define PWD_BUFFER_SIZE 1024
1N/A
1N/Astatic char keystore_path[MAXPATHLEN];
1N/Astatic boolean_t keystore_path_initialized = 0;
1N/A
1N/ATSS_HKEY hPrivateLeafKey;
1N/Astatic CK_RV
1N/Arestore_private_token_object(TSS_HCONTEXT, CK_BYTE *, CK_ULONG, OBJECT *);
1N/A
1N/Astatic struct flock fl = {
1N/A 0,
1N/A 0,
1N/A 0,
1N/A 0,
1N/A 0,
1N/A 0,
1N/A {0, 0, 0, 0}
1N/A};
1N/A
1N/Astatic int
1N/Alockfile(int fd, int op)
1N/A{
1N/A fl.l_type = op;
1N/A return (fcntl(fd, F_SETLKW, &fl));
1N/A}
1N/A
1N/Astatic char *
1N/Aget_user_default_path(char *home_path)
1N/A{
1N/A struct passwd pwd, *user_info;
1N/A char pwdbuf[PWD_BUFFER_SIZE];
1N/A
1N/A if (getpwuid_r(getuid(), &pwd, pwdbuf, PWD_BUFFER_SIZE,
1N/A &user_info) != 0)
1N/A return (NULL);
1N/A
1N/A (void) snprintf(home_path, MAXPATHLEN, "/var/user/%s/tpm",
1N/A user_info ? user_info->pw_name : "");
1N/A
1N/A return (home_path);
1N/A}
1N/A
1N/Achar *
1N/Aget_tpm_keystore_path()
1N/A{
1N/A char *env_val;
1N/A char home_path[MAXPATHLEN];
1N/A
1N/A if (!keystore_path_initialized) {
1N/A env_val = getenv(ALTERNATE_KEYSTORE_PATH);
1N/A bzero(keystore_path, sizeof (keystore_path));
1N/A /*
1N/A * If it isn't set or is set to the empty string use the
1N/A * default location. We need to check for the empty string
1N/A * because some users "unset" environment variables by giving
1N/A * them no value, this isn't the same thing as removing it
1N/A * from the environment.
1N/A *
1N/A * We don't want that to attempt to open the token area.
1N/A */
1N/A if ((env_val == NULL) || (strcmp(env_val, "") == 0)) {
1N/A /* alternate path not specified, use default dir */
1N/A char *p = get_user_default_path(home_path);
1N/A if (p == NULL)
1N/A return (NULL);
1N/A (void) snprintf(keystore_path, MAXPATHLEN, "%s", p);
1N/A } else {
1N/A (void) snprintf(keystore_path, MAXPATHLEN, "%s",
1N/A env_val);
1N/A }
1N/A keystore_path_initialized = 1;
1N/A }
1N/A return (keystore_path);
1N/A}
1N/A
1N/Astatic CK_RV
1N/Acreate_keystore_dir()
1N/A{
1N/A char *ksdir = get_tpm_keystore_path();
1N/A char objdir[MAXPATHLEN];
1N/A CK_RV rv = 0;
1N/A
1N/A if (ksdir == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A if (mkdir(ksdir, S_IRUSR|S_IWUSR|S_IXUSR) < 0) {
1N/A if (errno == EEXIST) {
1N/A rv = 0;
1N/A } else {
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A }
1N/A if (rv == 0) {
1N/A (void) snprintf(objdir, sizeof (objdir),
1N/A "%s/%s", ksdir, TOKEN_OBJ_DIR);
1N/A
1N/A if (mkdir(objdir, S_IRUSR|S_IWUSR|S_IXUSR) < 0) {
1N/A if (errno == EEXIST) {
1N/A rv = 0;
1N/A } else {
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A }
1N/A }
1N/A return (CKR_OK);
1N/A}
1N/A
1N/Astatic void
1N/Aset_perm(int file)
1N/A{
1N/A /*
1N/A * In the TPM token, with per-user data stores, we don't share the token
1N/A * object amongst a group. In fact, we want to restrict access to
1N/A * a single user.
1N/A */
1N/A (void) fchmod(file, S_IRUSR|S_IWUSR);
1N/A}
1N/A
1N/A#define READ_TOKEN_INFO_STR(fp, rec, reclen) rc = fread(rec, reclen, 1, fp); \
1N/A if (rc != 1) { rc = CKR_FUNCTION_FAILED; goto out_unlock; }
1N/A
1N/A#define READ_TOKEN_INFO_UINT32(fp, rec) rc = fread(&fieldval, \
1N/A sizeof (UINT32), 1, fp); \
1N/A if (rc != 1) { rc = CKR_FUNCTION_FAILED; goto out_unlock; } \
1N/A rec = (CK_ULONG)fieldval;
1N/A
1N/ACK_RV
1N/Aload_token_data(TSS_HCONTEXT hContext, TOKEN_DATA *td)
1N/A{
1N/A FILE *fp;
1N/A CK_BYTE fname[MAXPATHLEN];
1N/A CK_RV rc;
1N/A UINT32 fieldval;
1N/A char *p = get_tpm_keystore_path();
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s", p, TOKEN_DATA_FILE);
1N/A
1N/A rc = XProcLock(xproclock);
1N/A if (rc != CKR_OK)
1N/A return (rc);
1N/A
1N/A fp = fopen((char *)fname, "r");
1N/A if (!fp) {
1N/A /* Better error checking added */
1N/A if (errno == ENOENT) {
1N/A (void) XProcUnLock(xproclock);
1N/A rc = create_keystore_dir();
1N/A if (rc != 0)
1N/A goto out_nolock;
1N/A
1N/A rc = init_token_data(hContext, td);
1N/A if (rc != CKR_OK) {
1N/A goto out_nolock;
1N/A }
1N/A
1N/A rc = XProcLock(xproclock);
1N/A if (rc != CKR_OK) {
1N/A goto out_nolock;
1N/A }
1N/A
1N/A fp = fopen((char *)fname, "r");
1N/A if (!fp) {
1N/A LogError("failed opening %s for read: %s",
1N/A fname, (char *)strerror(errno));
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto out_unlock;
1N/A }
1N/A } else {
1N/A /* Could not open file for some unknown reason */
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto out_unlock;
1N/A }
1N/A }
1N/A if (lockfile(fileno(fp), F_RDLCK)) {
1N/A (void) fclose(fp);
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto out_unlock;
1N/A }
1N/A set_perm(fileno(fp));
1N/A
1N/A /* Read fields individually because of 64-bit size diffs */
1N/A READ_TOKEN_INFO_STR(fp, td->token_info.label,
1N/A sizeof (td->token_info.label));
1N/A READ_TOKEN_INFO_STR(fp, td->token_info.manufacturerID,
1N/A sizeof (td->token_info.manufacturerID));
1N/A READ_TOKEN_INFO_STR(fp, td->token_info.model,
1N/A sizeof (td->token_info.model));
1N/A READ_TOKEN_INFO_STR(fp, td->token_info.serialNumber,
1N/A sizeof (td->token_info.serialNumber));
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.flags);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulMaxSessionCount);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulSessionCount);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulRwSessionCount);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulMaxPinLen);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulMinPinLen);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulTotalPublicMemory);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulFreePublicMemory);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulTotalPrivateMemory);
1N/A READ_TOKEN_INFO_UINT32(fp, td->token_info.ulFreePrivateMemory);
1N/A READ_TOKEN_INFO_STR(fp, &td->token_info.hardwareVersion,
1N/A sizeof (td->token_info.hardwareVersion));
1N/A READ_TOKEN_INFO_STR(fp, &td->token_info.firmwareVersion,
1N/A sizeof (td->token_info.firmwareVersion));
1N/A READ_TOKEN_INFO_STR(fp, td->token_info.utcTime,
1N/A sizeof (td->token_info.utcTime));
1N/A READ_TOKEN_INFO_STR(fp, td->user_pin_sha,
1N/A sizeof (td->user_pin_sha));
1N/A READ_TOKEN_INFO_STR(fp, td->so_pin_sha,
1N/A sizeof (td->so_pin_sha));
1N/A READ_TOKEN_INFO_STR(fp, td->next_token_object_name,
1N/A sizeof (td->next_token_object_name));
1N/A READ_TOKEN_INFO_STR(fp, &td->tweak_vector,
1N/A sizeof (td->tweak_vector));
1N/A
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A
1N/A if (rc == 0)
1N/A rc = CKR_FUNCTION_FAILED;
1N/A else
1N/A rc = CKR_OK;
1N/A
1N/Aout_unlock:
1N/A (void) XProcUnLock(xproclock);
1N/A
1N/Aout_nolock:
1N/A return (rc);
1N/A}
1N/A
1N/A
1N/A#define WRITE_TOKEN_INFO_STR(fp, rec, reclen) rc = fwrite(rec, reclen, 1, fp); \
1N/A if (rc != 1) { rc = CKR_FUNCTION_FAILED; goto done; }
1N/A
1N/A#define WRITE_TOKEN_INFO_UINT32(fp, rec) fieldval = (UINT32)rec; \
1N/A rc = fwrite(&fieldval, sizeof (UINT32), 1, fp); \
1N/A if (rc != 1) { rc = CKR_FUNCTION_FAILED; goto done; }
1N/A
1N/ACK_RV
1N/Asave_token_data(TOKEN_DATA *td)
1N/A{
1N/A FILE *fp;
1N/A CK_RV rc;
1N/A CK_BYTE fname[MAXPATHLEN];
1N/A char *p = get_tpm_keystore_path();
1N/A UINT32 fieldval;
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s", p, TOKEN_DATA_FILE);
1N/A
1N/A rc = XProcLock(xproclock);
1N/A if (rc != CKR_OK)
1N/A goto out_nolock;
1N/A
1N/A fp = fopen((char *)fname, "w");
1N/A
1N/A if (!fp) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto done;
1N/A }
1N/A if (lockfile(fileno(fp), F_WRLCK)) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A (void) fclose(fp);
1N/A goto done;
1N/A }
1N/A set_perm(fileno(fp));
1N/A
1N/A /* Write token fields individually to maintain sizes on 64 and 32 bit */
1N/A WRITE_TOKEN_INFO_STR(fp, td->token_info.label,
1N/A sizeof (td->token_info.label));
1N/A WRITE_TOKEN_INFO_STR(fp, td->token_info.manufacturerID,
1N/A sizeof (td->token_info.manufacturerID));
1N/A WRITE_TOKEN_INFO_STR(fp, td->token_info.model,
1N/A sizeof (td->token_info.model));
1N/A WRITE_TOKEN_INFO_STR(fp, td->token_info.serialNumber,
1N/A sizeof (td->token_info.serialNumber));
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.flags);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulMaxSessionCount);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulSessionCount);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulRwSessionCount);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulMaxPinLen);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulMinPinLen);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulTotalPublicMemory);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulFreePublicMemory);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulTotalPrivateMemory);
1N/A WRITE_TOKEN_INFO_UINT32(fp, td->token_info.ulFreePrivateMemory);
1N/A WRITE_TOKEN_INFO_STR(fp, &td->token_info.hardwareVersion,
1N/A sizeof (td->token_info.hardwareVersion));
1N/A WRITE_TOKEN_INFO_STR(fp, &td->token_info.firmwareVersion,
1N/A sizeof (td->token_info.firmwareVersion));
1N/A WRITE_TOKEN_INFO_STR(fp, td->token_info.utcTime,
1N/A sizeof (td->token_info.utcTime));
1N/A WRITE_TOKEN_INFO_STR(fp, td->user_pin_sha,
1N/A sizeof (td->user_pin_sha));
1N/A WRITE_TOKEN_INFO_STR(fp, td->so_pin_sha,
1N/A sizeof (td->so_pin_sha));
1N/A WRITE_TOKEN_INFO_STR(fp, td->next_token_object_name,
1N/A sizeof (td->next_token_object_name));
1N/A WRITE_TOKEN_INFO_STR(fp, &td->tweak_vector,
1N/A sizeof (td->tweak_vector));
1N/A
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A
1N/A rc = CKR_OK;
1N/Adone:
1N/A (void) XProcUnLock(xproclock);
1N/A
1N/Aout_nolock:
1N/A return (rc);
1N/A}
1N/A
1N/ACK_RV
1N/Asave_token_object(TSS_HCONTEXT hContext, OBJECT *obj)
1N/A{
1N/A FILE *fp = NULL;
1N/A CK_BYTE line[100];
1N/A CK_RV rc;
1N/A CK_BYTE fname[MAXPATHLEN];
1N/A char *p = get_tpm_keystore_path();
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A if (object_is_private(obj) == TRUE)
1N/A rc = save_private_token_object(hContext, obj);
1N/A else
1N/A rc = save_public_token_object(obj);
1N/A
1N/A if (rc != CKR_OK)
1N/A return (rc);
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/%s", p, TOKEN_OBJ_DIR, TOKEN_OBJ_INDEX_FILE);
1N/A
1N/A fp = fopen((char *)fname, "r");
1N/A if (fp) {
1N/A if (lockfile(fileno(fp), F_RDLCK)) {
1N/A (void) fclose(fp);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A set_perm(fileno(fp));
1N/A while (!feof(fp)) {
1N/A (void) fgets((char *)line, 50, fp);
1N/A if (!feof(fp)) {
1N/A line[strlen((char *)line) - 1] = 0;
1N/A if (strcmp((char *)line,
1N/A (char *)(obj->name)) == 0) {
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A return (CKR_OK);
1N/A }
1N/A }
1N/A }
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A }
1N/A
1N/A fp = fopen((char *)fname, "a");
1N/A if (!fp)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A if (lockfile(fileno(fp), F_WRLCK)) {
1N/A (void) fclose(fp);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A set_perm(fileno(fp));
1N/A
1N/A (void) fprintf(fp, "%s\n", obj->name);
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A
1N/A return (CKR_OK);
1N/A}
1N/A
1N/ACK_RV
1N/Asave_public_token_object(OBJECT *obj)
1N/A{
1N/A FILE *fp = NULL;
1N/A CK_BYTE *cleartxt = NULL;
1N/A CK_BYTE fname[MAXPATHLEN];
1N/A UINT32 cleartxt_len;
1N/A CK_BBOOL flag = FALSE;
1N/A CK_RV rc;
1N/A UINT32 total_len;
1N/A
1N/A char *p = get_tpm_keystore_path();
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/%s", p, TOKEN_OBJ_DIR, obj->name);
1N/A
1N/A rc = object_flatten(obj, &cleartxt, &cleartxt_len);
1N/A if (rc != CKR_OK)
1N/A goto error;
1N/A
1N/A fp = fopen((char *)fname, "w");
1N/A if (!fp) {
1N/A LogError("Error opening %s - %s", fname,
1N/A (char *)strerror(errno));
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A if (lockfile(fileno(fp), F_WRLCK)) {
1N/A (void) fclose(fp);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A
1N/A set_perm(fileno(fp));
1N/A
1N/A total_len = cleartxt_len + sizeof (UINT32) + sizeof (CK_BBOOL);
1N/A
1N/A (void) fwrite(&total_len, sizeof (total_len), 1, fp);
1N/A (void) fwrite(&flag, sizeof (flag), 1, fp);
1N/A (void) fwrite(cleartxt, cleartxt_len, 1, fp);
1N/A
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A free(cleartxt);
1N/A
1N/A return (CKR_OK);
1N/A
1N/Aerror:
1N/A if (fp)
1N/A (void) fclose(fp);
1N/A if (cleartxt)
1N/A free(cleartxt);
1N/A return (rc);
1N/A}
1N/A
1N/ACK_RV
1N/Asave_private_token_object(TSS_HCONTEXT hContext, OBJECT *obj)
1N/A{
1N/A FILE *fp = NULL;
1N/A CK_BYTE *obj_data = NULL;
1N/A CK_BYTE *cleartxt = NULL;
1N/A CK_BYTE *ciphertxt = NULL;
1N/A CK_BYTE *ptr = NULL;
1N/A CK_BYTE fname[100];
1N/A CK_BYTE hash_sha[SHA1_DIGEST_LENGTH];
1N/A CK_BBOOL flag;
1N/A CK_RV rc;
1N/A CK_ULONG ciphertxt_len;
1N/A UINT32 cleartxt_len;
1N/A UINT32 padded_len;
1N/A UINT32 obj_data_len_32;
1N/A UINT32 total_len;
1N/A UINT32 chunksize, blocks;
1N/A char *p = get_tpm_keystore_path();
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A rc = object_flatten(obj, &obj_data, &obj_data_len_32);
1N/A if (rc != CKR_OK) {
1N/A goto error;
1N/A }
1N/A /*
1N/A * format for the object file:
1N/A * private flag
1N/A * ---- begin encrypted part <--+
1N/A * length of object data (4 bytes) |
1N/A * object data +---- sensitive part
1N/A * SHA of (object data) |
1N/A * ---- end encrypted part <--+
1N/A */
1N/A if ((rc = compute_sha(obj_data, obj_data_len_32, hash_sha)) != CKR_OK)
1N/A goto error;
1N/A
1N/A /*
1N/A * RSA OAEP crypto uses chunks smaller than the max to make room
1N/A * for the hashes.
1N/A * chunksize = RSA_Modulus_Size - (2 * SHA1_DIGEST_SIZE + 2) - (4 - 1)
1N/A * == 209
1N/A */
1N/A chunksize = RSA_BLOCK_SIZE - (2 * SHA1_DIGEST_LENGTH + 2) - 5;
1N/A
1N/A cleartxt_len = sizeof (UINT32) + obj_data_len_32 + SHA1_DIGEST_LENGTH;
1N/A
1N/A blocks = cleartxt_len / chunksize + ((cleartxt_len % chunksize) > 0);
1N/A padded_len = RSA_BLOCK_SIZE * blocks;
1N/A
1N/A cleartxt = (CK_BYTE *)malloc(padded_len);
1N/A ciphertxt = (CK_BYTE *)malloc(padded_len);
1N/A if (!cleartxt || !ciphertxt) {
1N/A rc = CKR_HOST_MEMORY;
1N/A goto error;
1N/A }
1N/A
1N/A ciphertxt_len = padded_len;
1N/A
1N/A ptr = cleartxt;
1N/A (void) memcpy(ptr, &obj_data_len_32, sizeof (UINT32));
1N/A ptr += sizeof (UINT32);
1N/A (void) memcpy(ptr, obj_data, obj_data_len_32);
1N/A ptr += obj_data_len_32;
1N/A (void) memcpy(ptr, hash_sha, SHA1_DIGEST_LENGTH);
1N/A
1N/A (void) add_pkcs_padding(cleartxt + cleartxt_len, RSA_BLOCK_SIZE,
1N/A cleartxt_len, padded_len);
1N/A
1N/A /* the encrypt function will compute the padded length */
1N/A rc = tpm_encrypt_data(hContext, hPrivateLeafKey, cleartxt, cleartxt_len,
1N/A ciphertxt, &ciphertxt_len);
1N/A
1N/A if (rc != CKR_OK) {
1N/A goto error;
1N/A }
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/%s", p, TOKEN_OBJ_DIR, obj->name);
1N/A
1N/A fp = fopen((char *)fname, "w");
1N/A if (!fp) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A if (lockfile(fileno(fp), F_WRLCK)) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A
1N/A set_perm(fileno(fp));
1N/A
1N/A total_len = sizeof (UINT32) + sizeof (CK_BBOOL) + (UINT32)ciphertxt_len;
1N/A
1N/A flag = TRUE;
1N/A
1N/A (void) fwrite(&total_len, sizeof (UINT32), 1, fp);
1N/A (void) fwrite(&flag, sizeof (CK_BBOOL), 1, fp);
1N/A (void) fwrite(ciphertxt, ciphertxt_len, 1, fp);
1N/A
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A
1N/A free(obj_data);
1N/A free(cleartxt);
1N/A free(ciphertxt);
1N/A return (CKR_OK);
1N/A
1N/Aerror:
1N/A if (fp)
1N/A (void) fclose(fp);
1N/A if (obj_data)
1N/A free(obj_data);
1N/A if (cleartxt)
1N/A free(cleartxt);
1N/A if (ciphertxt)
1N/A free(ciphertxt);
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/ACK_RV
1N/Aload_public_token_objects(void)
1N/A{
1N/A FILE *fp1 = NULL, *fp2 = NULL;
1N/A CK_BYTE *buf = NULL;
1N/A CK_BYTE tmp[MAXPATHLEN], fname[MAXPATHLEN], iname[MAXPATHLEN];
1N/A CK_BBOOL priv;
1N/A UINT32 size;
1N/A char *ksdir = get_tpm_keystore_path();
1N/A
1N/A if (ksdir == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf((char *)iname, sizeof (iname),
1N/A "%s/%s/%s", ksdir,
1N/A TOKEN_OBJ_DIR, TOKEN_OBJ_INDEX_FILE);
1N/A
1N/A fp1 = fopen((char *)iname, "r");
1N/A if (!fp1)
1N/A return (CKR_OK); // no token objects
1N/A
1N/A if (lockfile(fileno(fp1), F_RDLCK)) {
1N/A (void) fclose(fp1);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A
1N/A while (!feof(fp1)) {
1N/A (void) fgets((char *)tmp, 50, fp1);
1N/A if (feof(fp1))
1N/A break;
1N/A
1N/A tmp[strlen((char *)tmp) - 1] = 0;
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/", ksdir, TOKEN_OBJ_DIR);
1N/A
1N/A (void) strncat((char *)fname, (const char *)tmp,
1N/A (size_t)sizeof (fname));
1N/A
1N/A fp2 = fopen((char *)fname, "r");
1N/A if (!fp2)
1N/A continue;
1N/A
1N/A (void) fread(&size, sizeof (UINT32), 1, fp2);
1N/A (void) fread(&priv, sizeof (CK_BBOOL), 1, fp2);
1N/A if (priv == TRUE) {
1N/A (void) fclose(fp2);
1N/A continue;
1N/A }
1N/A
1N/A size = size - sizeof (UINT32) - sizeof (CK_BBOOL);
1N/A buf = (CK_BYTE *)malloc(size);
1N/A if (!buf) {
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A return (CKR_HOST_MEMORY);
1N/A }
1N/A
1N/A (void) fread(buf, size, 1, fp2);
1N/A
1N/A if (pthread_mutex_lock(&obj_list_mutex)) {
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A free(buf);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A
1N/A (void) object_mgr_restore_obj(buf, NULL);
1N/A
1N/A (void) pthread_mutex_unlock(&obj_list_mutex);
1N/A free(buf);
1N/A (void) fclose(fp2);
1N/A }
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A
1N/A return (CKR_OK);
1N/A}
1N/A
1N/ACK_RV
1N/Aload_private_token_objects(TSS_HCONTEXT hContext)
1N/A{
1N/A FILE *fp1 = NULL, *fp2 = NULL;
1N/A CK_BYTE *buf = NULL;
1N/A CK_BYTE tmp[MAXPATHLEN], fname[MAXPATHLEN], iname[MAXPATHLEN];
1N/A CK_BBOOL priv;
1N/A UINT32 size;
1N/A CK_RV rc;
1N/A char *ksdir = get_tpm_keystore_path();
1N/A
1N/A if (ksdir == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf((char *)iname, sizeof (iname),
1N/A "%s/%s/%s", ksdir, TOKEN_OBJ_DIR, TOKEN_OBJ_INDEX_FILE);
1N/A
1N/A fp1 = fopen((char *)iname, "r");
1N/A if (!fp1)
1N/A return (CKR_OK);
1N/A
1N/A if (lockfile(fileno(fp1), F_RDLCK)) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A
1N/A while (!feof(fp1)) {
1N/A (void) fgets((char *)tmp, sizeof (tmp), fp1);
1N/A if (feof(fp1))
1N/A break;
1N/A
1N/A tmp[strlen((char *)tmp) - 1] = 0;
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/%s", ksdir, TOKEN_OBJ_DIR, tmp);
1N/A
1N/A fp2 = fopen((char *)fname, "r");
1N/A if (!fp2)
1N/A continue;
1N/A
1N/A (void) fread(&size, sizeof (UINT32), 1, fp2);
1N/A (void) fread(&priv, sizeof (CK_BBOOL), 1, fp2);
1N/A if (priv == FALSE) {
1N/A (void) fclose(fp2);
1N/A continue;
1N/A }
1N/A
1N/A size = size - sizeof (UINT32) - sizeof (CK_BBOOL);
1N/A buf = (CK_BYTE *)malloc(size);
1N/A if (!buf) {
1N/A rc = CKR_HOST_MEMORY;
1N/A goto error;
1N/A }
1N/A
1N/A rc = fread((char *)buf, size, 1, fp2);
1N/A if (rc != 1) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A
1N/A if (pthread_mutex_lock(&obj_list_mutex)) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto error;
1N/A }
1N/A rc = restore_private_token_object(hContext, buf, size, NULL);
1N/A (void) pthread_mutex_unlock(&obj_list_mutex);
1N/A if (rc != CKR_OK)
1N/A goto error;
1N/A
1N/A free(buf);
1N/A (void) fclose(fp2);
1N/A }
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A
1N/A return (CKR_OK);
1N/A
1N/Aerror:
1N/A if (buf)
1N/A free(buf);
1N/A if (fp1) {
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A }
1N/A if (fp2)
1N/A (void) fclose(fp2);
1N/A return (rc);
1N/A}
1N/A
1N/Astatic CK_RV
1N/Arestore_private_token_object(
1N/A TSS_HCONTEXT hContext,
1N/A CK_BYTE *data,
1N/A CK_ULONG len,
1N/A OBJECT *pObj)
1N/A{
1N/A CK_BYTE * cleartxt = NULL;
1N/A CK_BYTE * obj_data = NULL;
1N/A CK_BYTE *ptr = NULL;
1N/A CK_BYTE hash_sha[SHA1_DIGEST_LENGTH];
1N/A UINT32 cleartxt_len;
1N/A UINT32 obj_data_len;
1N/A CK_RV rc;
1N/A
1N/A /*
1N/A * format for the object data:
1N/A * (private flag has already been read at this point)
1N/A * ---- begin encrypted part
1N/A * length of object data (4 bytes)
1N/A * object data
1N/A * SHA of object data
1N/A * ---- end encrypted part
1N/A */
1N/A
1N/A cleartxt_len = len;
1N/A
1N/A cleartxt = (CK_BYTE *)malloc(len);
1N/A if (!cleartxt) {
1N/A rc = CKR_HOST_MEMORY;
1N/A goto done;
1N/A }
1N/A
1N/A rc = tpm_decrypt_data(hContext, hPrivateLeafKey, data, len,
1N/A cleartxt, &len);
1N/A
1N/A if (rc != CKR_OK) {
1N/A goto done;
1N/A }
1N/A
1N/A (void) strip_pkcs_padding(cleartxt, len, &cleartxt_len);
1N/A
1N/A if (cleartxt_len > len) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto done;
1N/A }
1N/A
1N/A ptr = cleartxt;
1N/A
1N/A bcopy(ptr, &obj_data_len, sizeof (UINT32));
1N/A ptr += sizeof (UINT32);
1N/A obj_data = ptr;
1N/A
1N/A if ((rc = compute_sha(ptr, obj_data_len, hash_sha)) != CKR_OK)
1N/A goto done;
1N/A ptr += obj_data_len;
1N/A
1N/A if (memcmp((const void *)ptr, (const void *)hash_sha,
1N/A (size_t)SHA1_DIGEST_LENGTH) != 0) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto done;
1N/A }
1N/A
1N/A (void) object_mgr_restore_obj(obj_data, pObj);
1N/A rc = CKR_OK;
1N/Adone:
1N/A if (cleartxt)
1N/A free(cleartxt);
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/ACK_RV
1N/Areload_token_object(TSS_HCONTEXT hContext, OBJECT *obj)
1N/A{
1N/A FILE *fp = NULL;
1N/A CK_BYTE *buf = NULL;
1N/A CK_BYTE fname[MAXPATHLEN];
1N/A CK_BBOOL priv;
1N/A UINT32 size;
1N/A CK_RV rc;
1N/A char *p = get_tpm_keystore_path();
1N/A
1N/A if (p == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) memset((char *)fname, 0x0, sizeof (fname));
1N/A
1N/A (void) snprintf((char *)fname, sizeof (fname),
1N/A "%s/%s/", p, TOKEN_OBJ_DIR);
1N/A
1N/A (void) strncat((char *)fname, (char *)obj->name, sizeof (fname));
1N/A
1N/A fp = fopen((char *)fname, "r");
1N/A if (!fp) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto done;
1N/A }
1N/A if (lockfile(fileno(fp), F_RDLCK)) {
1N/A rc = CKR_FUNCTION_FAILED;
1N/A goto done;
1N/A }
1N/A
1N/A set_perm(fileno(fp));
1N/A
1N/A (void) fread(&size, sizeof (UINT32), 1, fp);
1N/A (void) fread(&priv, sizeof (CK_BBOOL), 1, fp);
1N/A
1N/A size = size - sizeof (UINT32) - sizeof (CK_BBOOL);
1N/A
1N/A buf = (CK_BYTE *)malloc(size);
1N/A if (!buf) {
1N/A rc = CKR_HOST_MEMORY;
1N/A goto done;
1N/A }
1N/A
1N/A (void) fread(buf, size, 1, fp);
1N/A
1N/A if (priv) {
1N/A rc = restore_private_token_object(hContext, buf, size, obj);
1N/A } else {
1N/A rc = object_mgr_restore_obj(buf, obj);
1N/A }
1N/A
1N/Adone:
1N/A if (fp) {
1N/A (void) lockfile(fileno(fp), F_UNLCK);
1N/A (void) fclose(fp);
1N/A }
1N/A if (buf)
1N/A free(buf);
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Aislink(char *fname)
1N/A{
1N/A struct stat st;
1N/A
1N/A if (stat((const char *)fname, &st))
1N/A return (-1);
1N/A else if (S_ISLNK(st.st_mode))
1N/A return (1);
1N/A return (0);
1N/A}
1N/A
1N/ACK_RV
1N/Adelete_token_object(OBJECT *obj)
1N/A{
1N/A FILE *fp1, *fp2;
1N/A CK_BYTE line[100];
1N/A char objidx[MAXPATHLEN], idxtmp[MAXPATHLEN], fname[MAXPATHLEN];
1N/A char *ksdir = get_tpm_keystore_path();
1N/A
1N/A if (ksdir == NULL)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A (void) snprintf(objidx, sizeof (objidx),
1N/A "%s/%s/%s", ksdir, TOKEN_OBJ_DIR, TOKEN_OBJ_INDEX_FILE);
1N/A
1N/A (void) snprintf(idxtmp, sizeof (idxtmp),
2N/A "%s/%s/IDX.TMP", ksdir, TOKEN_OBJ_DIR);
1N/A
1N/A /*
1N/A * If either file is a link, fail.
1N/A */
1N/A if (islink(objidx) != 0)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A /*
1N/A * idxtmp is a temporary file (and should not exist yet), only fail
1N/A * if it is already an existing link.
1N/A */
1N/A if (islink(idxtmp) == 1)
1N/A return (CKR_FUNCTION_FAILED);
1N/A
1N/A fp1 = fopen(objidx, "r");
1N/A fp2 = fopen(idxtmp, "w");
1N/A if (!fp1 || !fp2) {
1N/A if (fp1)
1N/A (void) fclose(fp1);
1N/A if (fp2)
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A
1N/A if (lockfile(fileno(fp1), F_RDLCK)) {
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A if (lockfile(fileno(fp2), F_WRLCK)) {
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A set_perm(fileno(fp2));
1N/A
1N/A while (!feof(fp1)) {
1N/A (void) fgets((char *)line, 50, fp1);
1N/A if (!feof(fp1)) {
1N/A line[ strlen((char *)line)-1 ] = 0;
1N/A if (strcmp((char *)line, (char *)obj->name))
1N/A (void) fprintf(fp2, "%s\n", line);
1N/A }
1N/A }
1N/A
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) lockfile(fileno(fp2), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A
1N/A fp2 = fopen(objidx, "w");
1N/A fp1 = fopen(idxtmp, "r");
1N/A if (!fp1 || !fp2) {
1N/A if (fp1)
1N/A (void) fclose(fp1);
1N/A if (fp2)
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A if (lockfile(fileno(fp1), F_RDLCK)) {
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A if (lockfile(fileno(fp2), F_WRLCK)) {
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A return (CKR_FUNCTION_FAILED);
1N/A }
1N/A
1N/A set_perm(fileno(fp2));
1N/A
1N/A while (!feof(fp1)) {
1N/A (void) fgets((char *)line, 50, fp1);
1N/A if (!feof(fp1))
1N/A (void) fprintf(fp2, "%s", (char *)line);
1N/A }
1N/A
1N/A (void) lockfile(fileno(fp1), F_UNLCK);
1N/A (void) lockfile(fileno(fp2), F_UNLCK);
1N/A (void) fclose(fp1);
1N/A (void) fclose(fp2);
1N/A
1N/A (void) snprintf(fname, sizeof (fname),
1N/A "%s/%s/%s", ksdir, TOKEN_OBJ_DIR, (char *)obj->name);
1N/A
1N/A (void) unlink(fname);
1N/A return (CKR_OK);
1N/A}