From patchwork Wed Feb 28 18:58:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Roevens X-Patchwork-Id: 7567 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4TlPMW1wJZz3wvx for ; Wed, 28 Feb 2024 19:20:39 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4TlPMP6nLsz3Lc; Wed, 28 Feb 2024 19:20:33 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4TlPMP61Nkz32gx; Wed, 28 Feb 2024 19:20:33 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4TlPML5TXBz3009 for ; Wed, 28 Feb 2024 19:20:30 +0000 (UTC) Received: from layka.disroot.org (layka.disroot.org [178.21.23.139]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPS id 4TlPMK2qghz2Jv for ; Wed, 28 Feb 2024 19:20:29 +0000 (UTC) Authentication-Results: mail01.ipfire.org; dkim=pass header.d=disroot.org header.s=mail header.b=QF8ObTPF; dmarc=pass (policy=reject) header.from=disroot.org; spf=pass (mail01.ipfire.org: domain of robin.roevens@disroot.org designates 178.21.23.139 as permitted sender) smtp.mailfrom=robin.roevens@disroot.org ARC-Seal: i=1; s=202003rsa; d=lists.ipfire.org; t=1709148029; a=rsa-sha256; cv=none; b=SdwWe8d3dtOZyAbCqxqAu1RgkN6umjAfYm3aPYoXyz6PyxT0N2QbnNT/tFPM7Qayj+AtTS i70mcvMV9XU3hUK04xtV6useXJgGDa1dG9pIODG9pHVPdBTr4+8lwwtRKZvVqMS1eET1Ua Sn7sykNV1gOJy80eXNszJTrTrY6yBEAL6yQYamIjiga/VrNmMJXTXWwKL2pLrdEUhe9lB/ LkUhW/Z8Cep4gg2TTjGrUxfCgf9iPGpqB5GZbhehRZTovupK0g3oSs5L01Agn5lCOgJMT8 8nXIe71jY17jMW1b5S0MKa+OQ73Y3p0c6Lo/+EMDaoxi5neBxOcQyHWL019Qog== ARC-Authentication-Results: i=1; mail01.ipfire.org; dkim=pass header.d=disroot.org header.s=mail header.b=QF8ObTPF; dmarc=pass (policy=reject) header.from=disroot.org; spf=pass (mail01.ipfire.org: domain of robin.roevens@disroot.org designates 178.21.23.139 as permitted sender) smtp.mailfrom=robin.roevens@disroot.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.ipfire.org; s=202003rsa; t=1709148029; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2D/780I4u0cFPPccHJJ9fKdf627GtLYlaI7z7I7F1i4=; b=LQz8PkHy3gMttWvdizOIjHKzRaX0ToHfY2mKuYF/XknlM4ixFVNDcJn+csm+DCAuzk5Aye cVHw4jsfKyO1cVB0ETLNDcNuhkLeYfZkbzOF4ks3cNdC9QYWqUdsfUohnLPYhfJsn2SiRH u7eJtTFsnO7ZzOZZj49s0/vm2UFn6wrOiWELYziEBkOYE9VO8lo1tOAgKbExrlTIbh0Udm 4jno4AeiBYGdos0PWJLU4e90uLGI42lA5ljR9+CaUxmogNLPmlYaT0Sebtpleu1x3nd4sK G0omfta32nZNLNaPjXRf/PJgQAQ8GXlRuf2mfrzZDAEzOiPkwsh5cI9slUe0IQ== Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id AB8C44142E for ; Wed, 28 Feb 2024 20:20:28 +0100 (CET) X-Virus-Scanned: SPAM Filter at disroot.org Received: from layka.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2UPXH0GgMoZU for ; Wed, 28 Feb 2024 20:20:26 +0100 (CET) Received: from chojin.sicho.home (amaterasu.sicho.home [192.168.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (no client certificate requested) (Authenticated sender) by hachiman (MailScanner Milter) with SMTP id 997F918A0F6; Wed, 28 Feb 2024 20:20:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1709148026; bh=YGZOVL/h2gf/6o7VW6vY1ywg9dVsvljLBA1ZNHcSJ1I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QF8ObTPFB2g4iQKT78hVxR7NEZM+uQ4xsYKMZDIwWDDcThG54VlftrJPwONxi2PgI Q3Y8DH9ptPJmp7gY7KyY8VnyWsR90PkdkttqG+fRy2Ub2Moigk8eCKATSITNMY4h1u /Ds/jRsmnhorcZPc2IFSTc5KU/tO7Vgou6r87JNuvE4rY8Ouj7I1SeN2/czPELEBzQ cx8UGhuO9bHV3M3JTlOJH+G6VgLIfN+yTl9FXX+rSJnED2bR/zSZXQQmWxSuvsXLjT e3kcNPxExcnhKVHSPqayjVu6NuCMBMRUF5o/XpT+56LNrSUZsxge9ig3RoRRdZ8eut kaaZ0BKttcy7w== From: Robin Roevens To: development@lists.ipfire.org Subject: [PATCH 2/3] zabbix_agentd: Add helper script to get and verify certificate details Date: Wed, 28 Feb 2024 19:58:35 +0100 Message-ID: <20240228191952.28258-3-robin.roevens@disroot.org> In-Reply-To: <20240228191952.28258-1-robin.roevens@disroot.org> References: <20240228191952.28258-1-robin.roevens@disroot.org> MIME-Version: 1.0 X-sicho-MailScanner-ID: 997F918A0F6.A8A80 X-sicho-MailScanner: Found to be clean X-sicho-MailScanner-From: robin.roevens@disroot.org X-sicho-MailScanner-Watermark: 1709752819.95121@JJxClYKcrnKfKGPUVqIsJQ X-Rspamd-Queue-Id: 4TlPMK2qghz2Jv X-Spamd-Result: default: False [-2.60 / 11.00]; BAYES_HAM(-2.51)[97.81%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM(-1.00)[-1.000]; DMARC_POLICY_ALLOW(-0.50)[disroot.org,reject]; R_MISSING_CHARSET(0.50)[]; SPF_REPUTATION_SPAM(0.44)[0.14513598913944]; R_SPF_ALLOW(-0.20)[+a:c]; R_DKIM_ALLOW(-0.20)[disroot.org:s=mail]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; IP_REPUTATION_HAM(-0.01)[asn: 50673(0.00), country: NL(-0.01), ip: 178.21.23.139(0.00)]; RCPT_COUNT_TWO(0.00)[2]; PREVIOUSLY_DELIVERED(0.00)[development@lists.ipfire.org]; FUZZY_BLOCKED(0.00)[rspamd.com]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; RCVD_TLS_LAST(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ASN(0.00)[asn:50673, ipnet:178.21.23.0/24, country:NL]; DKIM_REPUTATION(0.00)[0]; ARC_SIGNED(0.00)[lists.ipfire.org:s=202003rsa:i=1]; MISSING_XM_UA(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DKIM_TRACE(0.00)[disroot.org:+]; FROM_HAS_DN(0.00)[] X-Rspamd-Action: no action X-Rspamd-Server: mail01.haj.ipfire.org Message-ID-Hash: LWHO32RS234FPXIALVXBQUDNGAN6ASZL X-Message-ID-Hash: LWHO32RS234FPXIALVXBQUDNGAN6ASZL X-MailFrom: robin.roevens@disroot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: IPFire development talk Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add script to parse openssl output on certificates and return it as JSON for consumption by the Zabbix agent. Reviewed-by: Adolf Belka --- .../ipfire_certificate_detail.sh | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 config/zabbix_agentd/ipfire_certificate_detail.sh diff --git a/config/zabbix_agentd/ipfire_certificate_detail.sh b/config/zabbix_agentd/ipfire_certificate_detail.sh new file mode 100755 index 000000000..9ca0ef5de --- /dev/null +++ b/config/zabbix_agentd/ipfire_certificate_detail.sh @@ -0,0 +1,91 @@ +#!/bin/bash +############################################################################### +# ipfire_certificate_detail.sh - Get certificate details and validation results +# in JSON format for use by Zabbix agent +# +# Author: robin.roevens (at) disroot.org +# Version: 1.0 +# +# Copyright (C) 2007-2024 IPFire Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################### + +# Required binaries +OPENSSL=/usr/bin/openssl +DATE=/bin/date + +# Parameter checking +[[ $1 ]] || { echo "{\"error\":\"No CA certificate file given.\"}"; exit 1; } +[[ -f $1 ]] || { echo "{\"error\":\"CA certificate not found: $1.\"}"; exit 1; } +[[ -r $1 ]] || { echo "{\"error\":\"No read permission on CA certificate: $1.\"}"; exit 1; } +[[ $2 ]] || { echo "{\"error\":\"No certificate file given.\"}"; exit 1; } +[[ -f $2 ]] || { echo "{\"error\":\"Certificate not found: $2.\"}"; exit 1; } +[[ -r $2 ]] || { echo "{\"error\":\"No read permission on certificate $2.\"}"; exit 1; } +[[ -x $OPENSSL ]] || { echo "{\"error\":\"$OPENSSL binary not found or no permission.\"}"; exit 1; } +[[ -x $DATE ]] || { echo "{\"error\":\"$DATE binary not found or no permission.\"}"; exit 1; } + +cafile=$1 +cert=$2 + +# Parse certificate details +cert_details=$(${OPENSSL} x509 -in "${cert}" -noout -text -certopt no_header,no_sigdump) +version=$(echo "${cert_details}" | grep "Version:" | sed 's/^ \+Version: \([0-9]\+\) (.\+)$/\1/g') +serial_number=$(echo "${cert_details}" | grep -A1 "Serial Number:" | tr -d '\n' | sed 's/^ \+Serial Number:\(\( \(.*\) ([0-9]\+x[0-9]\+).*\)\|\( \+\(.*\)$\)\)/\3\5/g') +signature_algorithm=$(echo "${cert_details}" | grep "Signature Algorithm:" | sed 's/^ \+Signature Algorithm: //g') +issuer=$(echo "${cert_details}" | grep "Issuer:" | sed 's/^ \+Issuer: //g' | sed 's/"/\\"/g') +not_before_value=$(echo "${cert_details}" | grep "Not Before:" | sed 's/^ \+Not Before: //g') +not_before_timestamp=$(${DATE} -d "${not_before_value}" +%s) +not_after_value=$(echo "${cert_details}" | grep "Not After :" | sed 's/^ \+Not After : //g') +not_after_timestamp=$(${DATE} -d "${not_after_value}" +%s) +subject=$(echo "${cert_details}" | grep "Subject:" | sed 's/^ \+Subject: //g' | sed 's/"/\\"/g') +public_key_algorithm=$(echo "${cert_details}" | grep "Public Key Algorithm:" | sed 's/^ \+Public Key Algorithm: //g') + +# Verify certificate +cert_verify=$(${OPENSSL} verify -CAfile "${cafile}" "${cert}" 2>&1) +if [[ $? != 0 ]]; then + result_value="invalid" + result_message="failed to verify certificate: x509: $(echo "${cert_verify}" | grep -E "error [0-9]+" | sed 's/^.\+: \(.\+\)/\1/g')" +else + result_value="valid" + result_message="certificate verified successfully" +fi + +# Generate fingerprints +sha1_fingerprint=$(${OPENSSL} x509 -in "${cert}" -noout -fingerprint -sha1 | cut -d= -f2) +sha256_fingerprint=$(${OPENSSL} x509 -in "${cert}" -noout -fingerprint -sha256 | cut -d= -f2) + +# Print certificate details in JSON +echo -n "{\"x509\":{" +echo -n "\"version\":\"${version}\"," +echo -n "\"serial_number\":\"${serial_number}\"," +echo -n "\"signature_algorithm\":\"${signature_algorithm}\"," +echo -n "\"issuer\":\"${issuer}\"," +echo -n "\"not_before\":{" +echo -n "\"value\":\"${not_before_value}\"," +echo -n "\"timestamp\":\"${not_before_timestamp}\"}," +echo -n "\"not_after\":{" +echo -n "\"value\":\"${not_after_value}\"," +echo -n "\"timestamp\":\"${not_after_timestamp}\"}," +echo -n "\"subject\":\"${subject}\"," +echo -n "\"public_key_algorithm\":\"${public_key_algorithm}\"}," +echo -n "\"result\":{" +echo -n "\"value\":\"${result_value}\"," +echo -n "\"message\":\"${result_message}\"}," +echo -n "\"sha1_fingerprint\":\"${sha1_fingerprint}\"," +echo -n "\"sha256_fingerprint\":\"${sha256_fingerprint}\"" +echo -n "}" + +exit 0 \ No newline at end of file