libtasn1: Import libtasn1-4.19.0

Import a very trimmed-down set of libtasn1 files:

  curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
  tar xvzf libtasn1-4.19.0.tar.gz
  rm -rf grub-core/lib/libtasn1
  mkdir -p grub-core/lib/libtasn1/lib
  mkdir -p grub-core/lib/libtasn1/tests
  cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1
  cp libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} grub-core/libtasn1/lib
  cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1
  cp libtasn1-4.19.0/tests/{CVE-2018-1000654-1_asn1_tab.h,CVE-2018-1000654-2_asn1_tab.h,CVE-2018-1000654.c,object-id-decoding.c,object-id-encoding.c,octet-string.c,reproducers.c,Test_overflow.c,Test_simple.c,Test_strings.c} grub-core/lib/libtasn1/tests
  rm -rf libtasn1-4.19.0*

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
Daniel Axtens 2024-11-15 15:34:30 +08:00 committed by Daniel Kiper
parent c85c2b9f5f
commit 9a26abbc36
25 changed files with 10164 additions and 0 deletions

View File

@ -0,0 +1,16 @@
LICENSING
=========
The libtasn1 library is released under the GNU Lesser General Public
License (LGPL) version 2.1 or later; see [COPYING.LESSER](doc/COPYING.LESSER)
for the license terms.
The GNU LGPL applies to the main libtasn1 library, while the
included applications library are under the GNU GPL version 3.
The libtasn1 library is located in the lib directory, while the applications
in src/.
The documentation in doc/ is under the GNU FDL license 1.3.
For any copyright year range specified as YYYY-ZZZZ in this package
note that the range specifies every single year in that closed interval.

View File

