Skip to content

Commit

Permalink
[backend] BSX509: add method to create a pubkey from the key data
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Jan 19, 2022
1 parent 3da3168 commit 911d218
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/backend/BSX509.pm
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,27 @@ sub pubkey2keydata {
return $res;
}

sub keydata2pubkey {
my ($keydata) = @_;
my $algo = $keydata->{'algo'} || '?';
my ($algoparams, $bits);
if ($algo eq 'rsa') {
$bits = BSASN1::pack_sequence(BSASN1::pack_integer_mpi($keydata->{'mpis'}->[0]->{'data'}), BSASN1::pack_integer_mpi($keydata->{'mpis'}->[1]->{'data'}));
} elsif ($algo eq 'dsa') {
my @mpis = @{$keydata->{'mpis'} || []};
$bits = BSASN1::pack_integer_mpi((pop @mpis)->{'data'});
$algoparams = BSASN1::pack_sequence(map {BSASN1::pack_integer_mpi($_->{'data'})} @mpis);
} elsif ($algo eq 'ecdsa') {
$bits = $keydata->{'point'};
die("need a curve for ecdsa\n") unless $keydata->{'curve'};
$algoparams = $oid_prime256v1 if $keydata->{'curve'} eq 'prime256v1';
die("unsupported curve $keydata->{'curve'}\n") unless $algoparams;
} elsif ($algo eq 'ed25519' || $algo eq 'ed448') {
$bits = $keydata->{'point'};
} else {
die("unsupported pubkey algo $algo\n");
}
return BSASN1::pack_sequence(pack_sigalgo($algo, undef, $algoparams), BSASN1::pack_bytes($bits));
}

1;

0 comments on commit 911d218

Please sign in to comment.