This file is indexed.

/usr/share/perl5/Protocol/ACME.pm is in libprotocol-acme-perl 1.01-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
package Protocol::ACME;

use 5.007003;
use strict;
use warnings;


our $VERSION = '1.01';

=head1 NAME

Protocol::ACME - Interface to the Let's Encrypt ACME API

=head1 VERSION

Version 1.01

=head1 SYNOPSIS

 use Protocol::ACME;

 my @names = qw( www.example.com cloud.example.com );

 my $challenges = {
                    'www.example.com'   => Protocol::ACME::Challenge::SimpleSSH->new(
                      { ssh_host => "host1", www_root => "~/www" }
                    ),
                   'cloud.example.com' => Protocol::ACME::Challenge::SimpleSSH->new(
                     { ssh_host => "home2", www_root => "/opt/local/www/htdocs" }
                   )
                 };

 eval
 {
   my $acme = Protocol::ACME->new( host               => $host,
                                   account_key        => $account_key_pem_or_der,
                                 );

   $acme->directory();
   $acme->register();
   $acme->accept_tos();

   for my $domain ( @names )
   {
     $acme->authz( $domain );
     $acme->handle_challenge( $challenges->{$domain} );
     $acme->check_challenge();
     $acme->cleanup_challenge( $challenges->{$domain} );
   }

   my $cert = $acme->sign( $csr_file );
 };
 if ( $@ )
 {
   die if !UNIVERSAL::isa($@, 'Protocol::ACME::Exception');
   die "Error occurred: Status: $@->{status},
                        Detail: $@->{detail},
                        Type:   $@->{type}\n";
 }
 else
 {
   # do something appropriate with the DER encoded cert
   print "Success\n";
 }

=head1 DESCRIPTION

The C<Protocol::ACME> is a class implementing an interface for the
Let's Encrypt ACME API.

The class handles the protocol details behind provisioning a Let's
Encrypt certificate.

=head1 CONSTRUCTOR METHODS

The following constructor methods are available:

=over 4

=item $acme = Protcol::ACME->new( %options )

This method constructs a new C<Protocl::ACME> object and returns it.
Key/value pair arguments may be provided to set up the initial state.
The may be passed in as a hash or a hashref. The following options
correspond to attribute methods described below. Items marked with
a * are required.

   KEY                     DEFAULT
   -----------             --------------------
   *host                   undef
   account_key             undef
   openssl                 undef
   ua                      HTTP::Tiny->new()
   loglevel                error
   debug                   0
   mailto                  undef

B<host>: The API end point to connect to.  This will generally be acme-staging.api.letsencrypt.org
or acme-v01.api.letsencrypt.org

B<account_key>: The account private key in a scalar ref or filename.  See C<$self->account_key>
for details on this argument.

B<openssl>: The path to openssl.  If this option is used a local version of the openssl binary will
be used for crypto operations rather than C<Crypt::OpenSSL::RSA>.

B<ua>: An HTTP::Tiny object customized as you see fit

B<loglevel>: Set the loglevel to one of the C<Log::Any> values.

B<debug>: If set to non-zero this is a shortcut for C<loglevel => debug>

B<mailto>: This should be the email address that you want associated with your account.  This is used
my Let's Encrypt for expiration notification.

=back

=head2 METHODS

=over

=item account_key( $key_filename )

=item account_key( \$buffer )

=item account_key( \%explicit_args )


C<account_key> will load a the private account key if it was not already loaded
when the C<pProtocol::ACME> object was constructed.  There are three ways to call this:

If the arg is a B<SCALAR> it is assumed to be the filename of the
key.  C<account_key> will throw an error if there are problems reading the file.

If the arg is a B<SCALAR> reference it is assumed to be a buffer that
contains the KEY.

If the arg is a B<HASH> reference it contains named arguments.  The arguments
are:

   KEY          DEFAUL        DESC
   -----------  -----------   -------------------
   filename     undef         The key Filename
   buffer       undef         Buffer containing the key
   format       undef         Explicitly state the format ( DER | PEM )

