summaryrefslogtreecommitdiff
path: root/tests/object-id-decoding.c
blob: e3e96698511899698985d6b5aa09a4c0517ea13c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * 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_str;
  const char *oid;
  int expected_error;
};

static const struct tv tv[] = {
  {.der_len = 12,
   .der_str = (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_str =
   (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_str+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 %d expected %d\n",
		   __LINE__, (unsigned long) i, ret, tv[i].expected_error);
	  return 1;
	}

      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;
}