@ -0,0 +1,98 @@
# Libtasn1 README -- Introduction information
This is GNU Libtasn1, a small ASN.1 library.
The C library (libtasn1.*) is licensed under the GNU Lesser General
Public License version 2.1 or later. See the file COPYING.LIB.
The command line tool, self tests, examples, and other auxilliary
files, are licensed under the GNU General Public License version 3.0
or later. See the file COPYING.
## Building the library
We require several tools to build the software, including:
* [Make](https://www.gnu.org/software/make/)
* [Automake](https://www.gnu.org/software/automake/) (use 1.11.3 or later)
* [Autoconf](https://www.gnu.org/software/autoconf/)
* [Libtool](https://www.gnu.org/software/libtool/)
* [Texinfo](https://www.gnu.org/software/texinfo/)
* [help2man](http://www.gnu.org/software/help2man/)
* [Tar](https://www.gnu.org/software/tar/)
* [Gzip](https://www.gnu.org/software/gzip/)
* [bison](https://www.gnu.org/software/bison/)
* [Texlive & epsf](https://www.tug.org/texlive/) (for PDF manual)
* [GTK-DOC](https://www.gtk.org/gtk-doc/) (for API manual)
* [Git](https://git-scm.com/)
* [libabigail](https://pagure.io/libabigail/) (for abi comparison in make dist)
* [Valgrind](https://valgrind.org/) (optional)
The required software is typically distributed with your operating
system, and the instructions for installing them differ. Here are
some hints:
Debian/Ubuntu:
```
sudo apt-get install make git autoconf automake libtool bison
sudo apt-get install texinfo help2man gtk-doc-tools valgrind abigail-tools
```
PDF manual - Debian <= stretch:
```
sudo apt-get install texlive-generic-recommended texlive texlive-extra-utils
```
PDF manual - Debian >= buster:
```
sudo apt-get install texlive-plain-generic texlive texlive-extra-utils
```
The next step is to run autoreconf, ./configure, etc:
```
$ ./bootstrap
```
Then build the project normally:
```
$ ./configure
$ make check
```
Happy hacking!
## Manual
The manual is in the `doc/` directory of the release.
You can also browse the manual online at:
- https://www.gnu.org/software/libtasn1/manual/
- https://gnutls.gitlab.io/libtasn1/manual/
- https://gnutls.gitlab.io/libtasn1/manual/libtasn1.html
- https://gnutls.gitlab.io/libtasn1/manual/libtasn1.pdf
- https://gnutls.gitlab.io/libtasn1/reference/
- https://gnutls.gitlab.io/libtasn1/reference/libtasn1.pdf
## Code coverage report
The coverage report is at:
- https://gnutls.gitlab.io/libtasn1/coverage
## Issue trackers
- [Main issue tracker](https://gitlab.com/gnutls/libtasn1/issues)
- [oss-fuzz found issues](https://bugs.chromium.org/p/oss-fuzz/issues/list?q=libtasn1&can=2)
## Homepage
The project homepage at the gnu site is at:
https://www.gnu.org/software/libtasn1/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2000-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#ifndef _ELEMENT_H
# define _ELEMENT_H
struct node_tail_cache_st
{
asn1_node head; /* the first element of the sequence */
asn1_node tail;
};
int _asn1_append_sequence_set (asn1_node node,
struct node_tail_cache_st *pcached);
int _asn1_convert_integer (const unsigned char *value,
unsigned char *value_out,
int value_out_size, int *len);
void _asn1_hierarchical_name (asn1_node_const node, char *name,
int name_size);
#endif

View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include <int.h>
#ifdef STDC_HEADERS
# include <stdarg.h>
#endif
#define LIBTASN1_ERROR_ENTRY(name) { #name, name }
struct libtasn1_error_entry
{
const char *name;
int number;
};
typedef struct libtasn1_error_entry libtasn1_error_entry;
static const libtasn1_error_entry error_algorithms[] = {
LIBTASN1_ERROR_ENTRY (ASN1_SUCCESS),
LIBTASN1_ERROR_ENTRY (ASN1_FILE_NOT_FOUND),
LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_FOUND),
LIBTASN1_ERROR_ENTRY (ASN1_IDENTIFIER_NOT_FOUND),
LIBTASN1_ERROR_ENTRY (ASN1_DER_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_FOUND),
LIBTASN1_ERROR_ENTRY (ASN1_GENERIC_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_VALID),
LIBTASN1_ERROR_ENTRY (ASN1_TAG_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_TAG_IMPLICIT),
LIBTASN1_ERROR_ENTRY (ASN1_ERROR_TYPE_ANY),
LIBTASN1_ERROR_ENTRY (ASN1_SYNTAX_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_MEM_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_MEM_ALLOC_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_DER_OVERFLOW),
LIBTASN1_ERROR_ENTRY (ASN1_NAME_TOO_LONG),
LIBTASN1_ERROR_ENTRY (ASN1_ARRAY_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_EMPTY),
LIBTASN1_ERROR_ENTRY (ASN1_TIME_ENCODING_ERROR),
LIBTASN1_ERROR_ENTRY (ASN1_RECURSION),
{0, 0}
};
/**
* asn1_perror:
* @error: is an error returned by a libtasn1 function.
*
* Prints a string to stderr with a description of an error. This
* function is like perror(). The only difference is that it accepts
* an error returned by a libtasn1 function.
*
* Since: 1.6
**/
void
asn1_perror (int error)
{
const char *str = asn1_strerror (error);
fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)");
}
/**
* asn1_strerror:
* @error: is an error returned by a libtasn1 function.
*
* Returns a string with a description of an error. This function is
* similar to strerror. The only difference is that it accepts an
* error (number) returned by a libtasn1 function.
*
* Returns: Pointer to static zero-terminated string describing error
* code.
*
* Since: 1.6
**/
const char *
asn1_strerror (int error)
{
const libtasn1_error_entry *p;
for (p = error_algorithms; p->name != NULL; p++)
if (p->number == error)
return p->name + sizeof ("ASN1_") - 1;
return NULL;
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include <int.h>
#include "gstr.h"
/* These function are like strcat, strcpy. They only
* do bounds checking (they shouldn't cause buffer overruns),
* and they always produce null terminated strings.
*
* They should be used only with null terminated strings.
*/
void
_asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
{
size_t str_size = strlen (src);
size_t dest_size = strlen (dest);
if (dest_tot_size - dest_size > str_size)
{
strcat (dest, src);
}
else
{
if (dest_tot_size > dest_size)
{
strncat (dest, src, (dest_tot_size - dest_size) - 1);
dest[dest_tot_size - 1] = 0;
}
}
}
/* Returns the bytes copied (not including the null terminator) */
unsigned int
_asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
{
size_t str_size = strlen (src);
if (dest_tot_size > str_size)
{
strcpy (dest, src);
return str_size;
}
else
{
if (dest_tot_size > 0)
{
str_size = dest_tot_size - 1;
memcpy (dest, src, str_size);
dest[str_size] = 0;
return str_size;
}
else
return 0;
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#ifndef GSTR_H
# define GSTR_H
unsigned int _asn1_str_cpy (char *dest, size_t dest_tot_size,
const char *src);
void _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src);
# define Estrcpy(x,y) _asn1_str_cpy(x,ASN1_MAX_ERROR_DESCRIPTION_SIZE,y)
# define Estrcat(x,y) _asn1_str_cat(x,ASN1_MAX_ERROR_DESCRIPTION_SIZE,y)
inline static void
safe_memset (void *data, int c, size_t size)
{
volatile unsigned volatile_zero = 0;
volatile char *vdata = (volatile char *) data;
/* This is based on a nice trick for safe memset,
* sent by David Jacobson in the openssl-dev mailing list.
*/
if (size > 0)
do
{
memset (data, c, size);
}
while (vdata[volatile_zero] != c);
}
#endif /* GSTR_H */

View File

@ -0,0 +1,221 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#ifndef INT_H
# define INT_H
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <stdint.h>
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# include <libtasn1.h>
# define ASN1_SMALL_VALUE_SIZE 16
/* This structure is also in libtasn1.h, but then contains less
fields. You cannot make any modifications to these first fields
without breaking ABI. */
struct asn1_node_st
{
/* public fields: */
char name[ASN1_MAX_NAME_SIZE + 1]; /* Node name */
unsigned int name_hash;
unsigned int type; /* Node type */
unsigned char *value; /* Node value */
int value_len;
asn1_node down; /* Pointer to the son node */
asn1_node right; /* Pointer to the brother node */
asn1_node left; /* Pointer to the next list element */
/* private fields: */
unsigned char small_value[ASN1_SMALL_VALUE_SIZE]; /* For small values */
/* values used during decoding/coding */
int tmp_ival;
unsigned start; /* the start of the DER sequence - if decoded */
unsigned end; /* the end of the DER sequence - if decoded */
};
typedef struct tag_and_class_st
{
unsigned tag;
unsigned class;
const char *desc;
} tag_and_class_st;
/* the types that are handled in _asn1_tags */
# define CASE_HANDLED_ETYPES \
case ASN1_ETYPE_NULL: \
case ASN1_ETYPE_BOOLEAN: \
case ASN1_ETYPE_INTEGER: \
case ASN1_ETYPE_ENUMERATED: \
case ASN1_ETYPE_OBJECT_ID: \
case ASN1_ETYPE_OCTET_STRING: \
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: \
case ASN1_ETYPE_BIT_STRING: \
case ASN1_ETYPE_SEQUENCE: \
case ASN1_ETYPE_SEQUENCE_OF: \
case ASN1_ETYPE_SET: \
case ASN1_ETYPE_UTC_TIME: \
case ASN1_ETYPE_GENERALIZED_TIME: \
case ASN1_ETYPE_SET_OF
# define ETYPE_TAG(etype) (_asn1_tags[etype].tag)
# define ETYPE_CLASS(etype) (_asn1_tags[etype].class)
# define ETYPE_OK(etype) (((etype) != ASN1_ETYPE_INVALID && \
(etype) < _asn1_tags_size && \
_asn1_tags[(etype)].desc != NULL)?1:0)
# define ETYPE_IS_STRING(etype) ((etype == ASN1_ETYPE_GENERALSTRING || \
etype == ASN1_ETYPE_NUMERIC_STRING || etype == ASN1_ETYPE_IA5_STRING || \
etype == ASN1_ETYPE_TELETEX_STRING || etype == ASN1_ETYPE_PRINTABLE_STRING || \
etype == ASN1_ETYPE_UNIVERSAL_STRING || etype == ASN1_ETYPE_BMP_STRING || \
etype == ASN1_ETYPE_UTF8_STRING || etype == ASN1_ETYPE_VISIBLE_STRING || \
etype == ASN1_ETYPE_OCTET_STRING)?1:0)
extern unsigned int _asn1_tags_size;
extern const tag_and_class_st _asn1_tags[];
# define _asn1_strlen(s) strlen((const char *) s)
# define _asn1_strtol(n,e,b) strtol((const char *) n, e, b)
# define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b)
# define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
# define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
# define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
# if SIZEOF_UNSIGNED_LONG_INT == 8
# define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b)
# else
# define _asn1_strtou64(n,e,b) strtoull((const char *) n, e, b)
# endif
# define MAX_LOG_SIZE 1024 /* maximum number of characters of a log message */
/* Define used for visiting trees. */
# define UP 1
# define RIGHT 2
# define DOWN 3
/***********************************************************************/
/* List of constants to better specify the type of typedef asn1_node_st. */
/***********************************************************************/
/* Used with TYPE_TAG */
# define CONST_UNIVERSAL (1U<<8)
# define CONST_PRIVATE (1U<<9)
# define CONST_APPLICATION (1U<<10)
# define CONST_EXPLICIT (1U<<11)
# define CONST_IMPLICIT (1U<<12)
# define CONST_TAG (1U<<13) /* Used in ASN.1 assignement */
# define CONST_OPTION (1U<<14)
# define CONST_DEFAULT (1U<<15)
# define CONST_TRUE (1U<<16)
# define CONST_FALSE (1U<<17)
# define CONST_LIST (1U<<18) /* Used with TYPE_INTEGER and TYPE_BIT_STRING */
# define CONST_MIN_MAX (1U<<19)
# define CONST_1_PARAM (1U<<20)
# define CONST_SIZE (1U<<21)
# define CONST_DEFINED_BY (1U<<22)
/* Those two are deprecated and used for backwards compatibility */
# define CONST_GENERALIZED (1U<<23)
# define CONST_UTC (1U<<24)
/* #define CONST_IMPORTS (1U<<25) */
# define CONST_NOT_USED (1U<<26)
# define CONST_SET (1U<<27)
# define CONST_ASSIGN (1U<<28)
# define CONST_DOWN (1U<<29)
# define CONST_RIGHT (1U<<30)
# define ASN1_ETYPE_TIME 17
/****************************************/
/* Returns the first 8 bits. */
/* Used with the field type of asn1_node_st */
/****************************************/
inline static unsigned int
type_field (unsigned int ntype)
{
return (ntype & 0xff);
}
/* 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 (ntype & CONST_UTC)
type = ASN1_ETYPE_UTC_TIME;
else
type = ASN1_ETYPE_GENERALIZED_TIME;
ntype &= ~(CONST_UTC | CONST_GENERALIZED);
ntype &= 0xffffff00;
ntype |= type;
return ntype;
}
else
return ntype;
}
static inline void *
_asn1_realloc (void *ptr, size_t size)
{
void *ret;
if (size == 0)
return ptr;
ret = realloc (ptr, size);
if (ret == NULL)
{
free (ptr);
}
return ret;
}
#endif /* INT_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,172 @@
/*
* Copyright (C) 2000-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#ifndef _PARSER_AUX_H
# define _PARSER_AUX_H
/***********************************************/
/* Type: list_type */
/* Description: type used in the list during */
/* the structure creation. */
/***********************************************/
typedef struct list_struct
{
asn1_node node;
struct list_struct *next;
} list_type;
/***************************************/
/* Functions used by ASN.1 parser */
/***************************************/
asn1_node _asn1_add_static_node (list_type ** e_list, unsigned int type);
void _asn1_delete_list (list_type * e_list);
void _asn1_delete_list_and_nodes (list_type * e_list);
void _asn1_delete_node_from_list (list_type * list, asn1_node node);
asn1_node
_asn1_set_value (asn1_node node, const void *value, unsigned int len);
asn1_node _asn1_set_value_m (asn1_node node, void *value, unsigned int len);
asn1_node
_asn1_set_value_lv (asn1_node node, const void *value, unsigned int len);
asn1_node
_asn1_append_value (asn1_node node, const void *value, unsigned int len);
asn1_node _asn1_set_name (asn1_node node, const char *name);
asn1_node _asn1_cpy_name (asn1_node dst, asn1_node_const src);
asn1_node _asn1_set_right (asn1_node node, asn1_node right);
asn1_node _asn1_get_last_right (asn1_node_const node);
void _asn1_remove_node (asn1_node node, unsigned int flags);
/* Max 64-bit integer length is 20 chars + 1 for sign + 1 for null termination */
# define LTOSTR_MAX_SIZE 22
char *_asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE]);
asn1_node _asn1_find_up (asn1_node_const node);
int _asn1_change_integer_value (asn1_node node);
# define EXPAND_OBJECT_ID_MAX_RECURSION 16
int _asn1_expand_object_id (list_type ** list, asn1_node node);
int _asn1_type_set_config (asn1_node node);
int _asn1_check_identifier (asn1_node_const node);
int _asn1_set_default_tag (asn1_node node);
/******************************************************************/
/* Function : _asn1_get_right */
/* Description: returns the element pointed by the RIGHT field of */
/* a NODE_ASN element. */
/* Parameters: */
/* node: NODE_ASN element pointer. */
/* Return: field RIGHT of NODE. */
/******************************************************************/
inline static asn1_node
_asn1_get_right (asn1_node_const node)
{
if (node == NULL)
return NULL;
return node->right;
}
/******************************************************************/
/* Function : _asn1_set_down */
/* Description: sets the field DOWN in a NODE_ASN element. */
/* Parameters: */
/* node: element pointer. */
/* down: pointer to a NODE_ASN element that you want be pointed */
/* by NODE. */
/* Return: pointer to *NODE. */
/******************************************************************/
inline static asn1_node
_asn1_set_down (asn1_node node, asn1_node down)
{
if (node == NULL)
return node;
node->down = down;
if (down)
down->left = node;
return node;
}
/******************************************************************/
/* Function : _asn1_get_down */
/* Description: returns the element pointed by the DOWN field of */
/* a NODE_ASN element. */
/* Parameters: */
/* node: NODE_ASN element pointer. */
/* Return: field DOWN of NODE. */
/******************************************************************/
inline static asn1_node
_asn1_get_down (asn1_node_const node)
{
if (node == NULL)
return NULL;
return node->down;
}
/******************************************************************/
/* Function : _asn1_get_name */
/* Description: returns the name of a NODE_ASN element. */
/* Parameters: */
/* node: NODE_ASN element pointer. */
/* Return: a null terminated string. */
/******************************************************************/
inline static char *
_asn1_get_name (asn1_node_const node)
{
if (node == NULL)
return NULL;
return (char *) node->name;
}
/******************************************************************/
/* Function : _asn1_mod_type */
/* Description: change the field TYPE of an NODE_ASN element. */
/* The new value is the old one | (bitwise or) the */
/* paramener VALUE. */
/* Parameters: */
/* node: NODE_ASN element pointer. */
/* value: the integer value that must be or-ed with the current */
/* value of field TYPE. */
/* Return: NODE pointer. */
/******************************************************************/
inline static asn1_node
_asn1_mod_type (asn1_node node, unsigned int value)
{
if (node == NULL)
return node;
node->type |= value;
return node;
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
/*************************************************/
/* File: structure.h */
/* Description: list of exported object by */
/* "structure.c" */
/*************************************************/
#ifndef _STRUCTURE_H
# define _STRUCTURE_H
# include "parser_aux.h" /* list_type */
int _asn1_create_static_structure (asn1_node_const pointer,
char *output_file_name, char *vector_name);
asn1_node _asn1_copy_structure3 (asn1_node_const source_node);
asn1_node _asn1_add_single_node (unsigned int type);
asn1_node _asn1_find_left (asn1_node_const node);
int
_asn1_delete_structure (list_type * e_list, asn1_node * structure,
unsigned int flags);
#endif

View File

@ -0,0 +1,643 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* LIBTASN1 is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* LIBTASN1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with LIBTASN1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
/**
* SECTION:libtasn1
* @short_description: GNU ASN.1 library
*
* The Libtasn1 library provides Abstract Syntax Notation One (ASN.1, as
* specified by the X.680 ITU-T recommendation) parsing and structures
* management, and Distinguished Encoding Rules (DER, as per X.690)
* encoding and decoding functions.
*/
#ifndef LIBTASN1_H
# define LIBTASN1_H
# ifndef ASN1_API
# if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY
# define ASN1_API __attribute__((__visibility__("default")))
# elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC
# define ASN1_API __declspec(dllexport)
# elif defined _MSC_VER && ! defined ASN1_STATIC
# define ASN1_API __declspec(dllimport)
# else
# define ASN1_API
# endif
# endif
# ifdef __GNUC__
# define __LIBTASN1_CONST__ __attribute__((const))
# define __LIBTASN1_PURE__ __attribute__((pure))
# else
# define __LIBTASN1_CONST__
# define __LIBTASN1_PURE__
# endif
# include <sys/types.h>
# include <time.h>
# include <stdio.h> /* for FILE* */
# ifdef __cplusplus
extern "C"
{
# endif
/**
* ASN1_VERSION:
*
* Version of the library as a string.
*/
# define ASN1_VERSION "4.19.0"
/**
* ASN1_VERSION_MAJOR:
*
* Major version number of the library.
*/
# define ASN1_VERSION_MAJOR 4
/**
* ASN1_VERSION_MINOR:
*
* Minor version number of the library.
*/
# define ASN1_VERSION_MINOR 19
/**
* ASN1_VERSION_PATCH:
*
* Patch version number of the library.
*/
# define ASN1_VERSION_PATCH 0
/**
* ASN1_VERSION_NUMBER:
*
* Version number of the library as a number.
*/
# define ASN1_VERSION_NUMBER 0x041300
# if defined __GNUC__ && !defined ASN1_INTERNAL_BUILD
# define _ASN1_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
# if _ASN1_GCC_VERSION >= 30100
# define _ASN1_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__))
# endif
# endif
# ifndef _ASN1_GCC_ATTR_DEPRECATED
# define _ASN1_GCC_ATTR_DEPRECATED
# endif
/*****************************************/
/* Errors returned by libtasn1 functions */
/*****************************************/
# define ASN1_SUCCESS 0
# define ASN1_FILE_NOT_FOUND 1
# define ASN1_ELEMENT_NOT_FOUND 2
# define ASN1_IDENTIFIER_NOT_FOUND 3
# define ASN1_DER_ERROR 4
# define ASN1_VALUE_NOT_FOUND 5
# define ASN1_GENERIC_ERROR 6
# define ASN1_VALUE_NOT_VALID 7
# define ASN1_TAG_ERROR 8
# define ASN1_TAG_IMPLICIT 9
# define ASN1_ERROR_TYPE_ANY 10
# define ASN1_SYNTAX_ERROR 11
# define ASN1_MEM_ERROR 12
# define ASN1_MEM_ALLOC_ERROR 13
# define ASN1_DER_OVERFLOW 14
# define ASN1_NAME_TOO_LONG 15
# define ASN1_ARRAY_ERROR 16
# define ASN1_ELEMENT_NOT_EMPTY 17
# define ASN1_TIME_ENCODING_ERROR 18
# define ASN1_RECURSION 19
/*************************************/
/* Constants used in asn1_visit_tree */
/*************************************/
# define ASN1_PRINT_NAME 1
# define ASN1_PRINT_NAME_TYPE 2
# define ASN1_PRINT_NAME_TYPE_VALUE 3
# define ASN1_PRINT_ALL 4
/*****************************************/
/* Constants returned by asn1_read_tag */
/*****************************************/
# define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */
# define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */
# define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */
# define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */
# define ASN1_CLASS_STRUCTURED 0x20
/*****************************************/
/* Constants returned by asn1_read_tag */
/*****************************************/
# define ASN1_TAG_BOOLEAN 0x01
# define ASN1_TAG_INTEGER 0x02
# define ASN1_TAG_SEQUENCE 0x10
# define ASN1_TAG_SET 0x11
# define ASN1_TAG_OCTET_STRING 0x04
# define ASN1_TAG_BIT_STRING 0x03
# define ASN1_TAG_UTCTime 0x17
# define ASN1_TAG_GENERALIZEDTime 0x18
# define ASN1_TAG_OBJECT_ID 0x06
# define ASN1_TAG_ENUMERATED 0x0A
# define ASN1_TAG_NULL 0x05
# define ASN1_TAG_GENERALSTRING 0x1B
# define ASN1_TAG_NUMERIC_STRING 0x12
# define ASN1_TAG_IA5_STRING 0x16
# define ASN1_TAG_TELETEX_STRING 0x14
# define ASN1_TAG_PRINTABLE_STRING 0x13
# define ASN1_TAG_UNIVERSAL_STRING 0x1C
# define ASN1_TAG_BMP_STRING 0x1E
# define ASN1_TAG_UTF8_STRING 0x0C
# define ASN1_TAG_VISIBLE_STRING 0x1A
/**
* asn1_node:
*
* Structure definition used for the node of the tree
* that represents an ASN.1 DEFINITION.
*/
typedef struct asn1_node_st asn1_node_st;
typedef asn1_node_st *asn1_node;
typedef const asn1_node_st *asn1_node_const;
/**
* ASN1_MAX_NAME_SIZE:
*
* Maximum number of characters of a name
* inside a file with ASN1 definitions.
*/
# define ASN1_MAX_NAME_SIZE 64
/**
* asn1_static_node:
* @name: Node name
* @type: Node typ
* @value: Node value
*
* For the on-disk format of ASN.1 trees, created by asn1_parser2array().
*/
typedef struct asn1_static_node_st
{
const char *name; /* Node name */
unsigned int type; /* Node type */
const void *value; /* Node value */
} asn1_static_node;
/* List of constants for field type of asn1_static_node */
# define ASN1_ETYPE_INVALID 0
# define ASN1_ETYPE_CONSTANT 1
# define ASN1_ETYPE_IDENTIFIER 2
# define ASN1_ETYPE_INTEGER 3
# define ASN1_ETYPE_BOOLEAN 4
# define ASN1_ETYPE_SEQUENCE 5
# define ASN1_ETYPE_BIT_STRING 6
# define ASN1_ETYPE_OCTET_STRING 7
# define ASN1_ETYPE_TAG 8
# define ASN1_ETYPE_DEFAULT 9
# define ASN1_ETYPE_SIZE 10
# define ASN1_ETYPE_SEQUENCE_OF 11
# define ASN1_ETYPE_OBJECT_ID 12
# define ASN1_ETYPE_ANY 13
# define ASN1_ETYPE_SET 14
# define ASN1_ETYPE_SET_OF 15
# define ASN1_ETYPE_DEFINITIONS 16
# define ASN1_ETYPE_CHOICE 18
# define ASN1_ETYPE_IMPORTS 19
# define ASN1_ETYPE_NULL 20
# define ASN1_ETYPE_ENUMERATED 21
# define ASN1_ETYPE_GENERALSTRING 27
# define ASN1_ETYPE_NUMERIC_STRING 28
# define ASN1_ETYPE_IA5_STRING 29
# define ASN1_ETYPE_TELETEX_STRING 30
# define ASN1_ETYPE_PRINTABLE_STRING 31
# define ASN1_ETYPE_UNIVERSAL_STRING 32
# define ASN1_ETYPE_BMP_STRING 33
# define ASN1_ETYPE_UTF8_STRING 34
# define ASN1_ETYPE_VISIBLE_STRING 35
# define ASN1_ETYPE_UTC_TIME 36
# define ASN1_ETYPE_GENERALIZED_TIME 37
/**
* ASN1_DELETE_FLAG_ZEROIZE:
*
* Used by: asn1_delete_structure2()
*
* Zeroize values prior to deinitialization.
*/
# define ASN1_DELETE_FLAG_ZEROIZE 1
/**
* ASN1_DECODE_FLAG_ALLOW_PADDING:
*
* Used by: asn1_der_decoding2()
*
* This flag would allow arbitrary data past the DER data.
*/
# define ASN1_DECODE_FLAG_ALLOW_PADDING 1
/**
* ASN1_DECODE_FLAG_STRICT_DER:
*
* Used by: asn1_der_decoding2()
*
* This flag would ensure that no BER decoding takes place.
*/
# define ASN1_DECODE_FLAG_STRICT_DER (1<<1)
/**
* ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME:
*
* Used by: asn1_der_decoding2()
*
* This flag will tolerate Time encoding errors when in strict DER.
*/
# define ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME (1<<2)
/* *INDENT-OFF* */
/**
* asn1_data_node_st:
* @name: Node name
* @value: Node value
* @value_len: Node value size
* @type: Node value type (ASN1_ETYPE_*)
*
* Data node inside a #asn1_node structure.
*/
struct asn1_data_node_st
{
const char *name; /* Node name */
const void *value; /* Node value */
unsigned int value_len; /* Node value size */
unsigned int type; /* Node value type (ASN1_ETYPE_*) */
};
/* *INDENT-ON* */
typedef struct asn1_data_node_st asn1_data_node_st;
/***********************************/
/* Fixed constants */
/***********************************/
/**
* ASN1_MAX_ERROR_DESCRIPTION_SIZE:
*
* Maximum number of characters
* of a description message
* (null character included).
*/
# define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128
/***********************************/
/* Functions definitions */
/***********************************/
extern ASN1_API int
asn1_parser2tree (const char *file,
asn1_node * definitions, char *error_desc);
extern ASN1_API int
asn1_parser2array (const char *inputFileName,
const char *outputFileName,
const char *vectorName, char *error_desc);
extern ASN1_API int
asn1_array2tree (const asn1_static_node * array,
asn1_node * definitions, char *errorDescription);
extern ASN1_API void
asn1_print_structure (FILE * out, asn1_node_const structure,
const char *name, int mode);
extern ASN1_API int
asn1_create_element (asn1_node_const definitions,
const char *source_name, asn1_node * element);
extern ASN1_API int asn1_delete_structure (asn1_node * structure);
extern ASN1_API int asn1_delete_structure2 (asn1_node * structure,
unsigned int flags);
extern ASN1_API int
asn1_delete_element (asn1_node structure, const char *element_name);
extern ASN1_API int
asn1_write_value (asn1_node node_root, const char *name,
const void *ivalue, int len);
extern ASN1_API int
asn1_read_value (asn1_node_const root, const char *name,
void *ivalue, int *len);
extern ASN1_API int
asn1_read_value_type (asn1_node_const root, const char *name,
void *ivalue, int *len, unsigned int *etype);
extern ASN1_API int
asn1_read_node_value (asn1_node_const node, asn1_data_node_st * data);
extern ASN1_API int
asn1_number_of_elements (asn1_node_const element, const char *name,
int *num);
extern ASN1_API int
asn1_der_coding (asn1_node_const element, const char *name,
void *ider, int *len, char *ErrorDescription);
extern ASN1_API int
asn1_der_decoding2 (asn1_node * element, const void *ider,
int *max_ider_len, unsigned int flags,
char *errorDescription);
extern ASN1_API int
asn1_der_decoding (asn1_node * element, const void *ider,
int ider_len, char *errorDescription);
/* Do not use. Use asn1_der_decoding() instead. */
extern ASN1_API int
asn1_der_decoding_element (asn1_node * structure,
const char *elementName,
const void *ider, int len,
char *errorDescription)
_ASN1_GCC_ATTR_DEPRECATED;
extern ASN1_API int
asn1_der_decoding_startEnd (asn1_node element,
const void *ider, int ider_len,
const char *name_element,
int *start, int *end);
extern ASN1_API int
asn1_expand_any_defined_by (asn1_node_const definitions,
asn1_node * element);
extern ASN1_API int
asn1_expand_octet_string (asn1_node_const definitions,
asn1_node * element,
const char *octetName, const char *objectName);
extern ASN1_API int
asn1_read_tag (asn1_node_const root, const char *name,
int *tagValue, int *classValue);
extern ASN1_API const char *asn1_find_structure_from_oid (asn1_node_const
definitions,
const char
*oidValue);
__LIBTASN1_PURE__
extern ASN1_API const char *asn1_check_version (const char *req_version);
__LIBTASN1_PURE__ extern ASN1_API const char *asn1_strerror (int error);
extern ASN1_API void asn1_perror (int error);
# define ASN1_MAX_TAG_SIZE 4
# define ASN1_MAX_LENGTH_SIZE 9
# define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE)
extern ASN1_API long
asn1_get_length_der (const unsigned char *der, int der_len, int *len);
extern ASN1_API long
asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len);
extern ASN1_API void
asn1_length_der (unsigned long int len, unsigned char *der, int *der_len);
/* Other utility functions. */
extern ASN1_API
int asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
unsigned int _der_len,
const unsigned char **str,
unsigned int *str_len);
extern ASN1_API
int asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
unsigned int _der_len,
unsigned char **str,
unsigned int *str_len, unsigned int *ber_len);
extern ASN1_API int
asn1_encode_simple_der (unsigned int etype, const unsigned char *str,
unsigned int str_len, unsigned char *tl,
unsigned int *tl_len);
extern ASN1_API asn1_node
asn1_find_node (asn1_node_const pointer, const char *name);
extern ASN1_API int
asn1_copy_node (asn1_node dst, const char *dst_name,
asn1_node_const src, const char *src_name);
extern ASN1_API asn1_node
asn1_dup_node (asn1_node_const src, const char *src_name);
/* Internal and low-level DER utility functions. */
extern ASN1_API int
asn1_get_tag_der (const unsigned char *der, int der_len,
unsigned char *cls, int *len, unsigned long *tag);
extern ASN1_API void
asn1_octet_der (const unsigned char *str, int str_len,
unsigned char *der, int *der_len);
extern ASN1_API int
asn1_get_octet_der (const unsigned char *der, int der_len,
int *ret_len, unsigned char *str,
int str_size, int *str_len);
extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len,
unsigned char *der, int *der_len);
extern ASN1_API int
asn1_get_bit_der (const unsigned char *der, int der_len,
int *ret_len, unsigned char *str,
int str_size, int *bit_len);
extern ASN1_API int
asn1_get_object_id_der (const unsigned char *der,
int der_len, int *ret_len,
char *str, int str_size);
extern ASN1_API int
asn1_object_id_der (const char *str, unsigned char *der, int *der_len,
unsigned flags);
/* Compatibility types */
/**
* asn1_retCode:
*
* Type formerly returned by libtasn1 functions.
*
* Deprecated: 3.0: Use int instead.
*/
typedef int asn1_retCode _ASN1_GCC_ATTR_DEPRECATED;
/**
* node_asn_struct:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define node_asn_struct _Pragma ("GCC warning \"'node_asn_struct' macro is deprecated, use 'asn1_node' instead.\"") asn1_node_st
# else
# define node_asn_struct asn1_node_st
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* node_asn:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define node_asn _Pragma ("GCC warning \"'node_asn' macro is deprecated, use 'asn1_node' instead.\"") asn1_node_st
# else
# define node_asn asn1_node_st
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* ASN1_TYPE:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define ASN1_TYPE _Pragma ("GCC warning \"'ASN1_TYPE' macro is deprecated, use 'asn1_node' instead.\"") asn1_node
# else
# define ASN1_TYPE asn1_node
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* ASN1_TYPE_EMPTY:
*
* Compat #define.
*
* Deprecated: 3.0: Use NULL instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define ASN1_TYPE_EMPTY _Pragma ("GCC warning \"'ASN1_TYPE_EMPTY' macro is deprecated, use 'NULL' instead.\"") NULL
# else
# define ASN1_TYPE_EMPTY NULL
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* static_struct_asn:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_static_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define static_struct_asn _Pragma ("GCC warning \"'static_struct_asn' macro is deprecated, use 'asn1_static_node_st' instead.\"") asn1_static_node_st
# else
# define static_struct_asn asn1_static_node_st
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* ASN1_ARRAY_TYPE:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_static_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define ASN1_ARRAY_TYPE _Pragma ("GCC warning \"'ASN1_ARRAY_TYPE' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_static_node
# else
# define ASN1_ARRAY_TYPE asn1_static_node
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* asn1_static_node_t:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_static_node instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define asn1_static_node_t _Pragma ("GCC warning \"'asn1_static_node_t' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_static_node
# else
# define asn1_static_node_t asn1_static_node
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* node_data_struct:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_data_node_st instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define node_data_struct _Pragma ("GCC warning \"'node_data_struct' macro is deprecated, use 'asn1_data_node_st' instead.\"") asn1_data_node_st
# else
# define node_data_struct asn1_data_node_st
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
/**
* ASN1_DATA_NODE:
*
* Compat #define.
*
* Deprecated: 3.0: Use #asn1_data_node_st instead.
*/
# ifndef ASN1_DISABLE_DEPRECATED
# if _ASN1_GCC_VERSION >= 30100
# define ASN1_DATA_NODE _Pragma ("GCC warning \"'asn1_static_node_t' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_data_node_st
# else
# define ASN1_DATA_NODE asn1_data_node_st
# endif
# endif /* !ASN1_DISABLE_DEPRECATED */
# ifdef __cplusplus
}
# endif
#endif /* LIBTASN1_H */

View File

@ -0,0 +1,32 @@
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <libtasn1.h>
const asn1_static_node CVE_2018_1000654_1_asn1_tab[] = {
{ "TEST_TREE", 536875024, NULL },
{ NULL, 1610612748, NULL },
{ "iso", 1073741825, "1"},
{ "identified-organization", 1073741825, "3"},
{ "dod", 1073741825, "6"},
{ "internet", 1073741825, "1"},
{ "security", 1073741825, "5"},
{ "mechanisms", 1073741825, "5"},
{ "pkix", 1073741825, "7"},
{ "id-mod", 1073741825, "0"},
{ "id-pkix1-implicit-88", 1, "2"},
{ "id-xnyTest", 1879048204, NULL },
{ NULL, 1073741825, "id-ix"},
{ NULL, 1073741825, "29"},
{ NULL, 1, "1"},
{ "id-ix", 1880096780, "OBJECR"},
{ NULL, 1073741825, "id-ix"},
{ NULL, 1073741825, "29"},
{ NULL, 1, "2"},
{ "id-xnyTest", 805306380, NULL },
{ NULL, 1073741825, "id-ix"},
{ NULL, 1073741825, "29"},
{ NULL, 1, "1"},
{ NULL, 0, NULL }
};

View File

@ -0,0 +1,36 @@
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <libtasn1.h>
const asn1_static_node CVE_2018_1000654_2_asn1_tab[] = {
{ "TEST_TREE", 536875024, NULL },
{ NULL, 1610612748, NULL },
{ "iso", 1073741825, "1"},
{ "identified-organization", 1073741825, "3"},
{ "dod", 1073741825, "6"},
{ "internet", 1073741825, "1"},
{ "security", 1073741825, "5"},
{ "mechanisms", 1073741825, "5"},
{ "pkix", 1073741825, "7"},
{ "id-mod", 1073741825, "0"},
{ "id-pkix1-implicit-88", 1, "2"},
{ "id-oneTest", 1879048204, NULL },
{ NULL, 1073741825, "id-two"},
{ NULL, 1073741825, "9"},
{ NULL, 1, "1"},
{ "id-two", 1879048204, NULL },
{ NULL, 1073741825, "id-three"},
{ NULL, 1073741825, "2"},
{ NULL, 1, "2"},
{ "id-three", 1879048204, NULL },
{ NULL, 1073741825, "id-four"},
{ NULL, 1073741825, "3"},
{ NULL, 1, "3"},
{ "id-four", 805306380, NULL },
{ NULL, 1073741825, "id-two"},
{ NULL, 1073741825, "3"},
{ NULL, 1, "3"},
{ NULL, 0, NULL }
};

View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2002-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/****************************************************************/
/* Description: reproducer for CVE-2018-1000654 */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "CVE-2018-1000654-1_asn1_tab.h"
#include "CVE-2018-1000654-2_asn1_tab.h"
int
main (int argc, char *argv[])
{
int result, verbose = 0;
asn1_node definitions = NULL;
char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
if (argc > 1)
verbose = 1;
printf ("Test 1\n");
result =
asn1_array2tree (CVE_2018_1000654_1_asn1_tab, &definitions,
errorDescription);
if (result != ASN1_RECURSION)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (1);
}
asn1_delete_structure (&definitions);
printf ("Test 2\n");
result =
asn1_array2tree (CVE_2018_1000654_2_asn1_tab, &definitions,
errorDescription);
if (result != ASN1_RECURSION)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (1);
}
asn1_delete_structure (&definitions);
if (verbose)
printf ("Success\n");
exit (0);
}

View File

@ -0,0 +1,168 @@
/*
* Copyright (C) 2012-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/* Written by Simon Josefsson */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "libtasn1.h"
int
main (int argc, char **argv)
{
/* Test that values larger than long are rejected. This has worked
fine with all versions of libtasn1. */
int verbose = 0;
if (argc > 1)
verbose = 1;
{
unsigned char der[] = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
long l;
int len;
l = asn1_get_length_der (der, sizeof der, &len);
if (l == -2L)
{
if (verbose)
puts ("OK: asn1_get_length_der bignum");
}
else
{
printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len);
return 1;
}
}
/* Test that values larger than int but smaller than long are
rejected. This limitation was introduced with libtasn1 2.12. */
if (LONG_MAX > INT_MAX)
{
unsigned long num = ((long) UINT_MAX) << 2;
unsigned char der[20];
int der_len;
long l;
int len;
asn1_length_der (num, der, &der_len);
l = asn1_get_length_der (der, der_len, &len);
if (l == -2L)
{
if (verbose)
puts ("OK: asn1_get_length_der intnum");
}
else
{
printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
len);
return 1;
}
}
/* Test that values larger than would fit in the input string are
rejected. This problem was fixed in libtasn1 2.12. */
{
unsigned long num = 64;
unsigned char der[20];
int der_len;
long l;
int len;
asn1_length_der (num, der, &der_len);
der_len = sizeof (der);
l = asn1_get_length_der (der, der_len, &len);
if (l == -4L)
{
if (verbose)
puts ("OK: asn1_get_length_der overflow-small");
}
else
{
printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n",
l, len);
return 1;
}
}
/* Test that values larger than would fit in the input string are
rejected. This problem was fixed in libtasn1 2.12. */
{
unsigned long num = 1073741824;
unsigned char der[20];
int der_len;
long l;
int len;
asn1_length_der (num, der, &der_len);
der_len = sizeof (der);
l = asn1_get_length_der (der, der_len, &len);
if (l == -4L)
{
if (verbose)
puts ("OK: asn1_get_length_der overflow-large1");
}
else
{
printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n",
l, len);
return 1;
}
}
/* Test that values larger than would fit in the input string are
rejected. This problem was fixed in libtasn1 2.12. */
{
unsigned long num = 2147483649;
unsigned char der[20];
int der_len;
long l;
int len;
asn1_length_der (num, der, &der_len);
der_len = sizeof (der);
l = asn1_get_length_der (der, der_len, &len);
if (l == -2L)
{
if (verbose)
puts ("OK: asn1_get_length_der overflow-large2");
}
else
{
printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n",
l, len);
return 1;
}
}
return 0;
}

