some simplifications in time handling
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Sat, 24 Nov 2012 09:41:33 +0000 (10:41 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Sat, 24 Nov 2012 09:41:33 +0000 (10:41 +0100)
lib/coding.c
lib/decoding.c
lib/element.c
lib/int.h
lib/parser_aux.c
lib/structure.c

index c39c595..a64afd0 100644 (file)
@@ -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;
 
index 68f0634..f02fe10 100644 (file)
@@ -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;
index c1718e2..2c761c5 100644 (file)
@@ -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:
index 1cd3e66..f1a3f17 100644 (file)
--- a/lib/int.h
+++ b/lib/int.h
@@ -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 */
index 7d16fa3..4fcc7d7 100644 (file)
@@ -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;
 }
index 4d69765..1659067 100644 (file)
@@ -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;