If both C<filename> and C<buffer> are set the C<buffer> argument will be ignored.

If the format is not explcitly set C<Protocol::ACME> will look at the key and
try and determine what the format it.


=item load_key_from_disk( $key_path )

B<DEPRECATED>

Load a key from disk.  Currently the key needs to be unencrypted.
Callbacks for handling password protected keys are still to come.

=item directory()

Loads the directory from the ACME host.  This call must be made first
before any other calls to the API in order the bootstrap the API
resource list.

=item register( %args )

Call the new-reg resource and create an account associated with the
loaded account key.  If that key has already been registered this method
will gracefully and silently handle that.

Arguments that can be passed in:

   KEY                     DEFAULT
   -----------             --------------------
   mailto                  undef

B<mailto>: See C<new> for a desciption.  This will override the value passed to new
if any.


=item accept_tos()

In order to use the Let's Encrypt service, the account needs to accept
the Terms of Service.  This is provided in a link header in response
to the new-reg ( or reg ) resource call.  If the TOS have already been
accepted as indicated by the reg structure returned by the API this
call will be a noop.

=item authz( $domain )

C<authz> needs to be called for each domain ( called identifiers in
ACME speak ) in the certificate.  This included the domain in the subject
as well as the Subject Alternate Name (SAN) fields.  Each call to
C<authz> will result in a challenge being issued from Let's Encrypt.
These challenges need to be handled individually.

=item handle_challenge( $challenge_object )

C<handle_challenge> is called for each challenge issued by C<authz>.
The challenge object must be a subclass of C<Protocol::ACME::Challenge>
which implements a 'handle' method.  This objects handle method
will be passed three arguments and is expected to fulfill the
preconditions for the chosen challenge.  The three areguments
are:

  fingerprint: the sha256 hex digest of the account key
  token: the challenge token
  url: the url returned by the challenge

Fully describing how to handle every challenge type of out of the
scope of this documentation ( at least for now ).  Two challenge
classes have been included for reference:

C<Protocol::ACME::Challenge::SimpleSSH> is initialized with the
ssh host name and the www root for the web server for the http-01
challenge.  It will ssh to the host and create the file in
the correct location for challenge fulfillment.

C<Protocol::ACME::Challenge::LocalFile> is initialized with just the
www root for the web server for the http-01 challenge.  It will
simply create the challenge file in the correct place on the local
filesystem.

C<Protocol::ACME::Challenge::Manual> is intended to be run in an
interactive manner and will stop and prompt the user with the relevant
information so they can fulfill the challenge manually.

but below is an example for handling the simpleHTTP ( http-01 )
challenge.


=item check_challenge()

Called after C<handle_challenge>.  This will poll the challenge status
resource and will return when the state changes from 'pending'.

=item cleanup_challenge()

Called after C<check_challenge> to remove the challenge files.

=item $cert = sign( $csr_filename )

=item $cert = sign( \$buffer )

=item $cert = sign( \%explicit_args )


Call C<sign> after the challenge for each domain ( itentifier ) has
been fulfilled.  There are three ways to call this:

If the arg is a B<SCALAR> it is assumed to be the filename of the
CSR.  C<sign> will throw an error if there are problems reading the file.

If the arg is a B<SCALAR> reference it is assumed to be a buffer that
contains the CSR.

If the arg is a B<HASH> reference it contains named arguments.  The arguments
are:

   KEY          DEFAUL        DESC
   -----------  -----------   -------------------
   filename     undef         The CSR Filename
   buffer       undef         Buffer containing the CSR
   format       undef         Explicitly state the format ( DER | PEM )

If both C<filename> and C<buffer> are set the C<buffer> argument will be ignored.

If the format is not explcitly set Protocol::ACME will look at the CSR and
try and determine what the format it.

On success C<Protocol::ACME> will return the DER encoded signed certificate.

=item $cert_chain = chain()

After C<sign> has been called and a cert successfully created, C<chain> will
fetch and return the DER encoded certificate issuer.

