Use DES_set_key_unchecked().
[heimdal.git] / lib / hx509 / env.c
blob9d771c506fe632cb914dd516d4539d63d9d62af4
1 /*
2 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
35 RCSID("$Id$");
37 /**
38 * @page page_env Hx509 enviroment functions
40 * See the library functions here: @ref hx509_env
43 /**
44 * Add a new key/value pair to the hx509_env.
46 * @param context A hx509 context.
47 * @param env enviroment to add the enviroment variable too.
48 * @param key key to add
49 * @param value value to add
51 * @return An hx509 error code, see hx509_get_error_string().
53 * @ingroup hx509_env
56 int
57 hx509_env_add(hx509_context context, hx509_env *env,
58 const char *key, const char *value)
60 hx509_env n;
62 n = malloc(sizeof(*n));
63 if (n == NULL) {
64 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
65 return ENOMEM;
68 n->type = env_string;
69 n->next = NULL;
70 n->name = strdup(key);
71 if (n->name == NULL) {
72 free(n);
73 return ENOMEM;
75 n->u.string = strdup(value);
76 if (n->u.string == NULL) {
77 free(n->name);
78 free(n);
79 return ENOMEM;
82 /* add to tail */
83 if (*env) {
84 hx509_env e = *env;
85 while (e->next)
86 e = e->next;
87 e->next = n;
88 } else
89 *env = n;
91 return 0;
94 /**
95 * Add a new key/binding pair to the hx509_env.
97 * @param context A hx509 context.
98 * @param env enviroment to add the enviroment variable too.
99 * @param key key to add
100 * @param list binding list to add
102 * @return An hx509 error code, see hx509_get_error_string().
104 * @ingroup hx509_env
108 hx509_env_add_binding(hx509_context context, hx509_env *env,
109 const char *key, hx509_env list)
111 hx509_env n;
113 n = malloc(sizeof(*n));
114 if (n == NULL) {
115 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
116 return ENOMEM;
119 n->type = env_list;
120 n->next = NULL;
121 n->name = strdup(key);
122 if (n->name == NULL) {
123 free(n);
124 return ENOMEM;
126 n->u.list = list;
128 /* add to tail */
129 if (*env) {
130 hx509_env e = *env;
131 while (e->next)
132 e = e->next;
133 e->next = n;
134 } else
135 *env = n;
137 return 0;
142 * Search the hx509_env for a length based key.
144 * @param context A hx509 context.
145 * @param env enviroment to add the enviroment variable too.
146 * @param key key to search for.
147 * @param len length of key.
149 * @return the value if the key is found, NULL otherwise.
151 * @ingroup hx509_env
154 const char *
155 hx509_env_lfind(hx509_context context, hx509_env env,
156 const char *key, size_t len)
158 while(env) {
159 if (strncmp(key, env->name ,len) == 0
160 && env->name[len] == '\0' && env->type == env_string)
161 return env->u.string;
162 env = env->next;
164 return NULL;
168 * Search the hx509_env for a key.
170 * @param context A hx509 context.
171 * @param env enviroment to add the enviroment variable too.
172 * @param key key to search for.
174 * @return the value if the key is found, NULL otherwise.
176 * @ingroup hx509_env
179 const char *
180 hx509_env_find(hx509_context context, hx509_env env, const char *key)
182 while(env) {
183 if (strcmp(key, env->name) == 0 && env->type == env_string)
184 return env->u.string;
185 env = env->next;
187 return NULL;
191 * Search the hx509_env for a binding.
193 * @param context A hx509 context.
194 * @param env enviroment to add the enviroment variable too.
195 * @param key key to search for.
197 * @return the binding if the key is found, NULL if not found.
199 * @ingroup hx509_env
202 hx509_env
203 hx509_env_find_binding(hx509_context context,
204 hx509_env env,
205 const char *key)
207 while(env) {
208 if (strcmp(key, env->name) == 0 && env->type == env_list)
209 return env->u.list;
210 env = env->next;
212 return NULL;
215 static void
216 env_free(hx509_env b)
218 while(b) {
219 hx509_env next = b->next;
221 if (b->type == env_string)
222 free(b->u.string);
223 else if (b->type == env_list)
224 env_free(b->u.list);
226 free(b->name);
227 free(b);
228 b = next;
233 * Free an hx509_env enviroment context.
235 * @param env the enviroment to free.
237 * @ingroup hx509_env
240 void
241 hx509_env_free(hx509_env *env)
243 if (*env)
244 env_free(*env);
245 *env = NULL;