Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix certificate testing, and allowing RSA certs as well as pkcs12 #72

Closed
wants to merge 11 commits into from
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [#73](https://github.com/OS2Forms/os2forms/pull/73a)
Fix issue with nested elements in webform inherit
- [#72](https://github.com/OS2Forms/os2forms/pull/72)
Fix certificate testing, also testing for RSA/PEM certs as well as PKCS12

## [3.13.2] 2023-10-19

- Fixing CPR fetch pattern
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@
"tecnickcom/tcpdf": "~6",
"webmozart/path-util": "^2.3",
"wsdltophp/packagebase": "^5.0",
"zaporylie/composer-drupal-optimizations": "^1.2"
},
"zaporylie/composer-drupal-optimizations": "^1.2",
"ext-openssl": "*"
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will you fix the formatting?

"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"drupal/coder": "^8.3",
Expand Down Expand Up @@ -146,4 +147,4 @@
"zaporylie/composer-drupal-optimizations": true
}
}
}
}
23 changes: 21 additions & 2 deletions modules/os2forms_digital_post/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,27 @@ public function submitForm(array &$form, FormStateInterface $formState): void {
private function testCertificate(): void {
try {
$certificateLocator = $this->certificateLocatorHelper->getCertificateLocator();
$certificateLocator->getCertificates();
$this->messenger()->addStatus($this->t('Certificate succesfully tested'));
$certificatePath = $this->settings->getCertificate()[CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM]['path'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the returned array is empty, you will get a warning here - because value is not set. Might be wise to handle that gracefully


// Check if the certificate has the pkcs12 extension or not.
if (pathinfo($certificatePath, PATHINFO_EXTENSION) == 'pkcs12') {
// Check the certificate if it is a valid pkcs12 certificate.
$certificateLocator->getCertificates();
}
else {
// Get contents of certificate.
$certificateKeyFile = $certificateLocator->getCertificate();
// Create an array for checking the key with the certificate.
$keyCheckData = [$certificateKeyFile, $certificateLocator->getPassphrase()];
// Check the private key against the certificate.
$result = openssl_x509_check_private_key($certificateKeyFile, $keyCheckData);
// If the result is not "1", throw an exception.
if ($result != 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned result $result is of type boolean. It would be more correct to compare it against boolean value, instead of int.
If fact syntax can be simplified to:

if ($result) {
...

throw new \ErrorException('PEM certificate is not valid.');
}
}

$this->messenger()->addStatus($this->t('Certificate successfully tested'));
}
catch (\Throwable $throwable) {
$message = $this->t('Error testing certificate: %message', ['%message' => $throwable->getMessage()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Drupal\os2forms_forloeb\Plugin\EngineTasks;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\maestro_webform\Plugin\EngineTasks\MaestroWebformTask;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\Utility\WebformArrayHelper;

Expand Down Expand Up @@ -152,9 +154,18 @@ public static function webformSubmissionFormAlter(array &$form, FormStateInterfa
if ('webform_submission' === ($entityIdentifier['entity_type'] ?? NULL)) {
$submission = WebformSubmission::load($entityIdentifier['entity_id']);
$data = $submission->getData();
foreach ($data as $key => $value) {
if (isset($form['elements'][$key])) {
$form['elements'][$key]['#default_value'] = $value;

// The target element may be hidden inside sections or field groups
// on the target form. Therefore, we need to load that form and get
// element information to properly set default element values nested
// inside the form.
if ($targetWebform = Webform::load($form['#webform_id'] ?? NULL)) {
foreach ($data as $key => $value) {
if ($targetElement = $targetWebform->getElement($key)) {
if ($element = &NestedArray::getValue($form['elements'], $targetElement['#webform_parents'])) {
$element['#default_value'] = $value;
}
}
}
}
}
Expand Down
Loading