=item revoke( $certfile )

Call C<revoke> to revoke an already issued certificate. C<$certfile>
must point the a DER encoded form of the certificate.

=item recovery_key()

LE does not yet support recovery keys.  This method will die when
called.


=back

=cut

package Protocol::ACME;

use strict;
use warnings;

use Protocol::ACME::Exception;
use Protocol::ACME::Utils;

use Crypt::Format;
use Crypt::RSA::Parse ();

use MIME::Base64 qw( encode_base64url decode_base64url decode_base64 encode_base64 );

use HTTP::Tiny;
use JSON;
use Digest::SHA qw( sha256 );
use Carp;


my $USERAGENT = "Protocol::ACME v$VERSION";
my $NONCE_HEADER = "replay-nonce";

sub new
{
  my $class = shift;
  my $self = {};
  bless $self, $class;
  $self->_init( @_ );
  return $self;
}

sub _init
{
  my $self = shift;

  my $args;

  if ( ref $_[0] eq "HASH" )
  {
    $args = $_[0];
  }
  else
  {
    %$args = @_;
  }

  # TODO: There are more elegant and well baked ways to take care of the
  #       parameter handling that I am doing here
  $self->{host}     = $args->{host}    if exists $args->{host};
  $self->{ua}       = $args->{ua}      if exists $args->{ua};
  $self->{openssl}  = $args->{openssl} if exists $args->{openssl};
  $self->{debug}    = $args->{debug}   if exists $args->{debug};
  $self->{loglevel} = exists $args->{loglevel} ? $args->{loglevel} : "error";
  $self->{contact}->{mailto} = $args->{mailto} if exists $args->{mailto};

  if ( $self->{debug} )
  {
    $self->{loglevel} = "debug";
  }

  if ( ! exists $self->{ua} )
  {
    $self->{ua} = HTTP::Tiny->new( agent => $USERAGENT, verify_SSL => 1 );
  }

  if ( ! exists $self->{host} )
  {
    _throw( detail => "host parameter is required for Protocol::ACME::new" );
  }

  $self->{log} = $args->{'logger'} || do
  {
      require Log::Any::Adapter;
      Log::Any::Adapter->set('+Protocol::ACME::Logger', log_level => $self->{loglevel});
      Log::Any->get_logger;
  };

  if ( exists $args->{account_key} )
  {
    $self->account_key( $args->{account_key} );
  }

  $self->{links}->{directory} = "https://" . $self->{host} . '/directory';

  $self->{nonce} = undef;


}

sub _throw
{
  my (@args) = @_;
  if ( scalar(@_) == 1 )
  {
    @args = ( detail => $_[0] );
  }
  croak ( Protocol::ACME::Exception->new( { @args } ) );
}

sub load_key
{
  my ($self, $keystring) = @_;
  return $self->account_key( \$keystring );
}

sub load_key_from_disk
{
  my $self   = shift;
  my $path   = shift;

  return $self->account_key($path);
}

