extended asn1_algorithmIdentifier() to SHA-2

This commit is contained in:
Andreas Steffen
2008-01-22 10:32:37 +00:00
parent 2d49eaa131
commit cd543a69a2
12 changed files with 444 additions and 143 deletions
+81 -22
View File
@@ -1,6 +1,15 @@
/* Simple ASN.1 parser
* Copyright (C) 2000-2004 Andreas Steffen, Zuercher Hochschule Winterthur
* Copyright (C) 2006 Martin Will, Hochschule fuer Technik Rapperswil
/**
* @file asn1.c
*
* @brief Simple ASN.1 parser
*
*/
/*
* Copyright (C) 2006 Martin Will
* Copyright (C) 2000-2008 Andreas Steffen
*
* Hochschule fuer Technik Rapperswil
*
* 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
@@ -91,6 +100,27 @@ static u_char ASN1_sha1WithRSA_id_str[] = {
0x05, 0x00
};
static u_char ASN1_sha256WithRSA_id_str[] = {
0x30, 0x0D,
0x06, 0x09,
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
0x05, 0x00
};
static u_char ASN1_sha384WithRSA_id_str[] = {
0x30, 0x0D,
0x06, 0x09,
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C,
0x05, 0x00
};
static u_char ASN1_sha512WithRSA_id_str[] = {
0x30, 0x0D,
0x06, 0x09,
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0D,
0x05, 0x00
};
static u_char ASN1_rsaEncryption_id_str[] = {
0x30, 0x0D,
0x06, 0x09,
@@ -98,15 +128,18 @@ static u_char ASN1_rsaEncryption_id_str[] = {
0x05, 0x00
};
const chunk_t ASN1_md2_id = chunk_from_buf(ASN1_md2_id_str);
const chunk_t ASN1_md5_id = chunk_from_buf(ASN1_md5_id_str);
const chunk_t ASN1_sha1_id = chunk_from_buf(ASN1_sha1_id_str);
const chunk_t ASN1_sha256_id = chunk_from_buf(ASN1_sha256_id_str);
const chunk_t ASN1_sha384_id = chunk_from_buf(ASN1_sha384_id_str);
const chunk_t ASN1_sha512_id = chunk_from_buf(ASN1_sha512_id_str);
const chunk_t ASN1_rsaEncryption_id = chunk_from_buf(ASN1_rsaEncryption_id_str);
const chunk_t ASN1_md5WithRSA_id = chunk_from_buf(ASN1_md5WithRSA_id_str);
const chunk_t ASN1_sha1WithRSA_id = chunk_from_buf(ASN1_sha1WithRSA_id_str);
static const chunk_t ASN1_md2_id = chunk_from_buf(ASN1_md2_id_str);
static const chunk_t ASN1_md5_id = chunk_from_buf(ASN1_md5_id_str);
static const chunk_t ASN1_sha1_id = chunk_from_buf(ASN1_sha1_id_str);
static const chunk_t ASN1_sha256_id = chunk_from_buf(ASN1_sha256_id_str);
static const chunk_t ASN1_sha384_id = chunk_from_buf(ASN1_sha384_id_str);
static const chunk_t ASN1_sha512_id = chunk_from_buf(ASN1_sha512_id_str);
static const chunk_t ASN1_rsaEncryption_id = chunk_from_buf(ASN1_rsaEncryption_id_str);
static const chunk_t ASN1_md5WithRSA_id = chunk_from_buf(ASN1_md5WithRSA_id_str);
static const chunk_t ASN1_sha1WithRSA_id = chunk_from_buf(ASN1_sha1WithRSA_id_str);
static const chunk_t ASN1_sha256WithRSA_id = chunk_from_buf(ASN1_sha256WithRSA_id_str);
static const chunk_t ASN1_sha384WithRSA_id = chunk_from_buf(ASN1_sha384WithRSA_id_str);
static const chunk_t ASN1_sha512WithRSA_id = chunk_from_buf(ASN1_sha512WithRSA_id_str);
/* ASN.1 definiton of an algorithmIdentifier */
static const asn1Object_t algorithmIdentifierObjects[] = {
@@ -132,10 +165,24 @@ chunk_t asn1_algorithmIdentifier(int oid)
return ASN1_md5WithRSA_id;
case OID_SHA1_WITH_RSA:
return ASN1_sha1WithRSA_id;
case OID_SHA256_WITH_RSA:
return ASN1_sha256WithRSA_id;
case OID_SHA384_WITH_RSA:
return ASN1_sha384WithRSA_id;
case OID_SHA512_WITH_RSA:
return ASN1_sha512WithRSA_id;
case OID_MD2:
return ASN1_md2_id;
case OID_MD5:
return ASN1_md5_id;
case OID_SHA1:
return ASN1_sha1_id;
case OID_SHA256:
return ASN1_sha256_id;
case OID_SHA384:
return ASN1_sha384_id;
case OID_SHA512:
return ASN1_sha512_id;
default:
return chunk_empty;
}
@@ -705,6 +752,23 @@ chunk_t asn1_simple_object(asn1_t tag, chunk_t content)
return object;
}
/**
* Build an ASN.1 BITSTRING object
*/
chunk_t asn1_bitstring(const char *mode, chunk_t content)
{
chunk_t object;
u_char *pos = build_asn1_object(&object, ASN1_BIT_STRING, 1 + content.len);
*pos++ = 0x00;
memcpy(pos, content.ptr, content.len);
if (*mode == 'm')
{
free(content.ptr);
}
return object;
}
/**
* Build an ASN.1 object from a variable number of individual chunks.
* Depending on the mode, chunks either are moved ('m') or copied ('c').
@@ -736,17 +800,12 @@ chunk_t asn1_wrap(asn1_t type, const char *mode, ...)
{
chunk_t ch = va_arg(chunks, chunk_t);
switch (*mode++)
memcpy(pos, ch.ptr, ch.len);
pos += ch.len;
if (*mode++ == 'm')
{
case 'm':
memcpy(pos, ch.ptr, ch.len);
pos += ch.len;
free(ch.ptr);
break;
case 'c':
default:
memcpy(pos, ch.ptr, ch.len);
pos += ch.len;
free(ch.ptr);
}
}
va_end(chunks);
+15 -15
View File
@@ -1,6 +1,15 @@
/* Simple ASN.1 parser
* Copyright (C) 2000-2004 Andreas Steffen, Zuercher Hochschule Winterthur
* Copyright (C) 2006 Martin Will, Hochschule fuer Technik Rapperswil
/**
* @file asn1.h
*
* @brief Simple ASN.1 parser
*
*/
/*
* Copyright (C) 2006 Martin Will
* Copyright (C) 2000-2008 Andreas Steffen
*
* Hochschule fuer Technik Rapperswil
*
* 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
@@ -114,19 +123,9 @@ extern const chunk_t ASN1_INTEGER_0;
extern const chunk_t ASN1_INTEGER_1;
extern const chunk_t ASN1_INTEGER_2;
/* some popular algorithmIdentifiers */
extern const chunk_t ASN1_md2_id;
extern const chunk_t ASN1_md5_id;
extern const chunk_t ASN1_sha1_id;
extern const chunk_t ASN1_sha256_id;
extern const chunk_t ASN1_sha384_id;
extern const chunk_t ASN1_sha512_id;
extern const chunk_t ASN1_rsaEncryption_id;
extern const chunk_t ASN1_md5WithRSA_id;
extern const chunk_t ASN1_sha1WithRSA_id;
/* returns some popular algorithmIdentifiers */
extern chunk_t asn1_algorithmIdentifier(int oid);
extern int known_oid(chunk_t object);
extern u_int asn1_length(chunk_t *blob);
extern bool is_printablestring(chunk_t str);
@@ -144,6 +143,7 @@ extern void code_asn1_length(size_t length, chunk_t *code);
extern u_char* build_asn1_object(chunk_t *object, asn1_t type, size_t datalen);
extern chunk_t asn1_integer_from_mpz(const mpz_t value);
extern chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
extern chunk_t asn1_bitstring(const char *mode, chunk_t content);
extern chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
#endif /* _ASN1_H */