View File

@ -0,0 +1,226 @@
/*
* Copyright (C) 2011-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Written by Simon Josefsson
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libtasn1.h"
struct tv
{
int bitlen;
const char *bitstr;
int derlen;
const char *der;
};
static const struct tv tv[] = {
{0, "", 2, "\x01\x00"},
{1, "\x00", 3, "\x02\x07\x00"},
{2, "\x00", 3, "\x02\x06\x00"},
{3, "\x00", 3, "\x02\x05\x00"},
{4, "\x00", 3, "\x02\x04\x00"},
{5, "\x00", 3, "\x02\x03\x00"},
{6, "\x00", 3, "\x02\x02\x00"},
{7, "\x00", 3, "\x02\x01\x00"},
{8, "\x00\x00", 3, "\x02\x00\x00"},
{9, "\x00\x00", 4, "\x03\x07\x00\x00"},
{10, "\x00\x00", 4, "\x03\x06\x00\x00"},
{11, "\x00\x00", 4, "\x03\x05\x00\x00"},
{12, "\x00\x00", 4, "\x03\x04\x00\x00"},
{13, "\x00\x00", 4, "\x03\x03\x00\x00"},
{14, "\x00\x00", 4, "\x03\x02\x00\x00"},
{15, "\x00\x00", 4, "\x03\x01\x00\x00"},
{16, "\x00\x00", 4, "\x03\x00\x00\x00"},
{17, "\x00\x00\x00", 5, "\x04\x07\x00\x00\x00"},
{18, "\x00\x00\x00", 5, "\x04\x06\x00\x00\x00"},
{19, "\x00\x00\x00", 5, "\x04\x05\x00\x00\x00"},
{1, "\xFF", 3, "\x02\x07\x80"},
{2, "\xFF", 3, "\x02\x06\xc0"},
{3, "\xFF", 3, "\x02\x05\xe0"},
{4, "\xFF", 3, "\x02\x04\xf0"},
{5, "\xFF", 3, "\x02\x03\xf8"},
{6, "\xFF", 3, "\x02\x02\xfc"},
{7, "\xFF", 3, "\x02\x01\xfe"},
{8, "\xFF\xFF", 3, "\x02\x00\xff"},
{9, "\xFF\xFF", 4, "\x03\x07\xff\x80"},
{10, "\xFF\xFF", 4, "\x03\x06\xff\xc0"},
{11, "\xFF\xFF", 4, "\x03\x05\xff\xe0"},
{12, "\xFF\xFF", 4, "\x03\x04\xff\xf0"},
{13, "\xFF\xFF", 4, "\x03\x03\xff\xf8"},
{14, "\xFF\xFF", 4, "\x03\x02\xff\xfc"},
{15, "\xFF\xFF", 4, "\x03\x01\xff\xfe"},
{16, "\xFF\xFF", 4, "\x03\x00\xff\xff"},
{17, "\xFF\xFF\xFF", 5, "\x04\x07\xff\xff\x80"},
{18, "\xFF\xFF\xFF", 5, "\x04\x06\xff\xff\xc0"},
{19, "\xFF\xFF\xFF", 5, "\x04\x05\xff\xff\xe0"},
};
int
main (int argc, char *argv[])
{
int result;
unsigned char der[100];
unsigned char str[100];
int der_len = sizeof (der);
int str_size = sizeof (str);
int ret_len, bit_len;
size_t i;
{
unsigned int etype = 38;
unsigned int my_str_len = 10;
unsigned char my_str[10];
unsigned int tl_len = 10;
unsigned char tl[10];
/* https://gitlab.com/gnutls/libtasn1/-/issues/32 */
result = asn1_encode_simple_der (etype, my_str, my_str_len, tl, &tl_len);
if (result != ASN1_VALUE_NOT_VALID)
{
fprintf (stderr, "asn1_encode_simple_der out of range etype\n");
return 1;
}
}
/* Dummy test */
asn1_bit_der (NULL, 0, der, &der_len);
result = asn1_get_bit_der (der, 0, &ret_len, str, str_size, &bit_len);
if (result != ASN1_GENERIC_ERROR)
{
fprintf (stderr, "asn1_get_bit_der zero\n");
return 1;
}
/* Encode short strings with increasing bit lengths */
for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
{
/* Encode */
asn1_bit_der ((const unsigned char *) tv[i].bitstr, tv[i].bitlen,
der, &der_len);
#if 0
{
size_t j;
for (j = 0; j < der_len; j++)
printf ("\\x%02x", der[j]);
printf ("\n");
}
#endif
if (der_len != tv[i].derlen || memcmp (der, tv[i].der, der_len) != 0)
{
fprintf (stderr, "asn1_bit_der iter %lu\n", (unsigned long) i);
return 1;
}
/* Decode it */
result = asn1_get_bit_der (der, der_len, &ret_len, str,
str_size, &bit_len);
if (result != ASN1_SUCCESS || ret_len != tv[i].derlen
|| bit_len != tv[i].bitlen)
{
fprintf (stderr, "asn1_get_bit_der iter %lu, err: %d\n",
(unsigned long) i, result);
return 1;
}
}
/* Decode sample from "A Layman's Guide to a Subset of ASN.1, BER,
and DER" section 5.4 "BIT STRING": "The BER encoding of the BIT
STRING value "011011100101110111" can be any of the following,
among others, depending on the choice of padding bits, the form
of length octets [...]".
*/
/* 03 04 06 6e 5d c0 DER encoding */
memcpy (der, "\x04\x06\x6e\x5d\xc0", 5);
der_len = 5;
result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
if (result != ASN1_SUCCESS || ret_len != 5
|| bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0)
{
fprintf (stderr, "asn1_get_bit_der example\n");
return 1;
}
der_len = sizeof (der);
asn1_bit_der (str, bit_len, der, &der_len);
if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
{
fprintf (stderr, "asn1_bit_der example roundtrip\n");
return 1;
}
/* 03 04 06 6e 5d e0 padded with "100000" */
memcpy (der, "\x04\x06\x6e\x5d\xe0", 5);
der_len = 5;
result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
if (result != ASN1_SUCCESS || ret_len != 5
|| bit_len != 18 || memcmp (str, "\x6e\x5d\xe0", 3) != 0)
{
fprintf (stderr, "asn1_get_bit_der example padded\n");
return 1;
}
der_len = sizeof (der);
asn1_bit_der (str, bit_len, der, &der_len);
if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
{
fprintf (stderr, "asn1_bit_der example roundtrip\n");
return 1;
}
/* 03 81 04 06 6e 5d c0 long form of length octets */
memcpy (der, "\x81\x04\x06\x6e\x5d\xc0", 6);
der_len = 6;
result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
if (result != ASN1_SUCCESS || ret_len != 6
|| bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0)
{
fprintf (stderr, "asn1_get_bit_der example long form\n");
return 1;
}
der_len = sizeof (der);
asn1_bit_der (str, bit_len, der, &der_len);
if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
{
fprintf (stderr, "asn1_bit_der example roundtrip\n");
return 1;
}
return 0;
}

