2026-11-11T13:27:23Z
libpve-cluster-perl 9.1.9
pve-cluster 9.1.9
== Setup.pm 470-560
    gen_pveca_key();

    $generate_pveca_cert->($pveca_key_fn, $pveca_cert_fn);

    return 1;
}

# Keeping the key and the exact encoded subject lets node certificates issued by the previous CA
# certificate chain up to the renewed one, and clients trusting the previous one accept new node
# certificates until it expires. Returns the expiry of the previous one if it got renewed.
sub renew_pveca_cert {
    return undef if !-f $pveca_key_fn || !-f $pveca_cert_fn;

    my $renewal_reason = sub {
        my $min_days = $pvessl_cert_max_days + $pvessl_cert_days_before_ca;
        if (PVE::Certificate::check_expiry($pveca_cert_fn, time() + $min_days * 24 * 60 * 60)) {
            return 'expires within two years';
        }

        my $key_usage;
        my $cmd = ['openssl', 'x509', '-noout', '-ext', 'keyUsage', '-in', $pveca_cert_fn];
        PVE::Tools::run_command(
            $cmd,
            outfunc => sub { $key_usage = 1 },
            errfunc => sub { },
            timeout => $pveca_lock_cmd_timeout,
        );
        return 'lacks the keyUsage extension that strict TLS clients require' if !$key_usage;

        return undef;
    };
    my $reason = $renewal_reason->() or return undef;

    my $old_expiry = PVE::Cluster::cfs_lock_domain(
        'pve-root-ca',
        undef,
        sub {
            return undef if !$renewal_reason->(); # another node renewed it in the meantime

            my $old = PVE::Certificate::get_certificate_info($pveca_cert_fn);
            my $tmp_fn = "$pveca_cert_fn.tmp.$$";
            my $cmd = [
                [
                    'openssl', 'x509', '-x509toreq', # keeps the exact encoded subject
                    '-in', $pveca_cert_fn, '-key', $pveca_key_fn,
                ],
                [
                    'faketime', 'yesterday', # NOTE: wrap openssl with faketime to prevent bug #904
                    'openssl', 'req', '-x509', '-in', '/dev/stdin', '-key', $pveca_key_fn,
                    '-days', $pveca_cert_days,
                    '-addext', 'keyUsage=critical,keyCertSign,cRLSign',
                    '-out', $tmp_fn,
                ],
            ];
            eval {
                run_silent_cmd($cmd, timeout => $pveca_lock_cmd_timeout);
                my $new = PVE::Certificate::get_certificate_info($tmp_fn);
                if ($new->{subject} ne $old->{subject}) {
                    die "subject of renewed certificate differs\n";
                }
                rename($tmp_fn, $pveca_cert_fn)
                    or die "replacing '$pveca_cert_fn' failed - $!\n";
            };
            if (my $err = $@) {
                unlink $tmp_fn;
                die "renewing pve root certificate failed:\n$err";
            }

            return $old->{notafter};
        },
    );
    die $@ if $@;

    if ($old_expiry) {
        my $deadline = localtime($old_expiry);
        print "Renewed cluster CA certificate, as the previous one $reason. Clients that trust"
            . " the previous one must import the renewed $pveca_cert_fn before $deadline\n";
    }

    return $old_expiry;
}

