KHealthCertificate

jwkloader.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3  * SPDX-License-Identifier: LGPL-2.0-or-later
4  */
5 
6 #include "jwkloader_p.h"
7 #include "logging.h"
8 
9 #include <QFile>
10 #include <QJsonDocument>
11 #include <QJsonObject>
12 
13 #include <openssl/bn.h>
14 #include <openssl/obj_mac.h>
15 
16 openssl::evp_pkey_ptr JwkLoader::loadPublicKey(const QString &fileName)
17 {
18  QFile f(fileName);
19  if (!f.open(QFile::ReadOnly)) {
20  qCWarning(Log) << f.errorString();
21  return openssl::evp_pkey_ptr(nullptr, &EVP_PKEY_free);
22  }
23 
24  return loadPublicKey(QJsonDocument::fromJson(f.readAll()).object());
25 }
26 
27 openssl::evp_pkey_ptr JwkLoader::loadPublicKey(const QJsonObject &keyObj)
28 {
29  auto evp = openssl::evp_pkey_ptr(nullptr, &EVP_PKEY_free);
30 
31  const auto kty = keyObj.value(QLatin1String("kty")).toString();
32  if (kty == QLatin1String("EC")) {
33  auto ecKey = openssl::ec_key_ptr(nullptr, &EC_KEY_free);
34  const auto crv = keyObj.value(QLatin1String("crv")).toString();
35  if (crv == QLatin1String("P-256")) {
36  ecKey = openssl::ec_key_ptr(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), &EC_KEY_free);
37  } else if (crv == QLatin1String("P-384")) {
38  ecKey = openssl::ec_key_ptr(EC_KEY_new_by_curve_name(NID_secp384r1), &EC_KEY_free);
39  } else if (crv == QLatin1String("P-521")) {
40  ecKey = openssl::ec_key_ptr(EC_KEY_new_by_curve_name(NID_secp521r1), &EC_KEY_free);
41  } else {
42  qCWarning(Log) << "Unsupported curve type" << crv;
43  return evp;
44  }
45 
47  const auto x = BN_bin2bn(reinterpret_cast<const uint8_t*>(xData.constData()), xData.size(), nullptr);
49  const auto y = BN_bin2bn(reinterpret_cast<const uint8_t*>(yData.constData()), xData.size(), nullptr);
50  EC_KEY_set_public_key_affine_coordinates(ecKey.get(), x, y);
51 
52  evp.reset(EVP_PKEY_new());
53  EVP_PKEY_assign_EC_KEY(evp.get(), ecKey.release());
54  } else {
55  qCWarning(Log) << "unsuporrted key type:" << kty;
56  }
57 
58  return evp;
59 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString toString() const const
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
QJsonValue value(const QString &key) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:21:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.