2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-04-22 21:38:44 +08:00
|
|
|
* Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1999-09-03 09:08:34 +08:00
|
|
|
*
|
2018-12-06 21:00:36 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* 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 09:08:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-09-03 09:08:34 +08:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
|
|
|
|
int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((x == NULL) || (x->spkac == NULL))
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
|
|
|
return X509_PUBKEY_set(&(x->spkac->pubkey), pkey);
|
1999-09-03 09:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((x == NULL) || (x->spkac == NULL))
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get(x->spkac->pubkey);
|
1999-09-03 09:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a Netscape SPKI from a base64 encoded string */
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, int len)
|
1999-09-03 09:08:34 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *spki_der;
|
|
|
|
const unsigned char *p;
|
|
|
|
int spki_len;
|
|
|
|
NETSCAPE_SPKI *spki;
|
|
|
|
if (len <= 0)
|
|
|
|
len = strlen(str);
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((spki_der = OPENSSL_malloc(len + 1)) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
spki_len = EVP_DecodeBlock(spki_der, (const unsigned char *)str, len);
|
|
|
|
if (spki_len < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509, X509_R_BASE64_DECODE_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
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 09:08:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a base64 encoded string from an SPKI */
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki)
|
1999-09-03 09:08:34 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *der_spki, *p;
|
|
|
|
char *b64_str;
|
|
|
|
int der_len;
|
2021-04-12 11:58:14 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
der_len = i2d_NETSCAPE_SPKI(spki, NULL);
|
2021-04-12 11:58:14 +08:00
|
|
|
if (der_len <= 0)
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
der_spki = OPENSSL_malloc(der_len);
|
|
|
|
b64_str = OPENSSL_malloc(der_len * 2);
|
2015-10-30 19:12:26 +08:00
|
|
|
if (der_spki == NULL || b64_str == NULL) {
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(der_spki);
|
|
|
|
OPENSSL_free(b64_str);
|
2015-01-22 11:40:55 +08: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 09:08:34 +08:00
|
|
|
}
|