xref: /openbsd-src/usr.sbin/pkg_add/OpenBSD/Signer.pm (revision 039cbdaaca23c9e872a2bab23f91224c76c0f23b)
1de005353Sespie#! /usr/bin/perl
2de005353Sespie# ex:ts=8 sw=4:
3*039cbdaaSespie# $OpenBSD: Signer.pm,v 1.12 2023/06/13 09:07:17 espie Exp $
4de005353Sespie#
5de005353Sespie# Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org>
6de005353Sespie#
7de005353Sespie# Permission to use, copy, modify, and distribute this software for any
8de005353Sespie# purpose with or without fee is hereby granted, provided that the above
9de005353Sespie# copyright notice and this permission notice appear in all copies.
10de005353Sespie#
11de005353Sespie# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12de005353Sespie# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13de005353Sespie# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14de005353Sespie# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15de005353Sespie# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16de005353Sespie# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17de005353Sespie# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18de005353Sespie
19*039cbdaaSespieuse v5.36;
20de005353Sespie
2107126a4dSespie# code necessary to create signed packages
22de005353Sespie
23de005353Sespie# the factory that chooses what method to use to sign things
24*039cbdaaSespie# we keep that just in case we need to change scheme again
25de005353Sespiepackage OpenBSD::Signer;
2655d9f090Sespieuse OpenBSD::PackageInfo;
27de005353Sespie
28de005353Sespiemy $h = {
2955d9f090Sespie	signify2 => 'OpenBSD::Signer::SIGNIFY2',
30de005353Sespie};
31de005353Sespie
32*039cbdaaSespiesub factory($class, $state)
33de005353Sespie{
34de005353Sespie	my @p = @{$state->{signature_params}};
35de005353Sespie
36de005353Sespie	if (defined $h->{$p[0]}) {
37de005353Sespie		return $h->{$p[0]}->new($state, @p);
38de005353Sespie	} else {
39de005353Sespie		$state->usage("Unknown signature scheme $p[0]");
40de005353Sespie	}
41de005353Sespie}
42de005353Sespie
4355d9f090Sespiepackage OpenBSD::Signer::SIGNIFY2;
4455d9f090Sespieour @ISA = qw(OpenBSD::Signer);
45*039cbdaaSespiesub new($class, $state, @p)
4655d9f090Sespie{
4755d9f090Sespie	if (@p != 2 || !-f $p[1]) {
4855d9f090Sespie		$state->usage("$p[0] signature wants -s privkey");
4955d9f090Sespie	}
5055d9f090Sespie	my $o = bless {privkey => $p[1]}, $class;
5155d9f090Sespie	return $o;
5255d9f090Sespie}
5355d9f090Sespie
54*039cbdaaSespiesub sign($signer, $pkg, $state, $tmp)
5555d9f090Sespie{
5655d9f090Sespie	my $privkey = $signer->{privkey};
5755d9f090Sespie 	my $url = $pkg->url;
585aeb9f88Sespie	if (!$pkg->{repository}->is_local_file) {
595aeb9f88Sespie		$pkg->close(1);
605aeb9f88Sespie		$state->fatal("Signing distant package #1 is not supported",
615aeb9f88Sespie		    $url);
625aeb9f88Sespie	}
6355d9f090Sespie	$url =~ s/^file://;
6455d9f090Sespie	$state->system(OpenBSD::Paths->signify, '-zS', '-s', $privkey, '-m', $url, '-x', $tmp);
6555d9f090Sespie}
6655d9f090Sespie
67*039cbdaaSespiesub want_local($)
685aeb9f88Sespie{
695aeb9f88Sespie	return 1;
705aeb9f88Sespie}
71de005353Sespie# specific parameter handling plus element creation
72de005353Sespiepackage OpenBSD::CreateSign::State;
73de005353Sespieour @ISA = qw(OpenBSD::AddCreateDelete::State);
74de005353Sespie
75*039cbdaaSespiesub create_archive($state, $filename, $dir)
76de005353Sespie{
77de005353Sespie	require IO::Compress::Gzip;
7801c5a78cSespie	my $level = $state->{subst}->value('COMPRESSION_LEVEL') // 6;
79e333f2d2Sespie	my $fh = IO::Compress::Gzip->new($filename,
80e333f2d2Sespie	    -Level => $level, -Time => 0) or
814df1b723Sespie		$state->fatal("Can't create archive #1: #2", $filename, $!);
82b04e338fSespie	$state->{archive_filename} = $filename;
83de005353Sespie	return OpenBSD::Ustar->new($fh, $state, $dir);
84de005353Sespie}
85de005353Sespie
86*039cbdaaSespiesub new_gstream($state)
87b04e338fSespie{
88b04e338fSespie	close($state->{archive}{fh});
89b04e338fSespie	my $level = $state->{subst}->value('COMPRESSION_LEVEL') // 6;
90b04e338fSespie	$state->{archive}{fh} =IO::Compress::Gzip->new(
91e333f2d2Sespie	    $state->{archive_filename},
92e333f2d2Sespie	    -Level => $level, -Time => 0, -Append => 1) or
93e333f2d2Sespie		$state->fatal("Can't append to archive #1: #2",
94e333f2d2Sespie		    $state->{archive_filename}, $!);
95b04e338fSespie}
96b04e338fSespie
97*039cbdaaSespiesub ntodo($self, $offset = 0)
98de005353Sespie{
99de005353Sespie	return sprintf("%u/%u", $self->{done}-$offset, $self->{total});
100de005353Sespie}
101de005353Sespie
102de005353Sespie1;
103