working version with crypto
[anytun.git] / keyexchange / isakmpd-20041012 / attribute.c
blob362805b7a4328ef13104e560f97d9f3b8b661732
1 /* $OpenBSD: attribute.c,v 1.11 2004/05/14 08:42:56 hshoexer Exp $ */
2 /* $EOM: attribute.c,v 1.10 2000/02/20 19:58:36 niklas Exp $ */
4 /*
5 * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This code was written under funding by Ericsson Radio Systems.
32 #include <sys/types.h>
33 #include <string.h>
35 #include "sysdep.h"
37 #include "attribute.h"
38 #include "conf.h"
39 #include "log.h"
40 #include "isakmp.h"
41 #include "util.h"
43 u_int8_t *
44 attribute_set_basic(u_int8_t *buf, u_int16_t type, u_int16_t value)
46 SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(1, type));
47 SET_ISAKMP_ATTR_LENGTH_VALUE(buf, value);
48 return buf + ISAKMP_ATTR_VALUE_OFF;
51 u_int8_t *
52 attribute_set_var(u_int8_t *buf, u_int16_t type, u_int8_t *value,
53 u_int16_t len)
55 SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(0, type));
56 SET_ISAKMP_ATTR_LENGTH_VALUE(buf, len);
57 memcpy(buf + ISAKMP_ATTR_VALUE_OFF, value, len);
58 return buf + ISAKMP_ATTR_VALUE_OFF + len;
62 * Execute a function FUNC taking an attribute type, value, length and ARG
63 * as arguments for each attribute in the area of ISAKMP attributes located
64 * at BUF, sized SZ. If any invocation fails, the processing aborts with a
65 * -1 return value. If all goes well return zero.
67 int
68 attribute_map(u_int8_t *buf, size_t sz, int (*func)(u_int16_t, u_int8_t *,
69 u_int16_t, void *), void *arg)
71 u_int8_t *attr;
72 int fmt;
73 u_int16_t type;
74 u_int8_t *value;
75 u_int16_t len;
77 for (attr = buf; attr < buf + sz; attr = value + len) {
78 if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz)
79 return -1;
80 type = GET_ISAKMP_ATTR_TYPE(attr);
81 fmt = ISAKMP_ATTR_FORMAT(type);
82 type = ISAKMP_ATTR_TYPE(type);
83 value = attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF
84 : ISAKMP_ATTR_VALUE_OFF);
85 len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN
86 : GET_ISAKMP_ATTR_LENGTH_VALUE(attr));
87 if (value + len > buf + sz)
88 return -1;
89 if (func(type, value, len, arg))
90 return -1;
92 return 0;
95 int
96 attribute_set_constant(char *section, char *tag, struct constant_map *map,
97 int attr_class, u_int8_t **attr)
99 char *name;
100 int value;
102 name = conf_get_str(section, tag);
103 if (!name) {
104 LOG_DBG((LOG_MISC, 70,
105 "attribute_set_constant: no %s in the %s section", tag,
106 section));
107 return -1;
109 value = constant_value(map, name);
110 *attr = attribute_set_basic(*attr, attr_class, value);
111 return 0;