sub account_key
{
  my $self = shift;
  my $key = shift;

  my %args = ( filename => undef,
               buffer   => undef,
               format   => undef );

  if ( ! ref $key )
  {
    $args{filename} = $key;

    if ( ! -f $key )
    {
      _throw( "account_key file $key does not exist" );
    }
  }
  elsif( ref $key eq "SCALAR" )
  {
    $args{buffer} = $$key;
  }
  else
  {
    @args{ keys %$key } = values %$key;
  }

  if ( $args{filename} )
  {
    $args{buffer} = _slurp( $args{filename} );
    if ( ! $args{buffer} )
    {
      _throw( "Could not load the account key from file $args{filename}: $!" );
    }
  }

  if ( ! $args{buffer} )
  {
    _throw( "Either an account key buffer or filename must be passed into account_key" );
  }

  if ( ! $args{format} )
  {
    $args{format} = Protocol::ACME::Utils::looks_like_pem( $args{buffer} ) ? "PEM" : "DER";
  }

  my $keystring = $args{buffer};
  # TODO: This should detect/handle PKCS8-formatted private keys as well.
  if ( $args{format} eq "DER" )
  {
    $keystring = Crypt::Format::der2pem( $keystring, "RSA PRIVATE KEY" );
  }

  if ( exists $self->{openssl} )
  {
    require Protocol::ACME::Key;
    $key = Protocol::ACME::Key->new( keystring => $keystring,
                                     openssl   => $self->{openssl} );
  }
  else
  {
    eval
    {
      require Crypt::OpenSSL::RSA;
      require Crypt::OpenSSL::Bignum;
    };
    if ( $@ )
    {
      die "Invoked usage requires Crypt::OpenSSL::RSA and Crypt::OpenSSL::Bignum. " .
      "To avoid these dependencies use the openssl parameter when creating the " .
      "Protocol::ACME object.  This will use a native openssl binary instead.";
    }

    eval
    {
      $key = Crypt::OpenSSL::RSA->new_private_key($keystring);
    };
    if ( $@ )
    {
      _throw( "Error creating a key structure from the account key" );
    }
  }

  if ( ! $key )
  {
    _throw( "Could not load account key into key structure" );
  }

  $key->use_sha256_hash();

  $self->{key}->{key} = $key;

  my ( $n_b64, $e_b64 ) = map { encode_base64url(_bigint_to_binary($_)) } $key->get_key_parameters();
  $self->{key}->{n} = $n_b64;
  $self->{key}->{e} = $e_b64;

  $self->{log}->debug( "Private key loaded" );

}




sub directory
{
  my $self = shift;

  my $resp = $self->_request_get( $self->{links}->{directory} );



  if ( $resp->{status} != 200 )
  {
    _throw( detail => "Failed to fetch the directory for $self->{host}", resp => $resp );
  }

  my $data = _decode_json( $resp->{content} );

  @{$self->{links}}{keys %$data} = values %$data;


  $self->{log}->debug( "Let's Encrypt Directories loaded." );
}

#
# Register the account or load the reg url for an existing account ( new-reg or reg )
#
sub register
{
  my $self = shift;
  my %args = @_;

  my $obj = {};
  $obj->{resource} = 'new-reg';

  if ( exists $args{mailto} )
  {
    push @{$obj->{contact}}, "mailto:$args{mailto}";
  }
  elsif ( exists $self->{contact}->{mailto} )
  {
    push @{$obj->{contact}}, "mailto:$self->{contact}->{mailto}";
  }

  my $msg = _encode_json( $obj );

  my $json = $self->_create_jws( $msg );

  $self->{log}->debug( "Sending registration message" );

  my $resp = $self->_request_post( $self->{links}->{'new-reg'}, $json );

  if ( $resp->{status} == 409 )
  {
    $self->{links}->{'reg'} = $resp->{headers}->{'location'};

    $self->{log}->debug( "Known key used" );
    $self->{log}->debug( "Refetching with location URL" );

    my $json = $self->_create_jws( _encode_json( { "resource" => 'reg' } ) );

    $resp = $self->_request_post( $self->{links}->{'reg'}, $json );

    if ( $resp->{status} == 202 )
    {
      my $links = _link_to_hash( $resp->{headers}->{'link'} );

      @{$self->{links}}{keys %$links} = values %$links;
    }
    else
    {
      _throw( %{ $self->{content} } );
    }
  }
  elsif ( $resp->{status} == 201 )
  {
    my $links = _link_to_hash( $resp->{headers}->{'link'} );

    @{$self->{links}}{keys %$links} = values %$links;

    $self->{links}->{'reg'} = $resp->{headers}->{'location'};
    $self->{log}->debug( "New key used" );
  }
  else
  {
    _throw( %{ $self->{content} } );
  }


  $self->{reg} = $self->{content};
}

