2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-09-03 01:08:34 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-09-03 01:08:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-09-03 01:08:34 +00:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
|
|
|
|
int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((x == NULL) || (x->spkac == NULL))
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
|
|
|
return X509_PUBKEY_set(&(x->spkac->pubkey), pkey);
|
1999-09-03 01:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((x == NULL) || (x->spkac == NULL))
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get(x->spkac->pubkey);
|
1999-09-03 01:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a Netscape SPKI from a base64 encoded string */
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, int len)
|
1999-09-03 01:08:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *spki_der;
|
|
|
|
const unsigned char *p;
|
|
|
|
int spki_len;
|
|
|
|
NETSCAPE_SPKI *spki;
|
|
|
|
if (len <= 0)
|
|
|
|
len = strlen(str);
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((spki_der = OPENSSL_malloc(len + 1)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
spki_len = EVP_DecodeBlock(spki_der, (const unsigned char *)str, len);
|
|
|
|
if (spki_len < 0) {
|
|
|
|
X509err(X509_F_NETSCAPE_SPKI_B64_DECODE, X509_R_BASE64_DECODE_ERROR);
|
|
|
|
OPENSSL_free(spki_der);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = spki_der;
|
|
|
|
spki = d2i_NETSCAPE_SPKI(NULL, &p, spki_len);
|
|
|
|
OPENSSL_free(spki_der);
|
|
|
|
return spki;
|
1999-09-03 01:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a base64 encoded string from an SPKI */
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki)
|
1999-09-03 01:08:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *der_spki, *p;
|
|
|
|
char *b64_str;
|
|
|
|
int der_len;
|
|
|
|
der_len = i2d_NETSCAPE_SPKI(spki, NULL);
|
|
|
|
der_spki = OPENSSL_malloc(der_len);
|
|
|
|
b64_str = OPENSSL_malloc(der_len * 2);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (der_spki == NULL || b64_str == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
X509err(X509_F_NETSCAPE_SPKI_B64_ENCODE, ERR_R_MALLOC_FAILURE);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(der_spki);
|
|
|
|
OPENSSL_free(b64_str);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = der_spki;
|
|
|
|
i2d_NETSCAPE_SPKI(spki, &p);
|
|
|
|
EVP_EncodeBlock((unsigned char *)b64_str, der_spki, der_len);
|
|
|
|
OPENSSL_free(der_spki);
|
|
|
|
return b64_str;
|
1999-09-03 01:08:34 +00:00
|
|
|
}
|