Skip to content

Commit 9acdce8

Browse files
committed
Sub-packages for algorithms
0 parents  commit 9acdce8

10 files changed

+319
-0
lines changed

.github/CONTRIBUTING.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Contributing
2+
3+
This repository is a sub repository of [the JWT Framework](https://github.com/web-token/jwt-framework) project and is READ ONLY.
4+
Please do not submit any Pull Requests here. It will be automatically closed.

.github/PULL_REQUEST_TEMPLATE.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Please do not submit any Pull Requests here. It will be automatically closed.
2+
3+
You should submit it here: https://github.com/web-token/jwt-framework/pulls

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014-2018 Spomky-Labs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
RSA Based Key Encryption Algorithms For JWT-Framework
2+
=====================================================
3+
4+
This repository is a sub repository of [the JWT Framework](https://github.com/web-token/jwt-framework) project and is READ ONLY.
5+
6+
**Please do not submit any Pull Request here.**
7+
You should go to [the main repository](https://github.com/web-token/jwt-framework) instead.
8+
9+
# Documentation
10+
11+
The official documentation is available as https://web-token.spomky-labs.com/
12+
13+
# Licence
14+
15+
This software is release under [MIT licence](LICENSE).

RSA.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2018 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15+
16+
use Jose\Component\Core\JWK;
17+
use Jose\Component\Core\Util\RSAKey;
18+
use Jose\Component\Encryption\Util\RSACrypt;
19+
20+
abstract class RSA implements KeyEncryption
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function allowedKeyTypes(): array
26+
{
27+
return ['RSA'];
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function encryptKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string
34+
{
35+
$this->checkKey($key);
36+
$pub = RSAKey::toPublic(RSAKey::createFromJWK($key));
37+
38+
return RSACrypt::encrypt($pub, $cek, $this->getEncryptionMode(), $this->getHashAlgorithm());
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string
45+
{
46+
$this->checkKey($key);
47+
if (!$key->has('d')) {
48+
throw new \InvalidArgumentException('The key is not a private key');
49+
}
50+
$priv = RSAKey::createFromJWK($key);
51+
52+
return RSACrypt::decrypt($priv, $encrypted_cek, $this->getEncryptionMode(), $this->getHashAlgorithm());
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function getKeyManagementMode(): string
59+
{
60+
return self::MODE_ENCRYPT;
61+
}
62+
63+
/**
64+
* @param JWK $key
65+
*/
66+
protected function checkKey(JWK $key)
67+
{
68+
if (!in_array($key->get('kty'), $this->allowedKeyTypes())) {
69+
throw new \InvalidArgumentException('Wrong key type.');
70+
}
71+
}
72+
73+
/**
74+
* @return int
75+
*/
76+
abstract protected function getEncryptionMode(): int;
77+
78+
/**
79+
* @return null|string
80+
*/
81+
abstract protected function getHashAlgorithm(): ?string;
82+
}

RSA15.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2018 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15+
16+
use Jose\Component\Encryption\Util\RSACrypt;
17+
18+
final class RSA15 extends RSA
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
protected function getEncryptionMode(): int
24+
{
25+
return RSACrypt::ENCRYPTION_PKCS1;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function getHashAlgorithm(): ?string
32+
{
33+
return null;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function name(): string
40+
{
41+
return 'RSA1_5';
42+
}
43+
}

RSAOAEP.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2018 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15+
16+
use Jose\Component\Encryption\Util\RSACrypt;
17+
18+
final class RSAOAEP extends RSA
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
protected function getEncryptionMode(): int
24+
{
25+
return RSACrypt::ENCRYPTION_OAEP;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function getHashAlgorithm(): string
32+
{
33+
return 'sha1';
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function name(): string
40+
{
41+
return 'RSA-OAEP';
42+
}
43+
}

RSAOAEP256.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2018 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15+
16+
use Jose\Component\Encryption\Util\RSACrypt;
17+
18+
final class RSAOAEP256 extends RSA
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getEncryptionMode(): int
24+
{
25+
return RSACrypt::ENCRYPTION_OAEP;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getHashAlgorithm(): string
32+
{
33+
return 'sha256';
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function name(): string
40+
{
41+
return 'RSA-OAEP-256';
42+
}
43+
}

composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "web-token/jwt-encryption-algorithm-rsa",
3+
"description": "RSA Based Key Encryption Algorithms the JWT Framework.",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": ["JWS", "JWT", "JWE", "JWA", "JWK", "JWKSet", "Jot", "Jose", "RFC7515", "RFC7516", "RFC7517", "RFC7518", "RFC7519", "RFC7520", "Bundle", "Symfony"],
7+
"homepage": "https://github.com/web-token",
8+
"authors": [
9+
{
10+
"name": "Florent Morselli",
11+
"homepage": "https://github.com/Spomky"
12+
},{
13+
"name": "All contributors",
14+
"homepage": "https://github.com/web-token/jwt-core/contributors"
15+
}
16+
],
17+
"autoload": {
18+
"psr-4": {
19+
"Jose\\Component\\Encryption\\Algorithm\\KeyEncryption\\": ""
20+
}
21+
},
22+
"require": {
23+
"web-token/jwt-encryption": "^1.2"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^6.0|^7.0"
27+
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "1.2.x-dev"
31+
}
32+
},
33+
"config": {
34+
"sort-packages": true
35+
}
36+
}

phpunit.xml.dist

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="true"
11+
bootstrap="vendor/autoload.php"
12+
colors="true">
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>./Tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">./</directory>
22+
<exclude>
23+
<directory>./vendor</directory>
24+
<directory>./Tests</directory>
25+
<directory suffix="Test.php">./src</directory>
26+
</exclude>
27+
</whitelist>
28+
</filter>
29+
</phpunit>

0 commit comments

Comments
 (0)