sub recovery_key
{
  # LE does not yet support the key recovery resource
  # the below can be considered debug code

  die "Let's Encrypt does not yet support key recovery";

  my $self = shift;

  my $keyfile = shift;


  my $pem = _slurp( $keyfile );
  _throw( "recovery_key: $keyfile: $!" ) if ! $pem;

  my $url = "https://acme-staging.api.letsencrypt.org/acme/reg/101834";

  my $der = Crypt::Format::pem2der( $pem );

  my $pub = Crypt::PK::ECC->new( \$der );

  my $public_json_text = $pub->export_key_jwk('public');

  my $hash = $pub->export_key_jwk( 'public', 1 );

  my $msg = { "resource"     => "reg",
              "recoveryToken" => {
                "client"       => { "kty" => "EC",
                                    "crv" => "P-256",
                                    "x"   => $hash->{x},
                                    "y"   => $hash->{y}
                                  }
              }
            };

  my $json = $self->_create_jws( _encode_json($msg) );

  my $resp = $self->_request_post( $url, $json );

  # TODO: This is not complete
}

sub accept_tos
{
  my $self = shift;

  if ( exists $self->{reg}->{agreement} )
  {
    $self->{log}->debug( "TOS already accepted. Skipping" );
    return;
  }

  $self->{log}->debug( "Accepting TOS" );
  # TODO: check for existance of terms-of-service link
  # TODO: assert on reg url being present

  my $msg = _encode_json( { "resource"  => "reg",
                             "agreement" => $self->{links}->{'terms-of-service'},
                             "key"       => { "e"   => $self->{key}->{e},
                                             "kty" => "RSA",
                                             "n"   => $self->{key}->{n} } } );


  my $json = $self->_create_jws( $msg );

  my $resp = $self->_request_post( $self->{links}->{'reg'}, $json );

  if ( $resp->{status} == 202 )
  {
    $self->{log}->debug( "Accepted TOS" );
  }
  else
  {
    _throw( %{ $self->{content} } );
  }
}

sub revoke
{
  my $self = shift;
  my $certfile = shift;

  $self->{log}->debug( "Revoking Cert" );

  my $cert = _slurp( $certfile );

  if ( ! $cert )
  {
    _throw("revoke: Could not load cert from $certfile: $!");
  }


  my $msg = _encode_json( { "resource"    => "revoke-cert",
                            "certificate" => encode_base64url( $cert ) } );


  my $json = $self->_create_jws( $msg );

  my $resp = $self->_request_post( $self->{links}->{'revoke-cert'}, $json );

  if ( $resp->{status} != 200 )
  {
    _throw( %{ $self->{content} } );
  }

}

sub authz
{
  my $self   = shift;
  my $domain = shift;

  $self->{log}->debug( "Sending authz message for $domain" );
  # TODO: check for 'next' URL and that is it authz

  my $msg = _encode_json( { "identifier" => { "type" => "dns", "value" => $domain },
                            "resource"   => "new-authz" } );

  my $json = $self->_create_jws( $msg );

  my $resp = $self->_request_post( $self->{links}->{next}, $json );

  if ( $resp->{status} == 201 )
  {
    $self->{challenges} = $self->{content}->{challenges};
  }
  else
  {
    _throw( %{ $self->{content} } );
  }
}

sub handle_challenge
{
  my $self      = shift;
  my $challenge = shift;
  my @args = @_;

  my $key = $self->{key};

  my $jwk = _encode_json( { "e" => $key->{e}, "kty" => "RSA", "n" => $key->{n} } );
  my $token;
  my $challenge_url;

  # TODO: this is feeling hardcoded and messy - and fragile
  #       how do we handle other auth challenges?
  #       This is hardcoded for http-01
  for ( @{$self->{challenges}} )
  {
    if ( $_->{type} eq "http-01" )
    {
      $token = $_->{token};
      $challenge_url = $_->{uri};
    }
  }


  my $fingerprint = encode_base64url( sha256( $jwk ) );

  $self->{log}->debug( "Handing challenge for token: $token.$fingerprint" );

  my $ret = $challenge->handle( $token, $fingerprint, @args );

  if ( $ret == 0 )
  {
    $self->{fingerprint} = $fingerprint;
    $self->{token} = $token;
    $self->{links}->{challenge} = $challenge_url;
  }
  else
  {
    _throw( status => 0, detail => "Error in handling challenge: $ret", type => "challenge_exec" );
  }
}


