summaryrefslogtreecommitdiff
path: root/lib/x509/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/x509/common.c')
-rw-r--r--lib/x509/common.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/x509/common.c b/lib/x509/common.c
index d85d1b3898..3087183a58 100644
--- a/lib/x509/common.c
+++ b/lib/x509/common.c
@@ -1895,3 +1895,58 @@ int _gnutls_copy_data(gnutls_datum_t* str, uint8_t *out, size_t *out_size)
return 0;
}
+
+/* Converts an X.509 certificate to subjectPublicKeyInfo */
+int x509_crt_to_raw_pubkey(gnutls_x509_crt_t crt,
+ gnutls_datum_t * rpubkey)
+{
+ gnutls_pubkey_t pubkey = NULL;
+ int ret;
+
+ ret = gnutls_pubkey_init(&pubkey);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+
+ ret = gnutls_pubkey_import_x509(pubkey, crt, 0);
+ if (ret < 0) {
+ gnutls_assert();
+ goto cleanup;
+ }
+
+ ret =
+ gnutls_pubkey_export2(pubkey, GNUTLS_X509_FMT_DER, rpubkey);
+ if (ret < 0) {
+ gnutls_assert();
+ goto cleanup;
+ }
+
+ ret = 0;
+
+ cleanup:
+ gnutls_pubkey_deinit(pubkey);
+ return ret;
+}
+
+/* Converts an X.509 certificate to subjectPublicKeyInfo */
+int x509_raw_crt_to_raw_pubkey(const gnutls_datum_t * cert,
+ gnutls_datum_t * rpubkey)
+{
+ gnutls_x509_crt_t crt = NULL;
+ int ret;
+
+ ret = gnutls_x509_crt_init(&crt);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+
+ ret = gnutls_x509_crt_import(crt, cert, GNUTLS_X509_FMT_DER);
+ if (ret < 0) {
+ gnutls_assert();
+ goto cleanup;
+ }
+
+ ret = x509_crt_to_raw_pubkey(crt, rpubkey);
+ cleanup:
+ gnutls_x509_crt_deinit(crt);
+
+ return ret;
+}