Show
Ignore:
Timestamp:
04/08/08 00:07:10 (8 months ago)
Author:
mizzy
Message:

lang/perl/Punc: SSL まわり実装(まだ途中だけど一度commit)

Location:
lang/perl/Punc/branches/ssl
Files:
4 added
1 removed
9 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Punc/branches/ssl

    • Property svn:ignore set to
      META.yml
      Makefile.old
      _Inline
      inc

  • lang/perl/Punc/branches/ssl/bin

    • Property svn:ignore set to
      _Inline
  • lang/perl/Punc/branches/ssl/bin/puncd

    r8679 r9096  
    66use File::Spec; 
    77use lib File::Spec->catdir("$FindBin::Bin/..", 'lib'); 
     8use Punc; 
    89use Punc::Slave::Daemon; 
    910use Getopt::Long; 
    10  
    11 my $port = 7080; 
     11use Punc::ConfigLoader; 
     12use File::Path; 
    1213 
    1314GetOptions( 
    14     '--port=s' => $port, 
     15    '--port=i'    => \my $port, 
     16    '--confdir=s' => \my $confdir, 
    1517); 
    1618 
    17 my $daemon = Punc::Slave::Daemon->new({ port => $port }); 
     19$port    ||= 7080; 
     20$confdir ||= "$FindBin::Bin/../etc"; 
     21 
     22mkpath($confdir) unless -d $confdir; 
     23 
     24my $yaml = File::Spec->catfile($confdir, 'puncd.yaml'); 
     25 
     26my $conf = -f $yaml ? Punc::ConfigLoader->new->load($yaml) : {}; 
     27 
     28my $daemon = Punc::Slave::Daemon->new({ 
     29    port    => $port, 
     30    confdir => $confdir, 
     31    conf    => $conf, 
     32    context => Punc->new->context, 
     33}); 
     34 
    1835$daemon->run; 
    19  
    20  
    21  
  • lang/perl/Punc/branches/ssl/bin/puncmasterd

    r8679 r9096  
    66use File::Spec; 
    77use lib File::Spec->catdir("$FindBin::Bin/..", 'lib'); 
     8use Punc; 
    89use Punc::Master::Daemon; 
    910use Getopt::Long; 
     
    2021    port    => $port, 
    2122    confdir => $confdir, 
     23    context => Punc->new->context, 
    2224}); 
    2325 
  • lang/perl/Punc/branches/ssl/etc

    • Property svn:ignore set to
      *.cert
      *.key
      *.csr

  • lang/perl/Punc/branches/ssl/lib/Punc.pm

    r8644 r9096  
    44use warnings; 
    55our $VERSION = '0.01'; 
     6 
     7use Pfacter; 
     8use UNIVERSAL::require; 
     9 
     10my $context; 
     11sub context { 
     12    $context = $_[1] if $_[1]; 
     13    return $context; 
     14} 
     15 
     16sub new { 
     17    my $class = shift; 
     18    my $self = bless {}, $class; 
     19    $self->context($self); 
     20} 
     21 
     22sub fact { 
     23    my ( $self, $fact ) = @_; 
     24 
     25    foreach ( qw( kernel operatingsystem hostname domain ) ) { 
     26        $self->{'pfact'}->{$_} = $self->_pfact( $_ ); 
     27    } 
     28 
     29    return $self->_pfact($fact); 
     30} 
     31 
     32sub _pfact { 
     33    my $self   = shift; 
     34    my $module = shift; 
     35 
     36    return $self->{'pfact'}->{lc( $module )} 
     37        if $self->{'pfact'}->{lc( $module )}; 
     38 
     39    $module = 'Pfacter::' . lc $module; 
     40    $module->require or die $@; 
     41 
     42    my $pfact = $module->pfact($self); 
     43    chomp $pfact; 
     44    return $pfact; 
     45} 
    646 
    7471; 
  • lang/perl/Punc/branches/ssl/lib/Punc/Master/Daemon.pm

    r8679 r9096  
    44use warnings; 
    55use base qw( Punc::Daemon ); 
    6 use Punc::Util; 
    76use File::Spec; 
     7use File::Path; 
     8use Punc::Master::CA; 
    89use Crypt::OpenSSL::CA; 
    910use Crypt::OpenSSL::RSA; 
     
    1314    my $self = $class->SUPER::new(@_); 
    1415 
    15     $self->_find_or_create_ca_cert; 
     16    $self->{ca} = Punc::Master::CA->new; 
     17 
     18    $self->_find_or_create_ca_cert($self->{context}); 
    1619 
    1720    return $self; 
     
    1922 
    2023sub _find_or_create_ca_cert { 
    21     my $self = shift; 
    22     my $cert = File::Spec->catfile($self->{confdir}, 'ca.cert'); 
     24    my ( $self, $c ) = @_; 
     25 
     26    $self->{ssldir} = File::Spec->catdir($self->{confdir}, 'ssl'); 
     27    $self->{cadir}  = File::Spec->catdir($self->{ssldir}, 'ca'); 
     28    unless ( -d $self->{cadir} ) { 
     29        mkpath($self->{cadir}); 
     30        chmod 0700, $self->{cadir}; 
     31    } 
     32 
     33    my $cert = File::Spec->catfile($self->{cadir}, 'ca.cert'); 
    2334    unless ( -f $cert ) { 
    24         $self->_create_self_signed_cert; 
     35        $self->_create_self_signed_cert($c); 
    2536    } 
    2637} 
    2738 
    2839sub _create_self_signed_cert { 
    29     my $self = shift; 
     40    my ( $self, $c ) = @_; 
    3041 
     42    # 鍵作成 
    3143    my $rsa = Crypt::OpenSSL::RSA->generate_key(1024); 
    32  
    33     open my $out, '>', File::Spec->catfile($self->{confdir}, 'ca.key') or die $!; 
     44    open my $out, '>', File::Spec->catfile($self->{cadir}, 'ca.key') or die $!; 
    3445    print $out $rsa->get_private_key_string; 
    3546    close $out; 
    3647 
    37     my $dn      = Crypt::OpenSSL::CA::X509_NAME->new( CN => Punc::Util->fact('fqdn') ); 
    3848    my $privkey = Crypt::OpenSSL::CA::PrivateKey->parse($rsa->get_private_key_string); 
    3949    my $pubkey  = $privkey->get_public_key; 
    4050 
     51    # 自己署名証明書作成 
     52    my $dn   = Crypt::OpenSSL::CA::X509_NAME->new( CN => $c->fact('fqdn') ); 
    4153    my $x509 = Crypt::OpenSSL::CA::X509->new($pubkey); 
    4254 
     
    4557    $x509->set_issuer_DN($dn); 
    4658 
     59    ### TODO: 有効期限の設定 
     60 
    4761    my $pem = $x509->sign($privkey, 'sha1'); 
    48     open my $cert, '>', File::Spec->catfile($self->{confdir}, 'ca.cert') or die $!; 
     62    open my $cert, '>', File::Spec->catfile($self->{cadir}, 'ca.cert') or die $!; 
    4963    print $cert $pem; 
    5064    close $cert; 
     
    5569    my ( $self, $module, $method, $args ) = @_; 
    5670 
    57     my $res; 
    58     if ( $method eq 'description' ) { 
    59         $res = $module->description; 
    60     } 
    61     else { 
    62         my $obj = $module->new; 
    63         $res = $obj->exec($method, $args); 
    64     } 
     71    # CSR 取得 
     72    my $csr = $args->{csr}; 
    6573 
    66     return $res; 
     74    my $csrdir = File::Spec->catdir($self->{ssldir}, 'csrs'); 
     75    mkpath($csrdir) unless -d $csrdir; 
     76 
     77    Punc::Master::CA->save_csr({ 
     78        csr => $csr, 
     79        dir => $csrdir, 
     80    }); 
     81 
     82 
     83    ### TODO: 一度自己署名証明書をつくってもらって、sign しなおした方が早い? 
     84    ### TODO: 署名は puncmaster-ca コマンドでやるので、ここではやらない 
     85    ### TODO: 自動署名 
     86 
    6787} 
    6888 
     89 
    69901; 
  • lang/perl/Punc/branches/ssl/lib/Punc/Slave/Daemon.pm

    r8679 r9096  
    33use strict; 
    44use warnings; 
     5use File::Spec; 
     6use Crypt::OpenSSL::PKCS10 qw( :const ); 
     7use JSON::RPC::Client; 
     8use JSON; 
     9use File::Path; 
    510 
    611use base qw( Punc::Daemon ); 
     12 
     13sub new { 
     14    my $class = shift; 
     15    my $self = $class->SUPER::new(@_); 
     16 
     17    $self->_find_or_request_cert($self->{context}); 
     18 
     19    return $self; 
     20} 
     21 
     22sub _find_or_request_cert { 
     23    my ( $self, $c ) = @_; 
     24 
     25    $self->{ssldir}  = File::Spec->catdir($self->{confdir}, 'ssl'); 
     26    $self->{certdir} = File::Spec->catdir($self->{ssldir}, 'certs'); 
     27    $self->{keydir} = File::Spec->catdir($self->{ssldir}, 'keys'); 
     28    $self->{csrdir} = File::Spec->catdir($self->{ssldir}, 'csrs'); 
     29 
     30    mkpath($self->{certdir}) unless -d $self->{certdir}; 
     31    mkpath($self->{csrdir}) unless -d $self->{csrdir}; 
     32    unless ( -d $self->{keydir} ) { 
     33        mkpath($self->{keydir}); 
     34        chmod 0700, $self->{keydir}; 
     35    } 
     36 
     37    my $cert = File::Spec->catfile($self->{certdir}, $c->fact('fqdn') . '.cert'); 
     38    unless ( -f $cert ) { 
     39        $self->_request_cert($c); 
     40    } 
     41} 
     42 
     43sub _request_cert { 
     44    my ( $self, $c ) = @_; 
     45 
     46    my $req  = Crypt::OpenSSL::PKCS10->new; 
     47    my $fqdn = $c->fact('fqdn'); 
     48    $req->set_subject("/CN=$fqdn"); 
     49    $req->sign(); 
     50 
     51    $req->write_pem_req( File::Spec->catfile( $self->{csrdir}, "${fqdn}.csr" ) ); 
     52    $req->write_pem_pk( File::Spec->catfile( $self->{keydir}, "${fqdn}.key" ) ); 
     53 
     54    my $client = JSON::RPC::Client->new; 
     55    my $host = $self->{conf}->{puncmaster_host} || 'localhost'; 
     56    my $port = $self->{conf}->{puncmaster_port} || 7081; 
     57    my $url    = "http://$host:$port/cert"; 
     58 
     59    my $callobj = { 
     60        method  => 'request', 
     61        params  => { csr => $req->get_pem_req() }, 
     62    }; 
     63 
     64    my $res = $client->call($url, $callobj); 
     65 
     66    if( $res ) { 
     67        warn $res->content 
     68    } 
     69    else { 
     70        warn 'error'; 
     71    } 
     72} 
    773 
    874sub handle_request { 
  • lang/perl/Punc/branches/ssl/t/97_podspell.t

    r8644 r9096  
    88Gosuke Miyashita 
    99Punc 
     10TODO 
     11Facter 
     12punc 
     13