sub check_challenge
{
  my $self = shift;

  my $msg = _encode_json( { "resource" => "challenge", "keyAuthorization" => $self->{token} . '.' . $self->{fingerprint} } );

  my $json = $self->_create_jws( $msg );


  my $resp = $self->_request_post( $self->{links}->{challenge}, $json );

  my $status_url = $self->{content}->{uri};

  # TODO: check for failure of challenge check
  # TODO: check for other HTTP failures

  $self->{log}->debug( "Polling for challenge fulfillment" );
  while( 1 )
  {
    $self->{log}->debug( "Status: $self->{content}->{status}" );
    if ( $self->{content}->{status} eq "pending" )
    {
      sleep(2);
      $resp = $self->_request_get( $status_url );
    }
    elsif ( $self->{content}{status} eq "invalid" )
    {
      _throw(%{ $self->{content} });
    }
    else
    {
      last;
    }
  }
}

sub cleanup_challenge
{
  my $self = shift;
  my $challenge = shift;
  return $challenge->cleanup();
}

sub sign
{
  my $self = shift;
  my $csr = shift;

  $self->{log}->debug( "Signing" );

  my %args = ( filename => undef,
               buffer   => undef,
               format   => undef );

  if ( ! ref $csr )
  {
    $args{filename} = $csr;
  }
  elsif( ref $csr eq "SCALAR" )
  {
    $args{buffer} = $$csr;
  }
  else
  {
    @args{keys %$csr} = values %$csr;
  }

  if ( $args{filename} )
  {
    $args{buffer} = _slurp( $args{filename} );
    if ( ! $args{buffer} )
    {
      _throw( "Could not load CSR from file $args{filename}" );
    }
  }

  if ( ! $args{buffer} )
  {
    _throw( "Either a buffer or filename must be passed to sign" );
  }

  if ( ! $args{format} )
  {
    $args{format} = Protocol::ACME::Utils::looks_like_pem( $args{buffer} ) ? "PEM" : "DER";
  }

  my $der = $args{format} eq "DER" ? $args{buffer} : Crypt::Format::pem2der( $args{buffer} );

  my $msg = _encode_json( { "resource" => "new-cert", "csr" => encode_base64url( $der ) } );

  my $json = $self->_create_jws( $msg );

  my $resp = $self->_request_post( $self->{links}->{'new-cert'}, $json, 1 );

  if ( $resp->{status} != 201 )
  {
    _throw( %{_decode_json($resp->{content}) } );
  }

  my $links = _link_to_hash( $resp->{headers}->{'link'} );

  $self->{links}->{chain} = $links->{up} if exists $links->{up};
  $self->{links}->{cert}  = $resp->{headers}->{location} if exists $resp->{headers}->{location};

  $self->{cert} = $resp->{content};

  return $self->{cert};
}

sub chain
{
  my $self = shift;

  if ( ! exists $self->{links}->{chain} )
  {
    _throw( "URL for the cert chain missing.  Has sign() been called yet?" );
  }

  my $resp = $self->_request_get( $self->{links}->{chain}, 1 );

  if ( $resp->{status} != 200 )
  {
    _throw( detail => "Error received fetching the certificate chain",
            status => $resp->{status}  );
  }

  $self->{chain} = $resp->{content};

  return $self->{chain};
}

#############################################################
### "Private" functions

sub _request_get
{
  my $self = shift;
  my $url  = shift;
  my $nodecode = shift || 0;

  my $resp = $self->{ua}->get( $url );

  $self->{nonce} = $resp->{headers}->{$NONCE_HEADER};
  $self->{json} = $resp->{content};

  #Exception here should be fatal.
  $self->{content} = undef;
  $self->{content} = _decode_json( $resp->{content} ) unless $nodecode;

  $self->{response} = $resp;

  return $resp;
}