sub rotate_pveca {
    my $tmp_key_fn = "$pveca_key_fn.tmp.$$";
    my $tmp_cert_fn = "$pveca_cert_fn.tmp.$$";
    eval {
        $generate_pveca_key->($tmp_key_fn);
        $generate_pveca_cert->($tmp_key_fn, $tmp_cert_fn);

        PVE::Cluster::cfs_lock_domain(
            'pve-root-ca',
== Setup.pm 575-720
    }
}

# Returns who issued the node certificate, the 'current-ca', a 'replaced-ca' or an 'other' issuer,
# and its expiry. A certificate from another issuer was placed there by an admin and must be kept.
sub pve_ssl_cert_status {
    my ($nodename) = @_;

    my $pvessl_cert_fn = "$pmxcfs_base_dir/nodes/$nodename/pve-ssl.pem";
    return undef if !-f $pvessl_cert_fn;

    my $cert = PVE::Certificate::get_certificate_info($pvessl_cert_fn);
    my $issuer = $cert->{issuer};

    my $issued_by = 'other';
    if ($issuer eq PVE::Certificate::get_certificate_info($pveca_cert_fn)->{subject}) {
        $issued_by = 'current-ca';
    } elsif ($issuer =~ m!^/CN=Proxmox Virtual Environment/OU=[^/]+/O=PVE Cluster Manager CA$!) {
        $issued_by = 'replaced-ca';
    }

    return { 'issued-by' => $issued_by, notafter => $cert->{notafter} };
}

sub gen_pve_ssl_key {
    my ($nodename) = @_;

    die "no node name specified" if !$nodename;

    my $pvessl_key_fn = "$pmxcfs_base_dir/nodes/$nodename/pve-ssl.key";

    return if -f $pvessl_key_fn;

    eval { run_silent_cmd(['openssl', 'genrsa', '-out', $pvessl_key_fn, '2048']); };

    die "unable to generate pve ssl key for node '$nodename':\n$@" if $@;
}

sub gen_pve_www_key {

    return if -f $pvewww_key_fn;

    eval { run_silent_cmd(['openssl', 'genrsa', '-out', $pvewww_key_fn, '2048']); };

    die "unable to generate pve www key:\n$@" if $@;
}

sub update_serial {
    my ($serial) = @_;

    PVE::Tools::file_set_contents($pveca_srl_fn, $serial);
}

sub gen_pve_ssl_cert {
    my ($force, $nodename, $ip) = @_;

    die "no node name specified" if !$nodename;
    die "no IP specified" if !$ip;

    my $pvessl_cert_fn = "$pmxcfs_base_dir/nodes/$nodename/pve-ssl.pem";

    return if !$force && -f $pvessl_cert_fn;

    my $names = "IP:127.0.0.1,IP:::1,DNS:localhost,IP:${ip},DNS:${nodename}";

    my $fqdn = $nodename;

    my $rc = PVE::INotify::read_file('resolvconf');
    if ($rc && $rc->{search}) {
        $fqdn .= ".$rc->{search}";
        $names .= ",DNS:$fqdn";
    }

    my $reqfn = "/run/pve-cluster/pvecertreq-$$.tmp";
    unlink $reqfn;

    my $pvessl_key_fn = "$pmxcfs_base_dir/nodes/$nodename/pve-ssl.key";
    eval {
        run_silent_cmd([
            'openssl',
            'req',
            '-batch',
            '-new',
            '-key',
            $pvessl_key_fn,
            '-out',
            $reqfn,
            '-subj',
            "/OU=PVE Cluster Node/O=Proxmox Virtual Environment/CN=$fqdn",
            '-addext',
            'basicConstraints=CA:FALSE',
            '-addext',
            'extendedKeyUsage=serverAuth',
            '-addext',
            "subjectAltName=$names",
        ]);
    };

    if (my $err = $@) {
        unlink $reqfn;
        die "unable to generate pve certificate request:\n$err";
    }

    # the lock also covers the serial file that all nodes share
    PVE::Cluster::cfs_lock_domain(
        'pve-root-ca',
        undef,
        sub {
            update_serial("0000000000000000") if !-f $pveca_srl_fn;

            # get ca expiry
            my $cainfo = PVE::Certificate::get_certificate_info($pveca_cert_fn);
            my $daysleft = int(($cainfo->{notafter} - time()) / (24 * 60 * 60));

            if ($daysleft < 14) {
                die "CA expires in less than 2 weeks, unable to generate certificate.\n";
            }

            $daysleft -= $pvessl_cert_days_before_ca;

            if ($daysleft >= $pvessl_cert_max_days) {
                $daysleft = $pvessl_cert_max_days;
            }

            my $cmd = [
                'faketime', 'yesterday', # NOTE: wrap openssl with faketime to prevent bug #904
                'openssl', 'x509', '-req', '-in', $reqfn, '-days', $daysleft, '-out',
                $pvessl_cert_fn,
                '-CAkey', $pveca_key_fn, '-CA', $pveca_cert_fn, '-CAserial', $pveca_srl_fn,
                '-copy_extensions', 'copyall',
            ];
            run_silent_cmd($cmd, timeout => $pveca_lock_cmd_timeout);
        },
    );
    my $err = $@;

    unlink $reqfn or $!{ENOENT} or warn "failed to clean up '$reqfn' - $!";

    die "unable to generate pve ssl certificate:\n$err" if $err;
}

sub gen_pve_node_files {
    my ($nodename, $ip, $opt_force) = @_;

    gen_local_dirs($nodename);

== pveupdate 100-170
            );
        }
    }
};
syslog('err', "Renewing ACME certificate failed: $@") if $@;

