jwskate.jwk ¶
This module implements Json Web Key RFC7517.
ExpectedAlgRequired ¶
Bases: ValueError
Raised when the expected signature alg(s) must be provided.
Source code in jwskate/jwk/alg.py
15 16 |
|
MismatchingAlg ¶
Bases: ValueError
Raised when attempting a cryptographic operation with an unexpected algorithm.
Signature verification or a decryption operation with an algorithm that does not match the algorithm specified in the key or the token.
Source code in jwskate/jwk/alg.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
UnsupportedAlg ¶
Bases: ValueError
Raised when a unsupported alg is requested.
Source code in jwskate/jwk/alg.py
11 12 |
|
InvalidJwk ¶
Bases: ValueError
Raised when an invalid JWK is encountered.
Source code in jwskate/jwk/base.py
50 51 |
|
Jwk ¶
Bases: BaseJsonDict
Represents a Json Web Key (JWK), as specified in RFC7517.
A JWK is a JSON object that represents a cryptographic key. The members of the object represent properties of the key, also called parameters, which are name and value pairs.
Just like a parsed JSON object, a Jwk
is a dict
, so
you can do with a Jwk
anything you can do with a dict
. In
addition, all keys parameters are exposed as attributes. There are
subclasses of Jwk
for each specific Key Type, but unless you are
dealing with specific parameters for a given key type, you shouldn't
have to use the subclasses directly since they all present a common
interface for cryptographic operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Mapping[str, Any] | Any
|
one of
|
required |
include_kid_thumbprint |
bool
|
if |
False
|
Source code in jwskate/jwk/base.py
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 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
is_private
property
¶
1 |
|
Return True
if the key is private, False
otherwise.
Returns:
Type | Description |
---|---|
bool
|
|
alg
property
¶
1 |
|
Return the configured key alg, if any.
Returns:
Type | Description |
---|---|
str | None
|
the key alg |
kid
property
¶
1 |
|
Return the JWK key ID (kid).
If the kid is not explicitly set, the RFC7638 key thumbprint is returned.
use
property
¶
1 |
|
Return the key use.
If no alg
parameter is present, this returns the use
parameter from this JWK. If an
alg
parameter is present, the use is deduced from this alg. To check for the presence of
the use
parameter, use jwk.get('use')
.
key_ops
property
¶
1 |
|
Return the key operations.
If no alg
parameter is present, this returns the key_ops
parameter from this JWK. If an
alg
parameter is present, the key operations are deduced from this alg. To check for the
presence of the key_ops
parameter, use jwk.get('key_ops')
.
generate_for_alg
classmethod
¶
1 |
|
Generate a key for usage with a specific alg
and return the resulting Jwk
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str
|
a signature or key management algorithm identifier |
required |
**kwargs |
Any
|
specific parameters, depending on the key type, or additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
the generated |
Source code in jwskate/jwk/base.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
generate_for_kty
classmethod
¶
1 |
|
Generate a key with a specific type and return the resulting Jwk
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kty |
str
|
key type to generate |
required |
**kwargs |
Any
|
specific parameters depending on the key type, or additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
the resulting |
Raises:
Type | Description |
---|---|
UnsupportedKeyType
|
if the specified key type ( |
Source code in jwskate/jwk/base.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
__new__ ¶
1 2 3 |
|
Overridden __new__
to make the Jwk constructor smarter.
The Jwk
constructor will accept:
1 2 3 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the source for key materials |
required |
**kwargs |
Any
|
additional members to include in the Jwk |
{}
|
Source code in jwskate/jwk/base.py
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 |
|
_get_alg_class
classmethod
¶
1 |
|
Given an alg identifier, return the matching JWA wrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str
|
an alg identifier |
required |
Returns:
Type | Description |
---|---|
type[BaseAlg]
|
the matching JWA wrapper |
Source code in jwskate/jwk/base.py
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 |
|
__getattr__ ¶
1 |
|
Allow access to key parameters as attributes.
This is a convenience to allow jwk.param
instead of jwk['param']
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param |
str
|
the parameter name to access |
required |
Return
the param value
Raises:
Type | Description |
---|---|
AttributeError
|
if the param is not found |
Source code in jwskate/jwk/base.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
__setitem__ ¶
1 |
|
Override base method to avoid modifying cryptographic key attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
name of the attribute to set |
required |
value |
Any
|
value to set |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
when trying to modify cryptographic attributes |
Source code in jwskate/jwk/base.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
thumbprint ¶
1 |
|
Return the key thumbprint as specified by RFC 7638.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hashalg |
str
|
A hash function (defaults to SHA256) |
'sha-256'
|
Returns:
Type | Description |
---|---|
str
|
the calculated thumbprint |
Source code in jwskate/jwk/base.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
thumbprint_uri ¶
1 |
|
Return the JWK thumbprint URI for this key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hashalg |
str
|
the IANA registered name for the hash alg to use |
'sha-256'
|
Returns:
Type | Description |
---|---|
str
|
the JWK thumbprint URI for this |
Source code in jwskate/jwk/base.py
396 397 398 399 400 401 402 403 404 405 406 407 |
|
check ¶
1 2 3 4 5 6 |
|
Check this key for type, privateness and/or symmetricness.
This raises a ValueError
if the key is not as expected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_private |
bool | None
|
|
None
|
is_symmetric |
bool | None
|
|
None
|
kty |
str | None
|
the expected key type, if any |
None
|
Returns:
Type | Description |
---|---|
Jwk
|
this key, if all checks passed |
Raises:
Type | Description |
---|---|
ValueError
|
if any check fails |
Source code in jwskate/jwk/base.py
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 |
|
_validate ¶
1 |
|
Validate the content of this Jwk
.
It checks that all required parameters are present and well-formed.
If the key is private, it sets the is_private
flag to True
.
Raises:
Type | Description |
---|---|
TypeError
|
if the key type doesn't match the subclass |
InvalidJwk
|
if the JWK misses required members or has invalid members |
Source code in jwskate/jwk/base.py
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 |
|
signature_class ¶
1 2 3 |
|
Return the appropriate signature algorithm class to use with this key.
The returned class is a BaseSignatureAlg
subclass.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
type[BaseSignatureAlg]
|
the appropriate |
Source code in jwskate/jwk/base.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
encryption_class ¶
1 2 3 |
|
Return the appropriate encryption algorithm class to use with this key.
The returned class is a subclass of BaseAESEncryptionAlg
.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
type[BaseAESEncryptionAlg]
|
the appropriate |
Source code in jwskate/jwk/base.py
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
|
key_management_class ¶
1 2 3 |
|
Return the appropriate key management algorithm class to use with this key.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
type[BaseKeyManagementAlg]
|
the appropriate |
Source code in jwskate/jwk/base.py
553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
signature_wrapper ¶
1 2 3 |
|
Initialize a key management wrapper with this key.
This returns an instance of a BaseSignatureAlg
subclass.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
BaseSignatureAlg
|
a |
Source code in jwskate/jwk/base.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
|
encryption_wrapper ¶
1 2 3 |
|
Initialize an encryption wrapper with this key.
This returns an instance of a BaseAESEncryptionAlg
subclass.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
BaseAESEncryptionAlg
|
a |
Source code in jwskate/jwk/base.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
|
key_management_wrapper ¶
1 2 3 |
|
Initialize a key management wrapper with this key.
This returns an instance of a BaseKeyManagementAlg
subclass.
If this key doesn't have an alg
parameter, you must supply one as parameter to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the algorithm identifier, if not already present in this Jwk |
None
|
Returns:
Type | Description |
---|---|
BaseKeyManagementAlg
|
a |
Source code in jwskate/jwk/base.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
|
supported_signing_algorithms ¶
1 |
|
Return the list of Signature algorithms that can be used with this key.
Returns:
Type | Description |
---|---|
list[str]
|
a list of supported algs |
Source code in jwskate/jwk/base.py
630 631 632 633 634 635 636 637 |
|
supported_key_management_algorithms ¶
1 |
|
Return the list of Key Management algorithms that can be used with this key.
Returns:
Type | Description |
---|---|
list[str]
|
a list of supported algs |
Source code in jwskate/jwk/base.py
639 640 641 642 643 644 645 646 |
|
supported_encryption_algorithms ¶
1 |
|
Return the list of Encryption algorithms that can be used with this key.
Returns:
Type | Description |
---|---|
list[str]
|
a list of supported algs |
Source code in jwskate/jwk/base.py
648 649 650 651 652 653 654 655 |
|
sign ¶
1 2 3 |
|
Sign data using this Jwk, and return the generated signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
the data to sign |
required |
alg |
str | None
|
the alg to use (if this key doesn't have an |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the generated signature |
Source code in jwskate/jwk/base.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
|
verify ¶
1 2 3 4 5 6 7 |
|
Verify a signature using this Jwk
, and return True
if valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
the data to verify |
required |
signature |
bytes | SupportsBytes
|
the signature to verify |
required |
alg |
str | None
|
the allowed signature alg, if there is only one |
None
|
algs |
Iterable[str] | None
|
the allowed signature algs, if there are several |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwk/base.py
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 |
|
encrypt ¶
1 2 3 4 5 6 7 |
|
Encrypt a plaintext with Authenticated Encryption using this key.
Authenticated Encryption with Associated Data (AEAD) is supported,
by passing Additional Authenticated Data (aad
).
This returns a tuple with 3 raw data, in order: - the encrypted Data - the Initialization Vector that was used to encrypt data - the generated Authentication Tag
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plaintext |
bytes | SupportsBytes
|
the data to encrypt. |
required |
aad |
bytes | None
|
the Additional Authenticated Data (AAD) to include in the authentication tag |
None
|
alg |
str | None
|
the alg to use to encrypt the data |
None
|
iv |
bytes | None
|
the Initialization Vector to use. If |
None
|
Returns:
Type | Description |
---|---|
tuple[BinaPy, BinaPy, BinaPy]
|
a tuple (ciphertext, iv, authentication_tag), as raw data |
Source code in jwskate/jwk/base.py
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 |
|
decrypt ¶
1 2 3 4 5 6 7 8 |
|
Decrypt an encrypted data using this Jwk, and return the encrypted result.
This is implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ciphertext |
bytes | SupportsBytes
|
the data to decrypt |
required |
iv |
bytes | SupportsBytes
|
the Initialization Vector (IV) that was used for encryption |
required |
tag |
bytes | SupportsBytes
|
the Authentication Tag that will be verified while decrypting data |
required |
aad |
bytes | SupportsBytes | None
|
the Additional Authentication Data (AAD) to verify the Tag against |
None
|
alg |
str | None
|
the alg to use for decryption |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the clear-text data |
Source code in jwskate/jwk/base.py
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 |
|
sender_key ¶
1 2 3 4 5 6 7 8 |
|
Produce a Content Encryption Key, to use for encryption.
This method is meant to be used by encrypted token senders.
Recipients should use the matching method Jwk.recipient_key()
.
Returns a tuple with 3 items:
- the clear text CEK, as a SymmetricJwk instance. Use this key to encrypt your message, but do not communicate this key to anyone!
- the encrypted CEK, as bytes. You must send this to your recipient.
This may be
None
for Key Management algs which derive a CEK instead of generating one. - extra headers depending on the Key Management algorithm, as a dict of name to values. You must send those to your recipient as well.
For algorithms that rely on a randomly generated CEK, such as RSAES or AES, you can provide that CEK instead
of letting jwskate
generate a safe, unique random value for you.
Likewise, for algorithms that rely on an ephemeral key, you can provide an EPK that you generated yourself,
instead of letting jwskate
generate an appropriate value for you.
Only do this if you know what you are doing!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enc |
str
|
the encryption algorithm to use with the CEK |
required |
alg |
str | None
|
the Key Management algorithm to use to produce the CEK |
None
|
cek |
bytes | None
|
CEK to use (leave |
None
|
epk |
Jwk | None
|
EPK to use (leave |
None
|
**headers |
Any
|
additional headers to include for the CEK derivation |
{}
|
Returns:
Type | Description |
---|---|
tuple[Jwk, BinaPy, Mapping[str, Any]]
|
a tuple (cek, wrapped_cek, additional_headers_map) |
Raises:
Type | Description |
---|---|
UnsupportedAlg
|
if the requested alg identifier is not supported |
Source code in jwskate/jwk/base.py
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 |
|
recipient_key ¶
1 2 3 4 5 6 7 |
|
Produce a Content Encryption Key, to use for decryption.
This method is meant to be used by encrypted token recipient.
Senders should use the matching method Jwk.sender_key()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wrapped_cek |
bytes | SupportsBytes
|
the wrapped CEK |
required |
enc |
str
|
the encryption algorithm to use with the CEK |
required |
alg |
str | None
|
the Key Management algorithm to use to unwrap the CEK |
None
|
**headers |
Any
|
additional headers used to decrypt the CEK (e.g. "epk" for ECDH algs, "iv", "tag" for AES-GCM algs) |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
the clear-text CEK, as a SymmetricJwk instance |
Raises:
Type | Description |
---|---|
UnsupportedAlg
|
if the requested alg identifier is not supported |
Source code in jwskate/jwk/base.py
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 |
|
public_jwk ¶
1 |
|
Return the public Jwk associated with this key.
Returns:
Type | Description |
---|---|
Jwk
|
a Jwk with the public key |
Source code in jwskate/jwk/base.py
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 |
|
as_jwks ¶
1 |
|
Return a JwkSet with this key as single element.
Returns:
Type | Description |
---|---|
JwkSet
|
a JwsSet with this single key |
Source code in jwskate/jwk/base.py
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
|
from_cryptography_key
classmethod
¶
1 2 3 |
|
Initialize a Jwk from a key from the cryptography
library.
The input key can be any private or public key supported by cryptography.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cryptography_key |
Any
|
a |
required |
**kwargs |
Any
|
additional members to include in the Jwk (e.g. kid, use) |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
the matching |
Raises:
Type | Description |
---|---|
TypeError
|
if the key type is not supported |
Source code in jwskate/jwk/base.py
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 |
|
_to_cryptography_key ¶
1 |
|
Return a key from the cryptography
library that matches this Jwk.
This is implemented by subclasses.
Returns:
Type | Description |
---|---|
Any
|
a |
Source code in jwskate/jwk/base.py
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
|
from_pem
classmethod
¶
1 2 3 4 5 |
|
Load a Jwk
from a PEM encoded private or public key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pem |
bytes | str
|
the PEM encoded data to load |
required |
password |
bytes | str | None
|
the password to decrypt the PEM, if required. Should be bytes. If it is a string, it will be encoded with UTF-8. |
None
|
**kwargs |
Any
|
additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
a |
Source code in jwskate/jwk/base.py
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 |
|
to_pem ¶
1 |
|
Serialize this key to PEM format.
For private keys, you can provide a password for encryption. This password should be bytes
. A str
is also
accepted, and will be encoded to bytes
using UTF-8 before it is used as encryption key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
password |
bytes | str | None
|
password to use to encrypt the PEM. |
None
|
Returns:
Type | Description |
---|---|
str
|
the PEM serialized key |
Source code in jwskate/jwk/base.py
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 |
|
from_der
classmethod
¶
1 2 3 4 5 |
|
Load a Jwk
from DER.
Source code in jwskate/jwk/base.py
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 |
|
to_der ¶
1 |
|
Serialize this key to DER.
For private keys, you can provide a password for encryption. This password should be bytes. A str
is also
accepted, and will be encoded to bytes
using UTF-8 before it is used as encryption key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
password |
bytes | str | None
|
password to use to encrypt the PEM. Should be bytes. If it is a string, it will be encoded to bytes with UTF-8. |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the DER serialized key |
Source code in jwskate/jwk/base.py
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 |
|
from_x509
classmethod
¶
1 |
|
Read the public key from a X509 certificate, PEM formatted.
Source code in jwskate/jwk/base.py
1186 1187 1188 1189 1190 1191 1192 1193 |
|
generate
classmethod
¶
1 2 3 4 5 6 |
|
Generate a Private Key and return it as a Jwk
instance.
This method is implemented by subclasses for specific Key Types and returns an instance of that subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
intended algorithm to use with the generated key |
None
|
kty |
str | None
|
key type identifier |
None
|
**kwargs |
Any
|
specific parameters depending on the type of key, or additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
Jwk
|
a |
Source code in jwskate/jwk/base.py
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
|
copy ¶
1 |
|
Create a copy of this key.
Returns:
Type | Description |
---|---|
Jwk
|
a copy of this key, with the same value |
Source code in jwskate/jwk/base.py
1224 1225 1226 1227 1228 1229 1230 1231 |
|
with_kid_thumbprint ¶
1 |
|
Include the JWK thumbprint as kid
.
If key already has a kid
(Key ID):
- if
force
isTrue
, this erases the previous "kid". - if
force
isFalse
(default), do nothing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force |
bool
|
whether to overwrite a previously existing kid |
False
|
Returns:
Type | Description |
---|---|
Jwk
|
a copy of this Jwk, with a |
Source code in jwskate/jwk/base.py
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 |
|
with_usage_parameters ¶
1 2 3 4 5 6 7 |
|
Copy this Jwk and add the use
and key_ops
parameters.
The returned jwk alg
parameter will be the one passed as parameter to this method,
or as default the one declared as alg
parameter in this Jwk.
The use
(Public Key Use) param is deduced based on this alg
value.
The key_ops
(Key Operations) param is deduced based on the key use
and if the key is public, private,
or symmetric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str | None
|
the alg to use, if not present in this Jwk |
None
|
with_alg |
bool
|
whether to include an |
True
|
with_use |
bool
|
whether to include a |
True
|
with_key_ops |
bool
|
whether to include a |
True
|
Returns:
Type | Description |
---|---|
Jwk
|
a Jwk with the same key, with |
Source code in jwskate/jwk/base.py
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 |
|
minimize ¶
1 |
|
Strip out any optional or non-standard parameter from that key.
This will remove alg
, use
, key_ops
, optional parameters from RSA keys, and other
unknown parameters.
Source code in jwskate/jwk/base.py
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
|
__eq__ ¶
1 |
|
Compare JWK keys, ignoring optional/informational fields.
Source code in jwskate/jwk/base.py
1315 1316 1317 1318 |
|
UnsupportedKeyType ¶
Bases: ValueError
Raised when an unsupported Key Type is requested.
Source code in jwskate/jwk/base.py
46 47 |
|
ECJwk ¶
Bases: Jwk
Represent an Elliptic Curve key in JWK format.
Elliptic Curve keys have Key Type "EC"
.
Source code in jwskate/jwk/ec.py
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 |
|
curve
property
¶
1 |
|
Get the EllipticCurve
instance for this key.
Returns:
Type | Description |
---|---|
EllipticCurve
|
the |
coordinate_size
property
¶
1 |
|
The coordinate size to use with the key curve.
This is 32, 48, or 66 bits.
x_coordinate
cached
property
¶
1 |
|
Return the x coordinate, parameter x
from this ECJwk
.
y_coordinate
cached
property
¶
1 |
|
Return the y coordinate, parameter y
from this ECJwk
.
ecc_private_key
cached
property
¶
1 |
|
Return the ECC private key, parameter d
from this ECJwk
.
get_curve
classmethod
¶
1 |
|
Get the EllipticCurve instance for a given curve identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
the curve identifier |
required |
Returns:
Type | Description |
---|---|
EllipticCurve
|
the matching |
Raises:
Type | Description |
---|---|
UnsupportedEllipticCurve
|
if the curve identifier is not supported |
Source code in jwskate/jwk/ec.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
public
classmethod
¶
1 |
|
Initialize a public ECJwk
from its public parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
the curve to use |
required |
x |
int
|
the x coordinate |
required |
y |
int
|
the y coordinate |
required |
**params |
str
|
additional member to include in the Jwk |
{}
|
Returns:
Type | Description |
---|---|
ECJwk
|
an ECJwk initialized with the supplied parameters |
Source code in jwskate/jwk/ec.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
private
classmethod
¶
1 2 3 |
|
Initialize a private ECJwk from its private parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
the curve to use |
required |
x |
int
|
the x coordinate |
required |
y |
int
|
the y coordinate |
required |
d |
int
|
the elliptic curve private key |
required |
**params |
Any
|
additional members to include in the JWK |
{}
|
Returns:
Type | Description |
---|---|
ECJwk
|
an ECJwk initialized with the supplied parameters |
Source code in jwskate/jwk/ec.py
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 |
|
UnsupportedEllipticCurve ¶
Bases: KeyError
Raised when an unsupported Elliptic Curve is requested.
Source code in jwskate/jwk/ec.py
35 36 |
|
JwkSet ¶
Bases: BaseJsonDict
A set of JWK keys, with methods for easy management of keys.
A JwkSet is a dict subclass, so you can do anything with a JwkSet that you can do with a dict. In addition, it provides a few helpers methods to get the keys, add or remove keys, and verify signatures using keys from this set.
- a
dict
from the parsed JSON object representing this JwkSet (in parameterjwks
) - a list of
Jwk
(in parameterkeys
) - nothing, to initialize an empty JwkSet
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwks |
Mapping[str, Any] | None
|
a dict, containing the JwkSet, parsed as a JSON object. |
None
|
keys |
Iterable[Jwk | Mapping[str, Any]] | None
|
a list of |
None
|
Source code in jwskate/jwk/jwks.py
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 |
|
jwks
property
¶
1 |
|
Return the list of keys from this JwkSet, as Jwk
instances.
Returns:
Type | Description |
---|---|
list[Jwk]
|
a list of |
is_private
property
¶
1 |
|
True if the JwkSet contains at least one private key.
Returns:
Type | Description |
---|---|
bool
|
|
get_jwk_by_kid ¶
1 |
|
Return a Jwk from this JwkSet, based on its kid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kid |
str
|
the kid of the key to obtain |
required |
Returns:
Type | Description |
---|---|
Jwk
|
the key with the matching Key ID |
Raises:
Type | Description |
---|---|
KeyError
|
if no key matches |
Source code in jwskate/jwk/jwks.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
__len__ ¶
1 |
|
Return the number of Jwk in this JwkSet.
Returns:
Type | Description |
---|---|
int
|
the number of keys |
Source code in jwskate/jwk/jwks.py
80 81 82 83 84 85 86 87 |
|
add_jwk ¶
1 |
|
Add a Jwk in this JwkSet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the Jwk to add (either a |
required |
Returns:
Type | Description |
---|---|
str
|
the key ID. It will be generated if missing from the given Jwk. |
Source code in jwskate/jwk/jwks.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
remove_jwk ¶
1 |
|
Remove a Jwk from this JwkSet, based on a kid
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kid |
str
|
the |
required |
Raises:
Type | Description |
---|---|
KeyError
|
if no key matches |
Source code in jwskate/jwk/jwks.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
public_jwks ¶
1 |
|
Return another JwkSet with the public keys associated with the current keys.
Returns:
Type | Description |
---|---|
JwkSet
|
a public JwkSet |
Source code in jwskate/jwk/jwks.py
135 136 137 138 139 140 141 142 |
|
verification_keys ¶
1 |
|
Return the list of keys from this JWKS that are usable for signature verification.
To be usable for signature verification, a key must:
- be asymmetric
- be public
- be flagged for signature, either with
use=sig
or analg
that is compatible with signature
Returns:
Type | Description |
---|---|
list[Jwk]
|
a list of |
Source code in jwskate/jwk/jwks.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
verify ¶
1 2 3 4 5 6 7 |
|
Verify a signature with the keys from this key set.
If a kid
is provided, only that Key ID will be tried. Otherwise, all keys that are compatible with the
specified alg(s) will be tried.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes
|
the signed data to verify |
required |
signature |
bytes
|
the signature to verify against the signed data |
required |
alg |
str | None
|
alg to verify the signature, if there is only 1 |
None
|
algs |
Iterable[str] | None
|
list of allowed signature algs, if there are several |
None
|
kid |
str | None
|
the kid of the Jwk that will be used to validate the signature. If no kid is provided, multiple keys from this key set may be tried. |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwk/jwks.py
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 |
|
encryption_keys ¶
1 |
|
Return the list of keys from this JWKS that are usable for encryption.
To be usable for encryption, a key must:
- be asymmetric
- be public
- be flagged for encryption, either with
use=enc
or analg
parameter that is an encryption alg
Returns:
Type | Description |
---|---|
list[Jwk]
|
a list of |
Source code in jwskate/jwk/jwks.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
SymmetricJwk ¶
Bases: Jwk
Represent a Symmetric key in JWK format.
Symmetric keys have key type "oct"
.
Source code in jwskate/jwk/oct.py
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 |
|
public_jwk ¶
1 |
|
Raise an error since Symmetric Keys are always private.
Raises:
Type | Description |
---|---|
ValueError
|
symmetric keys are always private, it makes no sense to use them as public keys |
Source code in jwskate/jwk/oct.py
84 85 86 87 88 89 90 91 92 93 |
|
from_bytes
classmethod
¶
1 |
|
Initialize a SymmetricJwk
from a raw secret key.
The provided secret key is encoded and used as the k
parameter for the returned SymmetricKey
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k |
bytes | str
|
the key to use |
required |
**params |
Any
|
additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
SymmetricJwk
|
the resulting |
Source code in jwskate/jwk/oct.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
encrypt ¶
1 2 3 4 5 6 7 |
|
Encrypt arbitrary data using this key.
Supports Authenticated Encryption with Additional Authenticated Data (use parameter aad
for Additional
Authenticated Data).
An Initialization Vector (IV) will be generated automatically.
You can choose your own IV by providing the iv
parameter (only use this if you know what you are doing).
This returns the ciphertext, the authentication tag, and the generated IV. If an IV was provided as parameter, the same IV is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plaintext |
bytes | SupportsBytes
|
the plaintext to encrypt |
required |
aad |
bytes | None
|
the Additional Authentication Data, if any |
None
|
alg |
str | None
|
the encryption alg to use |
None
|
iv |
bytes | None
|
the IV to use, if you want a specific value |
None
|
Returns:
Type | Description |
---|---|
tuple[BinaPy, BinaPy, BinaPy]
|
a (ciphertext, authentication_tag, iv) tuple |
Source code in jwskate/jwk/oct.py
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 |
|
decrypt ¶
1 2 3 4 5 6 7 8 |
|
Decrypt arbitrary data, and verify Additional Authenticated Data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ciphertext |
bytes | SupportsBytes
|
the encrypted data |
required |
iv |
bytes | SupportsBytes
|
the Initialization Vector (must be the same as generated during encryption) |
required |
tag |
bytes | SupportsBytes
|
the authentication tag |
required |
aad |
bytes | SupportsBytes | None
|
the Additional Authenticated Data (must be the same data used during encryption) |
None
|
alg |
str | None
|
the decryption alg (must be the same as used during encryption) |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the decrypted clear-text |
Source code in jwskate/jwk/oct.py
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 |
|
OKPJwk ¶
Bases: Jwk
Represent an Octet Key Pair keys in JWK format.
Octet Key Pair keys have Key Type "OKP"
.
Source code in jwskate/jwk/okp.py
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 |
|
public_key
cached
property
¶
1 |
|
Get the public key from this Jwk
, from param x
, base64url-decoded.
private_key
cached
property
¶
1 |
|
Get the private key from this Jwk
, from param d
, base64url-decoded.
get_curve
classmethod
¶
1 |
|
Get the OKPCurve
instance from a curve identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
a curve identifier |
required |
Returns:
Type | Description |
---|---|
OKPCurve
|
the matching |
Raises:
Type | Description |
---|---|
UnsupportedOKPCurve
|
if the curve is not supported |
Source code in jwskate/jwk/okp.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
from_bytes
classmethod
¶
1 2 3 4 5 6 |
|
Initialize an OKPJwk
from its private key, as bytes
.
The public key will be automatically derived from the supplied private key, according to the OKP curve.
The appropriate curve will be guessed based on the key length or supplied crv
/use
hints:
- 56 bytes will use
X448
- 57 bytes will use
Ed448
- 32 bytes will use
Ed25519
orX25519
. Since there is no way to guess which one you want, it needs a hint with either acrv
oruse
parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key |
bytes
|
the 32, 56 or 57 bytes private key, as raw |
required |
crv |
str | None
|
the curve identifier to use |
None
|
use |
str | None
|
the key usage |
None
|
**kwargs |
Any
|
additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
OKPJwk
|
the matching |
Source code in jwskate/jwk/okp.py
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 |
|
public
classmethod
¶
1 |
|
Initialize a public OKPJwk
based on the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
the key curve |
required |
x |
bytes
|
the public key |
required |
**params |
Any
|
additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
OKPJwk
|
the resulting |
Source code in jwskate/jwk/okp.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
private
classmethod
¶
1 2 3 |
|
Initialize a private OKPJwk
based on the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crv |
str
|
the OKP curve |
required |
x |
bytes
|
the public key |
required |
d |
bytes
|
the private key |
required |
**params |
Any
|
additional members to include in the |
{}
|
Returns:
Type | Description |
---|---|
OKPJwk
|
the resulting |
Source code in jwskate/jwk/okp.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
UnsupportedOKPCurve ¶
Bases: KeyError
Raised when an unsupported OKP curve is requested.
Source code in jwskate/jwk/okp.py
36 37 |
|
RSAJwk ¶
Bases: Jwk
Represent an RSA key in JWK format.
RSA (Rivest-Shamir-Adleman) keys have Key Type "RSA"
.
Source code in jwskate/jwk/rsa.py
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 |
|
private_exponent
cached
property
¶
1 |
|
Return the private exponent d
from this Jwk
.
prime_factors
cached
property
¶
1 |
|
Return the 2 prime factors p
and q
from this Jwk
.
first_prime_factor
cached
property
¶
1 |
|
Return the first prime factor p
from this Jwk
.
second_prime_factor
cached
property
¶
1 |
|
Return the second prime factor q
from this Jwk
.
first_factor_crt_exponent
cached
property
¶
1 |
|
Return the first factor CRT exponent dp
from this Jwk
.
second_factor_crt_exponent
cached
property
¶
1 |
|
Return the second factor CRT exponent dq
from this Jwk
.
first_crt_coefficient
cached
property
¶
1 |
|
Return the first CRT coefficient qi
from this Jwk
.
public
classmethod
¶
1 |
|
Initialize a public RsaJwk
from a modulus and an exponent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
the modulus |
required |
e |
int
|
the exponent |
65537
|
**params |
Any
|
additional parameters to include in the |
{}
|
Returns:
Type | Description |
---|---|
RSAJwk
|
a |
Source code in jwskate/jwk/rsa.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
private
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Initialize a private RSAJwk
from its required parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
the modulus |
required |
e |
int
|
the exponent |
65537
|
d |
int
|
the private exponent |
required |
p |
int | None
|
the first prime factor |
None
|
q |
int | None
|
the second prime factor |
None
|
dp |
int | None
|
the first factor CRT exponent |
None
|
dq |
int | None
|
the second factor CRT exponent |
None
|
qi |
int | None
|
the first CRT coefficient |
None
|
**params |
Any
|
additional parameters to include in the |
{}
|
Returns:
Type | Description |
---|---|
RSAJwk
|
a |
Source code in jwskate/jwk/rsa.py
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 |
|
from_prime_factors
classmethod
¶
1 2 3 |
|
Initialise a RSAJwk
from its prime factors and exponent.
Modulus and Private Exponent are mathematically calculated based on those factors.
Exponent is usually 65537 (default).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
int
|
first prime factor |
required |
q |
int
|
second prime factor |
required |
e |
int
|
exponent |
65537
|
Returns:
Type | Description |
---|---|
RSAJwk
|
a |
Source code in jwskate/jwk/rsa.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
generate
classmethod
¶
1 |
|
Generate a new random private RSAJwk
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key_size |
int
|
the key size to use for the generated key, in bits |
4096
|
**params |
Any
|
additional parameters to include in the |
{}
|
Returns:
Type | Description |
---|---|
RSAJwk
|
a generated |
Source code in jwskate/jwk/rsa.py
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 |
|
with_optional_private_parameters ¶
1 |
|
Compute the optional RSA private parameters.
This returns a new Jwk
with those additional params included.
The optional parameters are:
- p: first prime factor
- q: second prime factor
- dp: first factor Chinese Remainder Theorem exponent
- dq: second factor Chinese Remainder Theorem exponent
- qi: first Chinese Remainder Theorem coefficient
Source code in jwskate/jwk/rsa.py
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 |
|
without_optional_private_parameters ¶
1 |
|
Remove the optional private parameters and return another Jwk
instance without them.
Source code in jwskate/jwk/rsa.py
327 328 329 330 331 332 333 |
|
select_alg_class ¶
1 2 3 4 5 6 7 |
|
Choose the appropriate alg class to use for cryptographic operations.
Given: - a mapping of supported algs names to wrapper classes - a preferred alg name (usually the one mentioned in a JWK) - and/or a user-specified alg this returns the wrapper class to use.
This checks the coherency between the user specified alg
and the jwk_alg
, and will emit a warning
if the user specified alg is different from the jwk_alg
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
supported_algs |
Mapping[str, T]
|
a mapping of supported alg names to alg wrapper |
required |
jwk_alg |
str | None
|
the alg from the JWK, if any |
None
|
alg |
str | None
|
a user specified alg |
None
|
strict |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
T
|
the alg to use |
Raises:
Type | Description |
---|---|
UnsupportedAlg
|
if the requested |
ValueError
|
if |
MismatchingAlg
|
if |
Source code in jwskate/jwk/alg.py
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 |
|
select_alg_classes ¶
1 2 3 4 5 6 7 8 |
|
Select several appropriate algs classes to use on cryptographic operations.
This method is typically used to get the list of valid algorithms when checking a signature, when several algorithms are allowed.
Given:
- a mapping of supported algorithms name to wrapper classes
- an alg parameter from a JWK
- and/or a user-specified alg
- and/or a user specified list of usable algs
this returns a list of supported alg wrapper classes that matches what the user specified, or, as default, the alg parameter from the JWK.
This checks the coherency between the user specified alg
and the jwk_alg
, and will emit a warning
if the user specified alg is different from the jwk_alg
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
supported_algs |
Mapping[str, T]
|
a mapping of alg names to alg wrappers |
required |
jwk_alg |
str | None
|
the alg from the JWK, if any |
None
|
alg |
str | None
|
a user specified alg to use, if any |
None
|
algs |
Iterable[str] | None
|
a user specified list of algs to use, if several are allowed |
None
|
strict |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
list[T]
|
a list of possible algs to check |
Raises:
Type | Description |
---|---|
ValueError
|
if both 'alg' and 'algs' parameters are used |
UnsupportedAlg
|
if none of the requested alg are supported |
Source code in jwskate/jwk/alg.py
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 |
|
to_jwk ¶
1 2 3 4 5 6 7 |
|
Convert any supported kind of key to a Jwk
.
This optionally checks if that key is private or symmetric.
The key can be any type supported by Jwk:
- a cryptography
key instance
- a bytes, to initialize a symmetric key
- a JWK, as a dict or as a JSON formatted string
- an existing Jwk instance
If the supplied param is already a Jwk, it is left untouched.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Any
|
the key material |
required |
kty |
str | None
|
the expected key type |
None
|
is_private |
bool | None
|
if |
None
|
is_symmetric |
bool | None
|
if |
None
|
Returns:
Type | Description |
---|---|
Jwk
|
a Jwk key |
Source code in jwskate/jwk/base.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 |
|
jwskate.jwt ¶
This module contains all Json Web Key (Jwk) related classes and utilities.
InvalidJwt ¶
Bases: ValueError
Raised when an invalid Jwt is parsed.
Source code in jwskate/jwt/base.py
18 19 |
|
Jwt ¶
Bases: BaseCompactToken
Represents a Json Web Token.
Source code in jwskate/jwt/base.py
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 |
|
__new__ ¶
1 2 3 |
|
Allow parsing both Signed and Encrypted JWTs.
This returns the appropriate subclass or instance depending on the number of dots (.) in the serialized JWT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
bytes | str
|
the token value |
required |
max_size |
int
|
maximum allowed size for the token |
16 * 1024
|
Source code in jwskate/jwt/base.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
sign
classmethod
¶
1 2 3 4 5 6 7 8 |
|
Sign a JSON payload with a private key and return the resulting SignedJwt
.
This method cannot generate a token without a signature. If you want to use an unsigned token (with alg=none),
use .unprotected()
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
claims |
Mapping[str, Any]
|
the payload to sign |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the key to use for signing |
required |
alg |
str | None
|
the alg to use for signing |
None
|
typ |
str | None
|
typ (token type) header to include. If |
'JWT'
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to include in the Jwt |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the resulting token |
Source code in jwskate/jwt/base.py
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 |
|
sign_arbitrary
classmethod
¶
1 2 3 4 5 6 7 |
|
Sign provided headers and claims with a private key and return the resulting SignedJwt
.
This does not check the consistency between headers, key, alg and kid.
DO NOT USE THIS METHOD UNLESS YOU KNOW WHAT YOU ARE DOING!!!
Use Jwt.sign()
to make sure you are signing tokens properly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
claims |
Mapping[str, Any]
|
the payload to sign |
required |
headers |
Mapping[str, Any]
|
the headers to sign |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the key to use for signing |
required |
alg |
str | None
|
the alg to use for signing |
None
|
Source code in jwskate/jwt/base.py
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 |
|
unprotected
classmethod
¶
1 2 3 4 5 6 |
|
Generate a JWT that is not signed and not encrypted (with alg=none).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
claims |
Mapping[str, Any]
|
the claims to set in the token. |
required |
typ |
str | None
|
typ (token type) header to include. If |
'JWT'
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to insert in the token. |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the resulting token |
Source code in jwskate/jwt/base.py
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 |
|
sign_and_encrypt
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Sign a JWT, then encrypt it as JWE payload.
This is a convenience method to do both the signing and encryption, in appropriate order.
This is deprecated, use Jwt.sign().encrypt()
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
claims |
Mapping[str, Any]
|
the payload to encrypt |
required |
sign_key |
Jwk | Mapping[str, Any] | Any
|
the Jwk to use for signature |
required |
sign_alg |
str | None
|
the alg to use for signature |
None
|
sign_extra_headers |
Mapping[str, Any] | None
|
additional headers for the inner signed JWT |
None
|
enc_key |
Jwk | Mapping[str, Any] | Any
|
the Jwk to use for encryption |
required |
enc_alg |
str | None
|
the alg to use for CEK encryption |
None
|
enc |
str
|
the alg to use for payload encryption |
required |
enc_extra_headers |
Mapping[str, Any] | None
|
additional headers for the outer encrypted JWE |
None
|
Returns:
Type | Description |
---|---|
JweCompact
|
the resulting JWE token, with the signed JWT as payload |
Source code in jwskate/jwt/base.py
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 |
|
decrypt_nested_jwt
classmethod
¶
1 2 3 4 |
|
Decrypt a JWE that contains a nested signed JWT.
It will return a [Jwt] instance for the inner JWT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwe |
str | JweCompact
|
the JWE containing a nested Token |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the decryption key |
required |
Returns:
Type | Description |
---|---|
SignedJwt
|
the inner JWT |
Raises:
Type | Description |
---|---|
InvalidJwt
|
if the inner JWT is not valid |
Source code in jwskate/jwt/base.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
decrypt_and_verify
classmethod
¶
1 2 3 4 5 6 7 |
|
Decrypt then verify the signature of a JWT nested in a JWE.
This can only be used with signed then encrypted Jwt, such as those produced by SignedJwt.encrypt()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwt |
str | JweCompact
|
the JWE containing a nested signed JWT |
required |
enc_key |
Jwk | Mapping[str, Any] | Any
|
the decryption key |
required |
sig_key |
Jwk | Mapping[str, Any] | Any
|
the signature verification key |
required |
sig_alg |
str | None
|
the signature verification alg, if only 1 is allowed |
None
|
sig_algs |
Iterable[str] | None
|
the signature verifications algs, if several are allowed |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the nested signed JWT, in clear-text, signature already verified |
Raises:
Type | Description |
---|---|
InvalidJwt
|
if the JWT is not valid |
InvalidSignature
|
if the nested JWT signature is not valid |
Source code in jwskate/jwt/base.py
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 |
|
timestamp
classmethod
¶
1 |
|
Return an integer timestamp that is suitable for use in JWT tokens.
Timestamps are used in particular for iat
, exp
and nbf
claims.
A timestamp is a number of seconds since January 1st, 1970 00:00:00 UTC, ignoring leap seconds.
By default, the current timestamp is returned. You can include delta_seconds
to have a timestamp
a number of seconds in the future (if positive) or in the past (if negative).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delta_seconds |
int
|
number of seconds in the future or in the past compared to current time |
0
|
Returns:
Type | Description |
---|---|
int
|
An integer timestamp |
Source code in jwskate/jwt/base.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
timestamp_to_datetime
classmethod
¶
1 |
|
Convert a JWT timestamp to a datetime
.
Returned datetime is always in the UTC timezone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamp |
int
|
a timestamp from a JWT token |
required |
Returns:
Type | Description |
---|---|
datetime
|
the corresponding |
Source code in jwskate/jwt/base.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
ExpiredJwt ¶
Bases: ValueError
Raised when trying to validate an expired JWT token.
Source code in jwskate/jwt/signed.py
19 20 |
|
InvalidClaim ¶
Bases: ValueError
Raised when trying to validate a JWT with unexpected claims.
Source code in jwskate/jwt/signed.py
23 24 |
|
SignedJwt ¶
Bases: Jwt
Represent a Signed Json Web Token (JWT), as defined in RFC7519.
A signed JWT contains a JSON object as payload, which represents claims.
To sign a JWT, use Jwt.sign.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
bytes | str
|
the token value. |
required |
Source code in jwskate/jwt/signed.py
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 |
|
signed_part
cached
property
¶
1 |
|
Return the actual signed data from this token.
The signed part is composed of the header and payload, encoded in Base64-Url, joined by a dot.
Returns:
Type | Description |
---|---|
bytes
|
the signed part as bytes |
expires_at
cached
property
¶
1 |
|
Get the Expires At (exp
) date from this token.
Returns:
Type | Description |
---|---|
datetime | None
|
a |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
issued_at
cached
property
¶
1 |
|
Get the Issued At (iat
) date from this token.
Returns:
Type | Description |
---|---|
datetime | None
|
a |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
not_before
cached
property
¶
1 |
|
Get the Not Before (nbf) date from this token.
Returns:
Type | Description |
---|---|
datetime | None
|
a |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
issuer
cached
property
¶
1 |
|
Get the Issuer (iss
) claim from this token.
Returns:
Type | Description |
---|---|
str | None
|
the issuer, as |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
audiences
cached
property
¶
1 |
|
Get the Audience(s) (aud
) claim from this token.
If this token has a single audience, this will return a list
anyway.
Returns:
Type | Description |
---|---|
list[str]
|
the list of audiences from this token, from the |
Raises:
Type | Description |
---|---|
AttributeError
|
if the audience is an unexpected type |
subject
cached
property
¶
1 |
|
Get the Subject (sub
) from this token.
Returns:
Type | Description |
---|---|
str | None
|
the subject, as |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
jwt_token_id
cached
property
¶
1 |
|
Get the JWT Token ID (jti
) from this token.
Returns:
Type | Description |
---|---|
str | None
|
the token identifier, as |
Raises:
Type | Description |
---|---|
AttributeError
|
if the |
verify_signature ¶
1 2 3 4 5 |
|
Verify this JWT signature using a given key and algorithm(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the private Jwk to use to verify the signature |
required |
alg |
str | None
|
the alg to use to verify the signature, if only 1 is allowed |
None
|
algs |
Iterable[str] | None
|
the allowed signature algs, if there are several |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwt/signed.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
verify ¶
1 2 3 4 5 6 |
|
Convenience method to verify the signature inline.
Returns self
on success, raises an exception on failure.
Raises:
Type | Description |
---|---|
InvalidSignature
|
if the signature does not verify. |
Return
the same SignedJwt
, if the signature is verified.
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Source code in jwskate/jwt/signed.py
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 |
|
is_expired ¶
1 |
|
Check if this token is expired, based on its exp
claim.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
leeway |
int
|
additional number of seconds for leeway. |
0
|
Returns:
Type | Description |
---|---|
bool | None
|
|
Source code in jwskate/jwt/signed.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
get_claim ¶
1 |
|
Get a claim by name from this Jwt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
the claim name. |
required |
default |
Any
|
a default value if the claim is not found |
None
|
Returns:
Type | Description |
---|---|
Any
|
the claim value if found, or |
Source code in jwskate/jwt/signed.py
291 292 293 294 295 296 297 298 299 300 301 302 |
|
__getitem__ ¶
1 |
|
Allow access to claim by name with subscription.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
str
|
the claim name |
required |
Returns:
Type | Description |
---|---|
Any
|
the claim value |
Source code in jwskate/jwt/signed.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
__getattr__ ¶
1 |
|
Allow claim access as attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
str
|
the claim name |
required |
Returns:
Type | Description |
---|---|
Any
|
the claim value |
Source code in jwskate/jwt/signed.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
__str__ ¶
1 |
|
Return the Jwt serialized value, as str
.
Returns:
Type | Description |
---|---|
str
|
the serialized token value. |
Source code in jwskate/jwt/signed.py
334 335 336 337 338 339 340 341 |
|
__bytes__ ¶
1 |
|
Return the Jwt serialized value, as bytes
.
Returns:
Type | Description |
---|---|
bytes
|
the serialized token value. |
Source code in jwskate/jwt/signed.py
343 344 345 346 347 348 349 350 |
|
validate ¶
1 2 3 4 5 6 7 8 9 10 |
|
Validate a SignedJwt
signature and expected claims.
This verifies the signature using the provided jwk
and alg
, then checks the token issuer, audience and
expiration date.
This can also check custom claims using extra kwargs
, whose values can be:
- a static value (
str
,int
, etc.): the value from the token will be compared "as-is". - a callable, taking the claim value as parameter: if that callable returns
True
, the claim is considered as valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the signing key to use to verify the signature. |
required |
alg |
str | None
|
the signature alg to use to verify the signature. |
None
|
algs |
Iterable[str] | None
|
allowed signature algs, if several |
None
|
issuer |
str | None
|
the expected issuer for this token. |
None
|
audience |
None | str
|
the expected audience for this token. |
None
|
check_exp |
bool
|
ìf |
True
|
**kwargs |
Any
|
additional claims to check |
{}
|
Returns:
Type | Description |
---|---|
None
|
Raises exceptions if any validation check fails. |
Raises:
Type | Description |
---|---|
InvalidSignature
|
if the signature is not valid |
InvalidClaim
|
if a claim doesn't validate |
ExpiredJwt
|
if the expiration date is passed |
Source code in jwskate/jwt/signed.py
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 |
|
encrypt ¶
1 2 3 4 5 6 |
|
Encrypt this JWT into a JWE.
The result is an encrypted (outer) JWT containing a signed (inner) JWT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Any
|
the encryption key to use |
required |
enc |
str
|
the encryption alg to use |
required |
alg |
str | None
|
the key management alg to use |
None
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to include in the outer JWE. |
None
|
Source code in jwskate/jwt/signed.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
JwtSigner ¶
A helper class to easily sign JWTs with standardized claims.
The standardized claims include:
ÃŒat
: issued at dateexp
: expiration datenbf
: not before dateiss
: issuer identifiersub
: subject identifieraud
: audience identifierjti
: JWT token ID
The issuer, signing keys, signing alg and default lifetime are
defined at initialization time, so you only have to define the
subject, audience and custom claims when calling JwtSigner.sign()
.
This can be used as an alternative to Jwt.sign()
when a single
issuer issues multiple tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer |
str | None
|
the issuer string to use as |
None
|
key |
Jwk | Any
|
the private Jwk to use to sign tokens. |
required |
alg |
str | None
|
the signing alg to use to sign tokens. |
None
|
default_lifetime |
int
|
the default lifetime, in seconds, to use for claim |
60
|
default_leeway |
int | None
|
the default leeway, in seconds, to use for claim |
None
|
Source code in jwskate/jwt/signer.py
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 |
|
sign ¶
1 2 3 4 5 6 7 8 9 |
|
Sign a Jwt.
Claim 'issuer' will have the value defined at initialization time. Claim iat
, nbf
and exp
will reflect
the current time when the token is signed. exp
includes lifetime
seconds in the future, and nbf
includes leeway
seconds in the past.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subject |
str | None
|
the subject to include in claim |
None
|
audience |
str | Iterable[str] | None
|
the audience identifier(s) to include in claim |
None
|
extra_claims |
Mapping[str, Any] | None
|
additional claims to include in the signed token. (Default value = None) |
None
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to include in the header part. (Default value = None) |
None
|
lifetime |
int | None
|
lifetime, in seconds, to use for the |
None
|
leeway |
int | None
|
leeway, in seconds, to use for the |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the resulting signed token. |
Source code in jwskate/jwt/signer.py
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 |
|
generate_jti ¶
1 |
|
Generate Jwt Token ID (jti) values.
Default uses UUID4. Can be overridden in subclasses.
Returns:
Type | Description |
---|---|
str
|
A unique value suitable for use as JWT Token ID (jti) claim. |
Source code in jwskate/jwt/signer.py
137 138 139 140 141 142 143 144 145 146 |
|
with_random_key
classmethod
¶
1 2 3 4 5 6 7 8 |
|
Initialize a JwtSigner with a randomly generated key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer |
str
|
the issuer identifier |
required |
alg |
str
|
the signing alg to use |
required |
default_lifetime |
int
|
lifetime for generated tokens expiration date ( |
60
|
default_leeway |
int | None
|
leeway for generated tokens not before date ( |
None
|
kid |
str | None
|
key id to use for the generated key |
None
|
Returns:
Type | Description |
---|---|
JwtSigner
|
a JwtSigner initialized with a random key |
Source code in jwskate/jwt/signer.py
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 |
|
verifier ¶
1 2 3 4 5 6 7 8 |
|
Return the matching JwtVerifier
, initialized with the public key.
Source code in jwskate/jwt/signer.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
JwtVerifier ¶
A helper class to validate JWTs tokens in a real application.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwkset |
JwkSet | Jwk | Mapping[str, Any]
|
a |
required |
issuer |
str | None
|
expected issuer value |
required |
audience |
str | None
|
expected audience value |
None
|
alg |
str | None
|
expected signature alg, if there is only one |
None
|
algs |
Iterable[str] | None
|
expected signature algs, if there are several |
None
|
leeway |
int
|
number of seconds to allow when verifying token validity period |
10
|
verifiers |
Iterable[Callable[[SignedJwt], None]] | None
|
additional verifiers to implement custom checks on the tokens |
None
|
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Source code in jwskate/jwt/verifier.py
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 |
|
verify ¶
1 |
|
Verify a given JWT token.
This checks the token signature, issuer, audience and expiration date, plus any custom verification, as configured at init time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwt |
SignedJwt | str | bytes
|
the JWT token to verify |
required |
Source code in jwskate/jwt/verifier.py
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 |
|
custom_verifier ¶
1 2 3 |
|
A decorator to add custom verification steps to this verifier.
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Source code in jwskate/jwt/verifier.py
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 |
|
jwskate.jws ¶
This module implements JWS token handling.
InvalidJws ¶
Bases: ValueError
Raised when an invalid Jws is parsed.
Source code in jwskate/jws/compact.py
20 21 |
|
JwsCompact ¶
Bases: BaseCompactToken
Represents a Json Web Signature (JWS), using compact serialization, as defined in RFC7515.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
bytes | str
|
the JWS token value |
required |
Source code in jwskate/jws/compact.py
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 |
|
signed_part
cached
property
¶
1 |
|
Returns the signed part (header + payload) from this JwsCompact.
Returns:
Type | Description |
---|---|
bytes
|
the signed part |
sign
classmethod
¶
1 2 3 4 5 6 |
|
Sign a payload and returns the resulting JwsCompact.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload |
bytes | SupportsBytes
|
the payload to sign |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the jwk to use to sign this payload |
required |
alg |
str | None
|
the alg to use |
None
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to add to the Jws Headers |
None
|
Returns:
Type | Description |
---|---|
JwsCompact
|
the resulting token |
Source code in jwskate/jws/compact.py
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 |
|
from_parts
classmethod
¶
1 2 3 4 |
|
Construct a JWS token based on its signed part and signature values.
Signed part is the concatenation of the header and payload, both encoded in Base64-Url, and joined by a dot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signed_part |
bytes | SupportsBytes | str
|
the signed part |
required |
signature |
bytes | SupportsBytes
|
the signature value |
required |
Returns:
Type | Description |
---|---|
JwsCompact
|
the resulting token |
Source code in jwskate/jws/compact.py
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 |
|
verify_signature ¶
1 2 3 4 5 6 |
|
Verify the signature from this JwsCompact using a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the Jwk to use to validate this signature |
required |
alg |
str | None
|
the alg to use, if there is only 1 allowed |
None
|
algs |
Iterable[str] | None
|
the allowed algs, if here are several |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jws/compact.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
verify ¶
1 2 3 4 5 6 |
|
Verify this JWS signature.
This is an alternative to .verify_signature()
that raises an exception if the signature is not
verified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the Jwk to use to validate this signature |
required |
alg |
str | None
|
the alg to use, if there is only 1 allowed |
None
|
algs |
Iterable[str] | None
|
the allowed algs, if here are several |
None
|
Raises:
Type | Description |
---|---|
InvalidSignature
|
if the signature does not verify |
Returns:
Type | Description |
---|---|
Self
|
The same JwsCompact |
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Source code in jwskate/jws/compact.py
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 |
|
flat_json ¶
1 |
|
Create a JWS in JSON flat format based on this Compact JWS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unprotected_header |
Any
|
optional unprotected header to include in the JWS JSON |
None
|
Returns:
Type | Description |
---|---|
JwsJsonFlat
|
the resulting token |
Source code in jwskate/jws/compact.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
general_json ¶
1 2 3 |
|
Create a JWS in JSON General format based on this JWS Compact.
The resulting token will have a single signature which is the one from this token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unprotected_header |
Any
|
optional unprotected header to include in the JWS JSON |
None
|
Returns:
Type | Description |
---|---|
JwsJsonGeneral
|
the resulting token |
Source code in jwskate/jws/compact.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
jws_signature ¶
1 2 3 |
|
Return a JwsSignature based on this JWS Compact token.
Source code in jwskate/jws/compact.py
237 238 239 |
|
JwsJsonFlat ¶
Bases: JwsSignature
Represent a JWS with a single signature in JSON flat format.
Source code in jwskate/jws/json.py
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 |
|
payload
cached
property
¶
1 |
|
The JWS payload, decoded.
Returns:
Type | Description |
---|---|
bytes
|
The raw JWS payload. |
jws_signature
cached
property
¶
1 |
|
sign
classmethod
¶
1 2 3 4 5 6 7 8 9 10 |
|
Signs a payload into a JWS in JSON flat format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload |
bytes
|
the data to sign. |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the key to use |
required |
alg |
str | None
|
the signature alg to use |
None
|
extra_protected_headers |
Mapping[str, Any] | None
|
additional protected headers to include |
None
|
header |
Any | None
|
the unprotected header to include |
None
|
**kwargs |
Any
|
extra attributes to include in the JWS |
{}
|
Returns:
Type | Description |
---|---|
JwsJsonFlat
|
The JWS with the payload, signature, header and extra claims. |
Source code in jwskate/jws/json.py
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 |
|
generalize ¶
1 |
|
Create a JWS in JSON general format from this JWS in JSON flat.
Returns:
Type | Description |
---|---|
JwsJsonGeneral
|
A JwsJsonGeneral with the same payload and signature. |
Source code in jwskate/jws/json.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
signed_part ¶
1 |
|
Return the signed part from this JWS, as bytes.
This is a concatenation of the protected header and the payload, separated by a dot (.
).
Returns:
Type | Description |
---|---|
bytes
|
The signed data part. |
Source code in jwskate/jws/json.py
96 97 98 99 100 101 102 103 104 105 |
|
compact ¶
1 |
|
Create a JWS in compact format from this JWS JSON.
Returns:
Type | Description |
---|---|
JwsCompact
|
A |
Source code in jwskate/jws/json.py
107 108 109 110 111 112 113 114 |
|
verify_signature ¶
1 2 3 4 5 6 |
|
Verify this JWS signature with a given key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the key to use to validate this signature. |
required |
alg |
str | None
|
the signature alg, if only 1 is allowed. |
None
|
algs |
Iterable[str] | None
|
the allowed signature algs, if there are several. |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jws/json.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
JwsJsonGeneral ¶
Bases: BaseJsonDict
Represents a JWS in JSON general format (possibly with multiple signatures).
Source code in jwskate/jws/json.py
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 |
|
payload
cached
property
¶
1 |
|
The raw signed data.
Returns:
Type | Description |
---|---|
bytes
|
The signed data. |
signatures
cached
property
¶
1 |
|
The list of JwsSignature
from this JWS.
Returns:
Type | Description |
---|---|
list[JwsSignature]
|
The list of signatures from this JWS. |
sign
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Sign a payload with several keys and return the resulting JWS in JSON general format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload |
bytes
|
the data to sign |
required |
*signature_parameters |
tuple[Jwk | Mapping[str, Any], str, Mapping[str, Any] | None, Mapping[str, Any] | None] | tuple[Jwk | Mapping[str, Any], str, Mapping[str, Any] | None] | tuple[Jwk | Mapping[str, Any], str] | Jwk | Mapping[str, Any]
|
each of those parameter can be:
with:
|
()
|
Returns:
Type | Description |
---|---|
JwsJsonGeneral
|
the generated signatures in JSON General format. |
Source code in jwskate/jws/json.py
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 |
|
add_signature ¶
1 2 3 4 5 6 7 8 |
|
Add a new signature in this JWS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the private key to use |
required |
alg |
str | None
|
the signature algorithm |
None
|
extra_protected_headers |
Mapping[str, Any] | None
|
additional headers to include, as a {key: value} mapping |
None
|
header |
Mapping[str, Any] | None
|
the raw unprotected header to include in the signature |
None
|
Returns:
Type | Description |
---|---|
JwsJsonGeneral
|
the same JWS with the new signature included. |
Source code in jwskate/jws/json.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
signed_part ¶
1 2 3 4 5 |
|
Return the signed part from a given signature.
The signed part is a concatenation of the protected header from a specific signature, then the payload,
separated by a dot (.
).
You can select the specific signature with the signature_chooser
parameter.
By default, the first signature is selected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature_chooser |
Callable[[list[JwsSignature]], JwsSignature]
|
a callable that takes the list of signatures from this JWS as parameter, and returns the chosen signature. |
lambda : sigs[0]
|
Returns:
Type | Description |
---|---|
bytes
|
The raw signed part from the chosen signature. |
Source code in jwskate/jws/json.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
compact ¶
1 2 3 4 5 |
|
Create a compact JWS from a specific signature from this JWS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature_chooser |
Callable[[list[JwsSignature]], JwsSignature]
|
a callable that takes the list of signatures from this JWS as parameter and returns the choosen signature. |
lambda : sigs[0]
|
Returns:
Type | Description |
---|---|
JwsCompact
|
A JwsCompact with the payload and the chosen signature from this JWS. |
Source code in jwskate/jws/json.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
flatten ¶
1 2 3 4 5 |
|
Create a JWS in JSON flat format from a specific signature from this JWS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature_chooser |
Callable[[list[JwsSignature]], JwsSignature]
|
a callable that takes the list of signatures from this JWS as parameter and returns the choosen signature. |
lambda : sigs[0]
|
Returns:
Type | Description |
---|---|
JwsJsonFlat
|
A JwsJsonFlat with the payload and the chosen signature from this JWS. |
Source code in jwskate/jws/json.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
verify_signature ¶
1 2 3 4 5 6 |
|
Verify the signatures from this JWS.
It tries to validate each signature with the given key, and returns True
if at least one signature verifies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the public key to use |
required |
alg |
str | None
|
the signature algorithm to use, if only 1 is allowed. |
None
|
algs |
Iterable[str] | None
|
the allowed signature algorithms, if there are several. |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jws/json.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
InvalidSignature ¶
Bases: ValueError
Raised when trying to validate a token with an invalid signature.
Source code in jwskate/jws/signature.py
14 15 16 17 18 19 20 21 |
|
JwsSignature ¶
Bases: BaseJsonDict
Represent a JWS Signature.
A JWS Signature has:
- a protected header (as a JSON object)
- a signature value (as raw data)
- an unprotected header (as arbitrary JSON data)
- optional extra JSON attributes
Source code in jwskate/jws/signature.py
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 |
|
protected
cached
property
¶
1 |
|
The protected header.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
the protected headers, as a |
Raises:
Type | Description |
---|---|
AttributeError
|
if this signature doesn't have protected headers. |
header
property
¶
1 |
|
The unprotected header, unaltered.
Returns:
Type | Description |
---|---|
Any
|
The unprotected header |
signature
cached
property
¶
1 |
|
The raw signature.
Returns:
Type | Description |
---|---|
bytes
|
The raw signed data, unencoded |
Raises:
Type | Description |
---|---|
AttributeError
|
if no 'signature' member is present |
from_parts
classmethod
¶
1 2 3 4 5 6 |
|
Initialize a JwsSignature based on the provided parts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protected |
Mapping[str, Any]
|
the protected headers, as a key: value mapping |
required |
signature |
bytes
|
the raw signature value |
required |
header |
Any | None
|
the unprotected header, if any |
required |
**kwargs |
Any
|
extra attributes, if any |
{}
|
Returns:
Type | Description |
---|---|
S
|
A |
Source code in jwskate/jws/signature.py
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 |
|
sign
classmethod
¶
1 2 3 4 5 6 7 8 9 10 |
|
Sign a payload and return the generated JWS signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload |
bytes
|
the raw data to sign |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the signature key to use |
required |
alg |
str | None
|
the signature algorithm to use |
None
|
extra_protected_headers |
Mapping[str, Any] | None
|
additional protected headers to include, if any |
None
|
header |
Any | None
|
the unprotected header, if any. |
None
|
**kwargs |
Any
|
additional members to include in this signature |
{}
|
Returns:
Type | Description |
---|---|
S
|
The generated signature. |
Source code in jwskate/jws/signature.py
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 |
|
assemble_signed_part
classmethod
¶
1 2 3 |
|
Assemble the protected header and payload to sign, as specified in.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
Mapping[str, Any]
|
the protected headers |
required |
payload |
bytes | str
|
the raw payload to sign |
required |
Returns:
Type | Description |
---|---|
bytes
|
the raw data to sign |
Source code in jwskate/jws/signature.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
verify ¶
1 2 3 4 5 6 7 |
|
Verify this signature against the given payload using the provided key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload |
bytes
|
the raw payload |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the validation key to use |
required |
alg |
str | None
|
the signature alg t if only 1 is allowed |
None
|
algs |
Iterable[str] | None
|
the allowed signature algs, if there are several |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jws/signature.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
jwskate.jwe ¶
This module implements Json Web Encryption as described in [RFC7516].
[RFC7516] : https: //www.rfc-editor.org/rfc/rfc7516
InvalidJwe ¶
Bases: ValueError
Raised when an invalid JWE token is parsed.
Source code in jwskate/jwe/compact.py
25 26 |
|
JweCompact ¶
Bases: BaseCompactToken
Represents a Json Web Encryption object, in compact representation, as defined in RFC7516.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
bytes | str
|
the compact representation for this Jwe |
required |
Source code in jwskate/jwe/compact.py
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 |
|
enc
cached
property
¶
1 |
|
Return the enc
from the JWE header.
The enc
header contains the identifier of the CEK encryption algorithm.
Returns:
Type | Description |
---|---|
str
|
the enc value |
Raises:
Type | Description |
---|---|
AttributeError
|
if there is no enc header or it is not a string |
from_parts
classmethod
¶
1 2 3 4 5 6 7 8 |
|
Initialize a JweCompact
from its different parts (header, cek, iv, ciphertext, tag).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
Mapping[str, Any]
|
the headers (as a mapping of name: value) |
required |
cek |
bytes
|
the raw CEK |
required |
iv |
bytes
|
the raw IV |
required |
ciphertext |
bytes
|
the raw ciphertext |
required |
tag |
bytes
|
the authentication tag |
required |
Returns:
Type | Description |
---|---|
JweCompact
|
the initialized |
Source code in jwskate/jwe/compact.py
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 |
|
encrypt
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Encrypt an arbitrary plaintext into a JweCompact
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plaintext |
bytes | SupportsBytes
|
the raw plaintext to encrypt |
required |
key |
Jwk | Mapping[str, Any] | Any
|
the public or symmetric key to use for encryption |
required |
enc |
str
|
the encryption algorithm to use |
required |
alg |
str | None
|
the Key Management algorithm to use, if there is no 'alg' header defined in the |
None
|
extra_headers |
Mapping[str, Any] | None
|
additional headers to include in the generated token |
None
|
cek |
bytes | None
|
the CEK to force use, for algorithms relying on a random CEK.
Leave |
None
|
iv |
bytes | None
|
the IV to force use. Leave |
None
|
epk |
Jwk | None
|
the EPK to force use. Leave |
None
|
Returns:
Type | Description |
---|---|
JweCompact
|
the generated JweCompact instance |
Source code in jwskate/jwe/compact.py
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 |
|
unwrap_cek ¶
1 2 3 4 5 |
|
Unwrap the CEK from this Jwe
using the provided key or password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key_or_password |
Jwk | Mapping[str, Any] | bytes | str
|
the decryption JWK or password |
required |
alg |
str | None
|
allowed key management algorithm, if there is only 1 |
None
|
algs |
Iterable[str] | None
|
allowed key managements algorithms, if there are several |
None
|
Returns:
Type | Description |
---|---|
Jwk
|
the unwrapped CEK, as a SymmetricJwk |
Source code in jwskate/jwe/compact.py
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 |
|
decrypt ¶
1 2 3 4 5 6 |
|
Decrypt the payload from this JWE using a decryption key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Jwk | Mapping[str, Any] | Any
|
the decryption key |
required |
alg |
str | None
|
allowed key management algorithm, if there is only 1 |
None
|
algs |
Iterable[str] | None
|
allowed keys management algorithms, if there are several |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the decrypted payload |
Source code in jwskate/jwe/compact.py
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 |
|
decrypt_jwt ¶
1 2 3 4 5 6 |
|
Convenience method to decrypt an inner JWT.
Takes the same args as decrypt(), but returns a SignedJwt
.
Raises:
Type | Description |
---|---|
InvalidJwt
|
if the content is not a syntactically valid signed JWT. |
Source code in jwskate/jwe/compact.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
encrypt_with_password
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Encrypt a payload with a password and return the resulting JweCompact.
This performs symmetric encryption using PBES2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plaintext |
SupportsBytes | bytes
|
the data to encrypt |
required |
password |
SupportsBytes | bytes | str
|
the password to use |
required |
alg |
str
|
the Key Management alg to use |
required |
enc |
str
|
the Payload Encryption alg to use |
required |
salt |
bytes | None
|
the salt to use. Leave |
None
|
count |
int
|
the number of PBES2 iterations (recommended minimum 1000) |
2000
|
cek |
bytes | None
|
the CEK to force use. Leave |
None
|
iv |
bytes | None
|
the IV to force use. Leave |
None
|
Returns:
Type | Description |
---|---|
JweCompact
|
the resulting JweCompact |
Raises:
Type | Description |
---|---|
UnsupportedAlg
|
if the key management alg is not supported |
ValueError
|
if the |
Source code in jwskate/jwe/compact.py
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 |
|
unwrap_cek_with_password ¶
1 |
|
Unwrap a CEK using a password. Works only for password-encrypted JWE Tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
password |
bytes | str
|
the decryption password |
required |
Returns:
Type | Description |
---|---|
Jwk
|
the CEK, as a SymmetricJwk instance |
Raises:
Type | Description |
---|---|
UnsupportedAlg
|
if the token key management algorithm is not supported |
AttributeError
|
if the token misses the PBES2-related headers |
Source code in jwskate/jwe/compact.py
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 |
|
decrypt_with_password ¶
1 |
|
Decrypt this JWE with a password.
This only works for tokens encrypted with a password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
password |
bytes | str
|
the password to use |
required |
Returns:
Type | Description |
---|---|
bytes
|
the unencrypted payload |
Source code in jwskate/jwe/compact.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
jwskate.jwa ¶
This module implements the Json Web Algorithms as defined in RFC7518.
Each algorithm is represented as a wrapper around a symmetric or asymmetric key, and exposes the
cryptographic operations as methods. The cryptographic operations themselves are delegated to
cryptography
.
P_256
module-attribute
¶
1 2 3 4 5 |
|
P-256 curve.
P_384
module-attribute
¶
1 2 3 4 5 |
|
P-384 curve.
P_521
module-attribute
¶
1 2 3 4 5 |
|
P-521 curve.
secp256k1
module-attribute
¶
1 2 3 4 5 |
|
X448
module-attribute
¶
1 2 3 4 5 6 7 8 |
|
X448 curve.
X25519
module-attribute
¶
1 2 3 4 5 6 7 8 |
|
X25519 curve.
Ed448
module-attribute
¶
1 2 3 4 5 6 7 8 |
|
Ed448 curve.
Ed25519
module-attribute
¶
1 2 3 4 5 6 7 8 |
|
Ed25519 curve.
BaseAESEncryptionAlg ¶
Bases: BaseSymmetricAlg
Base class for AES encryption algorithms.
Source code in jwskate/jwa/base.py
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 |
|
check_key
classmethod
¶
1 |
|
Check that a key is suitable for this algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
bytes
|
the key to check |
required |
Raises:
Type | Description |
---|---|
InvalidKey
|
if the key is not suitable |
Source code in jwskate/jwa/base.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
generate_key
classmethod
¶
1 |
|
Generate a key of an appropriate size for this AES alg subclass.
Returns:
Type | Description |
---|---|
BinaPy
|
a random AES key |
Source code in jwskate/jwa/base.py
245 246 247 248 249 250 251 252 253 |
|
generate_iv
classmethod
¶
1 |
|
Generate an Initialisation Vector of the appropriate size.
Returns:
Type | Description |
---|---|
BinaPy
|
a random IV |
Source code in jwskate/jwa/base.py
255 256 257 258 259 260 261 262 263 |
|
encrypt ¶
1 2 3 4 5 6 |
|
Encrypt arbitrary data, with optional Authenticated Encryption.
This needs as parameters:
- the raw data to encrypt (
plaintext
) - a given random Initialisation Vector (
iv
) of the appropriate size - optional Additional Authentication Data (
aad
)
And returns a tuple (ciphered_data, authentication_tag)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plaintext |
bytes | SupportsBytes
|
the data to encrypt |
required |
iv |
bytes | SupportsBytes
|
the Initialisation Vector to use |
required |
aad |
bytes | SupportsBytes | None
|
the Additional Authentication Data |
None
|
Returns:
Type | Description |
---|---|
tuple[BinaPy, BinaPy]
|
a tuple of ciphered data and authentication tag |
Source code in jwskate/jwa/base.py
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 |
|
decrypt ¶
1 2 3 4 5 6 7 |
|
Decrypt and verify a ciphertext with Authenticated Encryption.
This needs:
- the raw encrypted Data (
ciphertext
) and Authentication Tag (auth_tag
) that were produced by encryption, - the same Initialisation Vector (
iv
) and optional Additional Authentication Data that were provided for encryption.
Returns the resulting clear text data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ciphertext |
bytes | SupportsBytes
|
the data to decrypt |
required |
iv |
bytes | SupportsBytes
|
the Initialisation Vector to use. Must be the same one used during encryption |
required |
auth_tag |
bytes | SupportsBytes
|
the authentication tag |
required |
aad |
bytes | SupportsBytes | None
|
the Additional Authentication Data. Must be the same one used during encryption |
None
|
Returns:
Type | Description |
---|---|
BinaPy
|
the deciphered data |
Source code in jwskate/jwa/base.py
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 |
|
with_random_key
classmethod
¶
1 |
|
Initialize this alg with a random key.
Returns:
Type | Description |
---|---|
Self
|
a subclass of |
Source code in jwskate/jwa/base.py
323 324 325 326 327 328 329 330 331 332 |
|
BaseAlg ¶
Base class for all algorithms.
An algorithm has a name
and a description
, whose reference is found in IANA JOSE registry.
Source code in jwskate/jwa/base.py
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 |
|
read_only
class-attribute
instance-attribute
¶
1 |
|
For algs that are considered insecure, set to True to allow only signature verification or decryption of existing data, but don't allow new signatures or encryption.
__repr__ ¶
1 |
|
Use the name of the alg as repr.
Source code in jwskate/jwa/base.py
46 47 48 |
|
with_random_key
classmethod
¶
1 |
|
Initialize an instance of this alg with a randomly-generated key.
Source code in jwskate/jwa/base.py
50 51 52 53 |
|
BaseAsymmetricAlg ¶
Bases: Generic[Kpriv, Kpub]
, BaseAlg
Base class for asymmetric algorithms. Those can be initialised with a private or public key.
The available cryptographic operations will depend on the alg and the provided key type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Kpriv | Kpub
|
the key to use. |
required |
Source code in jwskate/jwa/base.py
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 |
|
check_key
classmethod
¶
1 |
|
Check that a given key is suitable for this alg class.
This must be implemented by subclasses as required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Kpriv | Kpub
|
the key to use. |
required |
Returns:
Type | Description |
---|---|
None
|
Returns None. Raises an exception if the key is not suitable. |
Raises:
Type | Description |
---|---|
Exception
|
if the key is not suitable for use with this alg class |
Source code in jwskate/jwa/base.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
private_key_required ¶
1 |
|
Check if this alg is initialised with a private key, as a context manager.
Yields:
Type | Description |
---|---|
Kpriv
|
the private key |
Raises:
Type | Description |
---|---|
PrivateKeyRequired
|
if the configured key is not private |
Source code in jwskate/jwa/base.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
public_key_required ¶
1 |
|
Check if this alg is initialised with a public key, as a context manager.
Yields:
Type | Description |
---|---|
Kpub
|
The public key |
Raises:
Type | Description |
---|---|
PublicKeyRequired
|
if the configured key is private |
Source code in jwskate/jwa/base.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
public_key ¶
1 |
|
Return the public key matching the private key.
Source code in jwskate/jwa/base.py
177 178 179 180 181 |
|
public_alg ¶
1 |
|
Return an alg instance initialised with the public key.
Source code in jwskate/jwa/base.py
183 184 185 186 |
|
BaseKeyManagementAlg ¶
Bases: BaseAlg
Base class for Key Management algorithms.
Source code in jwskate/jwa/base.py
335 336 337 338 |
|
BaseSignatureAlg ¶
Bases: BaseAlg
Base class for signature algorithms.
Source code in jwskate/jwa/base.py
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 |
|
sign ¶
1 |
|
Sign arbitrary data, return the signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
raw data to sign |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the raw signature |
Source code in jwskate/jwa/base.py
195 196 197 198 199 200 201 202 203 204 205 |
|
verify ¶
1 2 3 4 |
|
Verify a signature against some data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
the raw data to verify |
required |
signature |
bytes | SupportsBytes
|
the raw signature |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwa/base.py
207 208 209 210 211 212 213 214 215 216 217 218 |
|
BaseSymmetricAlg ¶
Bases: BaseAlg
Base class for Symmetric algorithms (using a raw bytes key).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
bytes
|
the key to use for cryptographic operations |
required |
Source code in jwskate/jwa/base.py
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 |
|
check_key
classmethod
¶
1 |
|
Check that a given key is suitable for this alg class.
This raises an exception if the key is not suitable. This method must be implemented by subclasses as required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
bytes
|
the key to check for this alg class |
required |
Returns:
Type | Description |
---|---|
None
|
Returns |
Raises:
Type | Description |
---|---|
InvalidKey
|
if the key is not suitable for this algorithm |
Source code in jwskate/jwa/base.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
supports_key
classmethod
¶
1 |
|
Return True
if the given key is suitable for this alg class, or False
otherwise.
This is a convenience wrapper around check_key(key)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
bytes
|
the key to check for this alg class |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwa/base.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
MismatchingAuthTag ¶
Bases: InvalidTag
Raised during decryption, when the Authentication Tag doesn't match the expected value.
Source code in jwskate/jwa/base.py
341 342 |
|
PrivateKeyRequired ¶
Bases: AttributeError
Raised when a public key is provided for an operation that requires a private key.
Source code in jwskate/jwa/base.py
14 15 |
|
PublicKeyRequired ¶
Bases: AttributeError
Raised when a private key is provided for an operation that requires a public key.
Source code in jwskate/jwa/base.py
18 19 |
|
EllipticCurve
dataclass
¶
A descriptive class for Elliptic Curves.
Elliptic Curves have a name, a cryptography.ec.EllipticCurve
, and a coordinate size.
Source code in jwskate/jwa/ec.py
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 |
|
name
instance-attribute
¶
1 |
|
Curve name as defined in IANA JOSE.
This name will appear in alg
or enc
fields in JOSE headers.
cryptography_curve
instance-attribute
¶
1 |
|
cryptography
curve instance.
instances
class-attribute
¶
1 |
|
Registry of subclasses, in a {name: instance} mapping.
__post_init__ ¶
1 |
|
Automatically register subclasses in the instance registry.
Source code in jwskate/jwa/ec.py
35 36 37 |
|
A128CBC_HS256 ¶
Bases: BaseAesCbcHmacSha2
AES_128_CBC_HMAC_SHA_256.
Source code in jwskate/jwa/encryption/aescbchmac.py
166 167 168 169 170 171 172 173 174 175 |
|
A128GCM ¶
Bases: BaseAESGCM
AES GCM using 128-bit key.
Source code in jwskate/jwa/encryption/aesgcm.py
100 101 102 103 104 105 |
|
A192CBC_HS384 ¶
Bases: BaseAesCbcHmacSha2
AES_192_CBC_HMAC_SHA_384.
Source code in jwskate/jwa/encryption/aescbchmac.py
178 179 180 181 182 183 184 185 186 187 |
|
A192GCM ¶
Bases: BaseAESGCM
AES GCM using 192-bit key.
Source code in jwskate/jwa/encryption/aesgcm.py
108 109 110 111 112 113 |
|
A256CBC_HS512 ¶
Bases: BaseAesCbcHmacSha2
AES_256_CBC_HMAC_SHA_512.
Source code in jwskate/jwa/encryption/aescbchmac.py
190 191 192 193 194 195 196 197 198 199 |
|
A256GCM ¶
Bases: BaseAESGCM
AES GCM using 256-bit key.
Source code in jwskate/jwa/encryption/aesgcm.py
116 117 118 119 120 121 |
|
A128GCMKW ¶
Bases: BaseAesGcmKeyWrap
Key wrapping with AES GCM using 128-bit key.
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
70 71 72 73 74 75 |
|
A128KW ¶
Bases: BaseAesKeyWrap
AES Key Wrap with default initial value using 128-bit key.
Source code in jwskate/jwa/key_mgmt/aeskw.py
71 72 73 74 75 76 |
|
A192GCMKW ¶
Bases: BaseAesGcmKeyWrap
Key wrapping with AES GCM using 192-bit key.
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
78 79 80 81 82 83 |
|
A192KW ¶
Bases: BaseAesKeyWrap
AES Key Wrap with default initial value using 192-bit key.
Source code in jwskate/jwa/key_mgmt/aeskw.py
79 80 81 82 83 84 |
|
A256GCMKW ¶
Bases: BaseAesGcmKeyWrap
Key wrapping with AES GCM using 256-bit key.
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
86 87 88 89 90 91 |
|
A256KW ¶
Bases: BaseAesKeyWrap
AES Key Wrap with default initial value using 256-bit key.
Source code in jwskate/jwa/key_mgmt/aeskw.py
87 88 89 90 91 92 |
|
BaseAesGcmKeyWrap ¶
Bases: BaseAESGCM
, BaseKeyManagementAlg
Base class for AES-GCM Key wrapping algorithms.
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
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 |
|
iv_size
class-attribute
instance-attribute
¶
1 |
|
Initialisation Vector size, in bits.
wrap_key ¶
1 2 3 4 5 |
|
Wrap a symmetric key, which is typically used as Content Encryption Key (CEK).
This method is used by the sender of the encrypted message.
This needs a random Initialisation Vector (iv
) of the appropriate size,
which you can generate using the classmethod generate_iv()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plainkey |
bytes | SupportsBytes
|
the key to wrap |
required |
iv |
bytes | SupportsBytes
|
the Initialisation Vector to use |
required |
Returns:
Type | Description |
---|---|
tuple[BinaPy, BinaPy]
|
a tuple (wrapped_key, authentication_tag) |
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
unwrap_key ¶
1 2 3 4 5 6 |
|
Unwrap a symmetric key.
This method is used by the recipient of an encrypted message.
This requires: - the same IV that was provided during encryption - the same Authentication Tag that was generated during encryption
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cipherkey |
bytes | SupportsBytes
|
the wrapped key |
required |
tag |
bytes | SupportsBytes
|
the authentication tag |
required |
iv |
bytes | SupportsBytes
|
the Initialisation Vector |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the unwrapped key. |
Source code in jwskate/jwa/key_mgmt/aesgcmkw.py
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 |
|
BaseAesKeyWrap ¶
Bases: BaseKeyManagementAlg
, BaseSymmetricAlg
Base class for AES KW algorithms.
Source code in jwskate/jwa/key_mgmt/aeskw.py
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 |
|
check_key
classmethod
¶
1 |
|
Check that a key is valid for usage with this algorithm.
To be valid, a key must be bytes
and be of appropriate length (128, 192 or 256 bits).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
bytes
|
a key to check |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the key is not appropriate |
Source code in jwskate/jwa/key_mgmt/aeskw.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
wrap_key ¶
1 |
|
Wrap a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plainkey |
bytes
|
the key to wrap. |
required |
Returns:
Name | Type | Description |
---|---|---|
BinaPy |
BinaPy
|
the wrapped key. |
Source code in jwskate/jwa/key_mgmt/aeskw.py
43 44 45 46 47 48 49 50 51 52 53 |
|
unwrap_key ¶
1 |
|
Unwrap a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cipherkey |
bytes | SupportsBytes
|
the wrapped key. |
required |
Returns:
Name | Type | Description |
---|---|---|
BinaPy |
BinaPy
|
the unwrapped key. |
Source code in jwskate/jwa/key_mgmt/aeskw.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
BaseEcdhEs_AesKw ¶
Bases: EcdhEs
Base class for ECDH-ES+AESKW algorithms.
Source code in jwskate/jwa/key_mgmt/ecdh.py
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 |
|
wrap_key_with_epk ¶
1 2 3 4 5 6 7 8 9 |
|
Wrap a key for content encryption.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plainkey |
bytes
|
the key to wrap |
required |
ephemeral_private_key |
EllipticCurvePrivateKey | X25519PrivateKey | X448PrivateKey
|
the EPK to use |
required |
**headers |
Any
|
additional headers for CEK derivation |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the wrapped CEK |
Source code in jwskate/jwa/key_mgmt/ecdh.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
unwrap_key_with_epk ¶
1 2 3 4 5 6 7 8 9 |
|
Unwrap a key for content decryption.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cipherkey |
bytes | SupportsBytes
|
the wrapped key |
required |
ephemeral_public_key |
EllipticCurvePublicKey | X25519PublicKey | X448PublicKey
|
the EPK |
required |
**headers |
Any
|
additional headers for CEK derivation |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the unwrapped key |
Source code in jwskate/jwa/key_mgmt/ecdh.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
BasePbes2 ¶
Bases: BaseKeyManagementAlg
Base class for PBES2 based algorithms.
PBES2 derives a cryptographic key from a human-provided password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
password |
SupportsBytes | bytes | str
|
the encryption/decryption password to use |
required |
Source code in jwskate/jwa/key_mgmt/pbes2.py
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 |
|
generate_salt
classmethod
¶
1 |
|
Generate a salt that is suitable for use for encryption.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
size of the generated salt, in bytes |
12
|
Returns:
Type | Description |
---|---|
BinaPy
|
the generated salt |
Raises:
Type | Description |
---|---|
ValueError
|
if the salt is less than 8 bytes long |
Source code in jwskate/jwa/key_mgmt/pbes2.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
derive ¶
1 |
|
Derive an encryption key.
Derivation is based on the configured password, a given salt and the number of PBKDF iterations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
salt |
bytes
|
the generated salt |
required |
count |
int
|
number of PBKDF iterations |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the generated encryption/decryption key |
Source code in jwskate/jwa/key_mgmt/pbes2.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
wrap_key ¶
1 2 3 |
|
Wrap a key using this alg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plainkey |
bytes
|
the key to wrap |
required |
salt |
bytes
|
the salt to use |
required |
count |
int
|
the number of PBKDF iterations |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the wrapped key |
Source code in jwskate/jwa/key_mgmt/pbes2.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
unwrap_key ¶
1 2 3 |
|
Unwrap a key using this alg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cipherkey |
bytes
|
the wrapped key |
required |
salt |
bytes
|
the salt to use |
required |
count |
int
|
the number of PBKDF iterations |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the unwrapped key |
Source code in jwskate/jwa/key_mgmt/pbes2.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
BaseRsaKeyWrap ¶
Bases: BaseKeyManagementAlg
, BaseAsymmetricAlg[RSAPrivateKey, RSAPublicKey]
Base class for RSA Key Wrapping algorithms.
Source code in jwskate/jwa/key_mgmt/rsa.py
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 |
|
wrap_key ¶
1 |
|
Wrap a symmetric key using this algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plainkey |
bytes
|
the symmetric key to wrap |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the wrapped key |
Raises:
Type | Description |
---|---|
PublicKeyRequired
|
if this algorithm is initialized with a private key instead of a public key |
Source code in jwskate/jwa/key_mgmt/rsa.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
unwrap_key ¶
1 |
|
Unwrap a symmetric key with this alg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cipherkey |
bytes | SupportsBytes
|
the wrapped key |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the unwrapped clear-text key |
Source code in jwskate/jwa/key_mgmt/rsa.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
DirectKeyUse ¶
Bases: BaseKeyManagementAlg
, BaseSymmetricAlg
Direct use of a shared symmetric key as the CEK.
Source code in jwskate/jwa/key_mgmt/dir.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
direct_key ¶
1 |
|
Check that the current key is appropriate for a given alg and return that same key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
aesalg |
type[BaseSymmetricAlg]
|
the AES encryption alg to use |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the current configured key, as-is |
Source code in jwskate/jwa/key_mgmt/dir.py
16 17 18 19 20 21 22 23 24 25 26 27 |
|
EcdhEs ¶
Bases: BaseKeyManagementAlg
, BaseAsymmetricAlg[Union[EllipticCurvePrivateKey, X25519PrivateKey, X448PrivateKey], Union[EllipticCurvePublicKey, X25519PublicKey, X448PublicKey]]
Elliptic Curve Diffie-Hellman Ephemeral Static key agreement using Concat KDF.
Source code in jwskate/jwa/key_mgmt/ecdh.py
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 |
|
otherinfo
classmethod
¶
1 2 3 |
|
Build the "otherinfo" parameter for Concat KDF Hash.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alg |
str
|
identifier for the encryption alg |
required |
apu |
bytes
|
Agreement PartyUInfo |
required |
apv |
bytes
|
Agreement PartyVInfo |
required |
key_size |
int
|
length of the generated key |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the "otherinfo" value |
Source code in jwskate/jwa/key_mgmt/ecdh.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
ecdh
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Perform an Elliptic Curve Diffie-Hellman key exchange.
This derives a shared key between a sender and a receiver, based on a public and a private key from each side. ECDH exchange produces the same key with either a sender private key and a recipient public key, or the matching sender public key and recipient private key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key |
EllipticCurvePrivateKey | X25519PrivateKey | X448PrivateKey
|
a private EC key |
required |
public_key |
EllipticCurvePublicKey | X25519PublicKey | X448PublicKey
|
a public EC key |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
a shared key |
Source code in jwskate/jwa/key_mgmt/ecdh.py
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 |
|
derive
classmethod
¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Derive a key using ECDH and Concat KDF Hash.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key |
EllipticCurvePrivateKey | X25519PrivateKey | X448PrivateKey
|
the private key |
required |
public_key |
EllipticCurvePublicKey | X25519PublicKey | X448PublicKey
|
the public key |
required |
otherinfo |
bytes
|
the Concat KDF "otherinfo" parameter |
required |
key_size |
int
|
the expected CEK key size |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the derived key |
Source code in jwskate/jwa/key_mgmt/ecdh.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
generate_ephemeral_key ¶
1 2 3 4 5 |
|
Generate an ephemeral key that is suitable for use with this algorithm.
Returns:
Type | Description |
---|---|
EllipticCurvePrivateKey | X25519PrivateKey | X448PrivateKey
|
a generated EllipticCurvePrivateKey, on the same curve as this algorithm key |
Source code in jwskate/jwa/key_mgmt/ecdh.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
sender_key ¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Compute a CEK for encryption of a message. This method is meant for usage by a sender.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ephemeral_private_key |
EllipticCurvePrivateKey | X25519PrivateKey | X448PrivateKey
|
the EPK to use for this key |
required |
alg |
str
|
the content encryption algorithm identifier |
required |
key_size |
int
|
the expected CEK size |
required |
**headers |
Any
|
additional headers to include for CEK derivation |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the CEK for encryption by the sender |
Source code in jwskate/jwa/key_mgmt/ecdh.py
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 |
|
recipient_key ¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Compute a shared key, for use by the recipient of an encrypted message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ephemeral_public_key |
EllipticCurvePublicKey | X25519PublicKey | X448PublicKey
|
the EPK, as received from sender |
required |
alg |
str
|
the content encryption algorithm identifier |
required |
key_size |
int
|
the CEK size |
required |
**headers |
Any
|
additional headers as received from sender |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the CEK for decryption by the recipient |
Source code in jwskate/jwa/key_mgmt/ecdh.py
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 |
|
EcdhEs_A128KW ¶
Bases: BaseEcdhEs_AesKw
ECDH-ES using Concat KDF and "A128KW" wrapping.
Source code in jwskate/jwa/key_mgmt/ecdh.py
256 257 258 259 260 261 |
|
EcdhEs_A192KW ¶
Bases: BaseEcdhEs_AesKw
ECDH-ES using Concat KDF and "A192KW" wrapping.
Source code in jwskate/jwa/key_mgmt/ecdh.py
264 265 266 267 268 269 |
|
EcdhEs_A256KW ¶
Bases: BaseEcdhEs_AesKw
ECDH-ES using Concat KDF and "A256KW" wrapping.
Source code in jwskate/jwa/key_mgmt/ecdh.py
272 273 274 275 276 277 |
|
Pbes2_HS256_A128KW ¶
Bases: BasePbes2
PBES2 with HMAC SHA-256 and "A128KW" wrapping.
Source code in jwskate/jwa/key_mgmt/pbes2.py
111 112 113 114 115 116 117 |
|
Pbes2_HS384_A192KW ¶
Bases: BasePbes2
PBES2 with HMAC SHA-384 and "A192KW" wrapping.
Source code in jwskate/jwa/key_mgmt/pbes2.py
120 121 122 123 124 125 126 |
|
Pbes2_HS512_A256KW ¶
Bases: BasePbes2
PBES2 with HMAC SHA-512 and "A256KW" wrapping.
Source code in jwskate/jwa/key_mgmt/pbes2.py
129 130 131 132 133 134 135 |
|
RsaEsOaep ¶
Bases: BaseRsaKeyWrap
RSAES OAEP using default parameters.
Source code in jwskate/jwa/key_mgmt/rsa.py
84 85 86 87 88 89 90 91 92 93 94 |
|
RsaEsOaepSha256 ¶
Bases: BaseRsaKeyWrap
RSAES OAEP using SHA-256 and MGF1 with SHA-256.
Source code in jwskate/jwa/key_mgmt/rsa.py
97 98 99 100 101 102 103 104 105 106 107 |
|
RsaEsOaepSha384 ¶
Bases: BaseRsaKeyWrap
RSA-OAEP using SHA-384 and MGF1 with SHA-384.
Source code in jwskate/jwa/key_mgmt/rsa.py
110 111 112 113 114 115 116 117 118 119 120 |
|
RsaEsOaepSha512 ¶
Bases: BaseRsaKeyWrap
RSA-OAEP using SHA-512 and MGF1 with SHA-512.
Source code in jwskate/jwa/key_mgmt/rsa.py
123 124 125 126 127 128 129 130 131 132 133 |
|
RsaEsPcks1v1_5 ¶
Bases: BaseRsaKeyWrap
RSAES-PKCS1-v1_5.
Source code in jwskate/jwa/key_mgmt/rsa.py
74 75 76 77 78 79 80 81 |
|
OKPCurve
dataclass
¶
Represent an Octet Key Pair (OKP) Curve.
Source code in jwskate/jwa/okp.py
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 |
|
name
instance-attribute
¶
1 |
|
Curve name as defined in IANA JOSE.
This name will appear in crv
headers.
cryptography_private_key_class
instance-attribute
¶
1 |
|
cryptography
private key class.
cryptography_public_key_class
instance-attribute
¶
1 |
|
cryptography
public key class.
instances
class-attribute
¶
1 |
|
Registry of subclasses, in a {name: instance} mapping.
__post_init__ ¶
1 |
|
Automatically registers subclasses in the instance registry.
Source code in jwskate/jwa/okp.py
79 80 81 |
|
ES256 ¶
Bases: BaseECSignatureAlg
ECDSA using P-256 and SHA-256.
Source code in jwskate/jwa/signature/ec.py
86 87 88 89 90 91 92 |
|
ES256K ¶
Bases: BaseECSignatureAlg
ECDSA using secp256k1 and SHA-256.
Source code in jwskate/jwa/signature/ec.py
113 114 115 116 117 118 119 |
|
ES384 ¶
Bases: BaseECSignatureAlg
ECDSA using P-384 and SHA-384.
Source code in jwskate/jwa/signature/ec.py
95 96 97 98 99 100 101 |
|
ES512 ¶
Bases: BaseECSignatureAlg
ECDSA using P-521 and SHA-512.
Source code in jwskate/jwa/signature/ec.py
104 105 106 107 108 109 110 |
|
HS256 ¶
Bases: BaseHMACSigAlg
HMAC using SHA-256.
Source code in jwskate/jwa/signature/hmac.py
49 50 51 52 53 54 55 |
|
HS384 ¶
Bases: BaseHMACSigAlg
HMAC using SHA-384.
Source code in jwskate/jwa/signature/hmac.py
58 59 60 61 62 63 64 |
|
HS512 ¶
Bases: BaseHMACSigAlg
HMAC using SHA-512.
Source code in jwskate/jwa/signature/hmac.py
67 68 69 70 71 72 73 |
|
PS256 ¶
Bases: BaseRSASigAlg
RSASSA-PSS using SHA-256 and MGF1 with SHA-256.
Source code in jwskate/jwa/signature/rsa.py
111 112 113 114 115 116 117 |
|
PS384 ¶
Bases: BaseRSASigAlg
RSASSA-PSS using SHA-384 and MGF1 with SHA-384.
Source code in jwskate/jwa/signature/rsa.py
120 121 122 123 124 125 126 |
|
PS512 ¶
Bases: BaseRSASigAlg
RSASSA-PSS using SHA-512 and MGF1 with SHA-512.
Source code in jwskate/jwa/signature/rsa.py
129 130 131 132 133 134 135 |
|
RS256 ¶
Bases: BaseRSASigAlg
RSASSA-PKCS1-v1_5 using SHA-256.
Source code in jwskate/jwa/signature/rsa.py
87 88 89 90 91 92 |
|
RS384 ¶
Bases: BaseRSASigAlg
RSASSA-PKCS1-v1_5 using SHA-384.
Source code in jwskate/jwa/signature/rsa.py
95 96 97 98 99 100 |
|
RS512 ¶
Bases: BaseRSASigAlg
RSASSA-PKCS1-v1_5 using SHA-256.
Source code in jwskate/jwa/signature/rsa.py
103 104 105 106 107 108 |
|
BaseECSignatureAlg ¶
Bases: BaseAsymmetricAlg[EllipticCurvePrivateKey, EllipticCurvePublicKey]
, BaseSignatureAlg
Base class for Elliptic Curve signature algorithms.
Source code in jwskate/jwa/signature/ec.py
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 |
|
BaseHMACSigAlg ¶
Bases: BaseSymmetricAlg
, BaseSignatureAlg
Base class for HMAC signature algorithms.
Source code in jwskate/jwa/signature/hmac.py
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 |
|
BaseRSASigAlg ¶
Bases: BaseAsymmetricAlg[RSAPrivateKey, RSAPublicKey]
, BaseSignatureAlg
Base class for RSA based signature algorithms.
Source code in jwskate/jwa/signature/rsa.py
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 |
|
sign ¶
1 |
|
Sign arbitrary data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
the data to sign |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
the generated signature |
Raises:
Type | Description |
---|---|
NotImplementedError
|
for algorithms that are considered insecure, only signature verification is available |
PrivateKeyRequired
|
if the configured key is not private |
Source code in jwskate/jwa/signature/rsa.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
verify ¶
1 2 3 4 |
|
Verify a signature against some data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes | SupportsBytes
|
the data to verify |
required |
signature |
bytes | SupportsBytes
|
the signature |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in jwskate/jwa/signature/rsa.py
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 |
|
Ed448Dsa ¶
Bases: BaseAsymmetricAlg[Ed448PrivateKey, Ed448PublicKey]
, BaseSignatureAlg
EdDSA signature algorithm with Ed25519 curve.
Source code in jwskate/jwa/signature/eddsa.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
Ed25519Dsa ¶
Bases: BaseAsymmetricAlg[Ed25519PrivateKey, Ed25519PublicKey]
, BaseSignatureAlg
EdDSA signature algorithm with Ed25519 curve.
Source code in jwskate/jwa/signature/eddsa.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
EdDsa ¶
Bases: BaseAsymmetricAlg[Union[Ed25519PrivateKey, Ed448PrivateKey], Union[Ed25519PublicKey, Ed448PublicKey]]
, BaseSignatureAlg
EdDSA signature algorithms.
Source code in jwskate/jwa/signature/eddsa.py
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 |
|