]> Raphaël G. Git Repositories - acme/blobdiff - acme.pm
Replace encode_json and decode_json with to_json and from_json to avoid
[acme] / acme.pm
diff --git a/acme.pm b/acme.pm
index 6a17c3c04b2ab1d379a83acebf9b9775d1534b12..db1c6d4c6ca1e0cf15209ea26b9ad3dc58a583e2 100644 (file)
--- a/acme.pm
+++ b/acme.pm
@@ -1,3 +1,20 @@
+# This file is part of Acmepl
+#
+# Acmepl is is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Copyright (C) 2016 - 2017 Raphaël Gertz <acmepl@rapsys.eu>
+
 # acme package
 package acme;
 
 # acme package
 package acme;
 
@@ -12,7 +29,6 @@ our @EXPORT_OK = qw(DS CERT_DIR KEY_DIR REQUEST_CSR ACCOUNT_KEY SERVER_KEY SERVE
 
 # Load dependancies
 use Carp qw(carp confess);
 
 # Load dependancies
 use Carp qw(carp confess);
-use Data::Dumper;
 use Date::Parse qw(str2time);
 use Digest::SHA qw(sha256_base64);
 use Email::Valid;
 use Date::Parse qw(str2time);
 use Digest::SHA qw(sha256_base64);
 use Email::Valid;
@@ -20,7 +36,7 @@ use File::Path qw(make_path);
 use File::Slurp qw(read_file write_file);
 use File::Temp; # qw( :seekable );
 use IPC::System::Simple qw(capturex);
 use File::Slurp qw(read_file write_file);
 use File::Temp; # qw( :seekable );
 use IPC::System::Simple qw(capturex);
-use JSON qw(encode_json decode_json);
+use JSON qw(from_json to_json);
 use LWP;
 use MIME::Base64 qw(encode_base64url encode_base64);
 use Net::Domain::TLD;
 use LWP;
 use MIME::Base64 qw(encode_base64url encode_base64);
 use Net::Domain::TLD;
@@ -68,10 +84,10 @@ use constant {
        ACME_CERT => 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem',
        ACME_DIR => 'https://acme-staging.api.letsencrypt.org/directory',
        ACME_PROD_DIR => 'https://acme-v01.api.letsencrypt.org/directory',
        ACME_CERT => 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem',
        ACME_DIR => 'https://acme-staging.api.letsencrypt.org/directory',
        ACME_PROD_DIR => 'https://acme-v01.api.letsencrypt.org/directory',
-       ACME_TERMS => 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf',
+       ACME_TERMS => 'https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf',
 
        # Version
 
        # Version
-       VERSION => 'v0.4',
+       VERSION => 'v0.6',
 
        # Config
        CONFIG => '/etc/acmepl/config'
 
        # Config
        CONFIG => '/etc/acmepl/config'
@@ -247,7 +263,7 @@ sub genKeys {
 
        # Store thumbprint
        #XXX: convert base64 to base64 url
 
        # Store thumbprint
        #XXX: convert base64 to base64 url
-       $self->{account}{thumbprint} = (sha256_base64(encode_json($self->{account}{jwk}{jwk})) =~ s/=+\z//r) =~ tr[+/][-_]r;
+       $self->{account}{thumbprint} = (sha256_base64(to_json($self->{account}{jwk}{jwk})) =~ s/=+\z//r) =~ tr[+/][-_]r;
 }
 
 # Generate certificate request
 }
 
 # Generate certificate request
@@ -299,7 +315,7 @@ sub directory {
        $self->{nonce} = $res->headers->{'replay-nonce'};
 
        # Merge uris in self content
        $self->{nonce} = $res->headers->{'replay-nonce'};
 
        # Merge uris in self content
-       %$self = (%$self, %{decode_json($res->content)});
+       %$self = (%$self, %{from_json($res->content)});
 }
 
 # Post request
 }
 
 # Post request
@@ -307,10 +323,10 @@ sub _post {
        my ($self, $uri, $payload) = @_;
 
        # Protected field
        my ($self, $uri, $payload) = @_;
 
        # Protected field
-       my $protected = encode_base64url(encode_json({nonce => $self->{nonce}}));
+       my $protected = encode_base64url(to_json({nonce => $self->{nonce}}));
 
        # Payload field
 
        # Payload field
-       $payload = encode_base64url(encode_json($payload));
+       $payload = encode_base64url(to_json($payload));
 
        # Sign temp file
        my $stf = File::Temp->new();
 
        # Sign temp file
        my $stf = File::Temp->new();
@@ -328,7 +344,7 @@ sub _post {
        my $req = HTTP::Request->new(POST => $uri);
        
        # Set new-reg request content
        my $req = HTTP::Request->new(POST => $uri);
        
        # Set new-reg request content
-       $req->content(encode_json({
+       $req->content(to_json({
                header => $self->{account}{jwk},
                protected => $protected,
                payload => $payload,
                header => $self->{account}{jwk},
                protected => $protected,
                payload => $payload,
@@ -385,14 +401,14 @@ sub _httpCheck {
        # Load config if available
        my $config = undef;
        if (
        # Load config if available
        my $config = undef;
        if (
-               #XXX: use eval to workaround a fatal in decode_json
+               #XXX: use eval to workaround a fatal in from_json
                defined eval {
                        # Check that file exists
                        -f CONFIG &&
                        # Read it
                        ($config = read_file(CONFIG)) &&
                        # Decode it
                defined eval {
                        # Check that file exists
                        -f CONFIG &&
                        # Read it
                        ($config = read_file(CONFIG)) &&
                        # Decode it
-                       ($config = decode_json($config)) &&
+                       ($config = from_json($config)) &&
                        # Check defined
                        $config->{thumbprint}
                }
                        # Check defined
                        $config->{thumbprint}
                }
@@ -471,14 +487,14 @@ sub authorize {
                # Load auth request content or post a new one
                #TODO: add more check on cache file ???
                if (
                # Load auth request content or post a new one
                #TODO: add more check on cache file ???
                if (
-                       #XXX: use eval to workaround a fatal in decode_json
+                       #XXX: use eval to workaround a fatal in from_json
                        ! defined eval {
                                # Check that file exists
                                -f $file &&
                                # Read it
                                ($content = read_file($file)) &&
                                # Decode it
                        ! defined eval {
                                # Check that file exists
                                -f $file &&
                                # Read it
                                ($content = read_file($file)) &&
                                # Decode it
-                               ($content = decode_json($content))
+                               ($content = from_json($content))
                        # Check expiration
                        } || (str2time($content->{expires}) <= time()+3600)
                ) {
                        # Check expiration
                        } || (str2time($content->{expires}) <= time()+3600)
                ) {
@@ -491,7 +507,7 @@ sub authorize {
                        }
 
                        # Decode content
                        }
 
                        # Decode content
-                       $content = decode_json($res->content);
+                       $content = from_json($res->content);
 
                        # Check domain
                        unless (defined $content->{identifier}{value} && $content->{identifier}{value} eq $_) {
 
                        # Check domain
                        unless (defined $content->{identifier}{value} && $content->{identifier}{value} eq $_) {
@@ -504,7 +520,7 @@ sub authorize {
                        }
 
                        # Write to file
                        }
 
                        # Write to file
-                       write_file($file, encode_json($content));
+                       write_file($file, to_json($content));
                }
 
                # Add challenge
                }
 
                # Add challenge
@@ -539,7 +555,7 @@ sub authorize {
                                                }
 
                                                # Extract content
                                                }
 
                                                # Extract content
-                                               my $content = decode_json($res->content);
+                                               my $content = from_json($res->content);
 
                                                # Save if valid
                                                if ($content->{status} eq 'valid') {
 
                                                # Save if valid
                                                if ($content->{status} eq 'valid') {
@@ -598,7 +614,7 @@ sub authorize {
                                }
 
                                # Extract content
                                }
 
                                # Extract content
-                               my $content = decode_json($res->content);
+                               my $content = from_json($res->content);
 
                                # Save status
                                if ($content->{status} ne 'pending') {
 
                                # Save status
                                if ($content->{status} ne 'pending') {
@@ -611,14 +627,14 @@ sub authorize {
        # Load config if available
        my $config = undef;
        if (
        # Load config if available
        my $config = undef;
        if (
-               #XXX: use eval to workaround a fatal in decode_json
+               #XXX: use eval to workaround a fatal in from_json
                defined eval {
                        # Check that file exists
                        -f CONFIG &&
                        # Read it
                        ($config = read_file(CONFIG)) &&
                        # Decode it
                defined eval {
                        # Check that file exists
                        -f CONFIG &&
                        # Read it
                        ($config = read_file(CONFIG)) &&
                        # Decode it
-                       ($config = decode_json($config)) &&
+                       ($config = from_json($config)) &&
                        # Check defined
                        $config->{thumbprint}
                }
                        # Check defined
                        $config->{thumbprint}
                }
@@ -636,7 +652,6 @@ sub authorize {
                #       my $res = $self->_post($self->{challenges}{$_}{http_uri}, {resource => 'authz', status => 'deactivated'});
                #       # Handle error
                #       unless ($res->is_success) {
                #       my $res = $self->_post($self->{challenges}{$_}{http_uri}, {resource => 'authz', status => 'deactivated'});
                #       # Handle error
                #       unless ($res->is_success) {
-               #               print Dumper($res);
                #               confess 'POST '.$self->{challenges}{$_}{http_uri}.' failed: '.$res->status_line;
                #       }
                #} map { $self->{challenges}{$_}{status} eq 'valid' ? $_ : () } keys %{$self->{challenges}};
                #               confess 'POST '.$self->{challenges}{$_}{http_uri}.' failed: '.$res->status_line;
                #       }
                #} map { $self->{challenges}{$_}{status} eq 'valid' ? $_ : () } keys %{$self->{challenges}};
@@ -669,7 +684,6 @@ sub issue {
 
        # Handle error
        unless ($res->is_success) {
 
        # Handle error
        unless ($res->is_success) {
-               #print Dumper($res);
                confess 'POST '.$self->{'new-cert'}.' failed: '.$res->status_line;
        }
 
                confess 'POST '.$self->{'new-cert'}.' failed: '.$res->status_line;
        }