App-CamelPKI

 view release on metacpan or  search on metacpan

lib/App/CamelPKI/Certificate.pm  view on Meta::CPAN

=cut

sub get_public_key {
    my ($self) = @_;
    App::CamelPKI::PublicKey->parse
        ($self->_as_x509_cached->get_public_key->to_PEM);
}

=head2 equals($cert)

Returns true only if $cert, another object of the same 
I<App::CamelPKI::Certificate> classe, modelise the same certificate.

=cut

sub equals {
    my ($self, $other) = @_;
    return ($other->isa(ref($self)) &&
            $self->{der} eq $other->{der});
}

=begin internals

=head2 _as_x509_cached()

As L</as_crypt_openssl_ca_x509>, but using cache. This method is private
because, the returned object being mutable, it must not be shared under
penalty of creating a subliminal canal between owners of a reference on the
same I<App::CamelPKI::Certificate> object.

=cut

sub _as_x509_cached {
    my ($self) = @_;
    $self->{cocx} ||= $self->as_crypt_openssl_ca_x509;
}


require My::Tests::Below unless caller;
1;

__END__

=head1 TEST SUITE

=cut

use Test::More qw(no_plan);
use Test::Group;
use File::Slurp;
use App::CamelPKI::Error;
use JSON;

my $certificate = <<"CERTIFICATE";
-----BEGIN CERTIFICATE-----
MIICsDCCAhmgAwIBAgIJANdqtXzdPS/1MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMDcwMTMxMTcwNjU5WhcNMzcwMTMxMTcwNjU5WjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQDfGYmlOYbpGkBD/agTUnixLcdh6H1XM13w17RbzaoA7byZD6L+Dn8MZd69PuXc
ZAQEUG4Oe6QyAcafsvDb7SHjyJHLoPTOsAZ0ex/0zIJVpw+XyppA8fZx6bnuHKUa
bqfj83OLk/ACfQSBX7bcL7Y8hwYcZJcqyjMzt9BT7oCldwIDAQABo4GnMIGkMB0G
A1UdDgQWBBTu+qGX79xcvFE8pG5zx2FcqAuV5TB1BgNVHSMEbjBsgBTu+qGX79xc
vFE8pG5zx2FcqAuV5aFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJANdqtXzd
PS/1MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEACQ+4e3MSlcqkhzgZ
rTXpsO/WpBT7aaM7AaecY54hB9uF9PmGC1q3axwZ2b/+Gh5ehQPyAwKevyjNz1y4
yP4YeUHO6FIHd0RyGEnM3cqcoqg8TewXlUwOkHphCrZ5eFbxxEarVz1wwkZqd5z0
3IInE3EJ7D8rxfbC1c1fdeh8akI=
-----END CERTIFICATE-----
CERTIFICATE

test "round trip" => sub {
    is(App::CamelPKI::Certificate->parse($certificate)->serialize(),
       $certificate, "round trip");
};

test "->parse() with bad arguments" => sub {
    try {
        App::CamelPKI::Certificate->parse();
        fail;
    } catch App::CamelPKI::Error::Internal with {
        pass;
    };

    try {
        App::CamelPKI::Certificate->parse(undef);
        fail;
    } catch App::CamelPKI::Error::Internal with {
        pass;
    };

	#TODO: verifier pourquoi ca marche pas
#    try {
#        App::CamelPKI::Certificate->parse(JSON::from_json("null"));
#        fail;
#    } catch App::CamelPKI::Error::Internal with {
#        pass;
#    };
};

use App::CamelPKI::Test qw(%test_entity_certs %test_public_keys
                      %test_rootca_certs
                     certificate_chain_ok);
test "equals" => sub {
    my ($cert1, $cert1too, $cert2) = map {
        App::CamelPKI::Certificate->parse($test_entity_certs{$_})
    } (qw(rsa1024 rsa1024 rsa2048));
    ok(! ($cert1 == $cert1too));
    ok($cert1->equals($cert1));
    ok($cert1->equals($cert1too));
    ok($cert2->equals($cert2));
    ok(! $cert1->equals($cert2));
};

test "load" => sub {
	my $tmpCert = My::Tests::Below->tempdir."/certtemp";
	write_file($tmpCert, $certificate);
	my $cert = App::CamelPKI::Certificate->load($tmpCert);
	ok($cert->isa('App::CamelPKI::Certificate'));



( run in 0.868 second using v1.01-cache-2.11-cpan-64ef6c95b5d )