#include "asn1/asn1c/Certificate.h"
#include <openssl/x509v3.h>
-#include <openssl/pem.h>
#include "extension.h"
#include "json_util.h"
pk2json(X509 const *x)
{
json_t *root;
- ASN1_OBJECT *xpoid;
- EVP_PKEY *pkey;
- BIO *bio;
+ json_t *child;
+ X509_PUBKEY *pubkey;
+ ASN1_OBJECT *oid;
root = json_obj_new();
if (root == NULL)
return NULL;
- /* algorithm */
- if (!X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, X509_get_X509_PUBKEY(x)))
- goto fail;
- bio = BIO_new(BIO_s_mem());
- if (bio == NULL)
- goto fail;
- if (i2a_ASN1_OBJECT(bio, xpoid) <= 0) {
- BIO_free_all(bio);
+ pubkey = X509_get_X509_PUBKEY(x);
+ if (pubkey == NULL)
goto fail;
- }
- if (json_object_add(root, "algorithm", bio2json(bio)))
+ if (!X509_PUBKEY_get0_param(&oid, NULL, NULL, NULL, pubkey))
goto fail;
- /* Actual pk */
- pkey = X509_get0_pubkey(x);
- if (pkey == NULL)
- goto fail;
- bio = BIO_new(BIO_s_mem());
- if (bio == NULL)
- goto fail;
- if (PEM_write_bio_PUBKEY(bio, pkey) <= 0) {
- BIO_free_all(bio);
+ child = oid2json(oid);
+ if (json_object_add(root, "algorithm", child))
goto fail;
- }
- if (json_object_add(root, "subjectPublicKey", bio2json(bio)))
+ child = pubkey2json(X509_PUBKEY_get0(pubkey));
+ if (json_object_add(root, "subjectPublicKey", child))
goto fail;
return root;
#include <stdlib.h>
#include <openssl/asn1.h>
#include <openssl/opensslv.h>
+#include <openssl/pem.h>
#include "alloc.h"
#include "extension.h"
#include "json_util.h"
+#include "asn1/asn1c/OBJECT_IDENTIFIER.h"
/* Swallows @bio. */
-char *
-bio2str(BIO *bio)
-{
- BUF_MEM *buffer;
- char *str;
-
- str = (BIO_get_mem_ptr(bio, &buffer) > 0)
- ? pstrndup(buffer->data, buffer->length)
- : NULL;
-
- BIO_free_all(bio);
- return str;
-}
-
-/* Swallows @bio. */
-json_t *
+static json_t *
bio2json(BIO *bio)
{
BUF_MEM *buffer;
json_t *
oid2json(ASN1_OBJECT const *oid)
{
- return oid ? json_str_new(OBJ_nid2sn(OBJ_obj2nid(oid))) : json_null();
+ char buf[OID_STR_MAXLEN];
+ return (oid != NULL)
+ ? json_strn_new(buf, OBJ_obj2txt(buf, OID_STR_MAXLEN, oid, 0))
+ : json_null();
}
json_t *
return NULL;
}
+json_t *
+pubkey2json(EVP_PKEY *pubkey)
+{
+ BIO *bio;
+
+ if (pubkey == NULL)
+ return NULL;
+
+ bio = BIO_new(BIO_s_mem());
+ if (bio == NULL)
+ return NULL;
+ if (PEM_write_bio_PUBKEY(bio, pubkey) <= 0) {
+ BIO_free_all(bio);
+ return NULL;
+ }
+
+ return bio2json(bio);
+}
+
static json_t *
ext2json_known(struct extension_metadata const *meta, X509_EXTENSION *ext)
{
json_t *parent;
json_t *child;
X509_EXTENSION *ex;
- BIO *bio;
- char *name;
int i;
if (sk_X509_EXTENSION_num(exts) <= 0)
ex = sk_X509_EXTENSION_value(exts, i);
- /* Get the extension name */
- bio = BIO_new(BIO_s_mem());
- if (bio == NULL)
- goto fail;
- if (i2a_ASN1_OBJECT(bio, X509_EXTENSION_get_object(ex)) <= 0) {
- BIO_free_all(bio);
- goto fail;
- }
-
- name = bio2str(bio);
- child = json_str_new(name);
- free(name);
-
+ child = oid2json(X509_EXTENSION_get_object(ex));
if (json_object_add(parent, "extnID", child))
goto fail;
child = json_boolean(X509_EXTENSION_get_critical(ex));
#include <openssl/bio.h>
#include <openssl/x509v3.h>
-char *bio2str(BIO *);
-json_t *bio2json(BIO *);
json_t *oid2json(ASN1_OBJECT const *);
json_t *asn1int2json(ASN1_INTEGER const *);
json_t *asn1str2json(ASN1_STRING const *); /* octet string, bit string, etc */
json_t *name2json(X509_NAME const *);
json_t *gn2json(GENERAL_NAME const *);
json_t *gns2json(GENERAL_NAMES const *);
+json_t *pubkey2json(EVP_PKEY *); /* LibreSSL needs not const */
json_t *exts2json(const STACK_OF(X509_EXTENSION) *);
#endif /* SRC_LIBCRYPTO_UTIL_H_ */