gnutls_certificate_get_x509_key - Linux


Overview

gnutls_certificate_get_x509_key retrieves the the X.509 certificate key,
either a public, private, or both, from a GnuTLS certificate.

Syntax

gnutls_certificate_get_x509_key(cert, type)

| Argument | Description | |
|——————-|———————————————————-|———————————-|
| cert | The GnuTLS certificate. | |
| type | The type of key to retrieve: GNUTLS_CRT_X509_KEY_PRIVATE | GNUTLS_CRT_X509_KEY_PUBLIC, or |
| | or GNUTLS_CRT_X509_KEY_BOTH. | GNUTLS_CRT_X509_KEY_ALL_TRUSTED |

Options/Flags

None.

Examples

The following code snippet demonstrates how to retrieve the private key from a GnuTLS certificate:

#include <gnutls/gnutls.h>

int main() {
  gnutls_certificate_t cert;
  gnutls_datum_t key;

  // Initialize the GnuTLS certificate.
  gnutls_certificate_init(&cert);

  // Load the certificate.
  gnutls_certificate_load_file(&cert, "certificate.pem", GNUTLS_X509_FMT_PEM);

  // Retrieve the private key.
  if (gnutls_certificate_get_x509_key(&cert, GNUTLS_CRT_X509_KEY_PRIVATE, &key) == 0) {
    // Use the private key.
  } else {
    // Handle the error.
  }

  // Clean up.
  gnutls_certificate_deinit(&cert);

  return 0;
}

Common Issues

None.

Integration

This command can be integrated with other Linux commands or tools to perform advanced tasks. For example, it can be used with the openssl command to convert a GnuTLS certificate to a PEM-formatted certificate.

gnutls-cli --print-cert -p 4433 www.example.com | gnutls_certificate_get_x509_key - | openssl x509 -inform DER -outform PEM -out certificate.pem

Related Commands

  • gnutls_certificate_init
  • gnutls_certificate_load_file
  • gnutls_certificate_deinit
  • openssl