1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/x509/x509spki.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,121 @@
1.4 +/* x509spki.c */
1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
1.6 + * project 1999.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * licensing@OpenSSL.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 +
1.62 +#include <stdio.h>
1.63 +#include "cryptlib.h"
1.64 +#include <openssl/x509.h>
1.65 +
1.66 +EXPORT_C int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
1.67 +{
1.68 + if ((x == NULL) || (x->spkac == NULL)) return(0);
1.69 + return(X509_PUBKEY_set(&(x->spkac->pubkey),pkey));
1.70 +}
1.71 +
1.72 +EXPORT_C EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x)
1.73 +{
1.74 + if ((x == NULL) || (x->spkac == NULL))
1.75 + return(NULL);
1.76 + return(X509_PUBKEY_get(x->spkac->pubkey));
1.77 +}
1.78 +
1.79 +/* Load a Netscape SPKI from a base64 encoded string */
1.80 +
1.81 +EXPORT_C NETSCAPE_SPKI * NETSCAPE_SPKI_b64_decode(const char *str, int len)
1.82 +{
1.83 + unsigned char *spki_der;
1.84 + const unsigned char *p;
1.85 + int spki_len;
1.86 + NETSCAPE_SPKI *spki;
1.87 + if(len <= 0) len = strlen(str);
1.88 + if (!(spki_der = OPENSSL_malloc(len + 1))) {
1.89 + X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, ERR_R_MALLOC_FAILURE);
1.90 + return NULL;
1.91 + }
1.92 + spki_len = EVP_DecodeBlock(spki_der, (const unsigned char *)str, len);
1.93 + if(spki_len < 0) {
1.94 + X509err(X509_F_NETSCAPE_SPKI_B64_DECODE,
1.95 + X509_R_BASE64_DECODE_ERROR);
1.96 + OPENSSL_free(spki_der);
1.97 + return NULL;
1.98 + }
1.99 + p = spki_der;
1.100 + spki = d2i_NETSCAPE_SPKI(NULL, &p, spki_len);
1.101 + OPENSSL_free(spki_der);
1.102 + return spki;
1.103 +}
1.104 +
1.105 +/* Generate a base64 encoded string from an SPKI */
1.106 +
1.107 +EXPORT_C char * NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki)
1.108 +{
1.109 + unsigned char *der_spki, *p;
1.110 + char *b64_str;
1.111 + int der_len;
1.112 + der_len = i2d_NETSCAPE_SPKI(spki, NULL);
1.113 + der_spki = OPENSSL_malloc(der_len);
1.114 + b64_str = OPENSSL_malloc(der_len * 2);
1.115 + if(!der_spki || !b64_str) {
1.116 + X509err(X509_F_NETSCAPE_SPKI_B64_ENCODE, ERR_R_MALLOC_FAILURE);
1.117 + return NULL;
1.118 + }
1.119 + p = der_spki;
1.120 + i2d_NETSCAPE_SPKI(spki, &p);
1.121 + EVP_EncodeBlock((unsigned char *)b64_str, der_spki, der_len);
1.122 + OPENSSL_free(der_spki);
1.123 + return b64_str;
1.124 +}