Codebase list libcryptx-perl / 6565fc9
Crypt::PK::ECC+RSA export_key_jwk() allows to export a perl HASH with JWK structure Karel Miko 8 years ago
4 changed file(s) with 83 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
439439 }
440440
441441 sub export_key_jwk {
442 my ($self, $type) = @_;
442 my ($self, $type, $wanthash) = @_;
443443 my $kh = $self->key2hash;
444444 my $curve = $self->_curve_name_lookup($kh);
445445 $curve = 'P-192' if $curve =~ /(secp192r1|nistp192|prime192v1)/;
453453 $kh->{$_} = "0$kh->{$_}" if length($kh->{$_}) % 2;
454454 }
455455 # NOTE: x + y are not necessary in privkey
456 # but they are used in https://tools.ietf.org/html/draft-ietf-jose-json-web-key-41#appendix-A.2
457 return sprintf '{"kty":"EC","crv":"%s","x":"%s","y":"%s","d":"%s"}',
458 $curve,
459 encode_base64url(pack("H*", $kh->{pub_x})),
460 encode_base64url(pack("H*", $kh->{pub_y})),
461 encode_base64url(pack("H*", $kh->{k}));
456 # but they are used in https://tools.ietf.org/html/rfc7517#appendix-A.2
457 my $hash = {
458 kty => "EC", crv=>$curve,
459 x => encode_base64url(pack("H*", $kh->{pub_x})),
460 y => encode_base64url(pack("H*", $kh->{pub_y})),
461 d => encode_base64url(pack("H*", $kh->{k})),
462 };
463 return $wanthash ? $hash : encode_json($hash);
462464 }
463465 elsif ($type && $type eq 'public') {
464466 return unless $kh->{pub_x} && $kh->{pub_y};
465467 for (qw/pub_x pub_y/) {
466468 $kh->{$_} = "0$kh->{$_}" if length($kh->{$_}) % 2;
467469 }
468 return sprintf '{"kty":"EC","crv":"%s","x":"%s","y":"%s"}',
469 $curve,
470 encode_base64url(pack("H*", $kh->{pub_x})),
471 encode_base64url(pack("H*", $kh->{pub_y}));
470 my $hash = {
471 kty => "EC", crv=>$curve,
472 x => encode_base64url(pack("H*", $kh->{pub_x})),
473 y => encode_base64url(pack("H*", $kh->{pub_y})),
474 };
475 return $wanthash ? $hash : encode_json($hash);
472476 }
473477 }
474478
494498 return $self->_import_hex($key->{x}, $key->{y}, $key->{d}, $curve);
495499 }
496500 }
497 croak "FATAL: unexpected key hash";
501 croak "FATAL: unexpected ECC key hash";
498502 }
499503
500504 my $data;
863867
864868 Supported key formats:
865869
870 # all formats can be loaded from a file
871 my $pk = Crypt::PK::ECC->new($filename);
872
873 # or from a buffer containing the key
874 my $pk = Crypt::PK::ECC->new(\$buffer_with_key);
875
866876 =over
867877
868878 =item * EC private keys with with all curve parameters
10251035
10261036 =head2 export_key_jwk
10271037
1028 Exports public/private keys as a JSON Web Key.
1038 Exports public/private keys as a JSON Web Key (JWK).
10291039
10301040 my $private_json_text = $pk->export_key_jwk('private');
10311041 #or
10321042 my $public_json_text = $pk->export_key_jwk('public');
1043
1044 Also exports public/private keys as a perl HASH with JWK structure.
1045
1046 my $jwk_hash = $pk->export_key_jwk('private', 1);
1047 #or
1048 my $jwk_hash = $pk->export_key_jwk('public', 1);
10331049
10341050 =head2 export_key_raw
10351051
3838 }
3939
4040 sub export_key_jwk {
41 my ($self, $type) = @_;
41 my ($self, $type, $wanthash) = @_;
4242 my $kh = $self->key2hash;
4343 if ($type eq 'private') {
4444 return unless $kh->{N} && $kh->{e} && $kh->{d} && $kh->{p} && $kh->{q} && $kh->{dP} && $kh->{dQ} && $kh->{qP};
4545 for (qw/N e d p q dP dQ qP/) {
4646 $kh->{$_} = "0$kh->{$_}" if length($kh->{$_}) % 2;
4747 }
48 return sprintf '{"kty":"RSA","n":"%s","e":"%s","d":"%s","p":"%s","q":"%s","dp":"%s","dq":"%s","qi":"%s"}',
49 encode_base64url(pack("H*", $kh->{N})),
50 encode_base64url(pack("H*", $kh->{e})),
51 encode_base64url(pack("H*", $kh->{d})),
52 encode_base64url(pack("H*", $kh->{p})),
53 encode_base64url(pack("H*", $kh->{q})),
54 encode_base64url(pack("H*", $kh->{dP})),
55 encode_base64url(pack("H*", $kh->{dQ})),
56 encode_base64url(pack("H*", $kh->{qP}));
48 my $hash = {
49 kty => "RSA",
50 n => encode_base64url(pack("H*", $kh->{N})),
51 e => encode_base64url(pack("H*", $kh->{e})),
52 d => encode_base64url(pack("H*", $kh->{d})),
53 p => encode_base64url(pack("H*", $kh->{p})),
54 q => encode_base64url(pack("H*", $kh->{q})),
55 dp => encode_base64url(pack("H*", $kh->{dP})),
56 dq => encode_base64url(pack("H*", $kh->{dQ})),
57 qi => encode_base64url(pack("H*", $kh->{qP})),
58 };
59 return $wanthash ? $hash : encode_json($hash);
5760 }
5861 elsif ($type eq 'public') {
5962 return unless $kh->{N} && $kh->{e};
6063 for (qw/N e/) {
6164 $kh->{$_} = "0$kh->{$_}" if length($kh->{$_}) % 2;
6265 }
63 return sprintf '{"kty":"RSA","n":"%s","e":"%s"}',
64 encode_base64url(pack("H*", $kh->{N})),
65 encode_base64url(pack("H*", $kh->{e}));
66 my $hash = {
67 kty => "RSA",
68 n => encode_base64url(pack("H*", $kh->{N})),
69 e => encode_base64url(pack("H*", $kh->{e})),
70 };
71 return $wanthash ? $hash : encode_json($hash);
6672 }
6773 }
6874
8389 }
8490 return $self->_import_hex($key->{n}, $key->{e}, $key->{d}, $key->{p}, $key->{q}, $key->{dp}, $key->{dq}, $key->{qi});
8591 }
92 croak "FATAL: unexpected RSA key hash";
8693 }
8794
8895 my $data;
365372 });
366373
367374 Supported key formats:
375
376 # all formats can be loaded from a file
377 my $pk = Crypt::PK::RSA->new($filename);
378
379 # or from a buffer containing the key
380 my $pk = Crypt::PK::RSA->new(\$buffer_with_key);
368381
369382 =over
370383
536549
537550 =head2 export_key_jwk
538551
539 Exports public/private keys as a JSON Web Key.
552 Exports public/private keys as a JSON Web Key (JWK).
540553
541554 my $private_json_text = $pk->export_key_jwk('private');
542555 #or
543556 my $public_json_text = $pk->export_key_jwk('public');
557
558 Also exports public/private keys as a perl HASH with JWK structure.
559
560 my $jwk_hash = $pk->export_key_jwk('private', 1);
561 #or
562 my $jwk_hash = $pk->export_key_jwk('public', 1);
544563
545564 =head2 encrypt
546565
22 use strict;
33 use warnings ;
44
5 our $VERSION = '0.024';
5 our $VERSION = '0.025';
66
77 require XSLoader;
88 XSLoader::load('CryptX', $VERSION);
5858 ok($rsa->is_private, "RSA private test HASH1");
5959 my $jwk = $rsa->export_key_jwk('private');
6060 my $jwkp = $rsa->export_key_jwk('public');
61 my $jwkh = $rsa->export_key_jwk('private', 1);
62 my $jwkhp = $rsa->export_key_jwk('public', 1);
63 is($jwkh->{kty}, "RSA", "RSA kty test export_key_jwk as hash");
64 is($jwkhp->{kty}, "RSA", "RSA(pub) kty test export_key_jwk as hash");
65 ok(exists $jwkhp->{n}, "RSA(pub) n test export_key_jwk as hash");
66 ok(exists $jwkhp->{e}, "RSA(pub) e test export_key_jwk as hash");
67 ok(!exists $jwkhp->{p}, "RSA(pub) p test export_key_jwk as hash");
68 ok(exists $jwkh->{n}, "RSA n test export_key_jwk as hash");
69 ok(exists $jwkh->{e}, "RSA e test export_key_jwk as hash");
70 ok(exists $jwkh->{p}, "RSA p test export_key_jwk as hash");
6171 ### jwk re-import private key
6272 $rsa->import_key(\$jwk);
6373 $kh = $rsa->key2hash;
153163 ok($ec->is_private, "EC private test HASH1");
154164 my $jwk = $ec->export_key_jwk('private');
155165 my $jwkp = $ec->export_key_jwk('public');
166 my $jwkh = $ec->export_key_jwk('private', 1);
167 my $jwkhp = $ec->export_key_jwk('public', 1);
168 is($jwkh->{kty}, "EC", "ECC kty test export_key_jwk as hash");
169 is($jwkhp->{kty}, "EC", "ECC(pub) kty test export_key_jwk as hash");
170 ok(exists $jwkhp->{x}, "ECC(pub) x test export_key_jwk as hash");
171 ok(exists $jwkhp->{y}, "ECC(pub) y test export_key_jwk as hash");
172 ok(!exists $jwkhp->{d}, "ECC(pub) d test export_key_jwk as hash");
173 ok(exists $jwkh->{x}, "ECC x test export_key_jwk as hash");
174 ok(exists $jwkh->{y}, "ECC y test export_key_jwk as hash");
175 ok(exists $jwkh->{d}, "ECC d test export_key_jwk as hash");
156176 ### jwk re-import private key
157177 $ec->import_key(\$jwk);
158178 $kh = $ec->key2hash;