Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
OpenSSLCryptoStream.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cryptography_OpenSSLCryptoStream_h_
5#define _Stroika_Foundation_Cryptography_OpenSSLCryptoStream_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#if qStroika_HasComponent_OpenSSL
10using EVP_CIPHER_CTX = struct evp_cipher_ctx_st;
11#endif
12
13#include "Stroika/Foundation/Common/Common.h"
18#include "Stroika/Foundation/Cryptography/SSL/Common.h"
19#include "Stroika/Foundation/Execution/Exceptions.h"
22#include "Stroika/Foundation/Memory/Common.h"
25
26/**
27 * \file
28 *
29 * TODO:
30 * @todo OpenSSLInputStream and OpenSSLOutputStream must be fixed to use Debug::AssertExternallySynchronized
31 *
32 * @todo Review https://www.openssl.org/docs/crypto/EVP_EncryptInit.html and things like setkeylength etc to get
33 * rc4 working (idnetically with windows version)
34 *
35 * See nt do_crypt(FILE *in, FILE *out, int do_encrypt) example.,
36 *
37 * From examples, one bug maybe the value of inital_iv - EVP_CipherInit_ex - that the call may expect
38 * its the right lenght but the length is user-specified.. I may need to init to zero (or some such), and
39 * copy in user data.
40 *
41 * I think for BOTH key and iv we must look at expected keylen/iplen and pass in data that matches the
42 * length. Maybe set what hack we did for CipherAlgorithm::eRC4?? set key length based on what is passed in?
43 *
44 * We maybe can (mostly/always) set iv to NULL???
45 *
46 * #if 0
47 * // DEBUG WHY THIS FAILS - I THINK WE NEED TO ENABLE PADDING FOR SOME CYPHERS!
48 * BLOB ((const byte*)kSrc4_, (const byte*)kSrc4_ + ::strlen(kSrc4_)),
49 * #endif
50 *
51 * @todo this module includes <openssl> stuff in the header. Add additional modules inside
52 * Crypto that just are called 'Blowfish', and 'rc2', and these have classes that take
53 * constructors with just the needed data = maybe not even ctors - maybe functions - that
54 * take a stream, and return a decrypting (or encrypting) stream - with arg params that make
55 * sense for that algoritjm. They are only defined #if qSSLAvail, but otherwise include
56 *
57 * DONE for AES - BUt do the others - just like that - and maybe cleanup Base64/MD5 APIs to
58 * be done like for AES...
59 * ...
60 *
61 *
62 */
63
64namespace Stroika::Foundation::Cryptography::Encoding {
65
66 using Memory::BLOB;
67
68#if qStroika_HasComponent_OpenSSL
69 /**
70 */
71 enum class Direction {
72 eEncrypt,
73 eDecrypt,
74 };
75
76 using Cryptography::Providers::OpenSSL::CipherAlgorithm;
77 using Cryptography::Providers::OpenSSL::DerivedKey;
78 using Cryptography::Providers::OpenSSL::DigestAlgorithm;
79
80 class OpenSSLCryptoParams {
81 public:
82 // use this CTOR and fill in parameters manually for EVP_EncryptInit_ex
83 OpenSSLCryptoParams (const function<void (::EVP_CIPHER_CTX*, Direction d)>& f);
84 // allowed CipherAlgorithm's for this CTOR include kAES_*, eBlowfish_*, eRC2'
85 OpenSSLCryptoParams (CipherAlgorithm alg, const BLOB& key, const BLOB& initialIV);
86 OpenSSLCryptoParams (CipherAlgorithm alg, const DerivedKey& derivedKey);
87
88 public:
89 function<void (::EVP_CIPHER_CTX*, Direction)> fInitializer;
90 };
91
92 /**
93 * @brief OpenSSLInputStream is a BinaryInputStream which does OpenSSL-based encryption or decryption (depending on direction arg)
94 *
95 * OpenSSLInputStream is a BinaryInputStream which wraps another BinaryInputStream
96 * and does OpenSSL-based encryption or decryption (depending on direction arg).
97 *
98 * Use OpenSSLInputStream is you wish to use the result of encryption in your program, so you prefer to structure
99 * your conversion code as a process of reading.
100 *
101 * @see OpenSSLOutputStream
102 *
103 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter">C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter</a>
104 */
105 namespace OpenSSLInputStream {
106 /**
107 */
108 Streams::InputStream::Ptr<byte> New (const OpenSSLCryptoParams& cryptoParams, Direction direction, const Streams::InputStream::Ptr<byte>& realIn);
109 Streams::InputStream::Ptr<byte> New (Execution::InternallySynchronized internallySynchronized, const OpenSSLCryptoParams& cryptoParams,
110 Direction direction, const Streams::InputStream::Ptr<byte>& realIn);
111 };
112
113 /**
114 * @brief OpenSSLOutputStream is a BinaryOutputStream which does OpenSSL-based encryption or decryption (depending on direction arg)
115 *
116 * OpenSSLOutputStream is a BinaryOutputStream which wraps another BinaryOutputStream
117 * and does OpenSSL-based encryption or decryption (depending on direction arg).
118 *
119 * Use OpenSSLOutputStream is you wish to produce an artifact (e.g. external file) as a result of incrementally writing
120 * to a stream.
121 *
122 * @see OpenSSLInputStream
123 *
124 * \note OpenSSLOutputStream aggregates its owned sub stream, so that a Close () on OpenSSLOutputStream
125 * will Close that sub stream.
126 *
127 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter">C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter</a>
128 */
129 namespace OpenSSLOutputStream {
130
131 /**
132 */
133 Streams::OutputStream::Ptr<byte> New (const OpenSSLCryptoParams& cryptoParams, Direction direction,
134 const Streams::OutputStream::Ptr<byte>& realOut);
135 Streams::OutputStream::Ptr<byte> New (Execution::InternallySynchronized internallySynchronized, const OpenSSLCryptoParams& cryptoParams,
136 Direction direction, const Streams::OutputStream::Ptr<byte>& realOut);
137
138 };
139#endif
140
141}
142
143/*
144 ********************************************************************************
145 ***************************** Implementation Details ***************************
146 ********************************************************************************
147 */
148#include "OpenSSLCryptoStream.inl"
149
150#endif /*_Stroika_Foundation_Cryptography_OpenSSLCryptoStream_h_*/