View File

@ -0,0 +1,156 @@
/*
* Copyright (C) 2012-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Written by Simon Josefsson
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libtasn1.h"
struct tv
{
unsigned int etype;
unsigned int str_len;
const void *str;
unsigned int der_len;
const void *der;
};
static const struct tv tv[] = {
{ASN1_ETYPE_IA5_STRING, 20,
"\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72",
22,
"\x16\x14\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72"},
{ASN1_ETYPE_PRINTABLE_STRING, 5, "\x4e\x69\x6b\x6f\x73",
7, "\x13\x05\x4e\x69\x6b\x6f\x73"},
{ASN1_ETYPE_UTF8_STRING, 12, "Αττική",
14, "\x0c\x0c\xce\x91\xcf\x84\xcf\x84\xce\xb9\xce\xba\xce\xae"},
{ASN1_ETYPE_TELETEX_STRING, 15,
"\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e",
17,
"\x14\x0f\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e"},
{ASN1_ETYPE_OCTET_STRING, 36,
"\x30\x22\x80\x0F\x32\x30\x31\x31\x30\x38\x32\x31\x30\x38\x30\x30\x30\x36\x5A\x81\x0F\x32\x30\x31\x31\x30\x38\x32\x33\x32\x30\x35\x39\x35\x39\x5A",
38,
"\x04\x24\x30\x22\x80\x0F\x32\x30\x31\x31\x30\x38\x32\x31\x30\x38\x30\x30\x30\x36\x5A\x81\x0F\x32\x30\x31\x31\x30\x38\x32\x33\x32\x30\x35\x39\x35\x39\x5A"}
};
#define SSTR(x) sizeof(x)-1,x
static const struct tv ber[] = {
{ASN1_ETYPE_OCTET_STRING,
SSTR ("\xa0\xa0"),
SSTR ("\x24\x80\x04\x82\x00\x02\xa0\xa0\x00\x00")},
{ASN1_ETYPE_OCTET_STRING,
SSTR ("\xa0\xa0\xb0\xb0\xb0"),
SSTR
("\x24\x80\x04\x82\x00\x02\xa0\xa0\x04\x82\x00\x03\xb0\xb0\xb0\x00\x00")},
{ASN1_ETYPE_OCTET_STRING,
SSTR ("\xa0\xa0\xb0\xb0\xb0\xa1\xa1"),
SSTR
("\x24\x80\x04\x82\x00\x02\xa0\xa0\x04\x82\x00\x03\xb0\xb0\xb0\x24\x80\x04\x82\x00\x02\xa1\xa1\x00\x00\x00\x00")},
{ASN1_ETYPE_OCTET_STRING,
SSTR ("\xa0\xa0\xb0\xb0\xb0\xa1\xa1\xc1"),
SSTR
("\x24\x80\x04\x82\x00\x02\xa0\xa0\x04\x82\x00\x03\xb0\xb0\xb0\x24\x80\x04\x82\x00\x02\xa1\xa1\x04\x82\x00\x01\xc1\x00\x00\x00\x00")},
};
int
main (int argc, char *argv[])
{
int ret;
unsigned char tl[ASN1_MAX_TL_SIZE];
unsigned int tl_len, der_len, str_len;
const unsigned char *str;
unsigned char *b;
unsigned int i;
/* Dummy test */
for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
{
/* Encode */
tl_len = sizeof (tl);
ret = asn1_encode_simple_der (tv[i].etype, tv[i].str, tv[i].str_len,
tl, &tl_len);
if (ret != ASN1_SUCCESS)
{
fprintf (stderr, "Encoding error in %u: %s\n", i,
asn1_strerror (ret));
return 1;
}
der_len = tl_len + tv[i].str_len;
if (der_len != tv[i].der_len || memcmp (tl, tv[i].der, tl_len) != 0)
{
fprintf (stderr,
"DER encoding differs in %u! (size: %u, expected: %u)\n",
i, der_len, tv[i].der_len);
return 1;
}
/* decoding */
ret =
asn1_decode_simple_der (tv[i].etype, tv[i].der, tv[i].der_len, &str,
&str_len);
if (ret != ASN1_SUCCESS)
{
fprintf (stderr, "Decoding error in %u: %s\n", i,
asn1_strerror (ret));
return 1;
}
if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0)
{
fprintf (stderr,
"DER decoded data differ in %u! (size: %u, expected: %u)\n",
i, der_len, tv[i].str_len);
return 1;
}
}
/* BER decoding */
for (i = 0; i < sizeof (ber) / sizeof (ber[0]); i++)
{
/* decoding */
ret =
asn1_decode_simple_ber (ber[i].etype, ber[i].der, ber[i].der_len, &b,
&str_len, NULL);
if (ret != ASN1_SUCCESS)
{
fprintf (stderr, "BER decoding error in %u: %s\n", i,
asn1_strerror (ret));
return 1;
}
if (str_len != ber[i].str_len || memcmp (b, ber[i].str, str_len) != 0)
{
fprintf (stderr,
"BER decoded data differ in %u! (size: %u, expected: %u)\n",
i, str_len, ber[i].str_len);
return 1;
}
free (b);
}
return 0;
}

