| 1 | /* |
| 2 | Copyright (C) 2003 Justin Karneges <justin@affinix.com> |
| 3 | Copyright (C) 2005 Brad Hards <bradh@frogmouth.net> |
| 4 | |
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | of this software and associated documentation files (the "Software"), to deal |
| 7 | in the Software without restriction, including without limitation the rights |
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | copies of the Software, and to permit persons to whom the Software is |
| 10 | furnished to do so, subject to the following conditions: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be included in |
| 13 | all copies or substantial portions of the Software. |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 19 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <QtCrypto> |
| 24 | |
| 25 | #include <QCoreApplication> |
| 26 | |
| 27 | #include <iostream> |
| 28 | |
| 29 | #ifdef QT_STATICPLUGIN |
| 30 | #include "import_plugins.h" |
| 31 | #endif |
| 32 | |
| 33 | int main(int argc, char **argv) |
| 34 | { |
| 35 | // the Initializer object sets things up, and |
| 36 | // also does cleanup when it goes out of scope |
| 37 | QCA::Initializer init; |
| 38 | |
| 39 | QCoreApplication app(argc, argv); |
| 40 | |
| 41 | // We need to ensure that we have certificate handling support |
| 42 | if (!QCA::isSupported(features: "cert" )) { |
| 43 | std::cout << "Sorry, no PKI certificate support" << std::endl; |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | // Read in a private key |
| 48 | QCA::PrivateKey privKey; |
| 49 | QCA::ConvertResult convRes; |
| 50 | QCA::SecureArray passPhrase = "start" ; |
| 51 | privKey = QCA::PrivateKey::fromPEMFile(QStringLiteral("Userkey.pem" ), passphrase: passPhrase, result: &convRes); |
| 52 | if (convRes != QCA::ConvertGood) { |
| 53 | std::cout << "Sorry, could not import Private Key" << std::endl; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | // Read in a matching public key cert |
| 58 | // you could also build this using the fromPEMFile() method |
| 59 | QCA::Certificate pubCert(QStringLiteral("User.pem" )); |
| 60 | if (pubCert.isNull()) { |
| 61 | std::cout << "Sorry, could not import public key certificate" << std::endl; |
| 62 | return 1; |
| 63 | } |
| 64 | // We are building the certificate into a SecureMessageKey object, via a |
| 65 | // CertificateChain |
| 66 | QCA::SecureMessageKey secMsgKey; |
| 67 | QCA::CertificateChain chain; |
| 68 | chain += pubCert; |
| 69 | secMsgKey.setX509CertificateChain(chain); |
| 70 | |
| 71 | // build up a SecureMessage object, based on our public key certificate |
| 72 | QCA::CMS cms; |
| 73 | QCA::SecureMessage msg(&cms); |
| 74 | msg.setRecipient(secMsgKey); |
| 75 | |
| 76 | // Some plain text - we use the first command line argument if provided |
| 77 | QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'" ; |
| 78 | |
| 79 | // Now use the SecureMessage object to encrypt the plain text. |
| 80 | msg.startEncrypt(); |
| 81 | msg.update(in: plainText); |
| 82 | msg.end(); |
| 83 | // I think it is reasonable to wait for 1 second for this |
| 84 | msg.waitForFinished(msecs: 1000); |
| 85 | |
| 86 | // check to see if it worked |
| 87 | if (!msg.success()) { |
| 88 | std::cout << "Error encrypting: " << msg.errorCode() << std::endl; |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | // get the result |
| 93 | QCA::SecureArray cipherText = msg.read(); |
| 94 | QCA::Base64 enc; |
| 95 | std::cout << plainText.data() << " encrypts to (in base 64): " ; |
| 96 | std::cout << qPrintable(enc.arrayToString(cipherText)) << std::endl; |
| 97 | |
| 98 | // Show we can decrypt it with the private key |
| 99 | if (!privKey.canDecrypt()) { |
| 100 | std::cout << "Private key cannot be used to decrypt" << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | QCA::SecureArray plainTextResult; |
| 104 | if (0 == privKey.decrypt(in: cipherText, out: &plainTextResult, alg: QCA::EME_PKCS1_OAEP)) { |
| 105 | std::cout << "Decryption process failed" << std::endl; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | std::cout << qPrintable(enc.arrayToString(cipherText)); |
| 110 | std::cout << " (in base 64) decrypts to: " ; |
| 111 | std::cout << plainTextResult.data() << std::endl; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |