From ae05bc8ea52f596f4ce55d75eb03496148d1d984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Tue, 19 Nov 2019 13:44:09 -0500 Subject: [PATCH] crypto: avoid goto --- src/crypto.cpp | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/crypto.cpp b/src/crypto.cpp index a05b59c3..5c6fe8eb 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -1037,32 +1037,24 @@ Certificate::getPreferredDigest() const std::pair Certificate::generateOcspRequest(gnutls_x509_crt_t& issuer) { - int ret; - gnutls_ocsp_req_t req; - gnutls_datum_t rdata, nonce; - ret = gnutls_ocsp_req_init(&req); - if (ret < 0) { - goto end; - } - ret = gnutls_ocsp_req_add_cert(req, GNUTLS_DIG_SHA512, issuer, cert); - if (ret < 0) { - goto end; - } - unsigned char noncebuf[64]; - nonce = { noncebuf, sizeof(noncebuf) }; - ret = gnutls_rnd(GNUTLS_RND_NONCE, nonce.data, nonce.size); - ret = gnutls_ocsp_req_set_nonce(req, 0, &nonce); - if (ret < 0) { - goto end; - } - ret = gnutls_ocsp_req_export(req, &rdata); - if (ret != 0) { - goto end; - } -end: - gnutls_ocsp_req_deinit(req); + gnutls_ocsp_req_t rreq; + int ret = gnutls_ocsp_req_init(&rreq); if (ret < 0) throw CryptoException(gnutls_strerror(ret)); + std::unique_ptr req(rreq, &gnutls_ocsp_req_deinit); + ret = gnutls_ocsp_req_add_cert(req.get(), GNUTLS_DIG_SHA512, issuer, cert); + if (ret < 0) + throw CryptoException(gnutls_strerror(ret)); + unsigned char noncebuf[64]; + gnutls_datum_t nonce = { noncebuf, sizeof(noncebuf) }; + ret = gnutls_rnd(GNUTLS_RND_NONCE, nonce.data, nonce.size); + ret = gnutls_ocsp_req_set_nonce(req.get(), 0, &nonce); + if (ret < 0) + throw CryptoException(gnutls_strerror(ret)); + gnutls_datum_t rdata; + ret = gnutls_ocsp_req_export(req.get(), &rdata); + if (ret != 0) + throw CryptoException(gnutls_strerror(ret)); return std::make_pair({rdata.data, rdata.data + rdata.size}, {nonce.data, nonce.data + nonce.size}); }