KHealthCertificate

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

KDE's Doxygen guidelines are available online.