Qore Programming Language  1.7.0
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 - 2022 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"
48 static const char SHA224_ERR[] = "SHA224-DIGEST-ERROR";
49 static const char SHA256_ERR[] = "SHA256-DIGEST-ERROR";
50 static const char SHA384_ERR[] = "SHA384-DIGEST-ERROR";
51 static const char SHA512_ERR[] = "SHA512-DIGEST-ERROR";
52 #define DSS_ERR "DSS-DIGEST-ERROR"
53 #define DSS1_ERR "DSS1-DIGEST-ERROR"
54 static const char MDC2_ERR[] = "MDC2-DIGEST-ERROR";
55 #define RIPEMD160_ERR "RIPEMD160-DIGEST-ERROR"
56 
57 DLLLOCAL void init_crypto_functions(QoreNamespace& ns);
58 
59 class BaseHelper {
60 protected:
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 
86 public:
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 {
105  QoreStringNode* str = new QoreStringNode;
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 
119 class QoreEvpHelper {
120 public:
121  DLLLOCAL QoreEvpHelper() {
122  mdctx = EVP_MD_CTX_create();
123  }
124 
125  DLLLOCAL ~QoreEvpHelper() {
126  if (mdctx)
127  EVP_MD_CTX_destroy(mdctx);
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 
138 private:
139  EVP_MD_CTX* mdctx;
140 };
141 
142 class QoreEvpCipherCtxHelper {
143 public:
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 
168 private:
169  EVP_CIPHER_CTX* ctx;
170 };
171 
172 class DigestHelper : public BaseHelper {
173 public:
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 = 0) {
196  QoreEvpHelper mdctx;
197  if (!*mdctx) {
198  if (xsink)
199  xsink->raiseException(err, "error creating digest object");
200  return -1;
201  }
202 
203  EVP_DigestInit_ex(*mdctx, md, 0);
204  if (!EVP_DigestUpdate(*mdctx, input, input_len) || !EVP_DigestFinal_ex(*mdctx, md_value, &md_len)) {
205  if (xsink) {
206  xsink->raiseException(err, "error calculating digest");
207  }
208  return -1;
209  }
210 
211  return 0;
212  }
213 };
214 
215 #ifndef OPENSSL_3_PLUS
216 class QoreHmacHelper {
217 public:
218  DLLLOCAL QoreHmacHelper() {
219 #if defined(HAVE_OPENSSL_INIT_CRYPTO)
220  ctx = HMAC_CTX_new();
221 #else
222  HMAC_CTX_init(&ctx);
223 #endif
224  }
225 
226  DLLLOCAL ~QoreHmacHelper() {
227 #ifdef HAVE_OPENSSL_INIT_CRYPTO
228  HMAC_CTX_free(ctx);
229 #else
230  HMAC_CTX_cleanup(&ctx);
231 #endif
232 }
233 
234  DLLLOCAL HMAC_CTX* operator*() {
235 #ifdef HAVE_OPENSSL_INIT_CRYPTO
236  return ctx;
237 #else
238  return &ctx;
239 #endif
240  }
241 
242  DLLLOCAL const HMAC_CTX* operator*() const {
243 #ifdef HAVE_OPENSSL_INIT_CRYPTO
244  return ctx;
245 #else
246  return &ctx;
247 #endif
248  }
249 
250 private:
251 #ifdef HAVE_OPENSSL_INIT_CRYPTO
252  typedef HMAC_CTX* q_hmac_t;
253 #else
254  typedef HMAC_CTX q_hmac_t;
255 #endif
256 
257  q_hmac_t ctx;
258 };
259 #endif
260 
261 class HMACHelper : public BaseHelper {
262 public:
263  DLLLOCAL HMACHelper(const QoreValue v) {
264  setInput(v);
265  }
266 
267  DLLLOCAL HMACHelper(const QoreListNode* params) {
268  setInput(get_param_value(params, 0));
269  }
270 
271  DLLLOCAL HMACHelper(const QoreStringNode& str) {
272  setInput(str);
273  }
274 
275  DLLLOCAL HMACHelper(const BinaryNode& b) {
276  setInput(b);
277  }
278 
279  DLLLOCAL HMACHelper(const void* buf, size_t len) {
280  input = (unsigned char*)buf;
281  input_len = len;
282  }
283 
284  DLLLOCAL int doHMAC(const char* err, const char* digest, const char* ptr, size_t len, ExceptionSink* xsink);
285 };
286 
287 #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:48
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
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
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...
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
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:275