eval { PVE::Cluster::Setup::renew_pveca_cert() };
syslog('err', "Renewing cluster CA certificate failed: $@") if $@;

eval {
    my $certpath = PVE::CertHelpers::default_cert_path_prefix($nodename) . ".pem";
    my $capath = "/etc/pve/pve-root-ca.pem";

    my $check_issued_by_ca = sub {
        # get CA info
        my $cainfo = PVE::Certificate::get_certificate_info($capath);

        # get cert and check issuer and chain metadata
        my $certinfo = PVE::Certificate::get_certificate_info($certpath);
        if ($certinfo->{issuer} ne $cainfo->{subject}) {
            die "SSL certificate ($certpath) is not issued by root CA ($capath)!\n";
        }

        # check if cert is really signed by the ca
        # TODO: replace by low level ssleay interface if version 1.86 is available
        my $cmd = [
            '/usr/bin/openssl', 'verify', '-no_check_time', '-CAfile', $capath, '--', $certpath,
        ];
        PVE::Tools::run_command($cmd);
    };

    my $renew = sub {
        my ($msg, $replaced_ca) = @_;

        # one from a replaced cluster CA fails these checks, but is still ours to replace
        $check_issued_by_ca->() if !$replaced_ca;

        print "PVE certificate $msg\n";
        # create new certificate
        my $ip = PVE::Cluster::remote_node_ip($nodename);
        PVE::Cluster::Setup::gen_pve_ssl_cert(1, $nodename, $ip);
        my $expiry = localtime(PVE::Certificate::get_expiration_as_epoch($certpath));
        print "Renewed PVE certificate is valid until $expiry\n";

        print "Restarting pveproxy after renewing certificate\n";
        PVE::Tools::run_command(['systemctl', 'reload-or-restart', 'pveproxy']);
    };

    my $status = eval { PVE::Cluster::Setup::pve_ssl_cert_status($nodename) };
    syslog('err', "Checking SSL certificate failed: $@") if $@;

    if ($status && $status->{'issued-by'} eq 'replaced-ca') {
        $renew->("was issued by a replaced cluster CA, renewing...", 1);
    } elsif (PVE::Certificate::check_expiry($certpath)) {
        # already expired
        $renew->("expired, renewing...");
    } elsif (PVE::Certificate::check_expiry($certpath, time() + 14 * 24 * 60 * 60)) {
        # expires in next 2 weeks
        $renew->("expires soon, renewing...");
    } elsif (!PVE::Certificate::check_expiry($certpath, time() + 2 * 365 * 24 * 60 * 60)) {
        # expires in more than 2 years
        $renew->(
            "expires in more than 2 years, renewing to reduce certificate life-span for client compatibility..."
        );
    }
};
syslog('err', "Checking/Renewing SSL certificate failed: $@") if $@;

sub cleanup_tasks {

    my $taskdir = "/var/log/pve/tasks";
== timer
NEXT                        LEFT LAST                         PASSED UNIT                   ACTIVATES
Thu 2026-11-12 01:46:13 CET  11h Wed 2026-11-11 02:22:58 CET 12h ago pve-daily-update.timer pve-daily-update.service

