/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* ** symkeyutil.c ** ** utility for managing symetric keys in the database or the token **
*/
/* * Wish List for this utility: * 1) Display and Set the CKA_ operation flags for the key. * 2) Modify existing keys * 3) Copy keys * 4) Read CKA_ID and display for keys. * 5) Option to store CKA_ID in a file on key creation. * 6) Encrypt, Decrypt, Hash, and Mac with generated keys. * 7) Use asymetric keys to wrap and unwrap keys. * 8) Derive. * 9) PBE keys.
*/
staticvoid
LongUsage(char *progName)
{ int i;
FPS "%-15s List all the keys.\n", "-L");
FPS "%-15s Generate a new key.\n", "-K");
FPS "%-20s Specify the nickname of the new key\n", " -n name");
FPS "%-20s Specify the id in hex of the new key\n", " -i key id");
FPS "%-20s Specify a file to read the id of the new key\n", " -j key id file");
FPS "%-20s Specify the keyType of the new key\n", " -t type");
FPS "%-20s", " valid types: "); for (i = 0; i < keyArraySize; i++) {
FPS "%s%c", keyArray[i].label, i == keyArraySize-1? '\n':',');
}
FPS "%-20s Specify the size of the new key in bytes (required by some types)\n", " -s size");
FPS "%-15s Delete a key.\n", "-D");
FPS "%-20s Specify the nickname of the key to delete\n", " -n name");
FPS "%-20s Specify the id in hex of the key to delete\n", " -i key id");
FPS "%-20s Specify a file to read the id of the key to delete\n", " -j key id file");
FPS "%-15s Import a new key from a data file.\n", "-I");
FPS "%-20s Specify the data file to read the key from.\n", " -k key file");
FPS "%-20s Specify the nickname of the new key\n", " -n name");
FPS "%-20s Specify the id in hex of the new key\n", " -i key id");
FPS "%-20s Specify a file to read the id of the new key\n", " -j key id file");
FPS "%-20s Specify the keyType of the new key\n", " -t type");
FPS "%-20s", " valid types: "); for (i = 0; i < keyArraySize; i++) {
FPS "%s%c", keyArray[i].label, i == keyArraySize-1? '\n':',');
}
FPS "%-15s Export a key to a data file.\n", "-E");
FPS "%-20s Specify the data file to write the key to.\n", " -k key file");
FPS "%-20s Specify the nickname of the key to export\n", " -n name");
FPS "%-20s Specify the id in hex of the key to export\n", " -i key id");
FPS "%-20s Specify a file to read the id of the key to export\n", " -j key id file");
FPS "%-15s Move a key to a new token.\n", "-M");
FPS "%-20s Specify the nickname of the key to move\n", " -n name");
FPS "%-20s Specify the id in hex of the key to move\n", " -i key id");
FPS "%-20s Specify a file to read the id of the key to move\n", " -j key id file");
FPS "%-20s Specify the token to move the key to\n", " -g target token");
FPS "%-15s Unwrap a new key from a data file.\n", "-U");
FPS "%-20s Specify the data file to read the encrypted key from.\n", " -k key file");
FPS "%-20s Specify the nickname of the new key\n", " -n name");
FPS "%-20s Specify the id in hex of the new key\n", " -i key id");
FPS "%-20s Specify a file to read the id of the new key\n", " -j key id file");
FPS "%-20s Specify the keyType of the new key\n", " -t type");
FPS "%-20s", " valid types: "); for (i = 0; i < keyArraySize; i++) {
FPS "%s%c", keyArray[i].label, i == keyArraySize-1? '\n':',');
}
FPS "%-20s Specify the nickname of the wrapping key\n", " -w wrap name");
FPS "%-20s Specify the id in hex of the wrapping key\n", " -x wrap key id");
FPS "%-20s Specify a file to read the id of the wrapping key\n", " -y wrap key id file");
FPS "%-15s Wrap a new key to a data file. [not yet implemented]\n", "-W");
FPS "%-20s Specify the data file to write the encrypted key to.\n", " -k key file");
FPS "%-20s Specify the nickname of the key to wrap\n", " -n name");
FPS "%-20s Specify the id in hex of the key to wrap\n", " -i key id");
FPS "%-20s Specify a file to read the id of the key to wrap\n", " -j key id file");
FPS "%-20s Specify the nickname of the wrapping key\n", " -w wrap name");
FPS "%-20s Specify the id in hex of the wrapping key\n", " -x wrap key id");
FPS "%-20s Specify a file to read the id of the wrapping key\n", " -y wrap key id file");
FPS "%-15s Options valid for all commands\n", "std_opts");
FPS "%-20s The directory where the NSS db's reside\n", " -d certdir");
FPS "%-20s Prefix for the NSS db's\n", " -P db prefix");
FPS "%-20s Specify password on the command line\n", " -p password");
FPS "%-20s Specify password file on the command line\n", " -f password file");
FPS "%-20s Specify token to act on\n", " -h token"); exit(1); #undef FPS
}
/* -h specify token name */ if (symKeyUtil.options[opt_TokenName].activated) { if (PL_strcmp(symKeyUtil.options[opt_TokenName].arg, "all") == 0)
slotname = NULL; else
slotname = PL_strdup(symKeyUtil.options[opt_TokenName].arg);
}
/* -t key type */ if (symKeyUtil.options[opt_KeyType].activated) {
keyType = GetKeyMechFromString(symKeyUtil.options[opt_KeyType].arg); if (keyType == (CK_MECHANISM_TYPE)-1) {
PR_fprintf(PR_STDERR, "%s unknown key type (%s).\n",
progName, symKeyUtil.options[opt_KeyType].arg); return 255;
}
}
/* -k for import and unwrap, it specifies an input file to read from,
* for export and wrap it specifies an output file to write to */ if (symKeyUtil.options[opt_KeyFile].activated) { if (symKeyUtil.commands[cmd_ImportKey].activated ||
symKeyUtil.commands[cmd_UnwrapKey].activated) { int ret = ReadBuf(symKeyUtil.options[opt_KeyFile].arg, &key); if (ret < 0) {
PR_fprintf(PR_STDERR, "%s Couldn't read key file (%s).\n",
progName, symKeyUtil.options[opt_KeyFile].arg); return 255;
}
}
}
/* -i specify the key ID */ if (symKeyUtil.options[opt_KeyID].activated) { int ret = HexToBuf(symKeyUtil.options[opt_KeyID].arg, &keyID); if (ret < 0) {
PR_fprintf(PR_STDERR, "%s invalid key ID (%s).\n",
progName, symKeyUtil.options[opt_KeyID].arg); return 255;
}
}
/* -i & -j are mutually exclusive */ if ((symKeyUtil.options[opt_KeyID].activated) &&
(symKeyUtil.options[opt_KeyIDFile].activated)) {
PR_fprintf(PR_STDERR, "%s -i and -j options are mutually exclusive.\n", progName); return 255;
}
/* -x specify the Wrap key ID */ if (symKeyUtil.options[opt_WrapKeyID].activated) { int ret = HexToBuf(symKeyUtil.options[opt_WrapKeyID].arg, &wrapKeyID); if (ret < 0) {
PR_fprintf(PR_STDERR, "%s invalid key ID (%s).\n",
progName, symKeyUtil.options[opt_WrapKeyID].arg); return 255;
}
}
/* -x & -y are mutually exclusive */ if ((symKeyUtil.options[opt_KeyID].activated) &&
(symKeyUtil.options[opt_KeyIDFile].activated)) {
PR_fprintf(PR_STDERR, "%s -i and -j options are mutually exclusive.\n", progName); return 255;
}
/* -y specify the key ID */ if (symKeyUtil.options[opt_WrapKeyIDFile].activated) { int ret = ReadBuf(symKeyUtil.options[opt_WrapKeyIDFile].arg,
&wrapKeyID); if (ret < 0) {
PR_fprintf(PR_STDERR, "%s Couldn't read key ID file (%s).\n",
progName, symKeyUtil.options[opt_WrapKeyIDFile].arg); return 255;
}
}
/* -P certdb name prefix */ if (symKeyUtil.options[opt_dbPrefix].activated)
certPrefix = symKeyUtil.options[opt_dbPrefix].arg;
/* Check number of commands entered. */
commandsEntered = 0; for (i = 0; i < symKeyUtil.numCommands; i++) { if (symKeyUtil.commands[i].activated) {
commandToRun = symKeyUtil.commands[i].flag;
commandsEntered++;
} if (commandsEntered > 1) break;
} if (commandsEntered > 1) {
PR_fprintf(PR_STDERR, "%s: only one command at a time!\n", progName);
PR_fprintf(PR_STDERR, "You entered: "); for (i = 0; i < symKeyUtil.numCommands; i++) { if (symKeyUtil.commands[i].activated)
PR_fprintf(PR_STDERR, " -%c", symKeyUtil.commands[i].flag);
}
PR_fprintf(PR_STDERR, "\n"); return 255;
} if (commandsEntered == 0) {
PR_fprintf(PR_STDERR, "%s: you must enter a command!\n", progName);
Usage(progName);
}
if ((symKeyUtil.commands[cmd_ImportKey].activated ||
symKeyUtil.commands[cmd_ExportKey].activated ||
symKeyUtil.commands[cmd_WrapKey].activated ||
symKeyUtil.commands[cmd_UnwrapKey].activated) &&
!symKeyUtil.options[opt_KeyFile].activated) {
PR_fprintf(PR_STDERR, "%s -%c: keyfile is required for this command (-k).\n",
progName, commandToRun); return 255;
}
/* -E, -D, -W, and all require -n, -i, or -j to identify the key */ if ((symKeyUtil.commands[cmd_ExportKey].activated ||
symKeyUtil.commands[cmd_DeleteKey].activated ||
symKeyUtil.commands[cmd_WrapKey].activated) &&
!(symKeyUtil.options[opt_Nickname].activated ||
symKeyUtil.options[opt_KeyID].activated ||
symKeyUtil.options[opt_KeyIDFile].activated)) {
PR_fprintf(PR_STDERR, "%s -%c: nickname or id is required for this command (-n, -i, -j).\n",
progName, commandToRun); return 255;
}
/* -W, -U, and all -w, -x, or -y to identify the wrapping key */ if ((symKeyUtil.commands[cmd_WrapKey].activated ||
symKeyUtil.commands[cmd_UnwrapKey].activated) &&
!(symKeyUtil.options[opt_WrapKeyName].activated ||
symKeyUtil.options[opt_WrapKeyID].activated ||
symKeyUtil.options[opt_WrapKeyIDFile].activated)) {
PR_fprintf(PR_STDERR, "%s -%c: wrap key is required for this command (-w, -x, or -y).\n",
progName, commandToRun); return 255;
}
/* -M needs the target slot (-g) */ if (symKeyUtil.commands[cmd_MoveKey].activated &&
!symKeyUtil.options[opt_TargetToken].activated) {
PR_fprintf(PR_STDERR, "%s -%c: target token is required for this command (-g).\n",
progName, commandToRun); return 255;
}
/* Using slotname == NULL for listing keys and certs on all slots,
* but only that. */ if (!(symKeyUtil.commands[cmd_ListKeys].activated) && slotname == NULL) {
PR_fprintf(PR_STDERR, "%s -%c: cannot use \"-h all\" for this command.\n",
progName, commandToRun); return 255;
}
name = SECU_GetOptionArg(&symKeyUtil, opt_Nickname);
wrapName = SECU_GetOptionArg(&symKeyUtil, opt_WrapKeyName);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.