|
| 1 | +/* |
| 2 | + * Copyright © 2023, Matthieu Gallien <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * for more details. |
| 13 | + */ |
| 14 | + |
| 15 | +#define OPENSSL_SUPPRESS_DEPRECATED |
| 16 | + |
| 17 | +#include "clientsidetokenselector.h" |
| 18 | + |
| 19 | +#include <QLoggingCategory> |
| 20 | + |
| 21 | +#include <libp11.h> |
| 22 | + |
| 23 | +namespace OCC |
| 24 | +{ |
| 25 | + |
| 26 | +Q_LOGGING_CATEGORY(lcCseSelector, "nextcloud.sync.clientsideencryption.selector", QtInfoMsg) |
| 27 | + |
| 28 | +ClientSideTokenSelector::ClientSideTokenSelector(QObject *parent) |
| 29 | + : QObject{parent} |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +bool ClientSideTokenSelector::isSetup() const |
| 35 | +{ |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +QVariantList ClientSideTokenSelector::discoveredTokens() const |
| 40 | +{ |
| 41 | + return _discoveredTokens; |
| 42 | +} |
| 43 | + |
| 44 | +void failedToInitialize(std::nullptr_t) |
| 45 | +{ |
| 46 | +} |
| 47 | + |
| 48 | +void ClientSideTokenSelector::searchForToken() |
| 49 | +{ |
| 50 | + auto account = nullptr; |
| 51 | + auto ctx = PKCS11_CTX_new(); |
| 52 | + |
| 53 | + auto rc = PKCS11_CTX_load(ctx, "opensc-pkcs11.so"); |
| 54 | + if (rc) { |
| 55 | + qCWarning(lcCseSelector()) << "loading pkcs11 engine failed:" << ERR_reason_error_string(ERR_get_error()); |
| 56 | + |
| 57 | + failedToInitialize(account); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + auto tokensCount = 0u; |
| 62 | + PKCS11_SLOT *tokenSlots = nullptr; |
| 63 | + /* get information on all slots */ |
| 64 | + if (PKCS11_enumerate_slots(ctx, &tokenSlots, &tokensCount) < 0) { |
| 65 | + qCWarning(lcCseSelector()) << "no slots available" << ERR_reason_error_string(ERR_get_error()); |
| 66 | + |
| 67 | + failedToInitialize(account); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + _discoveredTokens.clear(); |
| 72 | + auto currentSlot = static_cast<PKCS11_SLOT*>(nullptr); |
| 73 | + for(auto i = 0u; i < tokensCount; ++i) { |
| 74 | + currentSlot = PKCS11_find_next_token(ctx, tokenSlots, tokensCount, currentSlot); |
| 75 | + if (currentSlot == nullptr || currentSlot->token == nullptr) { |
| 76 | + qCWarning(lcCseSelector()) << "no token available" << ERR_reason_error_string(ERR_get_error()); |
| 77 | + |
| 78 | + failedToInitialize(account); |
| 79 | + return; |
| 80 | + } |
| 81 | + qCInfo(lcCseSelector()) << "Slot manufacturer......:" << currentSlot->manufacturer; |
| 82 | + qCInfo(lcCseSelector()) << "Slot description.......:" << currentSlot->description; |
| 83 | + qCInfo(lcCseSelector()) << "Slot token label.......:" << currentSlot->token->label; |
| 84 | + qCInfo(lcCseSelector()) << "Slot token manufacturer:" << currentSlot->token->manufacturer; |
| 85 | + qCInfo(lcCseSelector()) << "Slot token model.......:" << currentSlot->token->model; |
| 86 | + qCInfo(lcCseSelector()) << "Slot token serialnr....:" << currentSlot->token->serialnr; |
| 87 | + |
| 88 | + _discoveredTokens.push_back(QVariantMap{{QStringLiteral("manufacturer"), QString::fromLatin1(currentSlot->manufacturer)}, |
| 89 | + {QStringLiteral("description"), QString::fromLatin1(currentSlot->description)}, |
| 90 | + {QStringLiteral("label"), QString::fromLatin1(currentSlot->token->label)},}); |
| 91 | + } |
| 92 | + emit discoveredTokensChanged(); |
| 93 | +} |
| 94 | + |
| 95 | +} |
0 commit comments