Skip to content

Commit 432be85

Browse files
committed
added export method to PublicKey
1 parent b98f8ad commit 432be85

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

lib/Crypt/Cryptoki/Object.pm

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,56 @@ package Crypt::Cryptoki::Object;
22
use strict;
33
use Moo;
44
use Carp;
5+
use List::MoreUtils qw(zip);
56

67
use Crypt::Cryptoki::Raw qw(rv_to_str CKR_OK);
78

89
has 'session' => ( is => 'ro', required => 1 );
910
has 'id' => ( is => 'ro', required => 1 );
11+
has 'attributes' => ( is => 'lazy' );
12+
13+
sub _attribute_map {{
14+
}}
15+
16+
sub _build_attributes {
17+
my ( $self ) = @_;
18+
my @attr_names = keys %{$self->_attribute_map};
19+
my @attrs = $self->get_attributes(@attr_names);
20+
return { zip @attr_names, @attrs };
21+
}
22+
23+
sub hex_attributes {
24+
my ( $self ) = @_;
25+
my $attrs = $self->attributes;
26+
+{ map { $_ => unpack('H*',$attrs->{$_}) } keys %$attrs };
27+
}
1028

1129
sub destroy {
12-
my ( $self ) = @_;
13-
my $rv = $self->session->slot->ctx->_fl->C_DestroyObject($self->session->id,$self->id);
14-
if ( $rv != CKR_OK ) {
15-
croak rv_to_str($rv);
16-
}
17-
return 1;
30+
my ( $self ) = @_;
31+
my $rv = $self->session->slot->ctx->_fl->C_DestroyObject($self->session->id,$self->id);
32+
if ( $rv != CKR_OK ) {
33+
croak rv_to_str($rv);
34+
}
35+
return 1;
36+
}
37+
38+
sub get_attributes {
39+
my ( $self, @attributes ) = @_;
40+
41+
my @get_attributes_template;
42+
for (@attributes) {
43+
exists $self->_attribute_map->{$_} or croak 'illegal attribute';
44+
push @get_attributes_template, [ $self->_attribute_map->{$_}, '' ];
45+
}
46+
47+
my $rv = $self->session->slot->ctx->_fl->C_GetAttributeValue(
48+
$self->session->id,$self->id,\@get_attributes_template
49+
);
50+
if ( $rv != CKR_OK ) {
51+
croak rv_to_str($rv);
52+
}
53+
54+
return map { ''.$_->[1] } @get_attributes_template;
1855
}
1956

2057
1;

lib/Crypt/Cryptoki/PublicKey.pm

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,55 @@ use Moo;
44
extends 'Crypt::Cryptoki::Key';
55
use Carp;
66

7-
use Crypt::Cryptoki::Raw qw(rv_to_str CKR_OK CKM_RSA_PKCS NULL_PTR);
7+
use Crypt::Cryptoki::Raw qw(rv_to_str CKR_OK NULL_PTR
8+
CKM_RSA_PKCS
9+
CKA_MODULUS CKA_PUBLIC_EXPONENT
10+
);
11+
12+
sub _attribute_map {{
13+
modulus => CKA_MODULUS,
14+
public_exponent => CKA_PUBLIC_EXPONENT
15+
}};
16+
17+
sub export_as_string {
18+
my ( $self ) = @_;
19+
my $attrs = $self->attributes;
20+
require Crypt::OpenSSL::Bignum;
21+
require Crypt::OpenSSL::RSA;
22+
my $n = Crypt::OpenSSL::Bignum->new_from_bin($attrs->{modulus});
23+
my $e = Crypt::OpenSSL::Bignum->new_from_bin($attrs->{public_exponent});
24+
my $rsa_pub = Crypt::OpenSSL::RSA->new_key_from_parameters($n,$e);
25+
$rsa_pub->use_pkcs1_padding;
26+
$rsa_pub->get_public_key_string;
27+
}
828

929
sub encrypt {
10-
my ( $self, $plain_text_ref, $plain_text_len ) = @_;
30+
my ( $self, $plain_text_ref, $plain_text_len ) = @_;
1131

12-
my $rv = $self->session->slot->ctx->_fl->C_EncryptInit(
32+
my $rv = $self->session->slot->ctx->_fl->C_EncryptInit(
1333
$self->session->id,
1434
[ CKM_RSA_PKCS, NULL_PTR, 0 ],
1535
$self->id
1636
);
17-
if ( $rv != CKR_OK ) {
18-
croak rv_to_str($rv);
19-
}
37+
if ( $rv != CKR_OK ) {
38+
croak rv_to_str($rv);
39+
}
2040

2141
my $encrypted_text = '';
2242
my $encrypted_text_len = -1;
2343

24-
$rv = $self->session->slot->ctx->_fl->C_Encrypt(
44+
$rv = $self->session->slot->ctx->_fl->C_Encrypt(
2545
$self->session->id,
2646
$$plain_text_ref,
2747
$plain_text_len,
2848
$encrypted_text,
2949
$encrypted_text_len
3050
);
31-
if ( $rv != CKR_OK ) {
32-
croak rv_to_str($rv);
33-
}
51+
if ( $rv != CKR_OK ) {
52+
croak rv_to_str($rv);
53+
}
3454

35-
return ( \$encrypted_text, $encrypted_text_len );
55+
return ( \$encrypted_text, $encrypted_text_len );
3656
}
3757

3858
1;

t/cryptoki.t

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use strict;
22
use warnings;
33

44
use Test::More;
5+
use Try::Tiny;
56

67
use_ok 'Crypt::Cryptoki';
78
use_ok 'Crypt::Cryptoki::Template';
@@ -52,13 +53,18 @@ my $t_private = Crypt::Cryptoki::Template->new(
5253

5354
my ( $public_key, $private_key ) = $session->generate_key_pair($t_public,$t_private);
5455

55-
my $plain_text = 'plain text';
56-
my ( $encrypted_text_ref, $len ) = $public_key->encrypt(\$plain_text, length($plain_text));
56+
try {
57+
my $plain_text = 'plain text';
58+
my ( $encrypted_text_ref, $len ) = $public_key->encrypt(\$plain_text, length($plain_text));
5759

58-
my ( $encrypted_text_ref ) = $private_key->decrypt($encrypted_text_ref, $len);
59-
diag $$encrypted_text_ref;
60+
my ( $encrypted_text_ref ) = $private_key->decrypt($encrypted_text_ref, $len);
61+
diag $$encrypted_text_ref;
6062

61-
$public_key->destroy;
62-
$private_key->destroy;
63+
diag explain $public_key->hex_attributes;
64+
diag $public_key->export_as_string;
65+
} catch { diag $_; 0 };
66+
67+
ok $public_key->destroy;
68+
ok $private_key->destroy;
6369

6470
done_testing();

0 commit comments

Comments
 (0)