| 1 | package Net::DNS::SEC::Private; | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: Private.pm 1677 2018-05-22 11:59:10Z willem $ | ||
| 5 | # | ||
| 6 | our $VERSION = (qw$LastChangedRevision: 1677 $)[1]; | ||
| 7 | |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Net::DNS::SEC::Private - DNSSEC Private key object | ||
| 12 | |||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Net::DNS::SEC::Private; | ||
| 17 | |||
| 18 | $private = new Net::DNS::SEC::Private( $keypath ); | ||
| 19 | |||
| 20 | $private = new Net::DNS::SEC::Private( | ||
| 21 | 'algorithm' => '13', | ||
| 22 | 'keytag' => '26512', | ||
| 23 | 'privatekey' => 'h/mc+iq9VDUbNAjQgi8S8JzlEX29IALchwJmNM3QYKk=', | ||
| 24 | 'signame' => 'example.com.' | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Class representing private keys as read from a keyfile generated by BIND | ||
| 31 | dnssec-keygen. The class is written to be used only in the context of the | ||
| 32 | Net::DNS::RR::RRSIG create method. This class is not designed to interact | ||
| 33 | with any other system. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | |||
| 38 | use strict; | ||
| 39 | use integer; | ||
| 40 | use warnings; | ||
| 41 | use File::Spec; | ||
| 42 | use IO::File; | ||
| 43 | |||
| 44 | |||
| 45 | sub new { scalar(@_) > 2 ? &_new_params : &_new_keyfile } | ||
| 46 | |||
| 47 | sub _new_keyfile { | ||
| 48 | my ( $class, $file ) = @_; | ||
| 49 | |||
| 50 | my ( $vol, $dir, $keyname ) = File::Spec->splitpath($file); | ||
| 51 | |||
| 52 | # Format something like: /Kbla.foo.+001+12345.private' | ||
| 53 | # as created by BIND keygen. | ||
| 54 | # We determine the algorithm from the filename. | ||
| 55 | die "$keyname does not appear to be a BIND private key" | ||
| 56 | unless $keyname =~ /^K(.*\.)\+(\d+)\+(\d+)\.private$/; | ||
| 57 | my @identifier = ( signame => $1, algorithm => 0 + $2, keytag => 0 + $3 ); | ||
| 58 | |||
| 59 | my $handle = new IO::File($file) or die qq(open: "$file" $!); | ||
| 60 | |||
| 61 | my @content; | ||
| 62 | local $_; | ||
| 63 | while (<$handle>) { | ||
| 64 | chomp; | ||
| 65 | next if /^$/; | ||
| 66 | next if /^\s*[;]/; | ||
| 67 | s/\(.+\)//; | ||
| 68 | my ( $name, $value ) = split; | ||
| 69 | push @content, $name, $value; | ||
| 70 | } | ||
| 71 | |||
| 72 | return $class->_new_params( @content, @identifier ); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub _new_params { | ||
| 77 | my $class = shift; | ||
| 78 | my $param = {}; | ||
| 79 | while (@_) { | ||
| 80 | my $name = shift @_; | ||
| 81 | $name =~ tr/A-Za-z0-9\000-\377/a-za-z0-9/d; | ||
| 82 | $param->{$name} = shift @_; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $self = bless sub { $param->{shift()} }, $class; | ||
| 86 | die 'no algorithm specified' unless $self->algorithm; | ||
| 87 | die 'no signame specified' unless $self->signame; | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | our $AUTOLOAD; | ||
| 93 | |||
| 94 | sub AUTOLOAD { ## Default method | ||
| 95 | my ($self) = @_; | ||
| 96 | no strict q/refs/; | ||
| 97 | |||
| 98 | my ($attribute) = $AUTOLOAD =~ m/::([^:]*)$/; | ||
| 99 | $attribute =~ tr/A-Za-z0-9\000-\377/a-za-z0-9/d; | ||
| 100 | |||
| 101 | # Build a method in the class | ||
| 102 | *{$AUTOLOAD} = sub { &{shift()}($attribute) }; | ||
| 103 | |||
| 104 | # and jump to it | ||
| 105 | goto &{$AUTOLOAD}; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | 1; | ||
| 110 | __END__ |