Skip to content

Instantly share code, notes, and snippets.

@babelouest
Last active September 15, 2019 00:40
Show Gist options
  • Save babelouest/b239099b209176376e7bf2640429ba87 to your computer and use it in GitHub Desktop.
Save babelouest/b239099b209176376e7bf2640429ba87 to your computer and use it in GitHub Desktop.
/**
*
* gnutls_generate_pkcs12.c
*
* Generate a new RSA key
* Generate the associated certificate
* Sign the certificate with the issuer key and certificate
* Create the PKCS#12 structure and exports it to the file out.p12 protected with the password PASSWORD
*
* License: MIT
*
*/
#include <stdio.h>
#include <string.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/abstract.h>
#include <gnutls/pkcs12.h>
#define EXPIRATION 60*60*24*512
#define DN "cn=localhost,dc=glewlwyd,dc=babelouest,dc=org"
#define PASSWORD "password"
#define FRIENDLY_NAME "My name"
#define OUTFILE "out.p12"
unsigned char key_issuer_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n\
MIIJKAIBAAKCAgEArqDzGjUcgeuNrkZo3QbiXxMUDxUwCC9Z9B7KLRMF+Js2HtIz\n\
t5kCnLDZBysM20Knmkw3w1sXG1L0QFW0h2Mo6YbRDp0lmGSvJjIZEuQEwLNdEufd\n\
QrTKAlfUcq3wp9J90rR3smaQ9TjDPBLmCOoijYEHi0ZxA/fbt/119+rLf0as5RPP\n\
UiS57LjJZFk7MXuzRPitkZ5hilKiE10iLv3X61XJ0fEUMnXRv7BkoCH6HrYDYLOT\n\
9FJ3knOo5rkSO1fzyWsD9/KCnnpzPzHm3+WDlAlHmshw3fBReLR9/PCfYVadb2yy\n\
+b+C8BNQLlWPSAIUQFM8K03VaU8U62gpo5YZEjhOjHSXloX2+2mAyCZ3w0p0qwxn\n\
oqOrW67x/4DjPQngnvAgn+NWrlfrg8WC7zCRERJlzXR5bUTgpVC1RHlSW1vrlAk/\n\
1xys7pHDYOVgexmxkTOcONSCsMK8aO56lIrNzyh6Mr0xgnb+Oze85r5b0iGbiYO/\n\
MmuUo4VjtbJAvizZZESrx40RAOsli0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hHC+/wxa7X6HSi6mZIrSTxNyF3l8cPcIKNEOEFPM\n\
+HLaHFwXZIJca3fZPFN7CQPyPEkCvaHwq7TNLqNMOgInckFTv6B4HPOQQLFvS3nz\n\
u/dH3pXI+5f7hFbmfPhIK7NSvENwdzOGKauJVWp9CvR88UYSEK63jQTyidP0PoTL\n\
7rXC5YXQtYlAoaJQ7rrfZsH2NqF3K5W15QaU612eUOPchUH0O0+hlpyqNi09drW7\n\
iU9rp1ZT/MlpR1JbmsSmyNL9MTvqAtetLZ3KTGR07ebO3SuRrFb54Aale0ZYpFfk\n\
1I2PdxJuts6DSEeqACqRH2ZgZAk1AwN3qtoQBNQhJgJRNdwbghHqXtmaIYoyVUgm\n\
DKEClYu92MZfJf6tv4jgbTUcClLyXz7WpaboF81XKbihfG53A6kHGm7CUt228IkH\n\
wcTsiqp52gaMGVmSjEG6m8WYOG8Rc2olmAOTEulhmqIVedpbAK/hJVlVC+vfK1oJ\n\
7GsnMfZ2lfkbHugkd2g5rjLQyAcMC0DNT76xHL8YWWDI\n\
-----END CERTIFICATE-----";
gnutls_x509_privkey_t privkey_x509 = NULL;
gnutls_x509_crt_t crt = NULL;
gnutls_privkey_t privkey = NULL;
int generate_pkcs12() {
gnutls_pkcs12_t pkcs12;
int res, bag_index;
gnutls_pkcs12_bag_t bag, key_bag;
char pkcs12_struct[10 * 1024];
unsigned char key_id_dat[1024];
size_t pkcs12_struct_size, key_id_dat_size = 1024;
FILE *fd;
gnutls_datum_t key_id = {NULL, 0}, cert_dat = {NULL, 0}, key_dat = {NULL, 0};
do {
if ((res = gnutls_x509_privkey_get_key_id(privkey_x509, 0, key_id_dat, &key_id_dat_size)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_get_key_id: %d\n", res);
break;
}
key_id.data = key_id_dat;
key_id.size = key_id_dat_size;
if ((res = gnutls_pkcs12_bag_init(&bag)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_init: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_init(&key_bag)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_init: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_export2(crt, GNUTLS_X509_FMT_DER, &cert_dat)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_export2: %d\n", res);
break;
}
if ((res = gnutls_x509_privkey_export2_pkcs8(privkey_x509, GNUTLS_X509_FMT_DER, PASSWORD, GNUTLS_PKCS_USE_PBES2_AES_128, &key_dat)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_export2_pkcs8: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_set_data(bag, GNUTLS_BAG_CERTIFICATE, &cert_dat)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_data: %d\n", res);
break;
}
bag_index = res;
if ((res = gnutls_pkcs12_bag_set_friendly_name(bag, bag_index, FRIENDLY_NAME)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_friendly_name: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_set_key_id(bag, bag_index, &key_id)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_key_id: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_encrypt(bag, PASSWORD, GNUTLS_PKCS_USE_PKCS12_RC2_40)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_encrypt: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_set_data(key_bag, GNUTLS_BAG_PKCS8_ENCRYPTED_KEY, &key_dat)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_data: %d\n", res);
break;
}
bag_index = res;
if ((res = gnutls_pkcs12_bag_set_friendly_name(key_bag, bag_index, FRIENDLY_NAME)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_friendly_name: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_bag_set_key_id(key_bag, bag_index, &key_id)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_bag_set_key_id: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_init(&pkcs12)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_init: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_set_bag(pkcs12, bag)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_set_bag: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_set_bag(pkcs12, key_bag)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_set_bag: %d\n", res);
break;
}
if ((res = gnutls_pkcs12_generate_mac(pkcs12, PASSWORD)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_generate_mac: %d\n", res);
break;
}
pkcs12_struct_size = sizeof(pkcs12_struct);
if ((res = gnutls_pkcs12_export(pkcs12, GNUTLS_X509_FMT_PEM, pkcs12_struct, &pkcs12_struct_size)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_export: %d\n", res);
break;
} else {
printf("pkcs12:\n%.*s\n", (int)pkcs12_struct_size, pkcs12_struct);
}
pkcs12_struct_size = sizeof(pkcs12_struct);
if ((res = gnutls_pkcs12_export(pkcs12, GNUTLS_X509_FMT_DER, pkcs12_struct, &pkcs12_struct_size)) < 0) {
fprintf(stderr, "error gnutls_pkcs12_export: %d\n", res);
break;
}
fd = fopen(OUTFILE, "w");
if (fd == NULL) {
fprintf(stderr, "cannot open file %s\n", OUTFILE);
}
fwrite(pkcs12_struct, 1, pkcs12_struct_size, fd);
fclose(fd);
} while (0);
gnutls_pkcs12_bag_deinit(bag);
gnutls_pkcs12_bag_deinit(key_bag);
gnutls_pkcs12_deinit(pkcs12);
gnutls_free(cert_dat.data);
gnutls_free(key_dat.data);
return 0;
}
void generate_key_cert() {
gnutls_x509_privkey_t key_issuer = NULL;
gnutls_x509_crt_t crt_issuer = NULL;
gnutls_datum_t dat;
time_t now, exp;
const char * err = NULL;
char crt_export[1024*16] = {0};
size_t crt_export_len = 1024*16;
int res, serial = 42;
time(&now);
exp = now + EXPIRATION;
do {
if ((res = gnutls_x509_privkey_init(&key_issuer)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_init: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_init(&crt_issuer)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_init: %d\n", res);
break;
}
dat.data = key_issuer_pem;
dat.size = strlen((const char *)key_issuer_pem);
if ((res = gnutls_x509_privkey_import(key_issuer, &dat, GNUTLS_X509_FMT_PEM)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_import: %d\n", res);
break;
}
dat.data = cert_issuer_pem;
dat.size = strlen((const char *)cert_issuer_pem);
if ((res = gnutls_x509_crt_import(crt_issuer, &dat, GNUTLS_X509_FMT_PEM)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_import: %d\n", res);
break;
}
if ((res = gnutls_x509_privkey_generate(privkey_x509, GNUTLS_PK_RSA, gnutls_sec_param_to_pk_bits(GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_MEDIUM), GNUTLS_PRIVKEY_FLAG_PROVABLE)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_generate: %d\n", res);
break;
}
if ((res = gnutls_privkey_import_x509(privkey, privkey_x509, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE)) < 0) {
fprintf(stderr, "error gnutls_privkey_import_x509: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_set_key(crt, privkey_x509)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_set_key: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_set_expiration_time(crt, exp)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_set_expiration_time: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_set_activation_time(crt, now)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_set_activation_time: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_set_serial(crt, &serial, sizeof(int))) < 0) {
fprintf(stderr, "error gnutls_x509_crt_set_serial: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_set_dn(crt, DN, &err)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_set_dn: %d\n", res);
break;
}
if ((res = gnutls_x509_crt_sign2(crt, crt_issuer, key_issuer, GNUTLS_DIG_SHA256, 0)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_sign2: %d\n", res);
break;
}
if ((res = gnutls_x509_privkey_export(privkey_x509, GNUTLS_X509_FMT_PEM, crt_export, &crt_export_len)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_export: %d\n", res);
break;
}
printf("privkey:\n%.*s\n", (int)crt_export_len, crt_export);
if ((res = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, crt_export, &crt_export_len)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_export: %d\n", res);
break;
}
printf("cert:\n%.*s\n\n", (int)crt_export_len, crt_export);
} while (0);
if (key_issuer)
gnutls_x509_privkey_deinit(key_issuer);
if (crt_issuer)
gnutls_x509_crt_deinit(crt_issuer);
}
int main() {
int res;
if ((res = gnutls_x509_privkey_init(&privkey_x509)) < 0) {
fprintf(stderr, "error gnutls_x509_privkey_init: %d\n", res);
return 1;
}
if ((res = gnutls_privkey_init(&privkey)) < 0) {
fprintf(stderr, "error gnutls_privkey_init: %d\n", res);
return 1;
}
if ((res = gnutls_x509_crt_init(&crt)) < 0) {
fprintf(stderr, "error gnutls_x509_crt_init: %d\n", res);
return 1;
}
generate_key_cert();
generate_pkcs12();
if (crt)
gnutls_x509_crt_deinit(crt);
if (privkey)
gnutls_privkey_deinit(privkey);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment