2001-11-20 22:21:58 +00:00
|
|
|
|
/*
|
2002-01-10 12:33:56 +00:00
|
|
|
|
* pkcs15-sec.c: PKCS#15 cryptography functions
|
2001-11-20 22:21:58 +00:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#include "sc-internal.h"
|
2001-11-24 13:32:52 +00:00
|
|
|
|
#include "opensc-pkcs15.h"
|
2001-12-20 12:22:18 +00:00
|
|
|
|
#include "sc-log.h"
|
2001-11-20 22:21:58 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2001-12-15 01:29:51 +00:00
|
|
|
|
#include <unistd.h>
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
|
|
|
|
int sc_pkcs15_decipher(struct sc_pkcs15_card *p15card,
|
|
|
|
|
const struct sc_pkcs15_prkey_info *prkey,
|
|
|
|
|
const u8 * in, int inlen, u8 *out, int outlen)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
struct sc_security_env senv;
|
2001-12-20 12:22:18 +00:00
|
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
2002-01-16 20:20:40 +00:00
|
|
|
|
senv.algorithm_ref = 0x0102;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
senv.key_file_id = prkey->file_id;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
senv.operation = SC_SEC_OPERATION_DECIPHER;
|
2002-01-16 20:20:40 +00:00
|
|
|
|
senv.key_ref = 0x0100 | prkey->key_reference;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
2001-12-21 23:34:47 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
2001-12-22 23:51:12 +00:00
|
|
|
|
r = sc_select_file(p15card->card, &p15card->file_app.path,
|
2001-12-25 20:45:48 +00:00
|
|
|
|
NULL);
|
2001-12-20 13:57:58 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_select_file() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/* FIXME! */
|
2001-11-20 22:21:58 +00:00
|
|
|
|
r = sc_restore_security_env(p15card->card, 0); /* empty SE */
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_restore_security_env() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#endif
|
2001-11-20 22:21:58 +00:00
|
|
|
|
r = sc_set_security_env(p15card->card, &senv);
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_set_security_env() failed");
|
2001-11-20 22:21:58 +00:00
|
|
|
|
r = sc_decipher(p15card->card, in, inlen, out, outlen);
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_decipher() failed");
|
2001-11-21 21:19:58 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,
|
|
|
|
|
const struct sc_pkcs15_prkey_info *prkey,
|
|
|
|
|
int hash, const u8 *in, int inlen, u8 *out,
|
|
|
|
|
int outlen)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
struct sc_security_env senv;
|
2001-12-20 12:22:18 +00:00
|
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2001-11-21 21:19:58 +00:00
|
|
|
|
|
2002-01-16 20:20:40 +00:00
|
|
|
|
senv.algorithm_ref = 0x0102;
|
2001-11-21 21:19:58 +00:00
|
|
|
|
switch (hash) {
|
|
|
|
|
case SC_PKCS15_HASH_SHA1:
|
|
|
|
|
senv.algorithm_ref |= 0x10;
|
|
|
|
|
break;
|
|
|
|
|
case SC_PKCS15_HASH_NONE:
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
senv.key_file_id = prkey->file_id;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
senv.operation = SC_SEC_OPERATION_SIGN;
|
2002-01-16 20:20:40 +00:00
|
|
|
|
senv.key_ref = 0x0100 | prkey->key_reference;
|
2001-11-21 21:19:58 +00:00
|
|
|
|
|
2001-12-21 23:34:47 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
2001-12-22 23:51:12 +00:00
|
|
|
|
r = sc_select_file(p15card->card, &p15card->file_app.path,
|
2001-12-25 20:45:48 +00:00
|
|
|
|
NULL);
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_select_file() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/* FIXME! */
|
2001-11-21 21:19:58 +00:00
|
|
|
|
r = sc_restore_security_env(p15card->card, 0); /* empty SE */
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_restore_security_env() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#endif
|
2001-11-21 21:19:58 +00:00
|
|
|
|
r = sc_set_security_env(p15card->card, &senv);
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_set_security_env() failed");
|
2001-11-21 21:19:58 +00:00
|
|
|
|
r = sc_compute_signature(p15card->card, in, inlen, out, outlen);
|
2001-12-20 12:22:18 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "sc_compute_signature() failed");
|
2001-11-21 21:19:58 +00:00
|
|
|
|
|
|
|
|
|
return r;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
}
|