diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-11-24 10:41:33 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-11-24 10:41:33 +0100 |
commit | 2ac0d04b5f8571866e729e9d816ea69780892634 (patch) | |
tree | 38b4ec7e74c3dceb88726e7b42e18253af2e1a46 | |
parent | bbfe113c2d7540c6883eb3d83d7052f49f7988fc (diff) | |
download | libtasn1-2ac0d04b5f8571866e729e9d816ea69780892634.tar.gz |
some simplifications in time handling
-rw-r--r-- | lib/coding.c | 5 | ||||
-rw-r--r-- | lib/decoding.c | 2 | ||||
-rw-r--r-- | lib/element.c | 16 | ||||
-rw-r--r-- | lib/int.h | 16 | ||||
-rw-r--r-- | lib/parser_aux.c | 4 | ||||
-rw-r--r-- | lib/structure.c | 28 |
6 files changed, 55 insertions, 16 deletions
diff --git a/lib/coding.c b/lib/coding.c index c39c595..a64afd0 100644 --- a/lib/coding.c +++ b/lib/coding.c @@ -258,11 +258,10 @@ asn1_encode_simple_der (unsigned int etype, const unsigned char *str, unsigned i /* ASN1_SUCCESS otherwise */ /******************************************************/ static int -_asn1_time_der (unsigned char *str, unsigned char *der, int *der_len) +_asn1_time_der (unsigned char *str, int str_len, unsigned char *der, int *der_len) { int len_len; int max_len; - int str_len = _asn1_strlen (str); max_len = *der_len; @@ -1070,7 +1069,7 @@ asn1_der_coding (asn1_node element, const char *name, void *ider, int *len, goto error; } len2 = max_len; - err = _asn1_time_der (p->value, der + counter, &len2); + err = _asn1_time_der (p->value, p->value_len, der + counter, &len2); if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR) goto error; diff --git a/lib/decoding.c b/lib/decoding.c index 68f0634..f02fe10 100644 --- a/lib/decoding.c +++ b/lib/decoding.c @@ -1052,7 +1052,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, int len, tlen = strlen (temp); if (tlen > 0) - _asn1_set_value (p, temp, tlen + 1); + _asn1_set_value (p, temp, tlen); counter += len2; move = RIGHT; break; diff --git a/lib/element.c b/lib/element.c index c1718e2..2c761c5 100644 --- a/lib/element.c +++ b/lib/element.c @@ -536,12 +536,12 @@ asn1_write_value (asn1_node node_root, const char *name, default: return ASN1_VALUE_NOT_FOUND; } - _asn1_set_value (node, value, _asn1_strlen (value) + 1); + _asn1_set_value (node, value, _asn1_strlen (value)); } break; case ASN1_ETYPE_GENERALIZED_TIME: if (value) - _asn1_set_value (node, value, _asn1_strlen (value) + 1); + _asn1_set_value (node, value, _asn1_strlen (value)); break; case ASN1_ETYPE_OCTET_STRING: case ASN1_ETYPE_GENERALSTRING: @@ -628,6 +628,16 @@ asn1_write_value (asn1_node node_root, const char *name, _asn1_strcpy(ptr, data); \ } +#define PUT_AS_STR_VALUE( ptr, ptr_size, data, data_size) \ + *len = data_size + 1; \ + if (ptr_size < *len) { \ + return ASN1_MEM_ERROR; \ + } else { \ + /* this strcpy is checked */ \ + memcpy(ptr, data, data_size); \ + ptr[data_size] = 0; \ + } + #define ADD_STR_VALUE( ptr, ptr_size, data) \ *len = (int) _asn1_strlen(data) + 1; \ if (ptr_size < (int) _asn1_strlen(ptr)+(*len)) { \ @@ -895,7 +905,7 @@ asn1_read_value_type (asn1_node root, const char *name, void *ivalue, int *len, break; case ASN1_ETYPE_GENERALIZED_TIME: case ASN1_ETYPE_UTC_TIME: - PUT_STR_VALUE (value, value_size, node->value); + PUT_AS_STR_VALUE (value, value_size, node->value, node->value_len); break; case ASN1_ETYPE_OCTET_STRING: case ASN1_ETYPE_GENERALSTRING: @@ -170,5 +170,21 @@ unsigned int type = ntype & 0xff; return type; } +/* To convert old types from a static structure */ +inline static unsigned int convert_old_type(unsigned int ntype) +{ +unsigned int type = ntype & 0xff; + if (type == ASN1_ETYPE_TIME) + { + if (type & CONST_UTC) + type = ASN1_ETYPE_UTC_TIME; + else + type = ASN1_ETYPE_GENERALIZED_TIME; + + return type | ((ntype>>8)<<8); + } + else + return ntype; +} #endif /* INT_H */ diff --git a/lib/parser_aux.c b/lib/parser_aux.c index 7d16fa3..4fcc7d7 100644 --- a/lib/parser_aux.c +++ b/lib/parser_aux.c @@ -48,7 +48,7 @@ list_type *firstElement = NULL; /* Description: creates a new NODE_ASN element and */ /* puts it in the list pointed by firstElement. */ /* Parameters: */ -/* type: type of the new element (see ASN1_ETYPE_ */ +/* type: type of the new element (see ASN1_ETYPE_ */ /* and CONST_ constants). */ /* Return: pointer to the new element. */ /******************************************************/ @@ -73,7 +73,7 @@ _asn1_add_static_node (unsigned int type) listElement->next = firstElement; firstElement = listElement; - punt->type = type; + punt->type = convert_old_type(type); return punt; } diff --git a/lib/structure.c b/lib/structure.c index 4d69765..1659067 100644 --- a/lib/structure.c +++ b/lib/structure.c @@ -823,11 +823,6 @@ asn1_print_structure (FILE * out, asn1_node structure, const char *name, fprintf (out, "%02x", (p->value)[k + len2]); } break; - case ASN1_ETYPE_GENERALIZED_TIME: - case ASN1_ETYPE_UTC_TIME: - if (p->value) - fprintf (out, " value:%s", p->value); - break; case ASN1_ETYPE_BOOLEAN: if (p->value) { @@ -851,14 +846,21 @@ asn1_print_structure (FILE * out, asn1_node structure, const char *name, } } break; - case ASN1_ETYPE_OCTET_STRING: + case ASN1_ETYPE_GENERALIZED_TIME: + case ASN1_ETYPE_UTC_TIME: + if (p->value) + { + fprintf (out, " value:"); + for (k = 0; k < p->value_len; k++) + fprintf (out, "%c", (p->value)[k]); + } + break; case ASN1_ETYPE_GENERALSTRING: case ASN1_ETYPE_NUMERIC_STRING: case ASN1_ETYPE_IA5_STRING: case ASN1_ETYPE_TELETEX_STRING: case ASN1_ETYPE_PRINTABLE_STRING: case ASN1_ETYPE_UNIVERSAL_STRING: - case ASN1_ETYPE_BMP_STRING: case ASN1_ETYPE_UTF8_STRING: case ASN1_ETYPE_VISIBLE_STRING: if (p->value) @@ -868,6 +870,18 @@ asn1_print_structure (FILE * out, asn1_node structure, const char *name, fprintf (out, " value:"); if (len > 0) for (k = 0; k < len; k++) + fprintf (out, "%c", (p->value)[k + len2]); + } + break; + case ASN1_ETYPE_BMP_STRING: + case ASN1_ETYPE_OCTET_STRING: + if (p->value) + { + len2 = -1; + len = asn1_get_length_der (p->value, p->value_len, &len2); + fprintf (out, " value:"); + if (len > 0) + for (k = 0; k < len; k++) fprintf (out, "%02x", (p->value)[k + len2]); } break; |