From nicolas at babelouest.org Wed Sep 4 04:24:54 2019 From: nicolas at babelouest.org (Nicolas Mora) Date: Tue, 3 Sep 2019 22:24:54 -0400 Subject: [gnutls-help] How to generate a private key and sign the certificate using GnuTLS API? Message-ID: <32ab6ca5-96f1-7754-19a1-d7cbfc0b99c0@babelouest.org> Hello, I'm trying to generate a private RSA key, extract a certificate and sign the certificate using the GnuTLS C API. Basically, I try to reproduce the following certtool commands using the API only: certtool --generate-privkey --outfile client.key --bits=4096 certtool --generate-request --load-privkey client.key --outfile client.csr certtool --generate-certificate --load-request client.csr --load-ca-certificate root.crt --load-ca-privkey root.key --outfile client.crt The files root.key and root.crt already exist and contain the private key and self-signed certificate of the CA. I test with the code below but I got the following error message: error gnutls_x509_crt_sign2: -43 which is GNUTLS_E_CERTIFICATE_ERROR What did I do wrong with my certificate? Thanks in advance /Nicolas #include #include #include #include #include #define EXPIRATION 60*60*24*512 #define DN "cn=localhost,dc=glewlwyd,dc=babelouest,dc=org" unsigned char key_issuer_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n\ MIIJKAIBAAKCAgEArqDzGjUcgeuNrkZo3QbiXxMUDxUwCC9Z9B7KLRMF+Js2HtIz\n\ [...]\ sBlw0Z5kMXY3BEBA5FjawULzcCt9TEBcxGe9rRF4ZxLGjsm8d8tS3jERRLQ=\n\ -----END RSA PRIVATE KEY-----"; unsigned char cert_issuer_pem[] = "-----BEGIN CERTIFICATE-----\n\ MIIFjTCCA3WgAwIBAgIUJNuB/YIWqA+rQEbdzhI+rYw/iv4wDQYJKoZIhvcNAQEM\n\ [...]\ 7GsnMfZ2lfkbHugkd2g5rjLQyAcMC0DNT76xHL8YWWDI\n\ -----END CERTIFICATE-----"; int main() { gnutls_x509_privkey_t privkey_x509 = NULL, key_issuer = NULL; gnutls_privkey_t privkey = NULL; gnutls_x509_crt_t crt = NULL, 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; time(&now); exp = now + EXPIRATION; gnutls_global_init(); do { if ((res = gnutls_x509_privkey_init(&privkey_x509)) < 0) { fprintf(stderr, "error gnutls_x509_privkey_init: %d\n", res); break; } if ((res = gnutls_privkey_init(&privkey)) < 0) { fprintf(stderr, "error gnutls_privkey_init: %d\n", res); break; } if ((res = gnutls_x509_crt_init(&crt)) < 0) { fprintf(stderr, "error gnutls_x509_crt_init: %d\n", res); break; } 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_REPRODUCIBLE)) < 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_private_key_usage_period(crt, now, exp)) < 0) { fprintf(stderr, "error gnutls_x509_crt_set_private_key_usage_period: %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 (privkey) gnutls_privkey_deinit(privkey); if (crt) gnutls_x509_crt_deinit(crt); if (key_issuer) gnutls_x509_privkey_deinit(key_issuer); if (crt_issuer) gnutls_x509_crt_deinit(crt_issuer); gnutls_global_deinit(); return 0; } From elektroniker at elude.in Wed Sep 4 17:49:47 2019 From: elektroniker at elude.in (elektroniker at elude.in) Date: Wed, 4 Sep 2019 15:49:47 -0000 Subject: [gnutls-help] rsa_sec_decrypt resides in libhogweed but the gnutls looks for it in libnettle Message-ID: I have compiled nettle-3.5.1 on OS X 10.7.4 The configure options were: --prefix=$NETTLE --with-include-path=$SSL/include:$GMP/include --with-lib-path=$SSL/lib:$GMP/lib where NETTLE=/usr/local/nettle-3.5.1 SSL=/usr/local/openssl-1.1.1c GMP=/usr/local/gmp-6.1.2. With this configuration, the nettle got built perfectly fine. When I use this nettle for the configuration of gnutls-3.6.8 (or .9) with these options: --prefix=/usr/local/gnutls-3.6.8 --with-included-libtasn1 --without-p11-kit --with-included-unistring it says checking for nettle_rsa_sec_decrypt... no configure: error: Nettle lacks the required rsa_sec_decrypt function When I inspect: nm /usr/local/nettle-3.5.1/lib/libnettle.7.0.dylib | grep rsa returns nothing but nm /usr/local/nettle-3.5.1/lib/libhogweed.5.0.dylib | grep rsa_sec_decrypt returns: 0000000000005fd0 T _nettle_rsa_sec_decrypt It looks like rsa_sec_decrypt resides in libhogweed but the gnutls looks for it in libnettle. How to fix this problem? Thank you. From ametzler at bebt.de Sat Sep 14 15:19:25 2019 From: ametzler at bebt.de (Andreas Metzler) Date: Sat, 14 Sep 2019 15:19:25 +0200 Subject: [gnutls-help] rsa_sec_decrypt resides in libhogweed but the gnutls looks for it in libnettle In-Reply-To: References: Message-ID: <20190914131925.GC1438@argenau.bebt.de> On 2019-09-04 elektroniker at elude.in wrote: > I have compiled nettle-3.5.1 on OS X 10.7.4 The configure options were: [...] > NETTLE=/usr/local/nettle-3.5.1 > SSL=/usr/local/openssl-1.1.1c > GMP=/usr/local/gmp-6.1.2. > With this configuration, the nettle got built perfectly fine. When I use > this nettle for the configuration of gnutls-3.6.8 (or .9) with these > options: [...] > checking for nettle_rsa_sec_decrypt... no > configure: error: Nettle lacks the required rsa_sec_decrypt function > When I inspect: > nm /usr/local/nettle-3.5.1/lib/libnettle.7.0.dylib | grep rsa > returns nothing but > nm /usr/local/nettle-3.5.1/lib/libhogweed.5.0.dylib | grep rsa_sec_decrypt > returns: > 0000000000005fd0 T _nettle_rsa_sec_decrypt > It looks like rsa_sec_decrypt resides in libhogweed but the gnutls looks > for it in libnettle. [...] Hello, That diagnosis is not correct. The software library package?"Nettle" consist of libhogweed and libnettle and GnuTLS searches for nettle_rsa_sec_decrypt in both -lnettle and -lhogweed: configure.ac line 552 ff: # We MUST require a Nettle version that has rsa_sec_decrypt now. save_LIBS=$LIBS LIBS="$LIBS $HOGWEED_LIBS $NETTLE_LIBS" AC_CHECK_FUNCS(nettle_rsa_sec_decrypt, [], [AC_MSG_ERROR([Nettle lacks the required rsa_sec_decrypt function])] ) LIBS=$save_LIBS Look into config.log for the actual test run and resulting error. cu Andreas -- `What a good friend you are to him, Dr. Maturin. His other friends are so grateful to you.' `I sew his ears on from time to time, sure' From nmav at gnutls.org Tue Sep 17 15:43:34 2019 From: nmav at gnutls.org (Nikos Mavrogiannopoulos) Date: Tue, 17 Sep 2019 15:43:34 +0200 Subject: [gnutls-help] How to generate a private key and sign the certificate using GnuTLS API? In-Reply-To: <32ab6ca5-96f1-7754-19a1-d7cbfc0b99c0@babelouest.org> References: <32ab6ca5-96f1-7754-19a1-d7cbfc0b99c0@babelouest.org> Message-ID: On Wed, Sep 4, 2019 at 4:43 AM Nicolas Mora wrote: > > Hello, > > I'm trying to generate a private RSA key, extract a certificate and sign > the certificate using the GnuTLS C API. > > Basically, I try to reproduce the following certtool commands using the > API only: > certtool --generate-privkey --outfile client.key --bits=4096 > certtool --generate-request --load-privkey client.key --outfile client.csr > certtool --generate-certificate --load-request client.csr > --load-ca-certificate root.crt --load-ca-privkey root.key --outfile > client.crt > > The files root.key and root.crt already exist and contain the private > key and self-signed certificate of the CA. > > I test with the code below but I got the following error message: > error gnutls_x509_crt_sign2: -43 > which is GNUTLS_E_CERTIFICATE_ERROR > > What did I do wrong with my certificate? Most likely you have already figured the issue, but in general a way to debug such applications is by setting the GNUTLS_DEBUG_LEVEL variable to a number from 3 to 9. It will provide more output which can help figure the issue. regards, Nikos From nmav at gnutls.org Tue Sep 17 15:45:45 2019 From: nmav at gnutls.org (Nikos Mavrogiannopoulos) Date: Tue, 17 Sep 2019 15:45:45 +0200 Subject: [gnutls-help] dh parameters In-Reply-To: <864817614.20190827223949@sloop.net> References: <864817614.20190827223949@sloop.net> Message-ID: On Wed, Aug 28, 2019 at 7:56 AM Gregory Sloop wrote: > > For some applications - say OpenVPN servers - I need a dh.pem [dh parameters file] > It looks like GNUTLS doesn't have the option to generate dh params like OpenSSL does, but has the following as a option: > certtool --get-dh-params --outfile dh.pem --sec-param ultra > > 1) Will that ^^ do what I want? Yes it will do what you want. Though you can always generate DH parameters with --generate-dh-params. There is no reason to however, more background in: https://www.gnutls.org/manual/html_node/Parameter-generation.html regards, Nikos From nicolas at babelouest.org Wed Sep 18 02:24:42 2019 From: nicolas at babelouest.org (Nicolas Mora) Date: Tue, 17 Sep 2019 20:24:42 -0400 Subject: [gnutls-help] How to generate a private key and sign the certificate using GnuTLS API? In-Reply-To: References: <32ab6ca5-96f1-7754-19a1-d7cbfc0b99c0@babelouest.org> Message-ID: <21e53a9e-d486-8254-884e-eb6e6f4f6671@babelouest.org> Hello, Le 19-09-17 ? 09 h 43, Nikos Mavrogiannopoulos a ?crit?: > > Most likely you have already figured the issue, but in general a way > to debug such applications is by setting the GNUTLS_DEBUG_LEVEL > variable to a number from 3 to 9. It will provide more output which > can help figure the issue. > Yes, I've figured out what was missing in my code. Basically the certificate was incomplete, at least I didn't set the serial number. I made a gist to help other people and my future self with this problem: https://gist.github.com/babelouest/b239099b209176376e7bf2640429ba87 Thanks for the debug level tip, I'll remember to use it next time! /Nicolas From nmav at gnutls.org Sun Sep 29 13:09:50 2019 From: nmav at gnutls.org (Nikos Mavrogiannopoulos) Date: Sun, 29 Sep 2019 13:09:50 +0200 Subject: [gnutls-help] gnutls 3.6.10 Message-ID: Hello, I've just released gnutls 3.6.10. This is a bug fix release on the stable 3.6.x branch. I'd like to thank everyone who contributed in this release: Daiki Ueno, Dmitry Eremin-Solenikov, Ludovic Court?s, Tom Vrancken, Andreas Metzler, Karsten Ohme, Michael Catanzaro and Tim R?hsen. The detailed list of changes follows; they can be seen in more detail in our milestone tracker: https://gitlab.com/gnutls/gnutls/-/milestones/24 Changes ======= * Version 3.6.10 (released 2019-09-29) ** libgnutls: Added support for deterministic ECDSA/DSA (RFC6979) Deterministic signing can be enabled by setting GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE when calling gnutls_privkey_sign_*() functions (#94). ** libgnutls: add gnutls_aead_cipher_encryptv2 and gnutls_aead_cipher_decryptv2 functions that will perform in-place encryption/decryption on data buffers (#718). ** libgnutls: Corrected issue in gnutls_session_get_data2() which could fail under TLS1.3, if a timeout callback was not set using gnutls_transport_set_pull_timeout_function() (#823). ** libgnutls: added interoperability tests with gnutls 2.12.x; addressed issue with large record handling due to random padding (#811). ** libgnutls: the server now selects the highest TLS protocol version, if TLS 1.3 is enabled and the client advertises an older protocol version first (#837). ** libgnutls: fix non-PIC assembly on i386 (#818). ** libgnutls: added support for GOST 28147-89 cipher in CNT (GOST counter) mode and MAC generation based on GOST 28147-89 (IMIT). For description of the modes see RFC 5830. S-Box is id-tc26-gost-28147-param-Z (TC26Z) defined in RFC 7836. ** certtool: when outputting an encrypted private key do not insert the textual description of it. This fixes a regression since 3.6.5 (#840). ** API and ABI modifications: gnutls_aead_cipher_encryptv2: Added gnutls_aead_cipher_decryptv2: Added GNUTLS_CIPHER_GOST28147_TC26Z_CNT: Added GNUTLS_MAC_GOST28147_TC26Z_IMIT: Added Getting the Software ==================== GnuTLS may be downloaded directly from ;;;. A list of GnuTLS mirrors can be found at ;;;. Here are the XZ compressed sources: https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.10.tar.xz Here are OpenPGP detached signatures signed using key 0x96865171: https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.10.tar.xz.sig Note that it has been signed with my openpgp key: pub 3104R/96865171 2008-05-04 [expires: 2028-04-29] uid Nikos Mavrogiannopoulos gnutls.org> uid Nikos Mavrogiannopoulos gmail.com> sub 2048R/9013B842 2008-05-04 [expires: 2018-05-02] sub 2048R/1404A91D 2008-05-04 [expires: 2018-05-02] regards, Nikos