sub _request_post
{
  my $self     = shift;
  my $url      = shift;
  my $content  = shift;
  my $nodecode = shift || 0;

  my $resp = $self->{ua}->post( $url, { content => $content } );

  $self->{nonce} = $resp->{headers}->{$NONCE_HEADER};

  $self->{json} = $resp->{content};

  #Let exception from decode_json() propagate:
  #if we failed to decode the JSON, that’s a show-stopper.
  $self->{content} = undef;
  $self->{content} = _decode_json( $resp->{content} ) unless $nodecode;

  $self->{response} = $resp;

  return $resp;
}

sub _create_jws
{
  my $self = shift;

  my $msg = shift;
  return _create_jws_internal( $self->{key}, $msg, $self->{nonce} );
}


#############################################################
### Helper functions - not class methods

sub _slurp
{
  my $filename = shift;

  open my $fh, '<', $filename or return undef;

  sysread( $fh, my $content, -s $fh ) or return undef;

  return $content;
}


sub _link_to_hash
{
  my $arrayref = shift;
  my $links;

  return {} unless $arrayref;

  if ( ! ref $arrayref )
  {
    $arrayref = [ $arrayref ];
  }

  for my $link ( @$arrayref )
  {
    my ( $value, $key ) = split( ';', $link );
    my ($url) = $value =~ /<([^>]*)>/;
    my ($rel) = $key =~ /rel=\"([^"]*)"/;

    if ( $url && $rel )
    {
      $links->{$rel} = $url;
    }
    else
    {
      # TODO: Something wonderful
    }
  }

  return $links;
}

sub _bigint_to_binary {
    my ( $bigint ) = @_;

    # TODO: Inelegant hack to deal with different Bignum implementations
    my $hex;
    if ( UNIVERSAL::isa( $bigint, "Math::BigInt" ) )
    {
      $hex = substr( $bigint->as_hex(), 2 );
      #Prefix a 0 as needed to get an even number of digits.
      if (length($hex) % 2) {
        substr( $hex, 0, 0, 0 );
      }

      return pack 'H*', $hex;
    }
    else
    {
      $bigint->to_bin();
    }

}

sub _create_jws_internal
{
  my $key = shift;
  my $msg = shift;
  my $nonce = shift;

  my $protected_header = '{"nonce": "' . $nonce . '"}';

  my $sig = encode_base64url( $key->{key}->sign( encode_base64url($protected_header) . "." . encode_base64url($msg) ) );

  my $jws = { header    => { alg => "RS256", jwk => { "e" => $key->{e}, "kty" => "RSA", "n" => $key->{n} } },
              protected => encode_base64url( $protected_header ),
              payload   => encode_base64url( $msg ),
              signature => $sig };

  my $json = _encode_json( $jws );

  return $json;

}

sub _decode_json
{
  my $ref = shift;

  my $json = "";

  eval
  {
    $json = JSON->new->allow_nonref->decode($ref);
  };

  return $json;
}

sub _encode_json
{
  my $ref = shift;
#  my $json = JSON->new();
#  $json->canonical();
  #  return $json->encode($ref);
  return JSON->new->canonical->encode($ref);
}


=head1 AUTHOR

Stephen Ludin, C<< <sludin at ludin.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-protocol-acme at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Protocol-ACME>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 REPOSITORY

https://github.com/sludin/Protocol-ACME


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Protocol::ACME


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Protocol-ACME>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Protocol-ACME>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Protocol-ACME>

=item * Search CPAN

L<http://search.cpan.org/dist/Protocol-ACME/>

=back


=head1 CONTRIBUTORS

Felipe Gasper, C<< <felipe at felipegasper.com> >>

=head1 ACKNOWLEDGEMENTS



=head1 LICENSE AND COPYRIGHT

Copyright 2015 Stephen Ludin.

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:

L<http://www.perlfoundation.org/artistic_license_2_0>

Any use, modification, and distribution of the Standard or Modified
Version 1.01
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


=cut

1; # End of Protocol::ACME