- added support for base64 encoding

- added certtest tool


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@33 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2001-11-04 13:57:04 +00:00
parent 3f727ca8b7
commit 58fd72f22f
5 changed files with 133 additions and 2 deletions

115
src/libopensc/base64.c Normal file
View File

@ -0,0 +1,115 @@
/* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
#include "sc.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
static const u8 base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"0123456789+/=";
static const u8 bin_table[128] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,
0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,
0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
};
static void to_base64(unsigned int i, u8 *out, int fillers)
{
unsigned int c, s = 18;
for (c = 0; c < 4; c++) {
if (fillers >= 4 - c)
*out = base64_table[64];
else
*out = base64_table[(i >> s) & 0x3f];
out++;
s -= 6;
}
}
int sc_base64_convert_to(const u8 *in, int len, u8 *out, int outlen, int linelength)
{
unsigned int i, chars = 0, c;
#warning sc_base64_convert_to() does not work
linelength -= linelength & 0x03;
if (linelength < 0)
return SC_ERROR_INVALID_ARGUMENTS;
while (len >= 3) {
i = in[2] + (in[1] << 8) + (in[0] << 16);
in += 3;
len -= 3;
if (outlen < 4)
return SC_ERROR_BUFFER_TOO_SMALL;
to_base64(i, out, 0);
out += 4;
outlen -= 4;
chars += 4;
if (chars >= linelength && linelength > 0) {
if (outlen < 1)
return SC_ERROR_BUFFER_TOO_SMALL;
*out = '\n';
out++;
outlen--;
chars = 0;
}
}
i = c = 0;
c = (3-len) << 3;
while (c) {
i |= *in++ << c;
c -= 8;
}
if (len) {
if (outlen < 4)
return SC_ERROR_BUFFER_TOO_SMALL;
to_base64(i, out, 3-len);
out += 4;
outlen -= 4;
chars += 4;
}
if (chars && linelength > 0) {
if (outlen < 1)
return SC_ERROR_BUFFER_TOO_SMALL;
*out = '\n';
out++;
outlen--;
}
if (outlen < 1)
return SC_ERROR_BUFFER_TOO_SMALL;
*out = 0;
return 0;
}

View File

@ -39,6 +39,7 @@
#define SC_ERROR_SECURITY_STATUS_NOT_SATISFIED -1014
#define SC_ERROR_CONNECTING_TO_RES_MGR -1015
#define SC_ERROR_INVALID_ASN1_OBJECT -1016
#define SC_ERROR_BUFFER_TOO_SMALL -1017
#define SC_APDU_CASE_NONE 0
#define SC_APDU_CASE_1 1
@ -129,11 +130,15 @@ struct sc_security_env {
int key_ref;
};
/* ASN.1 parsing functions */
const u8 *sc_asn1_find_tag(const u8 * buf, int buflen, int tag, int *taglen);
const u8 *sc_asn1_verify_tag(const u8 * buf, int buflen, int tag, int *taglen);
const u8 *sc_asn1_skip_tag(const u8 ** buf, int *buflen, int tag, int *taglen);
/* ASN.1 printing functions */
void sc_asn1_print_tags(const u8 * buf, int buflen);
/* ASN.1 object decoding functions */
int sc_asn1_utf8string_to_ascii(const u8 * buf,
int buflen, u8 * outbuf, int outlen);
int sc_asn1_decode_bit_string(const u8 * inbuf,
@ -145,6 +150,10 @@ int sc_asn1_decode_integer(const u8 * inbuf, int inlen, int *out);
int sc_asn1_decode_object_id(const u8 * inbuf, int inlen,
struct sc_object_id *id);
/* Base64 converter functions */
int sc_base64_convert_to(const u8 *in, int inlen, u8 *out, int outlen,
int linelength);
/* internal functions */
int sc_check_apdu(const struct sc_apdu *apdu);
int sc_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu);

View File

@ -54,10 +54,12 @@ static int parse_cert(const u8 *buf, int buflen, struct sc_pkcs15_cert *cert)
u8 *tmpbuf;
int taglen, left, r;
struct sc_pkcs15_rsa_pubkey *key = &cert->key;
const u8 *buf0 = buf;
buf = sc_asn1_verify_tag(buf, buflen, 0x30, &buflen); /* SEQUENCE */
if (buf == NULL) /* Certificate */
return SC_ERROR_INVALID_ASN1_OBJECT;
cert->data_len = (buf - buf0) + buflen;
p = sc_asn1_skip_tag(&buf, &buflen, 0x30, &left); /* SEQUENCE */
if (p == NULL) /* tbsCertificate */
return SC_ERROR_INVALID_ASN1_OBJECT;
@ -175,7 +177,7 @@ int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card,
return SC_ERROR_INVALID_ASN1_OBJECT;
}
cert->data = data;
cert->data_len = len;
// cert->data_len = len;
*cert_out = cert;
return 0;
}

View File

@ -610,6 +610,7 @@ const char *sc_strerror(int error)
"PIN code incorrect",
"Security status not satisfied",
"Error connecting to Resource Manager",
"Buffer too small",
};
int nr_errors = sizeof(errors) / sizeof(errors[0]);

View File

@ -27,6 +27,10 @@ int main(int argc, char *argv[])
freq[i] = 0;
c = 0;
while (1) {
if ((c % 10) == 1) {
printf(".");
fflush(stdout);
}
if (c == 0)
gettimeofday(&tv1, NULL);
if (sc_get_random(card, buf, 8) != 0) {
@ -41,7 +45,7 @@ int main(int argc, char *argv[])
gettimeofday(&tv2, NULL);
foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000;
printf("Time to generate 64 bits of randomness: %lld ms\n",
printf("\nTime to generate 64 bits of randomness: %lld ms\n",
(foo - foo2)/100);
printf("Frequencies:\n");
for (i = 0; i < 256; i++) {