Qore Programming Language  0.9.16
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 - 2019 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.getBuffer();
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 void getString(QoreString& str) const {
96  for (unsigned i = 0; i < md_len; i++)
97  str.sprintf("%02x", md_value[i]);
98  }
99 
100  DLLLOCAL QoreStringNode* getString() const {
101  QoreStringNode* str = new QoreStringNode;
102  for (unsigned i = 0; i < md_len; i++)
103  str->sprintf("%02x", md_value[i]);
104 
105  return str;
106  }
107 
108  DLLLOCAL BinaryNode* getBinary() const {
109  BinaryNode* b = new BinaryNode;
110  b->append(md_value, md_len);
111  return b;
112  }
113 };
114 
115 class QoreEvpHelper {
116 public:
117  DLLLOCAL QoreEvpHelper() {
118  mdctx = EVP_MD_CTX_create();
119  }
120 
121  DLLLOCAL ~QoreEvpHelper() {
122  if (mdctx)
123  EVP_MD_CTX_destroy(mdctx);
124  }
125 
126  DLLLOCAL EVP_MD_CTX* operator*() {
127  return mdctx;
128  }
129 
130  DLLLOCAL const EVP_MD_CTX* operator*() const {
131  return mdctx;
132  }
133 
134 private:
135  EVP_MD_CTX* mdctx;
136 };
137 
138 class QoreEvpCipherCtxHelper {
139 public:
140  DLLLOCAL QoreEvpCipherCtxHelper() {
141  ctx = EVP_CIPHER_CTX_new();
142  }
143 
144  DLLLOCAL ~QoreEvpCipherCtxHelper() {
145  if (ctx)
146  EVP_CIPHER_CTX_free(ctx);
147  }
148 
149  DLLLOCAL EVP_CIPHER_CTX* operator*() {
150  return ctx;
151  }
152 
153  DLLLOCAL const EVP_CIPHER_CTX* operator*() const {
154  return ctx;
155  }
156 
157 private:
158  EVP_CIPHER_CTX* ctx;
159 };
160 
161 class DigestHelper : public BaseHelper {
162 public:
163  DLLLOCAL DigestHelper(const QoreValue v) {
164  setInput(v);
165  }
166 
167  DLLLOCAL DigestHelper(const QoreListNode* params) {
168  setInput(get_param_value(params, 0));
169  }
170 
171  DLLLOCAL DigestHelper(const QoreString& str) {
172  setInput(str);
173  }
174 
175  DLLLOCAL DigestHelper(const BinaryNode& b) {
176  setInput(b);
177  }
178 
179  DLLLOCAL DigestHelper(const void* buf, size_t len) {
180  input = (unsigned char*)buf;
181  input_len = len;
182  }
183 
184  DLLLOCAL int doDigest(const char* err, const EVP_MD* md, ExceptionSink* xsink = 0) {
185  QoreEvpHelper mdctx;
186  if (!*mdctx) {
187  if (xsink)
188  xsink->raiseException(err, "error creating digest object");
189  return -1;
190  }
191 
192  EVP_DigestInit_ex(*mdctx, md, 0);
193  if (!EVP_DigestUpdate(*mdctx, input, input_len) || !EVP_DigestFinal_ex(*mdctx, md_value, &md_len)) {
194  if (xsink)
195  xsink->raiseException(err, "error calculating digest");
196  return -1;
197  }
198 
199  return 0;
200  }
201 };
202 
203 class QoreHmacHelper {
204 public:
205  DLLLOCAL QoreHmacHelper() {
206 #ifdef HAVE_OPENSSL_INIT_CRYPTO
207  ctx = HMAC_CTX_new();
208 #else
209  HMAC_CTX_init(&ctx);
210 #endif
211  }
212 
213  DLLLOCAL ~QoreHmacHelper() {
214 #ifdef HAVE_OPENSSL_INIT_CRYPTO
215  HMAC_CTX_free(ctx);
216 #else
217  HMAC_CTX_cleanup(&ctx);
218 #endif
219 }
220 
221  DLLLOCAL HMAC_CTX* operator*() {
222 #ifdef HAVE_OPENSSL_INIT_CRYPTO
223  return ctx;
224 #else
225  return &ctx;
226 #endif
227  }
228 
229  DLLLOCAL const HMAC_CTX* operator*() const {
230 #ifdef HAVE_OPENSSL_INIT_CRYPTO
231  return ctx;
232 #else
233  return &ctx;
234 #endif
235  }
236 
237 private:
238 #ifdef HAVE_OPENSSL_INIT_CRYPTO
239  HMAC_CTX* ctx;
240 #else
241  HMAC_CTX ctx;
242 #endif
243 };
244 
245 class HMACHelper : public BaseHelper {
246 public:
247  DLLLOCAL HMACHelper(const QoreValue v) {
248  setInput(v);
249  }
250 
251  DLLLOCAL HMACHelper(const QoreListNode* params) {
252  setInput(get_param_value(params, 0));
253  }
254 
255  DLLLOCAL HMACHelper(const QoreStringNode& str) {
256  setInput(str);
257  }
258 
259  DLLLOCAL HMACHelper(const BinaryNode& b) {
260  setInput(b);
261  }
262 
263  DLLLOCAL HMACHelper(const void* buf, size_t len) {
264  input = (unsigned char*)buf;
265  input_len = len;
266  }
267 
268  DLLLOCAL int doHMAC(const char* err, const EVP_MD* md, const char* ptr, size_t len, ExceptionSink* xsink) {
269  QoreHmacHelper ctx;
270  if (!*ctx) {
271  xsink->raiseException(err, "error allocating HMAC object");
272  return -1;
273  }
274 
275 #ifdef HAVE_OPENSSL_HMAC_RV
276  int rc = HMAC_Init_ex(*ctx, ptr, len, md, 0);
277  if (!rc) {
278  xsink->raiseException(err, "error initalizing HMAC");
279  return -1;
280  }
281 #else
282  HMAC_Init_ex(*ctx, ptr, len, md, 0);
283 #endif
284 
285 #ifdef HAVE_OPENSSL_HMAC_RV
286  if (!HMAC_Update(*ctx, input, input_len)
287  || !HMAC_Final(*ctx, md_value, &md_len)) {
288  xsink->raiseException(err, "error calculating HMAC");
289  return -1;
290  }
291 #else
292  HMAC_Update(*ctx, input, input_len);
293  HMAC_Final(*ctx, md_value, &md_len);
294 #endif
295 
296  return 0;
297  }
298 };
299 
300 #endif // _QORE_QL_CRYPTO_H
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
QoreListNode
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
BinaryNode::append
DLLEXPORT void append(const void *nptr, qore_size_t size)
resizes the object and appends a copy of the data passed to the object
QoreString::sprintf
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
QoreNamespace
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
BinaryNode::size
DLLEXPORT qore_size_t size() const
returns the number of bytes in the object
ExceptionSink::raiseException
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
NT_STRING
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
QoreString::getBuffer
const DLLEXPORT char * getBuffer() const
returns the string's buffer; this data should not be changed
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreString::strlen
DLLEXPORT qore_size_t strlen() const
returns number of bytes in the string (not including the null pointer)
BinaryNode::getPtr
const DLLEXPORT void * getPtr() const
returns the pointer to the data
BinaryNode
holds arbitrary binary data
Definition: BinaryNode.h:41
QoreStringNode
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
NT_BINARY
const qore_type_t NT_BINARY
type value for BinaryNode
Definition: node_types.h:49
get_param_value
static QoreValue get_param_value(const QoreListNode *n, qore_size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:78