xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/netpgpverify/chk.sh (revision 530ddfcba798af4985ef22615b46feec7697da48)
125f78d91Sagc#! /bin/sh
225f78d91Sagc
3*530ddfcbSagc# $NetBSD: chk.sh,v 1.3 2015/02/05 01:26:54 agc Exp $
425f78d91Sagc
532b86961Sagc# Copyright (c) 2013,2014,2015 Alistair Crooks <agc@NetBSD.org>
625f78d91Sagc# All rights reserved.
725f78d91Sagc#
825f78d91Sagc# Redistribution and use in source and binary forms, with or without
925f78d91Sagc# modification, are permitted provided that the following conditions
1025f78d91Sagc# are met:
1125f78d91Sagc# 1. Redistributions of source code must retain the above copyright
1225f78d91Sagc#    notice, this list of conditions and the following disclaimer.
1325f78d91Sagc# 2. Redistributions in binary form must reproduce the above copyright
1425f78d91Sagc#    notice, this list of conditions and the following disclaimer in the
1525f78d91Sagc#    documentation and/or other materials provided with the distribution.
1625f78d91Sagc#
1725f78d91Sagc# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1825f78d91Sagc# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1925f78d91Sagc# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2025f78d91Sagc# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2125f78d91Sagc# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2225f78d91Sagc# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2325f78d91Sagc# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2425f78d91Sagc# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2525f78d91Sagc# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2625f78d91Sagc# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2725f78d91Sagc#
2825f78d91Sagc
2925f78d91Sagcdie() {
3025f78d91Sagc	echo "$*" >&2
3125f78d91Sagc	exit 1
3225f78d91Sagc}
3325f78d91Sagc
3425f78d91Sagcos=EdgeBSD
3525f78d91Sagcosrev=6
3625f78d91Sagcarch=amd64
3732b86961Sagcpkgsrc=pkgsrc-2013Q1
38*530ddfcbSagckeyring=pubring.gpg
3925f78d91Sagcwhile [ $# -gt 0 ]; do
4025f78d91Sagc	case "$1" in
4125f78d91Sagc	--arch|-a)	arch=$2; shift ;;
42*530ddfcbSagc	--keyring|-k)	keyring=$2; shift ;;
4325f78d91Sagc	--os|-o)	os=$2; shift ;;
4425f78d91Sagc	--pkgsrc)	pkgsrc=$2; shift ;;
4525f78d91Sagc	-v)		set -x ;;
4625f78d91Sagc	*)		break ;;
4725f78d91Sagc	esac
4825f78d91Sagc	shift
4925f78d91Sagcdone
5025f78d91Sagc
5125f78d91Sagc#fetch file
5232b86961Sagcrepo=ftp://ftp.edgebsd.org/pub/pkgsrc/packages/${os}/${os}-${osrev}/${arch}/${pkgsrc}/All/
5325f78d91Sagc
5425f78d91Sagcif [ ! -f $1 ]; then
5525f78d91Sagc	case "${repo}" in
5625f78d91Sagc	*/)	remote=${repo}$1 ;;
5725f78d91Sagc	*)	remote=${repo}/$1 ;;
5825f78d91Sagc	esac
5925f78d91Sagc	ftp ${remote}
6025f78d91Sagcfi
6125f78d91Sagc
6225f78d91Sagcname=$(basename $1 .tgz)
6325f78d91Sagcdir=$(mktemp -d /tmp/chk.XXXXXX)
6425f78d91Sagchere=$(pwd)
6525f78d91Sagccase "$1" in
6625f78d91Sagc/*)	archive=$1 ;;
6725f78d91Sagc*)	archive=${here}/$1 ;;
6825f78d91Sagcesac
6925f78d91Sagc(cd ${dir} && ar x ${archive})
7025f78d91Sagc
7125f78d91Sagc# grab values from already calculated hashes
7225f78d91Sagcdigest=$(awk '$1 ~ /algorithm:/ { print $2 }' ${dir}/+PKG_HASH)
7325f78d91Sagcblocksize=$(awk '/^block size:/ { print $3 }' ${dir}/+PKG_HASH)
7425f78d91Sagc
7525f78d91Sagc# check the hashes in +PKG_HASH match the original archive
7625f78d91Sagcsize=$(ls -l ${dir}/$1 | awk '{ print $5 }')
7725f78d91Sagcprintf "pkgsrc signature\n\nversion: 1\n" > ${dir}/calc
7825f78d91Sagcprintf "pkgname: %s\n" ${name} >> ${dir}/calc
7925f78d91Sagcprintf "algorithm: ${digest}\n" >> ${dir}/calc
8025f78d91Sagcprintf "block size: ${blocksize}\n" >> ${dir}/calc
8125f78d91Sagcprintf "file size: %s\n\n" ${size} >> ${dir}/calc
8225f78d91Sagcoff=0
8325f78d91Sagcn=0
8425f78d91Sagcwhile [ ${off} -lt ${size} ]; do
8525f78d91Sagc	rm -f ${dir}/in
8625f78d91Sagc	dd if=${dir}/$1 of=${dir}/in bs=${blocksize} count=1 skip=${n} 2>/dev/null
8725f78d91Sagc	digest ${digest} < ${dir}/in >> ${dir}/calc
8825f78d91Sagc	off=$(( off + ${blocksize} ))
8925f78d91Sagc	n=$(( n + 1 ))
9025f78d91Sagcdone
9125f78d91Sagcprintf "end pkgsrc signature\n" >> ${dir}/calc
9225f78d91Sagc
9325f78d91Sagc# make sure what was signed is what we have
9425f78d91Sagcdiff ${dir}/+PKG_HASH ${dir}/calc || die "Bad hashes generated"
9525f78d91Sagc
9625f78d91Sagc# use netpgpverify to verify the signature
9732b86961Sagcif [ -x /usr/bin/netpgpverify -o -x /usr/pkg/bin/netpgpverify ]; then
9832b86961Sagc	echo "=== Using netpgpverify to verify the package signature ==="
9925f78d91Sagc	# check the signature in +PKG_GPG_SIGNATURE
100*530ddfcbSagc	cp ${keyring} ${dir}/pubring.gpg
10125f78d91Sagc	# calculate the sig file we want to verify
10225f78d91Sagc	echo "-----BEGIN PGP SIGNED MESSAGE-----" > ${dir}/${name}.sig
10325f78d91Sagc	echo "Hash: ${digest}" >> ${dir}/${name}.sig
10425f78d91Sagc	echo "" >> ${dir}/${name}.sig
10525f78d91Sagc	cat ${dir}/+PKG_HASH ${dir}/+PKG_GPG_SIGNATURE >> ${dir}/${name}.sig
106*530ddfcbSagc	(cd ${dir} && ${here}/netpgpverify -k pubring.gpg ${name}.sig) || die "Bad signature"
10725f78d91Sagcelse
10832b86961Sagc	echo "=== Using gpg to verify the package signature ==="
10932b86961Sagc	gpg --recv --keyserver pgp.mit.edu 0x6F3AF5E2
11025f78d91Sagc	(cd ${dir} && gpg --verify --homedir=${dir} ./+PKG_GPG_SIGNATURE ./+PKG_HASH) || die "Bad signature"
11125f78d91Sagcfi
11225f78d91Sagcecho "Signatures match on ${name} package"
11325f78d91Sagc
11425f78d91Sagc# clean up
11525f78d91Sagcrm -rf ${dir}
11625f78d91Sagc
11725f78d91Sagcexit 0
118