Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / libdane / dane-params.c
1 /*
2  * Copyright (C) 2012 KU Leuven
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of libdane.
7  *
8  * The libdane library is free software; you can redistribute it
9  * and/or modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>
20  *
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <arpa/inet.h>
30 #include <unbound.h>
31 #include <gnutls/dane.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
34
35 typedef struct cert_type_entry {
36         const char *name;
37         dane_cert_type_t type;
38 } cert_type_entry;
39
40 static const cert_type_entry dane_cert_types[] = {
41         {"X.509", DANE_CERT_X509},
42         {"SubjectPublicKeyInfo", DANE_CERT_PK},
43         {NULL, 0}
44 };
45
46 typedef struct match_type_entry {
47         const char *name;
48         dane_match_type_t type;
49 } match_type_entry;
50
51 static const match_type_entry dane_match_types[] = {
52         {"Exact match", DANE_MATCH_EXACT},
53         {"SHA2-256 hash", DANE_MATCH_SHA2_256},
54         {"SHA2-512 hash", DANE_MATCH_SHA2_512},
55         {NULL, 0}
56 };
57
58 typedef struct cert_usage_entry {
59         const char *name;
60         dane_cert_usage_t usage;
61 } cert_usage_entry;
62
63 static const cert_usage_entry dane_cert_usages[] = {
64         {"CA", DANE_CERT_USAGE_CA},
65         {"End-entity", DANE_CERT_USAGE_EE},
66         {"Local CA", DANE_CERT_USAGE_LOCAL_CA},
67         {"Local end-entity", DANE_CERT_USAGE_LOCAL_EE},
68         {NULL, 0}
69 };
70
71
72
73 /**
74  * dane_cert_type_name:
75  * @type: is a DANE match type
76  *
77  * Convert a #dane_cert_type_t value to a string.
78  *
79  * Returns: a string that contains the name of the specified
80  *   type, or %NULL.
81  **/
82 const char *dane_cert_type_name(dane_cert_type_t type)
83 {
84         const cert_type_entry *e = dane_cert_types;
85
86         while (e->name != NULL) {
87                 if (e->type == type)
88                         return e->name;
89                 e++;
90         }
91
92         return NULL;
93 }
94
95 /**
96  * dane_match_type_name:
97  * @type: is a DANE match type
98  *
99  * Convert a #dane_match_type_t value to a string.
100  *
101  * Returns: a string that contains the name of the specified
102  *   type, or %NULL.
103  **/
104 const char *dane_match_type_name(dane_match_type_t type)
105 {
106         const match_type_entry *e = dane_match_types;
107
108         while (e->name != NULL) {
109                 if (e->type == type)
110                         return e->name;
111                 e++;
112         }
113
114         return NULL;
115 }
116
117 /**
118  * dane_cert_usage_name:
119  * @type: is a DANE match type
120  *
121  * Convert a #dane_cert_usage_t value to a string.
122  *
123  * Returns: a string that contains the name of the specified
124  *   type, or %NULL.
125  **/
126 const char *dane_cert_usage_name(dane_cert_usage_t usage)
127 {
128         const cert_usage_entry *e = dane_cert_usages;
129
130         while (e->name != NULL) {
131                 if (e->usage == usage)
132                         return e->name;
133                 e++;
134         }
135
136         return NULL;
137
138 }