1/*	$KAME: vendorid.c,v 1.8 2001/03/27 02:39:57 thorpej Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <errno.h>
39#include <ctype.h>
40
41#include "var.h"
42#include "misc.h"
43#include "vmbuf.h"
44#include "plog.h"
45#include "debug.h"
46
47#include "localconf.h"
48#include "isakmp_var.h"
49#include "isakmp.h"
50#include "vendorid.h"
51#include "crypto_openssl.h"
52
53const char *vendorid_strings[] = VENDORID_STRINGS;
54
55/*
56 * set hashed vendor id.
57 * hash function is always MD5.
58 */
59vchar_t *
60set_vendorid(int vendorid)
61{
62	vchar_t vid, *vidhash;
63
64	if (vendorid == VENDORID_UNKNOWN) {
65		/*
66		 * The default unknown ID gets translated to
67		 * KAME/racoon.
68		 */
69		vendorid = VENDORID_KAME;
70	}
71
72	if (vendorid < 0 || vendorid >= NUMVENDORIDS) {
73		plog(LLV_ERROR, LOCATION, NULL,
74		    "invalid vendor ID index: %d\n", vendorid);
75		return (NULL);
76	}
77
78	vid.v = (char *) vendorid_strings[vendorid];
79	vid.l = strlen(vendorid_strings[vendorid]);
80
81	vidhash = eay_md5_one(&vid);
82	if (vidhash == NULL)
83		plog(LLV_ERROR, LOCATION, NULL,
84		    "unable to hash vendor ID string\n");
85
86	return vidhash;
87}
88
89/*
90 * Check the vendor ID payload -- return the vendor ID index
91 * if we find a recognized one, or UNKNOWN if we don't.
92 */
93int
94check_vendorid(gen)
95	struct isakmp_gen *gen;		/* points to Vendor ID payload */
96{
97	vchar_t vid, *vidhash;
98	int i, vidlen;
99
100	if (gen == NULL)
101		return (VENDORID_UNKNOWN);
102
103	vidlen = ntohs(gen->len) - sizeof(*gen);
104
105	for (i = 0; i < NUMVENDORIDS; i++) {
106		vid.v = (char *) vendorid_strings[i];
107		vid.l = strlen(vendorid_strings[i]);
108
109		vidhash = eay_md5_one(&vid);
110		if (vidhash == NULL) {
111			plog(LLV_ERROR, LOCATION, NULL,
112			    "unable to hash vendor ID string\n");
113			return (VENDORID_UNKNOWN);
114		}
115
116		if (vidhash->l <= vidlen &&
117		    memcmp(vidhash->v, gen + 1, vidhash->l) == 0) {
118			plog(LLV_INFO, LOCATION, NULL,
119			    "received Vendor ID: %s\n",
120			    vendorid_strings[i]);
121			vfree(vidhash);
122			return (i);
123		}
124		vfree(vidhash);
125	}
126
127	plog(LLV_DEBUG, LOCATION, NULL, "received unknown Vendor ID\n");
128	return (VENDORID_UNKNOWN);
129}
130