View File

@ -0,0 +1,121 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libtasn1.h"
struct tv
{
int der_len;
const unsigned char *der;
const char *oid;
int expected_error;
};
static const struct tv tv[] = {
{.der_len = 5,
.der = (void *) "\x06\x03\x80\x37\x03",
.oid = "2.999.3",
.expected_error = ASN1_DER_ERROR /* leading 0x80 */
},
{.der_len = 12,
.der = (void *) "\x06\x0a\x2b\x06\x01\x80\x01\x92\x08\x09\x05\x01",
.oid = "1.3.6.1.4.1.2312.9.5.1",
.expected_error = ASN1_DER_ERROR /* leading 0x80 */
},
{.der_len = 6,
.der = (void *) "\x06\x04\x01\x02\x03\x04",
.oid = "0.1.2.3.4",
.expected_error = ASN1_SUCCESS},
{.der_len = 5,
.der = (void *) "\x06\x03\x51\x02\x03",
.oid = "2.1.2.3",
.expected_error = ASN1_SUCCESS},
{.der_len = 5,
.der = (void *) "\x06\x03\x88\x37\x03",
.oid = "2.999.3",
.expected_error = ASN1_SUCCESS},
{.der_len = 12,
.der = (void *) "\x06\x0a\x2b\x06\x01\x04\x01\x92\x08\x09\x05\x01",
.oid = "1.3.6.1.4.1.2312.9.5.1",
.expected_error = ASN1_SUCCESS},
{.der_len = 19,
.der =
(void *)
"\x06\x11\xfa\x80\x00\x00\x00\x0e\x01\x0e\xfa\x80\x00\x00\x00\x0e\x63\x6f\x6d",
.oid = "2.1998768.0.0.14.1.14.1998848.0.0.14.99.111.109",
.expected_error = ASN1_SUCCESS},
{.der_len = 19,
.der =
(void *)
"\x06\x11\x2b\x06\x01\x04\x01\x92\x08\x09\x02\xaa\xda\xbe\xbe\xfa\x72\x01\x07",
.oid = "1.3.6.1.4.1.2312.9.2.1467399257458.1.7",
.expected_error = ASN1_SUCCESS},
};
int
main (int argc, char *argv[])
{
char str[128];
int ret, ret_len;
size_t i;
for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
{
/* decode */
ret =
asn1_get_object_id_der (tv[i].der + 1,
tv[i].der_len - 1, &ret_len, str,
sizeof (str));
if (ret != tv[i].expected_error)
{
fprintf (stderr,
"%d: asn1_get_object_id_der iter %lu: got '%s' expected %d\n",
__LINE__, (unsigned long) i, asn1_strerror (ret),
tv[i].expected_error);
return 1;
}
if (tv[i].expected_error != ASN1_SUCCESS)
continue;
if (ret_len != tv[i].der_len - 1)
{
fprintf (stderr,
"%d: iter %lu: error in DER, length returned is %d, had %d\n",
__LINE__, (unsigned long) i, ret_len, tv[i].der_len - 1);
return 1;
}
if (strcmp (tv[i].oid, str) != 0)
{
fprintf (stderr,
"%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n",
__LINE__, (unsigned long) i, str, tv[i].oid);
return 1;
}
}
return 0;
}

