ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
KeyLoader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SAML2\Certificate;
4 
11 
15 class KeyLoader
16 {
20  private $loadedKeys;
21 
22  public function __construct()
23  {
24  $this->loadedKeys = new KeyCollection();
25  }
26 
37  public static function extractPublicKeys(
39  $usage = null,
40  $required = false
41  ) {
42  $keyLoader = new self();
43 
44  return $keyLoader->loadKeysFromConfiguration($config, $usage, $required);
45  }
46 
54  public function loadKeysFromConfiguration(
56  $usage = null,
57  $required = false
58  ) {
59  $keys = $config->getKeys();
60  $certificateData = $config->getCertificateData();
61  $certificateFile = $config->getCertificateFile();
62 
63  if ($keys !== null) {
64  $this->loadKeys($keys, $usage);
65  } elseif ($certificateData !== null) {
66  $this->loadCertificateData($certificateData);
67  } elseif ($certificateFile !== null) {
68  $this->loadCertificateFile($certificateFile);
69  }
70 
71  if ($required && !$this->hasKeys()) {
72  throw new NoKeysFoundException(
73  'No keys found in configured metadata, please ensure that either the "keys", "certData" or '
74  . '"certificate" entries is available.'
75  );
76  }
77 
78  return $this->getKeys();
79  }
80 
88  public function loadKeys(array $configuredKeys, $usage)
89  {
90  foreach ($configuredKeys as $keyData) {
91  if (isset($keyData['X509Certificate'])) {
92  $key = new X509($keyData);
93  } else {
94  $key = new Key($keyData);
95  }
96 
97  if ($usage && !$key->canBeUsedFor($usage)) {
98  continue;
99  }
100 
101  $this->loadedKeys->add($key);
102  }
103  }
104 
110  public function loadCertificateData($certificateData)
111  {
112  if (!is_string($certificateData)) {
113  throw InvalidArgumentException::invalidType('string', $certificateData);
114  }
115 
116  $this->loadedKeys->add(X509::createFromCertificateData($certificateData));
117  }
118 
124  public function loadCertificateFile($certificateFile)
125  {
126  $certificate = File::getFileContents($certificateFile);
127 
129  throw new InvalidCertificateStructureException(sprintf(
130  'Could not find PEM encoded certificate in "%s"',
131  $certificateFile
132  ));
133  }
134 
135  // capture the certificate contents without the delimiters
136  preg_match(Certificate::CERTIFICATE_PATTERN, $certificate, $matches);
137  $this->loadedKeys->add(X509::createFromCertificateData($matches[1]));
138  }
139 
143  public function getKeys()
144  {
145  return $this->loadedKeys;
146  }
147 
151  public function hasKeys()
152  {
153  return !!count($this->loadedKeys);
154  }
155 }
Pure-PHP X.509 Parser.
getKeys()
Returns an array or of keys, where each element represents a configured key.
static getFileContents($file)
Definition: File.php:18
$config
Definition: bootstrap.php:15
CertificateProvider interface.
loadCertificateFile($certificateFile)
Loads the certificate in the file given.
Definition: KeyLoader.php:124
loadKeys(array $configuredKeys, $usage)
Loads the keys given, optionally excluding keys when a usage is given and they are not configured to ...
Definition: KeyLoader.php:88
static extractPublicKeys(CertificateProvider $config, $usage=null, $required=false)
Extracts the public keys given by the configuration.
Definition: KeyLoader.php:37
$keys
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
Simple DTO wrapper for (X509) keys.
Definition: Key.php:12
static hasValidStructure($certificate)
Definition: Certificate.php:20
static createFromCertificateData($certificateContents)
Definition: X509.php:15
getCertificateData()
Returns the contents of an X509 pem certificate, without the &#39;--—BEGIN CERTIFICATE--—&#39; and &#39;--—END...
loadKeysFromConfiguration(CertificateProvider $config, $usage=null, $required=false)
Definition: KeyLoader.php:54
Simple collection object for transporting keys.
$key
Definition: croninfo.php:18
const CERTIFICATE_PATTERN
The pattern that the contents of a certificate should adhere to.
Definition: Certificate.php:13
getCertificateFile()
Returns the full path to the (local) file that contains the X509 pem certificate. ...
loadCertificateData($certificateData)
Attempts to load a key based on the given certificateData.
Definition: KeyLoader.php:110