cert.c revision 1.2
1/* $OpenBSD: cert.c,v 1.2 1998/11/15 00:43:50 niklas Exp $ */ 2 3/* 4 * Copyright (c) 1998 Niels Provos. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Ericsson Radio Systems. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * This code was written under funding by Ericsson Radio Systems. 34 */ 35 36#include <sys/param.h> 37#include <stdlib.h> 38#include <string.h> 39 40#include "cert.h" 41#include "isakmp_num.h" 42#include "x509.h" 43 44struct cert_handler cert_handler[] = { 45 {ISAKMP_CERTENC_X509_SIG, 46 x509_certreq_validate, x509_certreq_decode, x509_free_aca, 47 x509_cert_obtain, x509_cert_get_key, x509_cert_get_subject} 48}; 49 50struct cert_handler * 51cert_get (u_int16_t id) 52{ 53 int i; 54 55 for (i = 0; i < sizeof cert_handler / sizeof cert_handler[0]; i++) 56 if (id == cert_handler[i].id) 57 return &cert_handler[i]; 58 return NULL; 59} 60 61 62/* Decode a CERTREQ and return a parsed structure */ 63 64struct certreq_aca * 65certreq_decode (u_int16_t type, u_int8_t *data, u_int32_t datalen) 66{ 67 struct cert_handler *handler; 68 struct certreq_aca aca, *ret; 69 70 if ((handler = cert_get (type)) == NULL) 71 return NULL; 72 73 aca.id = type; 74 aca.handler = handler; 75 76 if (datalen > 0) 77 { 78 aca.data = handler->certreq_decode (data, datalen); 79 if (aca.data == NULL) 80 return NULL; 81 } 82 else 83 aca.data = NULL; 84 85 if ((ret = malloc (sizeof (aca))) == NULL) 86 { 87 handler->free_aca (aca.data); 88 return NULL; 89 } 90 91 memcpy (ret, &aca, sizeof (aca)); 92 93 return ret; 94} 95