cert.c revision 1.12
1/* $OpenBSD: cert.c,v 1.12 2000/02/19 19:32:53 niklas Exp $ */ 2/* $EOM: cert.c,v 1.14 2000/02/19 07:58:54 niklas Exp $ */ 3 4/* 5 * Copyright (c) 1998, 1999 Niels Provos. All rights reserved. 6 * Copyright (c) 1999, 2000 Niklas Hallqvist. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * 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. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Ericsson Radio Systems. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * This code was written under funding by Ericsson Radio Systems. 36 */ 37 38#include <sys/param.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42 43#include <ssl/ssl.h> 44 45#include "sysdep.h" 46 47#include "cert.h" 48#include "isakmp_num.h" 49#include "log.h" 50#include "x509.h" 51 52struct cert_handler cert_handler[] = { 53#ifdef USE_X509 54 { 55 ISAKMP_CERTENC_X509_SIG, 56 x509_cert_init, x509_cert_get, x509_cert_validate, 57 x509_cert_insert, x509_cert_free, 58 x509_certreq_validate, x509_certreq_decode, x509_free_aca, 59 x509_cert_obtain, x509_cert_get_key, x509_cert_get_subject 60 } 61#endif 62}; 63 64/* Initialize all certificate handlers */ 65 66int 67cert_init (void) 68{ 69 int i, err = 1; 70 71 for (i = 0; i < sizeof cert_handler / sizeof cert_handler[0]; i++) 72 if (cert_handler[i].cert_init && !(*cert_handler[i].cert_init) ()) 73 err = 0; 74 75 return err; 76} 77 78struct cert_handler * 79cert_get (u_int16_t id) 80{ 81 int i; 82 83 for (i = 0; i < sizeof cert_handler / sizeof cert_handler[0]; i++) 84 if (id == cert_handler[i].id) 85 return &cert_handler[i]; 86 return 0; 87} 88 89/* Decode a CERTREQ and return a parsed structure. */ 90struct certreq_aca * 91certreq_decode (u_int16_t type, u_int8_t *data, u_int32_t datalen) 92{ 93 struct cert_handler *handler; 94 struct certreq_aca aca, *ret; 95 96 handler = cert_get (type); 97 if (!handler) 98 return 0; 99 100 aca.id = type; 101 aca.handler = handler; 102 103 if (datalen > 0) 104 { 105 aca.data = handler->certreq_decode (data, datalen); 106 if (!aca.data) 107 return 0; 108 } 109 else 110 aca.data = 0; 111 112 ret = malloc (sizeof aca); 113 if (!ret) 114 { 115 log_error ("certreq_decode: malloc (%d) failed", sizeof aca); 116 handler->free_aca (aca.data); 117 return 0; 118 } 119 120 memcpy (ret, &aca, sizeof aca); 121 122 return ret; 123} 124