Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
ql_crypto.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 ql_crypto.h
4
5 libcrypto-based cryptographic functions
6
7 Qore Programming Language
8
9 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
10
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the "Software"),
13 to deal in the Software without restriction, including without limitation
14 the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 and/or sell copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28
29 Note that the Qore library is released under a choice of three open-source
30 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31 information.
32*/
33
34#ifndef _QORE_QL_CRYPTO_H
35
36#define _QORE_QL_CRYPTO_H
37
38#include <openssl/err.h>
39#include <openssl/evp.h>
40#include <openssl/des.h>
41#include <openssl/hmac.h>
42
43#define MD2_ERR "MD2-DIGEST-ERROR"
44#define MD4_ERR "MD4-DIGEST-ERROR"
45#define MD5_ERR "MD5-DIGEST-ERROR"
46#define SHA_ERR "SHA-DIGEST-ERROR"
47#define SHA1_ERR "SHA1-DIGEST-ERROR"
48static const char SHA224_ERR[] = "SHA224-DIGEST-ERROR";
49static const char SHA256_ERR[] = "SHA256-DIGEST-ERROR";
50static const char SHA384_ERR[] = "SHA384-DIGEST-ERROR";
51static const char SHA512_ERR[] = "SHA512-DIGEST-ERROR";
52#define DSS_ERR "DSS-DIGEST-ERROR"
53#define DSS1_ERR "DSS1-DIGEST-ERROR"
54static const char MDC2_ERR[] = "MDC2-DIGEST-ERROR";
55#define RIPEMD160_ERR "RIPEMD160-DIGEST-ERROR"
56
57DLLLOCAL void init_crypto_functions(QoreNamespace& ns);
58
59class BaseHelper {
60protected:
61 unsigned char* input;
62 size_t input_len;
63
64 unsigned char md_value[EVP_MAX_MD_SIZE > HMAC_MAX_MD_CBLOCK ? EVP_MAX_MD_SIZE : HMAC_MAX_MD_CBLOCK];
65 unsigned int md_len;
66
67 DLLLOCAL void setInput(const QoreString& str) {
68 input = (unsigned char*)str.c_str();
69 input_len = str.strlen();
70 }
71
72 DLLLOCAL void setInput(const BinaryNode& b) {
73 input = (unsigned char*)b.getPtr();
74 input_len = b.size();
75 }
76
77 DLLLOCAL void setInput(const QoreValue pt) {
78 if (pt.getType() == NT_STRING) {
79 setInput(*pt.get<const QoreStringNode>());
80 } else {
81 assert(pt.getType() == NT_BINARY);
82 setInput(*pt.get<const BinaryNode>());
83 }
84 }
85
86public:
87 DLLLOCAL unsigned int size() const {
88 return md_len;
89 }
90
91 DLLLOCAL const void* getBuffer() const {
92 return (const void*)md_value;
93 }
94
95 DLLLOCAL const void* c_str() const {
96 return (const void*)md_value;
97 }
98
99 DLLLOCAL void getString(QoreString& str) const {
100 for (unsigned i = 0; i < md_len; i++)
101 str.sprintf("%02x", md_value[i]);
102 }
103
104 DLLLOCAL QoreStringNode* getString() const {
106 for (unsigned i = 0; i < md_len; i++)
107 str->sprintf("%02x", md_value[i]);
108
109 return str;
110 }
111
112 DLLLOCAL BinaryNode* getBinary() const {
113 BinaryNode* b = new BinaryNode;
114 b->append(md_value, md_len);
115 return b;
116 }
117};
118
119class QoreEvpHelper {
120public:
121 DLLLOCAL QoreEvpHelper() : mdctx(EVP_MD_CTX_create()) {
122 }
123
124 DLLLOCAL ~QoreEvpHelper() {
125 if (mdctx) {
126 EVP_MD_CTX_destroy(mdctx);
127 }
128 }
129
130 DLLLOCAL EVP_MD_CTX* operator*() {
131 return mdctx;
132 }
133
134 DLLLOCAL const EVP_MD_CTX* operator*() const {
135 return mdctx;
136 }
137
138private:
139 EVP_MD_CTX* mdctx;
140};
141
142class QoreEvpCipherCtxHelper {
143public:
144 DLLLOCAL QoreEvpCipherCtxHelper() : ctx(EVP_CIPHER_CTX_new()) {
145 if (ctx) {
146 EVP_CIPHER_CTX_init(ctx);
147 }
148 }
149
150 DLLLOCAL ~QoreEvpCipherCtxHelper() {
151 if (ctx) {
152 EVP_CIPHER_CTX_free(ctx);
153 }
154 }
155
156 DLLLOCAL EVP_CIPHER_CTX* operator*() {
157 return ctx;
158 }
159
160 DLLLOCAL const EVP_CIPHER_CTX* operator*() const {
161 return ctx;
162 }
163
164 DLLLOCAL operator bool() const {
165 return ctx ? true : false;
166 }
167
168private:
169 EVP_CIPHER_CTX* ctx;
170};
171
172class DigestHelper : public BaseHelper {
173public:
174 DLLLOCAL DigestHelper(const QoreValue v) {
175 setInput(v);
176 }
177
178 DLLLOCAL DigestHelper(const QoreListNode* params) {
179 setInput(get_param_value(params, 0));
180 }
181
182 DLLLOCAL DigestHelper(const QoreString& str) {
183 setInput(str);
184 }
185
186 DLLLOCAL DigestHelper(const BinaryNode& b) {
187 setInput(b);
188 }
189
190 DLLLOCAL DigestHelper(const void* buf, size_t len) {
191 input = (unsigned char*)buf;
192 input_len = len;
193 }
194
195 DLLLOCAL int doDigest(const char* err, const EVP_MD* md, ExceptionSink* xsink = nullptr) {
196 //printd(5, "DigestHelper::doDigest() err: %p md: %p xs: %p\n", err, md, xsink);
197 QoreEvpHelper mdctx;
198 if (!*mdctx) {
199 if (xsink) {
200 xsink->raiseException(err, "error creating digest object");
201 } else {
202 printd(0, "DigestHelper::doDigest(): error creating digest object\n");
203 }
204 return -1;
205 }
206
207 if (!EVP_DigestInit_ex(*mdctx, md, nullptr)) {
208 if (xsink) {
209 xsink->raiseException(err, "error initializing digest");
210 } else {
211 printd(0, "DigestHelper::doDigest(): error initializing digest (%p, %p)\n", *mdctx, md);
212 }
213 return -1;
214 }
215 if (!EVP_DigestUpdate(*mdctx, input, input_len) || !EVP_DigestFinal_ex(*mdctx, md_value, &md_len)) {
216 if (xsink) {
217 xsink->raiseException(err, "error calculating digest");
218 } else {
219 printd(0, "DigestHelper::doDigest(): error calculating digest\n");
220 }
221 return -1;
222 }
223
224 return 0;
225 }
226};
227
228#if !defined(OPENSSL_VERSION_MAJOR) || OPENSSL_VERSION_MAJOR < 3
229class QoreHmacHelper {
230public:
231 DLLLOCAL QoreHmacHelper() {
232#if defined(HAVE_OPENSSL_INIT_CRYPTO)
233 ctx = HMAC_CTX_new();
234#else
235 HMAC_CTX_init(&ctx);
236#endif
237 }
238
239 DLLLOCAL ~QoreHmacHelper() {
240#ifdef HAVE_OPENSSL_INIT_CRYPTO
241 HMAC_CTX_free(ctx);
242#else
243 HMAC_CTX_cleanup(&ctx);
244#endif
245 }
246
247 DLLLOCAL HMAC_CTX* operator*() {
248#ifdef HAVE_OPENSSL_INIT_CRYPTO
249 return ctx;
250#else
251 return &ctx;
252#endif
253 }
254
255 DLLLOCAL const HMAC_CTX* operator*() const {
256#ifdef HAVE_OPENSSL_INIT_CRYPTO
257 return ctx;
258#else
259 return &ctx;
260#endif
261 }
262
263private:
264#ifdef HAVE_OPENSSL_INIT_CRYPTO
265 typedef HMAC_CTX* q_hmac_t;
266#else
267 typedef HMAC_CTX q_hmac_t;
268#endif
269
270 q_hmac_t ctx;
271};
272#endif
273
274class HMACHelper : public BaseHelper {
275public:
276 DLLLOCAL HMACHelper(const QoreValue v) {
277 setInput(v);
278 }
279
280 DLLLOCAL HMACHelper(const QoreListNode* params) {
281 setInput(get_param_value(params, 0));
282 }
283
284 DLLLOCAL HMACHelper(const QoreStringNode& str) {
285 setInput(str);
286 }
287
288 DLLLOCAL HMACHelper(const BinaryNode& b) {
289 setInput(b);
290 }
291
292 DLLLOCAL HMACHelper(const void* buf, size_t len) {
293 input = (unsigned char*)buf;
294 input_len = len;
295 }
296
297 DLLLOCAL int doHMAC(const char* err, const char* digest, const char* ptr, size_t len, ExceptionSink* xsink);
298};
299
300#endif // _QORE_QL_CRYPTO_H
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT void append(const void *nptr, size_t size)
resizes the object and appends a copy of the data passed to the object
DLLEXPORT size_t size() const
returns the number of bytes in the object
DLLEXPORT const void * getPtr() const
returns the pointer to the data
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
DLLLOCAL detail::QoreValueCastHelper< T >::Result get()
returns the value as the given type
Definition: QoreValue.h:214
DLLEXPORT qore_type_t getType() const
returns the type of value contained
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
DLLEXPORT size_t strlen() const
returns number of bytes in the string (not including the null pointer)
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
const qore_type_t NT_BINARY
type value for BinaryNode
Definition: node_types.h:49
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
static QoreValue get_param_value(const QoreListNode *n, size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:78
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276