|
Packit Service |
4684c1 |
/* This example code is placed in the public domain. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/x509.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/abstract.h>
|
|
Packit Service |
4684c1 |
#include <time.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This example will generate a private key and a certificate
|
|
Packit Service |
4684c1 |
* request.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int main(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_t crq;
|
|
Packit Service |
4684c1 |
gnutls_x509_privkey_t key;
|
|
Packit Service |
4684c1 |
unsigned char buffer[10 * 1024];
|
|
Packit Service |
4684c1 |
size_t buffer_size = sizeof(buffer);
|
|
Packit Service |
4684c1 |
unsigned int bits;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_init();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Initialize an empty certificate request, and
|
|
Packit Service |
4684c1 |
* an empty private key.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_init(&crq;;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_x509_privkey_init(&key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Generate an RSA key of moderate security.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
bits =
|
|
Packit Service |
4684c1 |
gnutls_sec_param_to_pk_bits(GNUTLS_PK_RSA,
|
|
Packit Service |
4684c1 |
GNUTLS_SEC_PARAM_MEDIUM);
|
|
Packit Service |
4684c1 |
gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, bits, 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Add stuff to the distinguished name
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_set_dn_by_oid(crq, GNUTLS_OID_X520_COUNTRY_NAME,
|
|
Packit Service |
4684c1 |
0, "GR", 2);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_set_dn_by_oid(crq, GNUTLS_OID_X520_COMMON_NAME,
|
|
Packit Service |
4684c1 |
0, "Nikos", strlen("Nikos"));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Set the request version.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_set_version(crq, 1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Set a challenge password.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_set_challenge_password(crq,
|
|
Packit Service |
4684c1 |
"something to remember here");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Associate the request with the private key
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_set_key(crq, key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Self sign the certificate request.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_sign2(crq, key, GNUTLS_DIG_SHA1, 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Export the PEM encoded certificate request, and
|
|
Packit Service |
4684c1 |
* display it.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_export(crq, GNUTLS_X509_FMT_PEM, buffer,
|
|
Packit Service |
4684c1 |
&buffer_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
printf("Certificate Request: \n%s", buffer);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Export the PEM encoded private key, and
|
|
Packit Service |
4684c1 |
* display it.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
buffer_size = sizeof(buffer);
|
|
Packit Service |
4684c1 |
gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, buffer,
|
|
Packit Service |
4684c1 |
&buffer_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
printf("\n\nPrivate key: \n%s", buffer);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_x509_crq_deinit(crq);
|
|
Packit Service |
4684c1 |
gnutls_x509_privkey_deinit(key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
}
|