32{
33 int ret;
34 unsigned len;
35 gnutls_session_t session;
36 gnutls_certificate_credentials_t cred;
37 gnutls_x509_privkey_t pkey;
38 gnutls_x509_crt_t cert, *certs;
39 gnutls_datum_t pkey_data, cert_data;
40 unsigned char serial[] = {0x99, 0x99, 0x99, 0x99};
41
42
43
44 chk (gnutls_certificate_allocate_credentials (&cred));
45
46 chk (gnutls_x509_privkey_init (&pkey));
47 chk (gnutls_x509_privkey_generate (pkey, GNUTLS_PK_RSA, 2048, 0));
48
49 chk (gnutls_x509_crt_init (&cert));
50 chk (gnutls_x509_crt_set_key (cert, pkey));
51
52 chk (gnutls_x509_crt_set_version (cert, 3));
53 chk (gnutls_x509_crt_set_serial (cert, serial,
sizeof (serial)));
54 chk (gnutls_x509_crt_set_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME, 0,
55 "Eg", strlen ("Eg")));
56 chk (gnutls_x509_crt_set_issuer_dn (cert,
"CN=Self-Signed Certificate", 0));
57
58 chk (gnutls_x509_crt_set_activation_time (cert, time (NULL)));
59 chk (gnutls_x509_crt_set_expiration_time (cert,
60 time (NULL) + 365 * 24 * 60 * 60));
61
62 chk (gnutls_x509_crt_sign2 (cert, cert, pkey, GNUTLS_DIG_SHA256, 0));
63
64 chk (gnutls_x509_privkey_export2 (pkey, GNUTLS_X509_FMT_PEM, &pkey_data));
65 chk (gnutls_x509_crt_export2 (cert, GNUTLS_X509_FMT_PEM, &cert_data));
66
67 chk (gnutls_certificate_set_x509_key_mem (cred, &cert_data, &pkey_data,
68 GNUTLS_X509_FMT_PEM));
69
70
71
73 assert_that (ret, is_equal_to (0));
74
75
76
77 chk (gnutls_certificate_get_x509_crt (cred, 0, &certs, &len));
78 assert_that (len, is_equal_to (1));
79 assert_that (gnutls_x509_crt_equals (cert, certs[0]), is_true);
80
81
82
83 gnutls_free (pkey_data.data);
84 gnutls_free (cert_data.data);
85 gnutls_x509_crt_deinit (cert);
86 for (unsigned i = 0; i < len; i++)
87 gnutls_x509_crt_deinit (certs[i]);
88 gnutls_deinit (session);
89 gnutls_free (certs);
90 gnutls_x509_privkey_deinit (pkey);
91 gnutls_certificate_free_credentials (cred);
92}
static int server_new_gnutls_set(unsigned int end_type, const char *priority, gnutls_session_t *server_session, gnutls_certificate_credentials_t *server_credentials)
Set the server credentials.