x509spki.c revision 296341
1139804Simp/* x509spki.c */
21541Srgrimes/*
31541Srgrimes * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
41541Srgrimes * 1999.
51541Srgrimes */
61541Srgrimes/* ====================================================================
71541Srgrimes * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes *
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes *
161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171541Srgrimes *    notice, this list of conditions and the following disclaimer in
181541Srgrimes *    the documentation and/or other materials provided with the
191541Srgrimes *    distribution.
201541Srgrimes *
211541Srgrimes * 3. All advertising materials mentioning features or use of this
221541Srgrimes *    software must display the following acknowledgment:
231541Srgrimes *    "This product includes software developed by the OpenSSL Project
241541Srgrimes *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
251541Srgrimes *
261541Srgrimes * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
271541Srgrimes *    endorse or promote products derived from this software without
281541Srgrimes *    prior written permission. For written permission, please contact
291541Srgrimes *    licensing@OpenSSL.org.
301541Srgrimes *
311541Srgrimes * 5. Products derived from this software may not be called "OpenSSL"
321541Srgrimes *    nor may "OpenSSL" appear in their names without prior written
331541Srgrimes *    permission of the OpenSSL Project.
341541Srgrimes *
351541Srgrimes * 6. Redistributions of any form whatsoever must retain the following
361541Srgrimes *    acknowledgment:
37116182Sobrien *    "This product includes software developed by the OpenSSL Project
38116182Sobrien *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39116182Sobrien *
40223785Sjonathan * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4131778Seivind * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42152272Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43176471Sdes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44224987Sjonathan * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4582188Sache * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
461541Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
471541Srgrimes * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48138361Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49223785Sjonathan * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5012577Sbde * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51174167Srwatson * OF THE POSSIBILITY OF SUCH DAMAGE.
52138361Sphk * ====================================================================
53138361Sphk *
541541Srgrimes * This product includes cryptographic software written by Eric Young
55138361Sphk * (eay@cryptsoft.com).  This product includes software written by Tim
56132157Scsjp * Hudson (tjh@cryptsoft.com).
571541Srgrimes *
58252163Sjhb */
59114216Skan
60138361Sphk#include <stdio.h>
61105253Sjhb#include "cryptlib.h"
62236698Sjhb#include <openssl/x509.h>
63138361Sphk
64152948Sdavidxuint NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
65105253Sjhb{
66138361Sphk    if ((x == NULL) || (x->spkac == NULL))
67221807Sstas        return (0);
68221807Sstas    return (X509_PUBKEY_set(&(x->spkac->pubkey), pkey));
69164033Srwatson}
701541Srgrimes
71224987SjonathanEVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x)
72174167Srwatson{
73220400Strasz    if ((x == NULL) || (x->spkac == NULL))
74138361Sphk        return (NULL);
75250376Strociny    return (X509_PUBKEY_get(x->spkac->pubkey));
76138361Sphk}
77138361Sphk
781541Srgrimes/* Load a Netscape SPKI from a base64 encoded string */
79138361Sphk
80138361SphkNETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, int len)
81138361Sphk{
82138361Sphk    unsigned char *spki_der;
83181905Sed    const unsigned char *p;
841541Srgrimes    int spki_len;
85221807Sstas    NETSCAPE_SPKI *spki;
86221807Sstas    if (len <= 0)
87174167Srwatson        len = strlen(str);
88138361Sphk    if (!(spki_der = OPENSSL_malloc(len + 1))) {
89176471Sdes        X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, ERR_R_MALLOC_FAILURE);
90176471Sdes        return NULL;
91176471Sdes    }
921541Srgrimes    spki_len = EVP_DecodeBlock(spki_der, (const unsigned char *)str, len);
93218757Sbz    if (spki_len < 0) {
94218757Sbz        X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, X509_R_BASE64_DECODE_ERROR);
95221807Sstas        OPENSSL_free(spki_der);
96221807Sstas        return NULL;
97221807Sstas    }
98155362Swsalamon    p = spki_der;
99155362Swsalamon    spki = d2i_NETSCAPE_SPKI(NULL, &p, spki_len);
10092751Sjeff    OPENSSL_free(spki_der);
101223785Sjonathan    return spki;
10212645Sbde}
103152272Srwatson
104152272Srwatson/* Generate a base64 encoded string from an SPKI */
105151897Srwatson
106151897Srwatsonchar *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki)
107115702Stegge{
10841086Struckman    unsigned char *der_spki, *p;
10930309Sphk    char *b64_str;
110234659Spho    int der_len;
111234659Spho    der_len = i2d_NETSCAPE_SPKI(spki, NULL);
112114293Smarkm    der_spki = OPENSSL_malloc(der_len);
11392654Sjeff    b64_str = OPENSSL_malloc(der_len * 2);
114252163Sjhb    if (!der_spki || !b64_str) {
11512675Sjulian        X509err(X509_F_NETSCAPE_SPKI_B64_ENCODE, ERR_R_MALLOC_FAILURE);
116179385Sed        return NULL;
117179385Sed    }
118179385Sed    p = der_spki;
119239856Skib    i2d_NETSCAPE_SPKI(spki, &p);
120102912Sjhb    EVP_EncodeBlock((unsigned char *)b64_str, der_spki, der_len);
121179385Sed    OPENSSL_free(der_spki);
122102912Sjhb    return b64_str;
123124548Sdes}
124124548Sdes