xref: /openbsd/sbin/isakmpd/attribute.c (revision 78b63d65)
1 /*	$OpenBSD: attribute.c,v 1.8 2000/02/25 17:23:38 niklas Exp $	*/
2 /*	$EOM: attribute.c,v 1.10 2000/02/20 19:58:36 niklas Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 1999 Niklas Hallqvist.  All rights reserved.
6  *
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Ericsson Radio Systems.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * This code was written under funding by Ericsson Radio Systems.
35  */
36 
37 #include <sys/types.h>
38 #include <string.h>
39 
40 #include "sysdep.h"
41 
42 #include "attribute.h"
43 #include "conf.h"
44 #include "log.h"
45 #include "isakmp.h"
46 #include "util.h"
47 
48 u_int8_t *
49 attribute_set_basic (u_int8_t *buf, u_int16_t type, u_int16_t value)
50 {
51   SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (1, type));
52   SET_ISAKMP_ATTR_LENGTH_VALUE (buf, value);
53   return buf + ISAKMP_ATTR_VALUE_OFF;
54 }
55 
56 u_int8_t *
57 attribute_set_var (u_int8_t *buf, u_int16_t type, u_int8_t *value,
58 		   u_int16_t len)
59 {
60   SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (0, type));
61   SET_ISAKMP_ATTR_LENGTH_VALUE (buf, len);
62   memcpy (buf + ISAKMP_ATTR_VALUE_OFF, value, len);
63   return buf + ISAKMP_ATTR_VALUE_OFF + len;
64 }
65 
66 /*
67  * Execute a function FUNC taking an attribute type, value, length and ARG
68  * as arguments for each attribute in the area of ISAKMP attributes located
69  * at BUF, sized SZ.  If any invocation fails, the processing aborts with a
70  * -1 return value.  If all goes well return zero.
71  */
72 int
73 attribute_map (u_int8_t *buf, size_t sz,
74 	       int (*func) (u_int16_t, u_int8_t *, u_int16_t, void *),
75 	       void *arg)
76 {
77   u_int8_t *attr;
78   int fmt;
79   u_int16_t type;
80   u_int8_t *value;
81   u_int16_t len;
82 
83   for (attr = buf; attr < buf + sz; attr = value + len)
84     {
85       if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz)
86 	return -1;
87       type = GET_ISAKMP_ATTR_TYPE (attr);
88       fmt = ISAKMP_ATTR_FORMAT (type);
89       type = ISAKMP_ATTR_TYPE (type);
90       value
91 	= attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF : ISAKMP_ATTR_VALUE_OFF);
92       len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN
93 	     : GET_ISAKMP_ATTR_LENGTH_VALUE (attr));
94       if (value + len > buf + sz)
95 	return -1;
96       if (func (type, value, len, arg))
97 	return -1;
98     }
99   return 0;
100 }
101 
102 int
103 attribute_set_constant (char *section, char *tag, struct constant_map *map,
104 			int attr_class, u_int8_t **attr)
105 {
106   char *name;
107   int value;
108 
109   name = conf_get_str (section, tag);
110   if (!name)
111     {
112       LOG_DBG ((LOG_MISC, 70,
113 		"attribute_set_constant: no %s in the %s section", tag,
114 		section));
115       return -1;
116     }
117   value = constant_value (map, name);
118   *attr = attribute_set_basic (*attr, attr_class, value);
119   return 0;
120 }
121