usr/src/lib/openssl/Patches/pkcs11-engine/hw_pk11_uri.c
author Cyril Plisko <cyril.plisko@grigale.com>
Mon, 08 Feb 2010 15:41:40 +0200
changeset 7 f96733ecc878
parent 3 ad3552f8e1ef
permissions -rw-r--r--
Import sfw build 133 Bugs Fixed ---------- 6462216 tomcat fails to build with java 1.6 6766826 SUNWopenldapr becomes inconsistent after a reboot 6900262 OpenSSL PKCS#11 engine files are not cstyle clean again 6902029 DoS vulnerability in ntpd NTP mode 7 (MODE_PRIVATE) handling [CVE-2009-3563] 6908571 77.5% snv_129 openssl-rsa1024-ncp_engine-verifys performance regression sparc 6909632 PKCS#11 engine should use CK_(TRUE|FALSE) instead of TRUE/FALSE 6910922 Apache 1.3 JServ module removal 6912691 don't need to reference /usr/openwin so much anymore 6912906 Apache 1.3 init.d script shouldn't refer to Tomcat 6913689 Several sfw packages no longer need to depend on SUNWmlib and SUNWmlibl 6917046 upgrade ncftp to 3.2.3 6918442 when /usr/bin/patch becomes gnu patch in opensolaris 131, it upsets a few things 6919344 SUNWgnu-readline consumers have undefined tgetent symbol 6919686 Tomcat vulnerable for CVE-2009-2901, CVE-2009-2902 and CVE-2009-2693 6920566 SUNWgnupg license is incorrect for gpgme 6920613 Apache Tomcat 6.0.24 stable 6920817 Wireshark update to version 1.2.6 6920889 make OpenSSL fips and non-fips builds run in parallel 6921161 SUNWusbccid should have gone away with SUNWocf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     1
/*
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
     2
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     3
 * Use is subject to license terms.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     4
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     5
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     8
 * are met:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
     9
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    10
 * 1. Redistributions of source code must retain the above copyright
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    11
 *    notice, this list of conditions and the following disclaimer.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    12
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    13
 * 2. Redistributions in binary form must reproduce the above copyright
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    14
 *    notice, this list of conditions and the following disclaimer in
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    15
 *    the documentation and/or other materials provided with the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    16
 *    distribution.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    17
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    18
 * 3. All advertising materials mentioning features or use of this
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    19
 *    software must display the following acknowledgment:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    20
 *    "This product includes software developed by the OpenSSL Project
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    21
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    22
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    23
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    24
 *    endorse or promote products derived from this software without
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    25
 *    prior written permission. For written permission, please contact
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    26
 *    [email protected].
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    27
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    28
 * 5. Products derived from this software may not be called "OpenSSL"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    29
 *    nor may "OpenSSL" appear in their names without prior written
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    30
 *    permission of the OpenSSL Project.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    31
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    32
 * 6. Redistributions of any form whatsoever must retain the following
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    33
 *    acknowledgment:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    34
 *    "This product includes software developed by the OpenSSL Project
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    35
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    36
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    37
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    38
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    39
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    40
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    41
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    42
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    43
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    44
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    45
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    46
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    47
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    48
 * OF THE POSSIBILITY OF SUCH DAMAGE.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    49
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    50
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
    51
#pragma ident	"@(#)hw_pk11_uri.c	1.2	10/02/01 SMI"
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    52
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    53
#include <stdio.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    54
#include <stdlib.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    55
#include <string.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    56
#include <sys/types.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    57
#include <sys/wait.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    58
#include <sys/mman.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    59
#include <unistd.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    60
#include <strings.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    61
#include <libgen.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    62
#include <pthread.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    63
#include <assert.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    64
#include <errno.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    65
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    66
#include <openssl/crypto.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    67
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    68
#ifndef OPENSSL_NO_HW
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    69
#ifndef OPENSSL_NO_HW_PK11
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    70
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    71
#include <security/cryptoki.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    72
#include <security/pkcs11.h>
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    73
#include "hw_pk11.h"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    74
#include "hw_pk11_uri.h"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    75
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    76
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    77
 * The keystore used is always from the pubkey slot so we need to know which one
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    78
 * was selected so that we can get the information needed for the URI
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    79
 * processing.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    80
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    81
extern CK_SLOT_ID pubkey_SLOTID;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    82
extern CK_FUNCTION_LIST_PTR pFuncList;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    83
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    84
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    85
 * Cached PIN so that child can use it during the re-login. Note that we do not
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    86
 * cache the PIN by default.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    87
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    88
static char *token_pin;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    89
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    90
static int mlock_pin_in_memory(char *pin);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    91
static char *run_askpass(char *dialog);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    92
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    93
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    94
 * Get the PIN. Either run the command and use its standard output as a PIN to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    95
 * fill in the PKCS11 URI structure, or read the PIN from the terminal. Using
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    96
 * the external command is of higher precedence. The memory for PIN is allocated
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    97
 * in this function and the PIN is always NULL terminated. The caller must take
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    98
 * care of freeing the memory used for the PIN. The maximum PIN length accepted
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
    99
 * is PK11_MAX_PIN_LEN.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   100
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   101
 * The function is used also during the re-initialization of the engine after
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   102
 * the fork.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   103
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   104
 * The function must not be called under the protection of the mutex "uri_lock"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   105
 * because the lock is acquired in the prefork function.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   106
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   107
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   108
 *	0 in case of troubles (and sets "*pin" to NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   109
 *	1 if we got the PIN
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   110
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   111
#define	EXEC_SPEC	"exec:"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   112
#define	BUILTIN_SPEC	"builtin"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   113
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   114
pk11_get_pin(char *dialog, char **pin)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   115
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   116
	/* Initialize as an error. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   117
	*pin = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   118
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   119
	if (strcmp(dialog, BUILTIN_SPEC) == 0)
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   120
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   121
		/* The getpassphrase() function is not MT safe. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   122
		(void) pthread_mutex_lock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   123
		/* Note that OpenSSL is not localized at all. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   124
		*pin = getpassphrase("Enter token PIN: ");
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   125
		if (*pin == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   126
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   127
			PK11err(PK11_F_GET_PIN, PK11_R_COULD_NOT_READ_PIN);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   128
			(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   129
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   130
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   131
		else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   132
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   133
			char *pw;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   134
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   135
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   136
			 * getpassphrase() uses an internal  buffer to hold the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   137
			 * entered password. Note that it terminates the buffer
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   138
			 * with '\0'.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   139
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   140
			if ((pw = strdup(*pin)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   141
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   142
				PK11err(PK11_F_GET_PIN, PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   143
				(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   144
				goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   145
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   146
			/* Zero the internal buffer to get rid of the PIN. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   147
			memset(*pin, 0, strlen(*pin));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   148
			*pin = pw;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   149
			(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   150
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   151
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   152
	else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   153
		{
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   154
		/*
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   155
		 * This is the "exec:" case. We will get the PIN from the output
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   156
		 * of an external command.
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   157
		 */
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   158
		if (strncmp(dialog, EXEC_SPEC, strlen(EXEC_SPEC)) == 0)
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   159
			{
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   160
			dialog += strlen(EXEC_SPEC);
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   161
			if ((*pin = run_askpass(dialog)) == NULL)
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   162
				goto err;
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   163
			}
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   164
		else
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   165
			{
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   166
			/*
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   167
			 * Invalid specification in the passphrasedialog
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   168
			 * keyword.
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   169
			 */
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   170
			PK11err(PK11_F_GET_PIN, PK11_R_BAD_PASSPHRASE_SPEC);
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   171
			goto err;
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   172
			}
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   173
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   174
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   175
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   176
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   177
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   178
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   179
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   180
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   181
 * Process the PKCS#11 URI and get the PIN. It uses information from the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   182
 * passphrasedialog keyword to get the PIN. If passphrasedialog is not present
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   183
 * it is not considered an error since it depends on the token attributes
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   184
 * whether C_Login() is required. The function expects an allocated 'uri_struct'
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   185
 * structure.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   186
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   187
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   188
 *	0 if URI is not valid at all, or if we could not get the PIN
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   189
 * 	1 if all is OK
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   190
 *	2 if the URI is not the PKCS#11 URI. In that case, put the string
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   191
 *	pointer to the filename to "*file". Note that the pointer just points
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   192
 *	inside of the "uristr", possibly skipping the file:// prefix if present.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   193
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   194
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   195
pk11_process_pkcs11_uri(const char *uristr, pkcs11_uri *uri_struct,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   196
	const char **file)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   197
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   198
	char *uristr2, *l1, *l2, *tok, *name;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   199
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   200
	/* Check the "file://" case. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   201
	if (strncmp(uristr, FILE_URI_PREFIX, strlen(FILE_URI_PREFIX)) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   202
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   203
		*file = uristr + strlen(FILE_URI_PREFIX);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   204
		return (2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   205
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   206
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   207
	/*  This is the "pkcs11:" case. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   208
	if (strncmp(uristr, PK11_URI_PREFIX, strlen(PK11_URI_PREFIX)) != 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   209
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   210
		/* Not PKCS#11 URI at all, could be a filename. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   211
		*file = (const char *)uristr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   212
		return (2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   213
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   214
	else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   215
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   216
		/* Dup the string and skip over the pkcs11: prefix then. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   217
		uristr2 = strdup(uristr + strlen(PK11_URI_PREFIX));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   218
		if (uristr2 == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   219
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   220
			PK11err(PK11_F_CHECK_TOKEN_ATTRS,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   221
			    PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   222
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   223
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   224
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   225
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   226
	/* Initialize the structure. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   227
	memset(uri_struct, 0, sizeof (*uri_struct));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   228
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   229
	/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   230
	 * Using strtok_r() would silently skip over multiple semicolons. We
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   231
	 * must check that before moving on. We must also avoid ';' as the first
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   232
	 * and the last character in the URI.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   233
	 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   234
	if (strstr(uristr2, ";;") != NULL || uristr2[0] == ';' ||
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   235
	    (strlen(uristr2) > 0 && uristr2[strlen(uristr2) - 1] == ';'))
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   236
		goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   237
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   238
	tok = strtok_r(uristr2, ";", &l1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   239
	for (; tok != NULL; tok = strtok_r(NULL, ";", &l1))
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   240
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   241
		/* "tok" is not empty so there will be something in "name". */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   242
		name = strtok_r(tok, "=", &l2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   243
		/* Check whether there is '=' at all. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   244
		if (l2 == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   245
			goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   246
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   247
		/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   248
		 * Fill out the URI structure. We do not accept duplicit
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   249
		 * attributes.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   250
		 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   251
		if (strcmp(name, PK11_TOKEN) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   252
			if (uri_struct->token == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   253
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   254
				if ((uri_struct->token = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   255
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   256
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   257
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   258
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   259
		else if (strcmp(name, PK11_MANUF) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   260
			if (uri_struct->manuf == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   261
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   262
				if ((uri_struct->manuf = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   263
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   264
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   265
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   266
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   267
		else if (strcmp(name, PK11_SERIAL) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   268
			if (uri_struct->serial == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   269
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   270
				if ((uri_struct->serial = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   271
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   272
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   273
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   274
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   275
		else if (strcmp(name, PK11_MODEL) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   276
			if (uri_struct->model == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   277
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   278
				if ((uri_struct->model = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   279
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   280
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   281
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   282
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   283
		else if (strcmp(name, PK11_OBJECT) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   284
			if (uri_struct->object == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   285
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   286
				if ((uri_struct->object = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   287
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   288
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   289
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   290
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   291
		else if (strcmp(name, PK11_OBJECTTYPE) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   292
			if (uri_struct->objecttype == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   293
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   294
				uri_struct->objecttype = strdup(l2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   295
				if (uri_struct->objecttype == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   296
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   297
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   298
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   299
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   300
		else if (strcmp(name, PK11_ASKPASS) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   301
			if (uri_struct->askpass == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   302
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   303
				if ((uri_struct->askpass = strdup(l2)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   304
					goto no_mem;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   305
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   306
			else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   307
				goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   308
		else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   309
			goto bad_uri;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   310
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   311
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   312
	/* The "object" token is mandatory in the PKCS#11 URI. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   313
	if (uri_struct->object == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   314
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   315
		PK11err(PK11_F_LOAD_PRIVKEY, PK11_R_MISSING_OBJECT_LABEL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   316
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   317
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   318
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   319
	free(uristr2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   320
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   321
bad_uri:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   322
	PK11err(PK11_F_LOAD_PRIVKEY, PK11_R_INVALID_PKCS11_URI);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   323
	if (uristr2 != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   324
		free(uristr2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   325
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   326
no_mem:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   327
	PK11err(PK11_F_LOAD_PRIVKEY, PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   328
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   329
	pk11_free_pkcs11_uri(uri_struct, CK_FALSE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   330
	if (uristr2 != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   331
		free(uristr2);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   332
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   333
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   334
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   335
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   336
 * Free the PKCS11 URI structure and anything that might be inside.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   337
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   338
void
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   339
pk11_free_pkcs11_uri(pkcs11_uri *uri_struct, CK_BBOOL free_uri_itself)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   340
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   341
	if (uri_struct->token != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   342
		free(uri_struct->token);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   343
	if (uri_struct->manuf != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   344
		free(uri_struct->manuf);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   345
	if (uri_struct->serial != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   346
		free(uri_struct->serial);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   347
	if (uri_struct->model != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   348
		free(uri_struct->model);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   349
	if (uri_struct->object != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   350
		free(uri_struct->object);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   351
	if (uri_struct->objecttype != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   352
		free(uri_struct->objecttype);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   353
	if (uri_struct->askpass != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   354
		free(uri_struct->askpass);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   355
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   356
	if (free_uri_itself == CK_TRUE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   357
		OPENSSL_free(uri_struct);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   358
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   359
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   360
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   361
 * While our keystore is always the one used by the pubkey slot (which is
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   362
 * usually the Metaslot) we must make sure that those URI attributes that
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   363
 * specify the keystore match the real attributes of our slot keystore. Note
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   364
 * that one can use the METASLOT_OBJECTSTORE_TOKEN environment variable to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   365
 * change the Metaslot's keystore from the softtoken to something else (see
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   366
 * libpkcs11(3LIB)). The user might want to use such attributes in the PKCS#11
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   367
 * URI to make sure that the intended keystore is used.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   368
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   369
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   370
 *	1 on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   371
 *	0 on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   372
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   373
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   374
pk11_check_token_attrs(pkcs11_uri *uri_struct)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   375
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   376
	CK_RV rv;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   377
	static CK_TOKEN_INFO_PTR token_info = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   378
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   379
	(void) pthread_mutex_lock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   380
	if (token_info == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   381
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   382
		token_info = OPENSSL_malloc(sizeof (CK_TOKEN_INFO));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   383
		if (token_info == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   384
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   385
			PK11err(PK11_F_CHECK_TOKEN_ATTRS,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   386
			    PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   387
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   388
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   389
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   390
		rv = pFuncList->C_GetTokenInfo(pubkey_SLOTID, token_info);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   391
		if (rv != CKR_OK)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   392
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   393
			PK11err_add_data(PK11_F_CHECK_TOKEN_ATTRS,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   394
			    PK11_R_GETTOKENINFO, rv);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   395
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   396
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   397
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   398
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   399
	if (uri_struct->token != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   400
		if (strncmp(uri_struct->token, (char *)token_info->label,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   401
		    strlen(uri_struct->token) > 32 ? 32 :
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   402
		    strlen(uri_struct->token)) != 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   403
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   404
			goto urierr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   405
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   406
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   407
	if (uri_struct->manuf != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   408
		if (strncmp(uri_struct->manuf,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   409
		    (char *)token_info->manufacturerID,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   410
		    strlen(uri_struct->manuf) > 32 ? 32 :
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   411
		    strlen(uri_struct->manuf)) != 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   412
			goto urierr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   413
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   414
	if (uri_struct->model != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   415
		if (strncmp(uri_struct->model, (char *)token_info->model,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   416
		    strlen(uri_struct->model) > 16 ? 16 :
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   417
		    strlen(uri_struct->model)) != 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   418
			goto urierr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   419
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   420
	if (uri_struct->serial != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   421
		if (strncmp(uri_struct->serial,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   422
		    (char *)token_info->serialNumber,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   423
		    strlen(uri_struct->serial) > 16 ? 16 :
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   424
		    strlen(uri_struct->serial)) != 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   425
			goto urierr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   426
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   427
	(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   428
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   429
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   430
urierr:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   431
	PK11err(PK11_F_CHECK_TOKEN_ATTRS, PK11_R_TOKEN_ATTRS_DO_NOT_MATCH);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   432
	/* Correct error already set above for the "err" label. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   433
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   434
	(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   435
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   436
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   437
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   438
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   439
 * Return the process PIN caching policy. We initialize it just once so if the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   440
 * process change OPENSSL_PKCS11_PIN_CACHING_POLICY during the operation it will
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   441
 * not have any affect on the policy.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   442
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   443
 * We assume that the "uri_lock" mutex is already locked.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   444
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   445
 * Returns the caching policy number.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   446
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   447
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   448
pk11_get_pin_caching_policy(void)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   449
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   450
	char *value = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   451
	static int policy = POLICY_NOT_INITIALIZED;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   452
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   453
	if (policy != POLICY_NOT_INITIALIZED)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   454
		return (policy);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   455
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   456
	value = getenv("OPENSSL_PKCS11_PIN_CACHING_POLICY");
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   457
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   458
	if (value == NULL || strcmp(value, "none") == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   459
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   460
		policy = POLICY_NONE;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   461
		goto done;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   462
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   463
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   464
	if (strcmp(value, "memory") == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   465
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   466
		policy = POLICY_MEMORY;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   467
		goto done;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   468
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   469
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   470
	if (strcmp(value, "mlocked-memory") == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   471
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   472
		policy = POLICY_MLOCKED_MEMORY;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   473
		goto done;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   474
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   475
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   476
	return (POLICY_WRONG_VALUE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   477
done:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   478
	return (policy);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   479
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   480
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   481
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   482
 * Cache the PIN in memory once. We already know that we have either "memory" or
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   483
 * "mlocked-memory" keyword correctly set.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   484
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   485
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   486
 *	1 on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   487
 *	0 on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   488
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   489
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   490
pk11_cache_pin(char *pin)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   491
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   492
	(void) pthread_mutex_lock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   493
	/* We set the PIN only once since all URIs must have it the same. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   494
	if (token_pin != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   495
		goto ok;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   496
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   497
	if (pk11_get_pin_caching_policy() == POLICY_MEMORY)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   498
		if ((token_pin = strdup(pin)) == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   499
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   500
			PK11err(PK11_F_CACHE_PIN, PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   501
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   502
			}
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   503
	else
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   504
		if (pk11_get_pin_caching_policy() == POLICY_MLOCKED_MEMORY)
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   505
			{
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   506
			if (mlock_pin_in_memory(pin) == 0)
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   507
				goto err;
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   508
			}
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   509
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   510
ok:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   511
	(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   512
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   513
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   514
	(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   515
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   516
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   517
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   518
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   519
 * Cache the PIN in mlock(3C)ed memory. If mlock(3C) fails we will not resort to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   520
 * the normal memory caching.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   521
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   522
 * Note that this function must be called under the protection of the "uri_lock"
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   523
 * mutex.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   524
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   525
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   526
 *	1 on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   527
 *	0 on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   528
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   529
static int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   530
mlock_pin_in_memory(char *pin)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   531
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   532
	void *addr = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   533
	long pagesize = 0;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   534
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   535
	/* mlock(3C) locks pages so we need one whole page for the PIN. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   536
	if ((pagesize = sysconf(_SC_PAGESIZE)) == -1)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   537
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   538
		PK11err(PK11_F_MLOCK_PIN_IN_MEMORY, PK11_R_SYSCONF_FAILED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   539
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   540
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   541
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   542
	/* This will ensure we have a page aligned pointer... */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   543
	if ((addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   544
	    MAP_PRIVATE | MAP_ANON, -1, 0)) == MAP_FAILED)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   545
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   546
		PK11err(PK11_F_MLOCK_PIN_IN_MEMORY, PK11_R_MMAP_FAILED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   547
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   548
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   549
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   550
	/* ...because "addr" must be page aligned here. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   551
	if (mlock(addr, pagesize) == -1)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   552
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   553
		/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   554
		 * Missing the PRIV_PROC_LOCK_MEMORY privilege might be a common
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   555
		 * problem so distinguish this situation from other issues.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   556
		 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   557
		if (errno == EPERM)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   558
			PK11err(PK11_F_MLOCK_PIN_IN_MEMORY,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   559
			    PK11_R_PRIV_PROC_LOCK_MEMORY_MISSING);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   560
		else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   561
			PK11err(PK11_F_MLOCK_PIN_IN_MEMORY,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   562
			    PK11_R_MLOCK_FAILED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   563
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   564
		/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   565
		 * We already have a problem here so there is no need to check
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   566
		 * that we could unmap the page. The PIN is not there yet
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   567
		 * anyway.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   568
		 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   569
		(void) munmap(addr, pagesize);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   570
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   571
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   572
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   573
	/* Copy the PIN to the mlocked memory. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   574
	token_pin = (char *)addr;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   575
	strlcpy(token_pin, pin, PK11_MAX_PIN_LEN + 1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   576
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   577
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   578
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   579
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   580
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   581
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   582
 * Log in to the keystore if we are supposed to do that at all. Take care of
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   583
 * reading and caching the PIN etc. Log in only once even when called from
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   584
 * multiple threads.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   585
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   586
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   587
 *	1 on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   588
 *	0 on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   589
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   590
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   591
pk11_token_login(CK_SESSION_HANDLE session, CK_BBOOL *login_done,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   592
    pkcs11_uri *uri_struct, CK_BBOOL is_private)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   593
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   594
	CK_RV rv;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   595
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   596
	if ((pubkey_token_flags & CKF_TOKEN_INITIALIZED) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   597
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   598
		PK11err(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   599
		    PK11_R_TOKEN_NOT_INITIALIZED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   600
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   601
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   602
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   603
	/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   604
	 * If login is required or needed but the PIN has not been even
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   605
	 * initialized we can bail out right now. Note that we are supposed to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   606
	 * always log in if we are going to access private keys. However, we may
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   607
	 * need to log in even for accessing public keys in case that the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   608
	 * CKF_LOGIN_REQUIRED flag is set.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   609
	 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   610
	if ((pubkey_token_flags & CKF_LOGIN_REQUIRED ||
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   611
	    is_private == CK_TRUE) && ~pubkey_token_flags &
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   612
	    CKF_USER_PIN_INITIALIZED)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   613
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   614
		PK11err(PK11_F_TOKEN_LOGIN, PK11_R_TOKEN_PIN_NOT_SET);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   615
		goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   616
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   617
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   618
	/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   619
	 * Note on locking: it is possible that more than one thread gets into
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   620
	 * pk11_get_pin() so we must deal with that. We cannot avoid it since we
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   621
	 * cannot guard fork() in there with a lock because we could end up in
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   622
	 * a dead lock in the child. Why? Remember we are in a multithreaded
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   623
	 * environment so we must lock all mutexes in the prefork function to
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   624
	 * avoid a situation in which a thread that did not call fork() held a
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   625
	 * lock, making future unlocking impossible. We lock right before
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   626
	 * C_Login().
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   627
	 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   628
	if (pubkey_token_flags & CKF_LOGIN_REQUIRED || is_private == CK_TRUE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   629
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   630
		if (*login_done == CK_FALSE &&
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   631
		    uri_struct->askpass == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   632
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   633
			PK11err(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   634
			    PK11_R_TOKEN_PIN_NOT_PROVIDED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   635
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   636
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   637
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   638
		if (*login_done == CK_FALSE &&
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   639
		    uri_struct->askpass != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   640
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   641
			if (pk11_get_pin(uri_struct->askpass,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   642
			    &uri_struct->pin) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   643
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   644
				PK11err(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   645
				    PK11_R_TOKEN_PIN_NOT_PROVIDED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   646
				goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   647
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   648
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   649
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   650
		/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   651
		 * Note that what we are logging into is the keystore from
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   652
		 * pubkey_SLOTID because we work with OP_RSA session type here.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   653
		 * That also means that we can work with only one keystore in
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   654
		 * the engine.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   655
		 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   656
		 * We must make sure we do not try to login more than once.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   657
		 * Also, see the comment above on locking strategy.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   658
		 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   659
		(void) pthread_mutex_lock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   660
		if (*login_done == CK_FALSE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   661
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   662
			if ((rv = pFuncList->C_Login(session,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   663
			    CKU_USER, (CK_UTF8CHAR*)uri_struct->pin,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   664
			    strlen(uri_struct->pin))) != CKR_OK)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   665
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   666
				PK11err_add_data(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   667
				    PK11_R_TOKEN_LOGIN_FAILED, rv);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   668
				goto err_locked;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   669
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   670
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   671
			*login_done = CK_TRUE;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   672
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   673
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   674
			 * Cache the passphrasedialog for possible child (which
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   675
			 * would need to relogin).
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   676
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   677
			if (passphrasedialog == NULL &&
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   678
			    uri_struct->askpass != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   679
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   680
				passphrasedialog =
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   681
				    strdup(uri_struct->askpass);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   682
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   683
				if (passphrasedialog == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   684
					{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   685
					PK11err_add_data(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   686
					    PK11_R_MALLOC_FAILURE, rv);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   687
					goto err_locked;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   688
					}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   689
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   690
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   691
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   692
			 * Check the PIN caching policy. Note that user might
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   693
			 * have provided a PIN even when no PIN was required -
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   694
			 * in that case we always remove the PIN from memory.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   695
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   696
			if (pk11_get_pin_caching_policy() ==
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   697
			    POLICY_WRONG_VALUE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   698
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   699
				PK11err(PK11_F_TOKEN_LOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   700
				    PK11_R_PIN_CACHING_POLICY_INVALID);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   701
				goto err_locked;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   702
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   703
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   704
			if (pk11_get_pin_caching_policy() != POLICY_NONE)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   705
				if (pk11_cache_pin(uri_struct->pin) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   706
					goto err_locked;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   707
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   708
		(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   709
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   710
	else
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   711
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   712
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   713
			 * If token does not require login we take it as the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   714
			 * login was done.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   715
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   716
			*login_done = CK_TRUE;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   717
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   718
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   719
	/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   720
	 * If we raced at pk11_get_pin() we must make sure that all threads that
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   721
	 * called pk11_get_pin() will erase the PIN from memory, not just the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   722
	 * one that called C_Login(). Note that if we were supposed to cache the
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   723
	 * PIN it was already cached by now so filling "uri_struct.pin" with
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   724
	 * zero bytes is always OK since pk11_cache_pin() makes a copy of it.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   725
	 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   726
	if (uri_struct->pin != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   727
		memset(uri_struct->pin, 0, strlen(uri_struct->pin));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   728
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   729
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   730
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   731
err_locked:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   732
	(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   733
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   734
	/* Always get rid of the PIN. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   735
	if (uri_struct->pin != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   736
		memset(uri_struct->pin, 0, strlen(uri_struct->pin));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   737
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   738
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   739
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   740
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   741
 * Log in to the keystore in the child if we were logged in in the parent. There
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   742
 * are similarities in the code with pk11_token_login() but still it is quite
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   743
 * different so we need a separate function for this.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   744
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   745
 * Note that this function is called under the locked session mutex when fork is
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   746
 * detected. That means that C_Login() will be called from the child just once.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   747
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   748
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   749
 *	1 on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   750
 *	0 on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   751
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   752
int
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   753
pk11_token_relogin(CK_SESSION_HANDLE session)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   754
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   755
	CK_RV rv;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   756
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   757
	/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   758
	 * We are in the child so check if we should login to the token again.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   759
	 * Note that it is enough to log in to the token through one session
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   760
	 * only, all already open and all future sessions can access the token
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   761
	 * then.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   762
	 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   763
	if (passphrasedialog != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   764
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   765
		char *pin = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   766
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   767
		/* If we cached the PIN then use it. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   768
		if (token_pin != NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   769
			pin = token_pin;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   770
		else if (pk11_get_pin(passphrasedialog, &pin) == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   771
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   772
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   773
		(void) pthread_mutex_lock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   774
		if ((rv = pFuncList->C_Login(session, CKU_USER,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   775
		    (CK_UTF8CHAR_PTR)pin, strlen(pin))) != CKR_OK)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   776
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   777
			PK11err_add_data(PK11_F_TOKEN_RELOGIN,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   778
			    PK11_R_TOKEN_LOGIN_FAILED, rv);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   779
			(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   780
			goto err;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   781
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   782
		(void) pthread_mutex_unlock(uri_lock);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   783
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   784
		/* Forget the PIN now if we did not cache it before. */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   785
		if (pin != token_pin)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   786
			{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   787
			memset(pin, 0, strlen(pin));
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   788
			OPENSSL_free(pin);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   789
			}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   790
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   791
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   792
	return (1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   793
err:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   794
	return (0);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   795
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   796
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   797
/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   798
 * This function forks and runs an external command. It would be nice if we
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   799
 * could use popen(3C)/pclose(3C) for that but unfortunately we need to be able
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   800
 * to get rid of the PIN from the memory. With p(open|close) function calls we
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   801
 * cannot control the stdio's memory used for buffering and our tests showed
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   802
 * that the PIN really stays there even after pclose().
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   803
 *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   804
 * Returns:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   805
 *	allocated buffer on success
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   806
 *	NULL on failure
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   807
 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   808
static char *
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   809
run_askpass(char *dialog)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   810
	{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   811
	pid_t pid;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   812
	int n, p[2];
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   813
	char *buf = NULL;
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   814
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   815
	if (pipe(p) == -1)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   816
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   817
		PK11err(PK11_F_RUN_ASKPASS, PK11_R_PIPE_FAILED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   818
		return (NULL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   819
		}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   820
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   821
	switch (pid = fork())
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   822
		{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   823
		case -1:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   824
			PK11err(PK11_F_RUN_ASKPASS, PK11_R_FORK_FAILED);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   825
			return (NULL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   826
		/* child */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   827
		case 0:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   828
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   829
			 * This should make sure that dup2() will not fail on
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   830
			 * file descriptor shortage.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   831
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   832
			close(p[0]);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   833
			(void) dup2(p[1], 1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   834
			close(p[1]);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   835
			/*
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   836
			 * Note that we cannot use PK11err() here since we are
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   837
			 * in the child. However, parent will get read() error
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   838
			 * so do not worry.
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   839
			 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   840
			(void) execl(dialog, basename(dialog), NULL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   841
			exit(1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   842
		/* parent */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   843
		default:
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   844
			/* +1 is for the terminating '\0' */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   845
			buf = (char *)OPENSSL_malloc(PK11_MAX_PIN_LEN + 1);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   846
			if (buf == NULL)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   847
				{
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   848
				PK11err(PK11_F_RUN_ASKPASS,
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   849
				    PK11_R_MALLOC_FAILURE);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   850
				return (NULL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   851
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   852
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   853
			close(p[1]);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   854
			n = read(p[0], buf, PK11_MAX_PIN_LEN);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   855
			if (n == -1 || n == 0)
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   856
				{
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   857
				PK11err(PK11_F_RUN_ASKPASS,
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   858
				    PK11_R_PIN_NOT_READ_FROM_COMMAND);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   859
				OPENSSL_free(buf);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   860
				return (NULL);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   861
				}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   862
			buf[n] = '\0';
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   863
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   864
			(void) waitpid(pid, NULL, 0);
7
f96733ecc878 Import sfw build 133
Cyril Plisko <cyril.plisko@grigale.com>
parents: 3
diff changeset
   865
		}
3
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   866
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   867
	return (buf);
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   868
	}
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   869
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   870
#endif	/* OPENSSL_NO_HW_PK11 */
ad3552f8e1ef Import sfw build 129
Cyril Plisko <cyril.plisko@grigale.com>
parents:
diff changeset
   871
#endif	/* OPENSSL_NO_HW */