/* * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /***************************************************************************** softhsm2-util.cpp This program can be used for interacting with HSMs using PKCS#11. The default library is the libsofthsm2.so *****************************************************************************/ #include #include "softhsm2-util.h" #include "findslot.h" #include "getpw.h" #include "library.h" #include "log.h" #include "Configuration.h" #include "SimpleConfigLoader.h" #include "Directory.h" #include "MutexFactory.h" #include "ObjectStoreToken.h" #include "OSPathSep.h" #if defined(WITH_OPENSSL) #include "OSSLCryptoFactory.h" #else #include "BotanCryptoFactory.h" #endif #include #include #include #include #ifndef _WIN32 #include #include #include #include #else #include #include #endif #include #include // Initialise the one-and-only instance #ifdef HAVE_CXX11 std::unique_ptr MutexFactory::instance(nullptr); std::unique_ptr SecureMemoryRegistry::instance(nullptr); #if defined(WITH_OPENSSL) std::unique_ptr OSSLCryptoFactory::instance(nullptr); #else std::unique_ptr BotanCryptoFactory::instance(nullptr); #endif #else std::auto_ptr MutexFactory::instance(NULL); std::auto_ptr SecureMemoryRegistry::instance(NULL); #if defined(WITH_OPENSSL) std::auto_ptr OSSLCryptoFactory::instance(NULL); #else std::auto_ptr BotanCryptoFactory::instance(NULL); #endif #endif // Display the usage void usage() { printf("Support tool for PKCS#11\n"); printf("Usage: softhsm2-util [ACTION] [OPTIONS]\n"); printf("Action:\n"); printf(" --delete-token Delete the token at a given slot.\n"); printf(" Use with --token or --serial.\n"); printf(" WARNING: Any content in token will be erased.\n"); printf(" -h Shows this help screen.\n"); printf(" --help Shows this help screen.\n"); printf(" --import Import a key pair from the given path.\n"); printf(" The file must be in PKCS#8-format.\n"); printf(" Use with --slot or --token or --serial, --file-pin,\n"); printf(" --label, --id, --no-public-key, and --pin.\n"); printf(" --init-token Initialize the token at a given slot.\n"); printf(" Use with --slot or --token or --serial or --free,\n"); printf(" --label, --so-pin, and --pin.\n"); printf(" WARNING: Any content in token will be erased.\n"); printf(" --show-slots Display all the available slots.\n"); printf(" -v Show version info.\n"); printf(" --version Show version info.\n"); printf("Options:\n"); printf(" --aes Used to tell import to use file as is and import it as AES.\n"); printf(" --file-pin Supply a PIN if the file is encrypted.\n"); printf(" --force Used to override a warning.\n"); printf(" --free Use the first free/uninitialized token.\n"); printf(" --id Defines the ID of the object. Hexadecimal characters.\n"); printf(" Use with --force if multiple key pairs may share\n"); printf(" the same ID.\n"); printf(" --label Defines the label of the object or the token.\n"); printf(" --module Use another PKCS#11 library than SoftHSM.\n"); printf(" --no-public-key Do not import the public key.\n"); printf(" --pin The PIN for the normal user.\n"); printf(" --serial Will use the token with a matching serial number.\n"); printf(" --slot The slot where the token is located.\n"); printf(" --so-pin The PIN for the Security Officer (SO).\n"); printf(" --token