diff options
Diffstat (limited to 'Source/charon/asn1')
-rw-r--r-- | Source/charon/asn1/Makefile.asn1 | 24 | ||||
-rw-r--r-- | Source/charon/asn1/asn1.c | 60 | ||||
-rw-r--r-- | Source/charon/asn1/asn1.h | 116 | ||||
-rw-r--r-- | Source/charon/asn1/der_decoder.c | 218 | ||||
-rw-r--r-- | Source/charon/asn1/der_decoder.h | 61 | ||||
-rw-r--r-- | Source/charon/asn1/der_encoder.c | 218 | ||||
-rw-r--r-- | Source/charon/asn1/der_encoder.h | 60 |
7 files changed, 757 insertions, 0 deletions
diff --git a/Source/charon/asn1/Makefile.asn1 b/Source/charon/asn1/Makefile.asn1 new file mode 100644 index 000000000..4aaedf4fe --- /dev/null +++ b/Source/charon/asn1/Makefile.asn1 @@ -0,0 +1,24 @@ +# Copyright (C) 2005 Jan Hutter, Martin Willi +# 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 +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. +# +# 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. +# + +ASN1_DIR= $(MAIN_DIR)asn1/ + + +OBJS+= $(BUILD_DIR)asn1.o +$(BUILD_DIR)asn1.o : $(ASN1_DIR)asn1.c $(ASN1_DIR)asn1.h + $(CC) $(CFLAGS) -c -o $@ $< + +OBJS+= $(BUILD_DIR)der_decoder.o +$(BUILD_DIR)der_decoder.o : $(ASN1_DIR)der_decoder.c $(ASN1_DIR)der_decoder.h + $(CC) $(CFLAGS) -c -o $@ $<
\ No newline at end of file diff --git a/Source/charon/asn1/asn1.c b/Source/charon/asn1/asn1.c new file mode 100644 index 000000000..cbd030bb5 --- /dev/null +++ b/Source/charon/asn1/asn1.c @@ -0,0 +1,60 @@ + + + + + + +#include "asn1.h" + + + + + +mapping_t asn1_type_m[] = { + {ASN1_END, "ASN1_END"}, + {ASN1_BOOLEAN, "ASN1_BOOLEAN"}, + {ASN1_INTEGER, "ASN1_INTEGER"}, + {ASN1_BIT_STRING, "ASN1_BIT_STRING"}, + {ASN1_OCTET_STRING, "ASN1_OCTET_STRING"}, + {ASN1_NULL, "ASN1_NULL"}, + {ASN1_OID, "ASN1_OID"}, + {ASN1_ENUMERATED, "ASN1_ENUMERATED"}, + {ASN1_UTF8STRING, "ASN1_UTF8STRING"}, + {ASN1_NUMERICSTRING, "ASN1_NUMERICSTRING"}, + {ASN1_PRINTABLESTRING, "ASN1_PRINTABLESTRING"}, + {ASN1_T61STRING, "ASN1_T61STRING"}, + {ASN1_VIDEOTEXSTRING, "ASN1_VIDEOTEXSTRING"}, + {ASN1_IA5STRING, "ASN1_IA5STRING"}, + {ASN1_UTCTIME, "ASN1_UTCTIME"}, + {ASN1_GENERALIZEDTIME, "ASN1_GENERALIZEDTIME"}, + {ASN1_GRAPHICSTRING, "ASN1_GRAPHICSTRING"}, + {ASN1_VISIBLESTRING, "ASN1_VISIBLESTRING"}, + {ASN1_GENERALSTRING, "ASN1_GENERALSTRING"}, + {ASN1_UNIVERSALSTRING, "ASN1_UNIVERSALSTRING"}, + {ASN1_BMPSTRING, "ASN1_BMPSTRING"}, + {ASN1_CONSTRUCTED, "ASN1_CONSTRUCTED"}, + {ASN1_SEQUENCE, "ASN1_SEQUENCE"}, + {ASN1_SET, "ASN1_SET"}, + {ASN1_TAG_E_0, "ASN1_TAG_E_0"}, + {ASN1_TAG_E_1, "ASN1_TAG_E_1"}, + {ASN1_TAG_E_2, "ASN1_TAG_E_2"}, + {ASN1_TAG_E_3, "ASN1_TAG_E_3"}, + {ASN1_TAG_E_4, "ASN1_TAG_E_4"}, + {ASN1_TAG_E_5, "ASN1_TAG_E_5"}, + {ASN1_TAG_E_6, "ASN1_TAG_E_6"}, + {ASN1_TAG_E_7, "ASN1_TAG_E_7"}, + {ASN1_TAG_I_1, "ASN1_TAG_I_1"}, + {ASN1_TAG_I_2, "ASN1_TAG_I_2"}, + {ASN1_TAG_I_3, "ASN1_TAG_I_3"}, + {ASN1_TAG_I_4, "ASN1_TAG_I_4"}, + {ASN1_TAG_I_5, "ASN1_TAG_I_5"}, + {ASN1_TAG_I_6, "ASN1_TAG_I_6"}, + {ASN1_TAG_I_7, "ASN1_TAG_I_7"}, +}; + +mapping_t asn1_flag_m[] = { + {ASN1_OPTIONAL, "ASN1_OPTIONAL"}, + {ASN1_DEFAULT, "ASN1_DEFAULT"}, + {ASN1_MPZ, "ASN1_MPZ"}, + {ASN1_OF, "ASN1_OF"}, +}; diff --git a/Source/charon/asn1/asn1.h b/Source/charon/asn1/asn1.h new file mode 100644 index 000000000..a828034ee --- /dev/null +++ b/Source/charon/asn1/asn1.h @@ -0,0 +1,116 @@ +/** + * @file asn1.h + * + * @brief Definition of asn1_type_t and asn1_rule_t. + * + */ + +/* + * Copyright (C) 2006 Martin Willi + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#ifndef ASN1_H_ +#define ASN1_H_ + +#include <types.h> + +typedef enum asn1_type_t asn1_type_t; + +enum asn1_type_t { + ASN1_END = 0x00, + ASN1_BOOLEAN = 0x01, + ASN1_INTEGER = 0x02, + ASN1_BIT_STRING = 0x03, + ASN1_OCTET_STRING = 0x04, + ASN1_NULL = 0x05, + ASN1_OID = 0x06, + ASN1_ENUMERATED = 0x0A, + ASN1_UTF8STRING = 0x0C, + ASN1_NUMERICSTRING = 0x12, + ASN1_PRINTABLESTRING = 0x13, + ASN1_T61STRING = 0x14, + ASN1_VIDEOTEXSTRING = 0x15, + ASN1_IA5STRING = 0x16, + ASN1_UTCTIME = 0x17, + ASN1_GENERALIZEDTIME = 0x18, + ASN1_GRAPHICSTRING = 0x19, + ASN1_VISIBLESTRING = 0x1A, + ASN1_GENERALSTRING = 0x1B, + ASN1_UNIVERSALSTRING = 0x1C, + ASN1_BMPSTRING = 0x1E, + ASN1_CONSTRUCTED = 0x20, + ASN1_SEQUENCE = 0x30, + ASN1_SET = 0x31, + ASN1_TAG_E_0 = 0xA0, + ASN1_TAG_E_1 = 0xA1, + ASN1_TAG_E_2 = 0xA2, + ASN1_TAG_E_3 = 0xA3, + ASN1_TAG_E_4 = 0xA4, + ASN1_TAG_E_5 = 0xA5, + ASN1_TAG_E_6 = 0xA6, + ASN1_TAG_E_7 = 0xA7, + ASN1_TAG_I_1 = 0x81, + ASN1_TAG_I_2 = 0x82, + ASN1_TAG_I_3 = 0x83, + ASN1_TAG_I_4 = 0x84, + ASN1_TAG_I_5 = 0x85, + ASN1_TAG_I_6 = 0x86, + ASN1_TAG_I_7 = 0x87, +}; + +extern mapping_t asn1_type_m[]; + +typedef enum asn1_flag_t asn1_flag_t; + +enum asn1_flag_t { + ASN1_OPTIONAL = 0x01, + ASN1_DEFAULT = 0x02, + ASN1_MPZ = 0x04, + ASN1_OF = 0x08, +}; + +extern mapping_t asn1_flag_m[]; + + +typedef struct asn1_rule_t asn1_rule_t; + +struct asn1_rule_t { + /** + * ASN1 type + */ + asn1_type_t type; + /** + * implicit or explicit tag, if any + */ + asn1_flag_t flags; + /** + * offset of data in structure + */ + u_int data_offset; +// union { + /** + * offset to a boolean, which says if optional + * data is available at data_offset. Used if + * flags & ASN1_OPTIONAL. + */ +// u_int available_offset; + /** + * default value, used if flags & ASN1_DEFAULT + */ + u_int default_value; +// }; +}; + + +#endif /* ASN1_H_ */ diff --git a/Source/charon/asn1/der_decoder.c b/Source/charon/asn1/der_decoder.c new file mode 100644 index 000000000..59ea4b077 --- /dev/null +++ b/Source/charon/asn1/der_decoder.c @@ -0,0 +1,218 @@ +/** + * @file der_decoder.c + * + * @brief Implementation of der_decoder_t. + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#include <gmp.h> + +#include "der_decoder.h" + +#include <utils/allocator.h> +#include <daemon.h> + + + +typedef struct private_der_decoder_t private_der_decoder_t; + +/** + * Private data of a der_decoder_t object. + */ +struct private_der_decoder_t { + /** + * Public interface for this signer. + */ + der_decoder_t public; + + asn1_rule_t *rule; + + asn1_rule_t *first_rule; + + void *output; + + logger_t *logger; +}; + +status_t read_hdr(private_der_decoder_t *this, chunk_t *data); + +status_t read_sequence(private_der_decoder_t *this, chunk_t data) +{ + while (this->rule->type != ASN1_END) + { + read_hdr(this, &data); + } + return SUCCESS; +} + + +status_t read_int(private_der_decoder_t *this, chunk_t data) +{ + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER", data); + u_int *integ = (u_int*)((u_int8_t*)this->output + this->rule->data_offset); + + *integ = 0; + while (data.len-- > 0) + { + *integ = 256 * (*integ) + *data.ptr++; + } + return SUCCESS; +} + +status_t read_mpz(private_der_decoder_t *this, chunk_t data) +{ + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER as mpz", data); + mpz_t *mpz = (mpz_t*)((u_int8_t*)this->output + this->rule->data_offset); + + mpz_import(*mpz, data.len, 1, 1, 1, 0, data.ptr); + return SUCCESS; +} + +u_int32_t read_length(chunk_t *data) +{ + u_int8_t n; + size_t len; + + /* read first octet of length field */ + n = *data->ptr++; + + if ((n & 0x80) == 0) + { + /* single length octet */ + return n; + } + + /* composite length, determine number of length octets */ + n &= 0x7f; + + if (n > data->len) + { + /* length longer than available bytes */ + return -1; + } + + if (n > sizeof(len)) + { + /* larger than size_t can hold */ + return -1; + } + + len = 0; + while (n-- > 0) + { + len = 256 * len + *data->ptr++; + } + return len; +} + +status_t read_hdr(private_der_decoder_t *this, chunk_t *data) +{ + chunk_t inner; + + /* advance to the next rule */ + this->rule++; + + if (this->rule->type == ASN1_END) + { + return SUCCESS; + } + + this->logger->log(this->logger, CONTROL|LEVEL2, "reading header of rule %s", + mapping_find(asn1_type_m, this->rule->type)); + + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "reading from:", *data); + + /* read type, advance in data */ + if (*(data->ptr) != this->rule->type) + { + this->logger->log(this->logger, CONTROL|LEVEL2, "Bad byte found (%x)", *data->ptr); + return PARSE_ERROR; + } + data->ptr++; + data->len--; + + /* read length, advance in data */ + inner.len = read_length(data); + if (inner.len == -1) + { + this->logger->log(this->logger, CONTROL|LEVEL2, "Error reading length"); + return PARSE_ERROR; + } + this->logger->log(this->logger, CONTROL|LEVEL2, "Length is %d", + inner.len); + inner.ptr = data->ptr; + + /* advance in data */ + data->ptr += inner.len; + data->len -= inner.len; + + /* process inner */ + switch (this->rule->type) + { + case ASN1_INTEGER: + if (this->rule->flags & ASN1_MPZ) + { + read_mpz(this, inner); + } + else + { + read_int(this, inner); + } + break; + case ASN1_SEQUENCE: + read_sequence(this, inner); + break; + default: + break; + } + + return SUCCESS; +} + + + +status_t decode(private_der_decoder_t *this, chunk_t input, void *output) +{ + this->rule = this->first_rule - 1; + this->output = output; + return read_hdr(this, &input); +} + +/** + * Implementation of der_decoder.destroy. + */ +static void destroy(private_der_decoder_t *this) +{ + allocator_free(this); +} + +/* + * Described in header. + */ +der_decoder_t *der_decoder_create(asn1_rule_t *rules) +{ + private_der_decoder_t *this = allocator_alloc_thing(private_der_decoder_t); + + /* public functions */ + this->public.decode = (status_t (*) (der_decoder_t*,chunk_t,void*))decode; + this->public.destroy = (void (*) (der_decoder_t*))destroy; + + this->first_rule = rules; + this->logger = charon->logger_manager->get_logger(charon->logger_manager, DER_DECODER); + + return &(this->public); +} diff --git a/Source/charon/asn1/der_decoder.h b/Source/charon/asn1/der_decoder.h new file mode 100644 index 000000000..d6ccaf4cc --- /dev/null +++ b/Source/charon/asn1/der_decoder.h @@ -0,0 +1,61 @@ +/** + * @file der_decoder.h + * + * @brief Interface of der_decoder_t. + * + */ + +/* + * Copyright (C) 2006 Martin Willi + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#ifndef DER_DECODER_H_ +#define DER_DECODER_H_ + +#include <types.h> +#include <asn1/asn1.h> + +typedef struct der_decoder_t der_decoder_t; + +/** + * @brief Decode der_encoded bytes to usable structures. + * + * @b Constructors: + * - der_decoder_create() + * + * @ingroup asn1 + */ +struct der_decoder_t { + + status_t (*decode) (der_decoder_t *this, chunk_t input, void *output); + + /** + * @brief Destroys a der_decoder object. + * + * @param der_decoder calling object + */ + void (*destroy) (der_decoder_t *this); +}; + + +/** + * @brief Create a der_decoder instance. + * + * @return der_decoder_t object + * + * @ingroup ans1 + */ +der_decoder_t * der_decoder_create(asn1_rule_t* rules); + +#endif /* DER_DECODER_H_ */ diff --git a/Source/charon/asn1/der_encoder.c b/Source/charon/asn1/der_encoder.c new file mode 100644 index 000000000..669a140ed --- /dev/null +++ b/Source/charon/asn1/der_encoder.c @@ -0,0 +1,218 @@ +/** + * @file der_encoder.c + * + * @brief Implementation of der_encoder_t. + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#include <gmp.h> + +#include "der_encoder.h" + +#include <utils/allocator.h> +#include <daemon.h> + + + +typedef struct private_der_encoder_t private_der_encoder_t; + +/** + * Private data of a der_encoder_t object. + */ +struct private_der_encoder_t { + /** + * Public interface for this signer. + */ + der_encoder_t public; + + asn1_rule_t *rule; + + asn1_rule_t *first_rule; + + void *output; + + logger_t *logger; +}; + +status_t read_hdr(private_der_encoder_t *this, chunk_t *data); + +status_t read_sequence(private_der_encoder_t *this, chunk_t data) +{ + while (this->rule->type != ASN1_END) + { + read_hdr(this, &data); + } + return SUCCESS; +} + + +status_t read_int(private_der_encoder_t *this, chunk_t data) +{ + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER", data); + u_int *integ = (u_int*)((u_int8_t*)this->output + this->rule->data_offset); + + *integ = 0; + while (data.len-- > 0) + { + *integ = 256 * (*integ) + *data.ptr++; + } + return SUCCESS; +} + +status_t read_mpz(private_der_encoder_t *this, chunk_t data) +{ + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER as mpz", data); + mpz_t *mpz = (mpz_t*)((u_int8_t*)this->output + this->rule->data_offset); + + mpz_import(*mpz, data.len, 1, 1, 1, 0, data.ptr); + return SUCCESS; +} + +u_int32_t read_length(chunk_t *data) +{ + u_int8_t n; + size_t len; + + /* read first octet of length field */ + n = *data->ptr++; + + if ((n & 0x80) == 0) + { + /* single length octet */ + return n; + } + + /* composite length, determine number of length octets */ + n &= 0x7f; + + if (n > data->len) + { + /* length longer than available bytes */ + return -1; + } + + if (n > sizeof(len)) + { + /* larger than size_t can hold */ + return -1; + } + + len = 0; + while (n-- > 0) + { + len = 256 * len + *data->ptr++; + } + return len; +} + +status_t read_hdr(private_der_encoder_t *this, chunk_t *data) +{ + chunk_t inner; + + /* advance to the next rule */ + this->rule++; + + if (this->rule->type == ASN1_END) + { + return SUCCESS; + } + + this->logger->log(this->logger, CONTROL|LEVEL2, "reading header of rule %s", + mapping_find(asn1_type_m, this->rule->type)); + + this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "reading from:", *data); + + /* read type, advance in data */ + if (*(data->ptr) != this->rule->type) + { + this->logger->log(this->logger, CONTROL|LEVEL2, "Bad byte found (%x)", *data->ptr); + return PARSE_ERROR; + } + data->ptr++; + data->len--; + + /* read length, advance in data */ + inner.len = read_length(data); + if (inner.len == -1) + { + this->logger->log(this->logger, CONTROL|LEVEL2, "Error reading length"); + return PARSE_ERROR; + } + this->logger->log(this->logger, CONTROL|LEVEL2, "Length is %d", + inner.len); + inner.ptr = data->ptr; + + /* advance in data */ + data->ptr += inner.len; + data->len -= inner.len; + + /* process inner */ + switch (this->rule->type) + { + case ASN1_INTEGER: + if (this->rule->flags & ASN1_MPZ) + { + read_mpz(this, inner); + } + else + { + read_int(this, inner); + } + break; + case ASN1_SEQUENCE: + read_sequence(this, inner); + break; + default: + break; + } + + return SUCCESS; +} + + + +status_t decode(private_der_encoder_t *this, chunk_t input, void *output) +{ + this->rule = this->first_rule - 1; + this->output = output; + return read_hdr(this, &input); +} + +/** + * Implementation of der_encoder.destroy. + */ +static void destroy(private_der_encoder_t *this) +{ + allocator_free(this); +} + +/* + * Described in header. + */ +der_encoder_t *der_encoder_create(asn1_rule_t *rules) +{ + private_der_encoder_t *this = allocator_alloc_thing(private_der_encoder_t); + + /* public functions */ + this->public.decode = (status_t (*) (der_encoder_t*,chunk_t,void*))decode; + this->public.destroy = (void (*) (der_encoder_t*))destroy; + + this->first_rule = rules; + this->logger = charon->logger_manager->get_logger(charon->logger_manager, DER_DECODER); + + return &(this->public); +} diff --git a/Source/charon/asn1/der_encoder.h b/Source/charon/asn1/der_encoder.h new file mode 100644 index 000000000..ee4443eb8 --- /dev/null +++ b/Source/charon/asn1/der_encoder.h @@ -0,0 +1,60 @@ +/** + * @file der_encoder.h + * + * @brief Interface of der_encoder_t. + * + */ + +/* + * Copyright (C) 2006 Martin Willi + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * 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. + */ + +#ifndef DER_ENCODER_H_ +#define DER_ENCODER_H_ + +#include <types.h> + +typedef struct der_encoder_t der_encoder_t; + +/** + * @brief Decode der_encoded bytes to usable structures. + * + * @b Constructors: + * - der_encoder_create() + * + * @ingroup asn1 + */ +struct der_encoder_t { + + status_t encode(der_encoder_t *this, void *input, chunk_t output); + + /** + * @brief Destroys a der_encoder object. + * + * @param der_encoder calling object + */ + void (*destroy) (der_encoder_t *this); +}; + + +/** + * @brief Create a der_encoder instance. + * + * @return der_encoder_t object + * + * @ingroup ans1 + */ +der_encoder_t * der_encoder_create(asn1_rule_t *rules); + +#endif /* DER_ENCODER_H_ */ |