View File

@ -0,0 +1,133 @@
/*
* Copyright (C) 2019 Nikos Mavrogiannopoulos
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libtasn1.h"
struct tv
{
int der_len;
const unsigned char *der;
const char *oid;
int expected_error;
};
static const struct tv tv[] = {
{.der_len = 0,
.der = (void *) "",
.oid = "5.999.3",
.expected_error = ASN1_VALUE_NOT_VALID /* cannot start with 5 */
},
{.der_len = 0,
.der = (void *) "",
.oid = "0.48.9",
.expected_error = ASN1_VALUE_NOT_VALID /* second field cannot be 48 */
},
{.der_len = 0,
.der = (void *) "",
.oid = "1.40.9",
.expected_error = ASN1_VALUE_NOT_VALID /* second field cannot be 40 */
},
{.der_len = 4,
.der = (void *) "\x06\x02\x4f\x63",
.oid = "1.39.99",
.expected_error = ASN1_SUCCESS,
},
{.der_len = 6,
.der = (void *) "\x06\x04\x01\x02\x03\x04",
.oid = "0.1.2.3.4",
.expected_error = ASN1_SUCCESS},
{.der_len = 5,
.der = (void *) "\x06\x03\x51\x02\x03",
.oid = "2.1.2.3",
.expected_error = ASN1_SUCCESS},
{.der_len = 5,
.der = (void *) "\x06\x03\x88\x37\x03",
.oid = "2.999.3",
.expected_error = ASN1_SUCCESS},
{.der_len = 12,
.der = (void *) "\x06\x0a\x2b\x06\x01\x04\x01\x92\x08\x09\x05\x01",
.oid = "1.3.6.1.4.1.2312.9.5.1",
.expected_error = ASN1_SUCCESS},
{.der_len = 19,
.der =
(void *)
"\x06\x11\xfa\x80\x00\x00\x00\x0e\x01\x0e\xfa\x80\x00\x00\x00\x0e\x63\x6f\x6d",
.oid = "2.1998768.0.0.14.1.14.1998848.0.0.14.99.111.109",
.expected_error = ASN1_SUCCESS},
{.der_len = 19,
.der =
(void *)
"\x06\x11\x2b\x06\x01\x04\x01\x92\x08\x09\x02\xaa\xda\xbe\xbe\xfa\x72\x01\x07",
.oid = "1.3.6.1.4.1.2312.9.2.1467399257458.1.7",
.expected_error = ASN1_SUCCESS},
};
int
main (int argc, char *argv[])
{
unsigned char der[128];
int ret, der_len, i, j;
for (i = 0; i < (int) (sizeof (tv) / sizeof (tv[0])); i++)
{
der_len = sizeof (der);
ret = asn1_object_id_der (tv[i].oid, der, &der_len, 0);
if (ret != ASN1_SUCCESS)
{
if (ret == tv[i].expected_error)
continue;
fprintf (stderr,
"%d: iter %lu, encoding of OID failed: %s\n",
__LINE__, (unsigned long) i, asn1_strerror (ret));
return 1;
}
else if (ret != tv[i].expected_error)
{
fprintf (stderr,
"%d: iter %lu, encoding of OID %s succeeded when expecting failure\n",
__LINE__, (unsigned long) i, tv[i].oid);
return 1;
}
if (der_len != tv[i].der_len || memcmp (der, tv[i].der, der_len) != 0)
{
fprintf (stderr,
"%d: iter %lu, re-encoding of OID %s resulted to different string (%d vs %d bytes)\n",
__LINE__, (unsigned long) i, tv[i].oid, der_len,
tv[i].der_len);
fprintf (stderr, "\nGot:\t\t");
for (j = 0; j < der_len; j++)
fprintf (stderr, "%.2x", der[j]);
fprintf (stderr, "\nExpected:\t");
for (j = 0; j < tv[i].der_len; j++)
fprintf (stderr, "%.2x", tv[i].der[j]);
fprintf (stderr, "\n");
return 1;
}
}
return 0;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2019-2022 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/****************************************************************/
/* Description: run reproducers for several fixed issues */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libtasn1.h>
#include <int.h>
/* produces endless loop (fixed by d4b624b2):
* The following translates into a single node with all pointers
* (right,left,down) set to NULL. */
const asn1_static_node endless_asn1_tab[] = {
{"TEST_TREE", 536875024, NULL},
{NULL, 0, NULL}
};
/* produces memory leak (fixed by f16d1ff9):
* 152 bytes in 1 blocks are definitely lost in loss record 1 of 1
* at 0x4837B65: calloc (vg_replace_malloc.c:762)
* by 0x4851C0D: _asn1_add_static_node (parser_aux.c:71)
* by 0x4853AAC: asn1_array2tree (structure.c:200)
* by 0x10923B: main (single_node.c:67)
*/
const asn1_static_node tab[] = {
{"a", CONST_DOWN, ""},
{"b", 0, ""},
{"c", 0, ""},
{NULL, 0, NULL}
};
int
main (int argc, char *argv[])
{
int result, verbose = 0;
asn1_node definitions = NULL;
char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
if (argc > 1)
verbose = 1;
result = asn1_array2tree (endless_asn1_tab, &definitions, errorDescription);
if (result != ASN1_SUCCESS)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (EXIT_FAILURE);
}
asn1_delete_structure (&definitions);
definitions = NULL;
result = asn1_array2tree (tab, &definitions, errorDescription);
if (result != ASN1_SUCCESS)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (EXIT_FAILURE);
}
asn1_delete_structure (&definitions);
if (verbose)
printf ("Success\n");
exit (EXIT_SUCCESS);
}