From nobody Thu Sep 19 00:18:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1717771590208995.9022986038011; Fri, 7 Jun 2024 07:46:30 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 4B7F52545; Fri, 7 Jun 2024 10:46:28 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 6C1BE260D; Fri, 7 Jun 2024 10:43:05 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id BB0122602; Fri, 7 Jun 2024 10:42:58 -0400 (EDT) Received: from mx1.osci.io (polly.osci.io [8.43.85.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 769E02621 for ; Fri, 7 Jun 2024 10:40:25 -0400 (EDT) Received: by mx1.osci.io (Postfix, from userid 994) id 7C10A2236E; Fri, 7 Jun 2024 10:40:24 -0400 (EDT) Received: from mx3.osci.io (carla.osci.io [IPv6:2607:f0d0:1e02:35::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits)) (No client certificate requested) by mx1.osci.io (Postfix) with ESMTPS id E43BF2254F for ; Fri, 7 Jun 2024 10:40:15 -0400 (EDT) Received: by mx3.osci.io (Postfix, from userid 990) id 49FCF30721D5; Fri, 7 Jun 2024 09:32:55 -0500 (CDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by mx3.osci.io (Postfix) with ESMTPS id B575530721D1 for ; Fri, 7 Jun 2024 09:32:53 -0500 (CDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-77-bAnFEtftOGi0jEKfSHktDg-1; Fri, 07 Jun 2024 10:26:19 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 412A1811E79 for ; Fri, 7 Jun 2024 14:26:19 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id BEC25492BC6; Fri, 7 Jun 2024 14:26:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717770382; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=smT8ANyVuM9il1EIloSa7zTDcIMeh51A3YC7t8ummW4=; b=D+o/fhE063PeDI2Y3jzU/JexhjMv+TTkxa1gaQkwWLRsXI9MXLJz08fKSrxuRUvnQXCC2L LaK7RxnwDug6lm1CRHv09YrJR0Cy1dfk4ceI1EMlYAfKmFkQ1ziV6dPSV6DlafaIZgqSVP XsvVUlbeWib5VxPVaoTDMtIw+Gw6R9Q= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717770510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=smT8ANyVuM9il1EIloSa7zTDcIMeh51A3YC7t8ummW4=; b=VZbNaMg9f9tU3Cw21vBWsBlQIKJcpOHXS8YbPIVmReMevld1X4cZGpgx1Krc8D/9J6pFsF 4UATOApPMIgafwNKhRoefWzEf4kSN1mjgN0ERJqWzqpyXrUT8obgaI5u2/TBHyWnNoeFaB saodr/r8mMFFYPeGBCGtF9NDfaNj8mw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717770642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=smT8ANyVuM9il1EIloSa7zTDcIMeh51A3YC7t8ummW4=; b=f/hXTNUKcnFgk7mAbmhPTy5S1mcEScN/dgXKHu9QxYtk/mx5cHF8ebuWMmVPsfHcDE6IHd KP4SR3mJxmwO/Ib721/lLYwYM+3UzthwiwLowuTuI07ulQpbNRcdrLVWwNBT0smAG+QqsA L8xAfI4iAHEeCxMfZZ/AA040OXTQaBc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717770773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=smT8ANyVuM9il1EIloSa7zTDcIMeh51A3YC7t8ummW4=; b=KXZYHAEsfl/1AHuDoxmMimUCME/KzWEE5y7mNT+er8NXbduuDt7Pms5IqRQTQqj26P0B1L E7uW0VivCvOzPJ/jgbItdMzabWX/kCcDHsrMMFvHq8DF8nGlzGWwV27Mer1uPY2WetvVZs 1+YYs85ho6Bs2bNq+fCv1zL8/PKvk1E= X-MC-Unique: bAnFEtftOGi0jEKfSHktDg-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: devel@lists.libvirt.org Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH 2/9] rpc: refactor method for checking session certificates Date: Fri, 7 Jun 2024 15:26:09 +0100 Message-ID: <20240607142616.749339-3-berrange@redhat.com> In-Reply-To: <20240607142616.749339-1-berrange@redhat.com> References: <20240607142616.749339-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: IEWPFTFQR2VCN3XY3BW2Z2FT4GMACSWN X-Message-ID-Hash: IEWPFTFQR2VCN3XY3BW2Z2FT4GMACSWN X-MailFrom: SRS0=RZ0v=NJ=redhat.com=berrange@osci.io X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (found 4 invalid signatures) X-ZM-MESSAGEID: 1717771591104100001 Content-Type: text/plain; charset="utf-8" This will facilitate moving much of the code into a new file in the subsequent commit. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/rpc/virnettlscontext.c | 109 ++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c index 4223043bc9..a56abee009 100644 --- a/src/rpc/virnettlscontext.c +++ b/src/rpc/virnettlscontext.c @@ -876,6 +876,65 @@ virNetTLSContext *virNetTLSContextNewClient(const char= *cacert, sanityCheckCert, requireValidCert, false); } =20 +static int virNetTLSContextCertValidateCA(gnutls_x509_crt_t cert, + bool isServer) +{ + if (virNetTLSContextCheckCertTimes(cert, "[session]", isServer, true) = < 0) + return -1; + + return 0; +} + +static char *virNetTLSContextCertValidate(gnutls_x509_crt_t cert, + bool isServer, + const char *hostname, + const char *const *x509dnACL) +{ + size_t dnamesize =3D 256; + g_autofree char *dname =3D g_new0(char, dnamesize); + int ret; + + if (virNetTLSContextCheckCertTimes(cert, "[session]", + isServer, false) < 0) + return NULL; + + ret =3D gnutls_x509_crt_get_dn(cert, dname, &dnamesize); + if (ret =3D=3D GNUTLS_E_SHORT_MEMORY_BUFFER) { + VIR_DEBUG("Reallocating dname to fit %zu bytes", dnamesize); + dname =3D g_realloc(dname, dnamesize); + ret =3D gnutls_x509_crt_get_dn(cert, dname, &dnamesize); + } + if (ret !=3D 0) { + virReportError(VIR_ERR_SYSTEM_ERROR, + _("Failed to get certificate %1$s distinguished nam= e: %2$s"), + "[session]", gnutls_strerror(ret)); + return NULL; + } + + VIR_DEBUG("Peer DN is %s", dname); + + if (virNetTLSContextCheckCertDN(cert, "[session]", hostname, + dname, x509dnACL) < 0) + return NULL; + + /* !isServer, since on the client, we're validating the + * server's cert, and on the server, the client's cert + */ + if (virNetTLSContextCheckCertBasicConstraints(cert, "[session]", + !isServer, false) < 0) + return NULL; + + if (virNetTLSContextCheckCertKeyUsage(cert, "[session]", + false) < 0) + return NULL; + + /* !isServer - as above */ + if (virNetTLSContextCheckCertKeyPurpose(cert, "[session]", + !isServer) < 0) + return NULL; + + return g_steal_pointer(&dname); +} =20 static int virNetTLSContextValidCertificate(virNetTLSContext *ctxt, virNetTLSSession *sess) @@ -945,57 +1004,21 @@ static int virNetTLSContextValidCertificate(virNetTL= SContext *ctxt, goto authfail; } =20 - if (virNetTLSContextCheckCertTimes(cert, "[session]", - sess->isServer, i > 0) < 0) { - gnutls_x509_crt_deinit(cert); - goto authdeny; - } - if (i =3D=3D 0) { - ret =3D gnutls_x509_crt_get_dn(cert, dname, &dnamesize); - if (ret =3D=3D GNUTLS_E_SHORT_MEMORY_BUFFER) { - VIR_DEBUG("Reallocating dname to fit %zu bytes", dnamesize= ); - dname =3D g_realloc(dname, dnamesize); - dnameptr =3D dname; - ret =3D gnutls_x509_crt_get_dn(cert, dname, &dnamesize); - } - if (ret !=3D 0) { - virReportError(VIR_ERR_SYSTEM_ERROR, - _("Failed to get certificate %1$s distingui= shed name: %2$s"), - "[session]", gnutls_strerror(ret)); - goto authfail; - } - sess->x509dname =3D g_steal_pointer(&dname); - VIR_DEBUG("Peer DN is %s", dnameptr); - - if (virNetTLSContextCheckCertDN(cert, "[session]", sess->hostn= ame, - dnameptr, ctxt->x509dnACL) < 0= ) { - gnutls_x509_crt_deinit(cert); - goto authdeny; - } - - /* !sess->isServer, since on the client, we're validating the - * server's cert, and on the server, the client's cert - */ - if (virNetTLSContextCheckCertBasicConstraints(cert, "[session]= ", - !sess->isServer,= false) < 0) { - gnutls_x509_crt_deinit(cert); - goto authdeny; - } - - if (virNetTLSContextCheckCertKeyUsage(cert, "[session]", - false) < 0) { + if (!(sess->x509dname =3D virNetTLSContextCertValidate(cert, + sess->isS= erver, + sess->hos= tname, + ctxt->x50= 9dnACL))) { gnutls_x509_crt_deinit(cert); goto authdeny; } - - /* !sess->isServer - as above */ - if (virNetTLSContextCheckCertKeyPurpose(cert, "[session]", - !sess->isServer) < 0) { + } else { + if (virNetTLSContextCertValidateCA(cert, sess->isServer) < 0) { gnutls_x509_crt_deinit(cert); goto authdeny; } } + gnutls_x509_crt_deinit(cert); } =20 --=20 2.43.0