phpseclib/tests/Unit/File/X509/SPKACTest.php

105 lines
3.8 KiB
PHP
Raw Normal View History

2014-06-04 05:11:57 +00:00
<?php
2022-02-17 02:25:59 +00:00
2014-06-04 05:11:57 +00:00
/**
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2014 Jim Wigginton
2014-06-04 05:11:57 +00:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
2022-06-04 15:31:21 +00:00
declare(strict_types=1);
namespace phpseclib3\Tests\Unit\File\X509;
use phpseclib3\Crypt\RSA;
2022-01-30 15:34:42 +00:00
use phpseclib3\File\X509;
use phpseclib3\Tests\PhpseclibTestCase;
2014-06-04 05:11:57 +00:00
class SPKACTest extends PhpseclibTestCase
2014-06-04 05:11:57 +00:00
{
2022-06-04 15:31:21 +00:00
public function testLoadSPKAC(): void
2014-06-04 05:11:57 +00:00
{
2014-06-05 14:23:22 +00:00
$test = 'MIICQDCCASgwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQChgo9mWzQm3TSwGgpZnIc54' .
'TZ8gYpfAO/AI0etvyWDqnFfdNCUQsqxTdSi6/rtrJdLGBsszRGrRIc/0JqmjM+jCHGYutLeo4xwgr' .
'a3HAZrWDypL5IlRWnLmLA4U/qGXCXNSk+9NrJl39X3IDA8o/aOJyr9iMUJMvswcWjVjPom3NhAgmJ' .
'ZwW0vUEMw9zszExpiRnGSO5XXntQW2qvfzo+J3NzS3BBbKxEmTsfOLHextcXeFQUaBQHXB/WOtweW' .
'Y/Bd4iZ8ETmhal28g1HWVcTFPD+V+KPRFeARlVEW6JmcJucW2WdJlBGKXXXPEfdHrDS3OgD/eDWfM' .
'JE4mChZ/icxAgMBAAEWADANBgkqhkiG9w0BAQQFAAOCAQEAUMvIKhlSgEgbC081b/FJwh6mbuVgYN' .
'ZV37Ts2WjrHoDFlabu9WXU8xzgaXct3sO51vJM5I36rY4UPyc6w3y9dLaamEwKUoWnpHG8mlXs2JG' .
'GEUOvxh5z9yfk/2ZmdCVBlKnU1LDB+ZDyNyNh5B0YULrJKw9e0jV+ymP7srwUSBcdUfZh1KEKGVIN' .
'Uv4J3GuL8V63E2unWCHGRPw4EmFVTbWpgMx96XR7p/pMavu6/pVKgYQqWLOmEeOK+dmT/QVon28d5' .
'dmeL7aWrpP+3x3L0A9cATksracQX676XogdAEXJ59fcr/S5AGw1TFErbyBbfyeAWvzDZIXeMXpb9h' .
'yNtA==';
2014-06-04 05:11:57 +00:00
2014-12-10 00:53:05 +00:00
$x509 = new X509();
2014-06-04 05:11:57 +00:00
$spkac = $x509->loadSPKAC($test);
$this->assertIsArray($spkac);
2014-06-04 05:11:57 +00:00
$spkac = $x509->loadSPKAC('SPKAC=' . $test);
$this->assertIsArray($spkac);
2014-06-04 05:11:57 +00:00
$this->assertTrue(
$x509->validateSignature(),
'Failed asserting that the signature is valid'
);
$pubKey = $x509->getPublicKey();
$this->assertIsString("$pubKey");
2014-06-04 05:11:57 +00:00
}
2022-06-04 15:31:21 +00:00
public function testSaveSPKAC(): void
2014-06-04 05:11:57 +00:00
{
$privatekey = RSA::createKey(512)
->withPadding(RSA::SIGNATURE_PKCS1)
->withHash('sha1');
2014-06-04 05:11:57 +00:00
2014-12-10 00:53:05 +00:00
$x509 = new X509();
$x509->setPrivateKey($privatekey);
2014-06-04 05:11:57 +00:00
$x509->setChallenge('...');
$spkac = $x509->signSPKAC();
$this->assertIsArray($spkac);
2014-06-04 05:11:57 +00:00
$this->assertIsString($x509->saveSPKAC($spkac));
2014-06-04 05:11:57 +00:00
2014-12-10 00:53:05 +00:00
$x509 = new X509();
$x509->setPrivateKey($privatekey);
2014-06-04 05:11:57 +00:00
$spkac = $x509->signSPKAC();
$this->assertIsArray($spkac);
2014-06-04 05:11:57 +00:00
$this->assertIsString($x509->saveSPKAC($spkac));
2014-06-04 05:11:57 +00:00
}
2022-06-04 15:31:21 +00:00
public function testBadSignatureSPKAC(): void
2014-06-04 05:11:57 +00:00
{
2014-06-05 14:23:22 +00:00
$test = 'MIICQDCCASgwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQChgo9mWzQm3TSwGgpZnIc54' .
'TZ8gYpfAO/AI0etvyWDqnFfdNCUQsqxTdSi6/rtrJdLGBsszRGrRIc/0JqmjM+jCHGYutLeo4xwgr' .
'a3HAZrWDypL5IlRWnLmLA4U/qGXCXNSk+9NrJl39X3IDA8o/aOJyr9iMUJMvswcWjVjPom3NhAgmJ' .
'ZwW0vUEMw9zszExpiRnGSO5XXntQW2qvfzo+J3NzS3BBbKxEmTsfOLHextcXeFQUaBQHXB/WOtweW' .
'Y/Bd4iZ8ETmhal28g1HWVcTFPD+V+KPRFeARlVEW6JmcJucW2WdJlBGKXXXPEfdHrDS3OgD/eDWfM' .
'JE4mChZ/icxAgMBAAEWADANBgkqhkiG9w0BAQQFAAOCAQEAUMvIKhlSgEgbC081b/FJwh6mbuVgYN' .
'ZV37Ts2WjrHoDFlabu9WXU8xzgaXct3sO51vJM5I36rY4UPyc6w3y9dLaamEwKUoWnpHG8mlXs2JG' .
'GEUOvxh5z9yfk/2ZmdCVBlKnU1LDB+ZDyNyNh5B0YULrJKw9e0jV+ymP7srwUSBcdUfZh1KEKGVIN' .
'Uv4J3GuL8V63E2unWCHGRPw4EmFVTbWpgMx96XR7p/pMavu6/pVKgYQqWLOmEeOK+dmT/QVon28d5' .
'dmeL7aWrpP+3x3L0A9cATksracQX676XogdAEXJ59fcr/S5AGw1TFErbyBbfyeAWvzDZIXeMXpb9h' .
'yNtA==';
2014-06-04 05:11:57 +00:00
2014-12-10 00:53:05 +00:00
$x509 = new X509();
2014-06-04 05:11:57 +00:00
$spkac = $x509->loadSPKAC($test);
$spkac['publicKeyAndChallenge']['challenge'] = 'zzzz';
$x509->loadSPKAC($x509->saveSPKAC($spkac));
$this->assertFalse(
$x509->validateSignature(),
'Failed asserting that the signature is invalid'
);
}
}