You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1212 lines
44 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. mecevp.c
  3. gSOAP interface for streaming message encryption and decryption
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2012, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under one of the following licenses:
  7. GPL, the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. gSOAP public license.
  10. The contents of this file are subject to the gSOAP Public License Version 1.3
  11. (the "License"); you may not use this file except in compliance with the
  12. License. You may obtain a copy of the License at
  13. http://www.cs.fsu.edu/~engelen/soaplicense.html
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the License.
  17. The Initial Developer of the Original Code is Robert A. van Engelen.
  18. Copyright (C) 2000-2012, Robert van Engelen, Genivia, Inc., All Rights Reserved.
  19. --------------------------------------------------------------------------------
  20. GPL license.
  21. This program is free software; you can redistribute it and/or modify it under
  22. the terms of the GNU General Public License as published by the Free Software
  23. Foundation; either version 2 of the License, or (at your option) any later
  24. version.
  25. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  26. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  27. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License along with
  29. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  30. Place, Suite 330, Boston, MA 02111-1307 USA
  31. Author contact information:
  32. engelen@genivia.com / engelen@acm.org
  33. This program is released under the GPL with the additional exemption that
  34. compiling, linking, and/or using OpenSSL is allowed.
  35. --------------------------------------------------------------------------------
  36. A commercial use license is available from Genivia, Inc., contact@genivia.com
  37. --------------------------------------------------------------------------------
  38. */
  39. /**
  40. @page mecevp The mecevp streaming message encryption and decryption engine
  41. The gSOAP mecevp engine encrypts and decrypts messages using the EVP interface
  42. of OpenSSL. It supports envelope encryption/decryption with public and private
  43. RSA keys and symmetric encryption with shared secret keys. Streaming and
  44. buffered message encryption modes are supported.
  45. An encryption and decryption algorithm and mode is selected with one of the
  46. following:
  47. - @ref SOAP_MEC_ENV_ENC_DES_CBC envelope encryption with triple DES CBC
  48. - @ref SOAP_MEC_ENV_ENC_DES_CBC envelope decryption with triple DES CBC
  49. - @ref SOAP_MEC_ENC_DES_CBC symmetric encryption with triple DES CBC
  50. - @ref SOAP_MEC_ENC_DES_CBC symmetric decryption with triple DES CBC
  51. - @ref SOAP_MEC_ENV_ENC_AES256_CBC envelope encryption with triple DES CBC
  52. - @ref SOAP_MEC_ENV_ENC_AES256_CBC envelope decryption with triple DES CBC
  53. - @ref SOAP_MEC_ENC_AES256_CBC symmetric encryption with triple DES CBC
  54. - @ref SOAP_MEC_ENC_AES256_CBC symmetric decryption with triple DES CBC
  55. where AES256 can be replaced with any one of AES128, AES192, AES512.
  56. Algorithm options:
  57. - @ref SOAP_MEC_STORE buffer all output in memory
  58. - @ref SOAP_MEC_OAEP use OAEP padding
  59. The mecevp engine wraps the EVP API with four new functions:
  60. - @ref soap_mec_init to initialize the engine
  61. - @ref soap_mec_update to encrypt/decrypt a message part
  62. - @ref soap_mec_final to finalize encryption/decryption
  63. - @ref soap_mec_cleanup to deallocate the engine and buffers
  64. All cipher data is written and read in base64 format.
  65. A higher-level interface for message encryption/decryption in parts (such as
  66. individual XML elements) is defined by two new functions:
  67. - @ref soap_mec_begin to begin a streaming sequence of encryptions/decryptions
  68. - @ref soap_mec_start to start encryption/decryption of a message part
  69. - @ref soap_mec_stop to stop encryption/decryption of a message part
  70. - @ref soap_mec_end to end the sequence and deallocate the engine buffers
  71. Compile all source codes with -DWITH_OPENSSL and link with ssl and crypto
  72. libraries.
  73. Here is an example to encrypt a message while streaming it to the output. The
  74. example uses the public key of the recipient/reader of the message. The
  75. recipient/reader uses its private key to decrypt. Envelope encryption is used
  76. with SOAP_MEC_ENV_ENC_DES_CBC, which means an ephemeral secret key is generated
  77. and encrypted with the public key. This encrypted secret key should be
  78. communicated to the recipient/reader with the message to decrypt:
  79. @code
  80. #include "mecevp.h"
  81. soap_mec_data mec;
  82. ns__Object object;
  83. int alg = SOAP_MEC_ENV_ENC_DES_CBC;
  84. FILE *fd = fopen("key.pem", "r");
  85. EVP_PKEY *pubk;
  86. unsigned char *key;
  87. int keylen;
  88. if (...) // key file contains public key?
  89. pubk = PEM_read_PUBKEY(fd, NULL, NULL, NULL);
  90. else // key file contains certificate
  91. { X509 *cert = PEM_read_X509(fd, NULL, NULL, NULL);
  92. pubk = X509_get_pubkey(cert);
  93. X509_free(cert);
  94. }
  95. fclose(fd);
  96. key = soap_malloc(soap, soap_mec_size(alg, pubk));
  97. if (soap_begin_send(soap)
  98. || soap_mec_begin(soap, &mec, alg, pubk, key, &keylen)
  99. || soap_mec_start(soap, NULL)
  100. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL)
  101. || soap_mec_stop(soap)
  102. || soap_mec_end(soap, &mec)
  103. || soap_end_send(soap))
  104. { soap_mec_cleanup(soap, &mec); // clean up when error
  105. soap_print_fault(soap, stderr);
  106. }
  107. EVP_PKEY_free(pubk);
  108. @endcode
  109. The decryption by the recipient/reader requires the ephemeral encrypted secret
  110. key generated by soap_mec_begin by the sender (as set above) to decrypt the
  111. message using envelope decryption with SOAP_MEC_ENV_DEC_DES_CBC.
  112. @code
  113. #include "mecevp.h"
  114. soap_mec_data mec;
  115. ns__Object object;
  116. int alg = SOAP_MEC_ENV_DEC_DES_CBC;
  117. FILE *fd = fopen("key.pem", "r");
  118. EVP_PKEY *privk = PEM_read_PrivateKey(fd, NULL, NULL, "password");
  119. unsigned char *key;
  120. int keylen;
  121. fclose(fd);
  122. key = ... // value set as above by sender
  123. keylen = ... // value set as above by sender
  124. if (soap_begin_recv(soap)
  125. || soap_mec_begin(soap, &mec, alg, privk, key, &keylen)
  126. || soap_mec_start(soap)
  127. || soap_in_ns__Object(soap, "ns:Object", &object, NULL) == NULL
  128. || soap_mec_stop(soap)
  129. || soap_mec_end(soap, &mec)
  130. || soap_end_recv(soap))
  131. { soap_mec_cleanup(soap, &mec); // clean up when error
  132. soap_print_fault(soap, stderr);
  133. }
  134. EVP_PKEY_free(privk);
  135. @endcode
  136. Note that the encrypted secret key can be send in the clear or stored openly,
  137. since only the recipient/reader will be able to decode it for use in message
  138. decryption.
  139. Symmetric encryption and decryption can be used if both parties can safely
  140. share a secret symmetric key that no other party has access to. We use
  141. SOAP_MEC_ENC_DES_CBC for encryption and SOAP_MEC_DEC_DES_CBC for decryption
  142. using a 160-bit triple DES key.
  143. Here is an example to encrypt a message using a shared secret key while
  144. streaming it to the output.
  145. @code
  146. #include "mecevp.h"
  147. soap_mec_data mec;
  148. ns__Object object;
  149. int alg = SOAP_MEC_ENC_DES_CBC;
  150. unsigned char key[20] = { ... }; // shared secret triple DES key
  151. int keylen = 20;
  152. if (soap_begin_send(soap)
  153. || soap_mec_begin(soap, &mec, alg, NULL, key, &keylen)
  154. || soap_mec_start(soap, NULL)
  155. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL)
  156. || soap_mec_stop(soap)
  157. || soap_mec_end(soap, &mec)
  158. || soap_end_send(soap))
  159. { soap_mec_cleanup(soap, &mec); // clean up when error
  160. soap_print_fault(soap, stderr);
  161. }
  162. @endcode
  163. The decryption by the recipient/reader requires the same shared secret key to
  164. decrypt the message using envelope decryption with SOAP_MEC_DEC_DES_CBC. This
  165. key is secret and unencrypted, so it should never be shared with any other
  166. party besides the sender/writer and recipient/reader.
  167. @code
  168. #include "mecevp.h"
  169. soap_mec_data mec;
  170. ns__Object object;
  171. int alg = SOAP_MEC_DEC_DES_CBC;
  172. unsigned char key[20] = { ... }; // shared secret triple DES key
  173. int keylen = 20;
  174. if (soap_begin_recv(soap)
  175. || soap_mec_begin(soap, &mec, alg, NULL, key, &keylen)
  176. || soap_mec_start(soap)
  177. || soap_in_ns__Object(soap, "ns:Object", &object, NULL) == NULL
  178. || soap_mec_stop(soap)
  179. || soap_mec_end(soap, &mec)
  180. || soap_end_recv(soap))
  181. { soap_mec_cleanup(soap, &mec); // clean up when error
  182. soap_print_fault(soap, stderr);
  183. }
  184. @endcode
  185. Note: the mecevp engine uses callbacks of the gSOAP engine that were introduced
  186. in version 2.8.1. Earlier gSOAP version releases are not compatible with the
  187. mecevp plugin and engine.
  188. */
  189. #include "mecevp.h"
  190. #ifdef __cplusplus
  191. extern "C" {
  192. #endif
  193. /******************************************************************************\
  194. *
  195. * Static local functions used
  196. *
  197. \******************************************************************************/
  198. static int soap_mec_upd(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final);
  199. static int soap_mec_upd_enc(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final);
  200. static int soap_mec_upd_dec(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final);
  201. static int soap_mec_check(struct soap *soap, struct soap_mec_data *data, int err, const char *msg);
  202. static void soap_mec_put_base64(struct soap *soap, struct soap_mec_data *data, const unsigned char *s, int n);
  203. static void soap_mec_end_base64(struct soap *soap, struct soap_mec_data *data);
  204. static int soap_mec_get_base64(struct soap *soap, struct soap_mec_data *data, char *t, size_t *l, const char *s, size_t n, const char **r, size_t *k);
  205. static int soap_mec_filtersend(struct soap *soap, const char **s, size_t *n);
  206. static int soap_mec_filterrecv(struct soap *soap, char *buf, size_t *len, size_t maxlen);
  207. /******************************************************************************\
  208. *
  209. * soap_mec API functions
  210. *
  211. \******************************************************************************/
  212. /**
  213. @fn int soap_mec_init(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen)
  214. @brief Initialize mecevp engine state and create context for
  215. encryption/decryption algorithm using a private/public key or symmetric secret
  216. key.
  217. @param soap context
  218. @param[in,out] data mecevp engine context
  219. @param[in] alg encryption/decryption algorithm
  220. @param[in] pkey public/private key or NULL
  221. @param[in,out] key secret key or encrypted ephemeral secret key set with envelope encryption, or NULL
  222. @param[in,out] keylen secret key length
  223. @return SOAP_OK or SOAP_SSL_ERROR
  224. */
  225. int
  226. soap_mec_init(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen)
  227. { int ok = 1;
  228. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_mec_init()\n"));
  229. soap_ssl_init();
  230. data->ctx = //(EVP_CIPHER_CTX*)SOAP_MALLOC(soap, sizeof(EVP_CIPHER_CTX));
  231. EVP_CIPHER_CTX_new();
  232. if (!data->ctx)
  233. return soap->error = SOAP_EOM;
  234. EVP_CIPHER_CTX_init(data->ctx);
  235. data->alg = alg;
  236. data->state = SOAP_MEC_STATE_NONE;
  237. if (alg & SOAP_MEC_DES_CBC)
  238. data->type = EVP_des_ede3_cbc(); /* triple DES CBC */
  239. else if (alg & SOAP_MEC_AES128_CBC)
  240. data->type = EVP_get_cipherbyname("AES128");
  241. else if (alg & SOAP_MEC_AES192_CBC)
  242. data->type = EVP_get_cipherbyname("AES192");
  243. else if (alg & SOAP_MEC_AES256_CBC)
  244. data->type = EVP_get_cipherbyname("AES256");
  245. else if (alg & SOAP_MEC_AES512_CBC)
  246. data->type = EVP_get_cipherbyname("AES512");
  247. else
  248. data->type = EVP_enc_null();
  249. data->buf = NULL;
  250. data->rest = NULL;
  251. data->restlen = 0;
  252. if (alg & SOAP_MEC_ENC)
  253. { if (!data->type)
  254. return soap_mec_check(soap, data, 0, "soap_mec_init() failed: cannot load cipher");
  255. EVP_EncryptInit_ex(data->ctx, data->type, NULL, NULL, NULL);
  256. }
  257. if (alg & SOAP_MEC_OAEP)
  258. EVP_CIPHER_CTX_set_padding(data->ctx, RSA_PKCS1_OAEP_PADDING);
  259. else
  260. EVP_CIPHER_CTX_set_padding(data->ctx, RSA_PKCS1_PADDING);
  261. switch (alg & SOAP_MEC_MASK)
  262. { case SOAP_MEC_ENV_ENC_AES128_CBC:
  263. case SOAP_MEC_ENV_ENC_AES192_CBC:
  264. case SOAP_MEC_ENV_ENC_AES256_CBC:
  265. case SOAP_MEC_ENV_ENC_AES512_CBC:
  266. case SOAP_MEC_ENV_ENC_DES_CBC:
  267. ok = EVP_CIPHER_CTX_rand_key(data->ctx, data->ekey);
  268. /* generate ephemeral secret key */
  269. #if (OPENSSL_VERSION_NUMBER >= 0x01000000L)
  270. *keylen = EVP_PKEY_encrypt_old(key, data->ekey, EVP_CIPHER_CTX_key_length(data->ctx), pkey);
  271. #else
  272. *keylen = EVP_PKEY_encrypt(key, data->ekey, EVP_CIPHER_CTX_key_length(data->ctx), pkey);
  273. #endif
  274. key = data->ekey;
  275. /* fall through to next arm */
  276. case SOAP_MEC_ENC_DES_CBC:
  277. case SOAP_MEC_ENC_AES128_CBC:
  278. case SOAP_MEC_ENC_AES192_CBC:
  279. case SOAP_MEC_ENC_AES256_CBC:
  280. case SOAP_MEC_ENC_AES512_CBC:
  281. data->bufidx = 0;
  282. data->buflen = 1024; /* > iv in base64 must fit */
  283. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  284. data->key = key;
  285. break;
  286. case SOAP_MEC_ENV_DEC_AES128_CBC:
  287. case SOAP_MEC_ENV_DEC_AES192_CBC:
  288. case SOAP_MEC_ENV_DEC_AES256_CBC:
  289. case SOAP_MEC_ENV_DEC_AES512_CBC:
  290. case SOAP_MEC_ENV_DEC_DES_CBC:
  291. case SOAP_MEC_DEC_DES_CBC:
  292. case SOAP_MEC_DEC_AES128_CBC:
  293. case SOAP_MEC_DEC_AES192_CBC:
  294. case SOAP_MEC_DEC_AES256_CBC:
  295. case SOAP_MEC_DEC_AES512_CBC:
  296. data->pkey = pkey;
  297. data->key = key;
  298. data->keylen = *keylen;
  299. break;
  300. default:
  301. return soap_set_receiver_error(soap, "Unsupported encryption algorithm", NULL, SOAP_SSL_ERROR);
  302. }
  303. return soap_mec_check(soap, data, ok, "soap_mec_init() failed");
  304. }
  305. /**
  306. @fn int soap_mec_update(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  307. @brief Update mecevp engine state: encrypts plain text (or raw data) or
  308. decrypts cipher data in base64 format.
  309. @param soap context
  310. @param[in,out] data mecevp engine context
  311. @param[in,out] s input data to convert, afterwards points to converted data (original content is unchanged)
  312. @param[in,out] n size of input, afterwards size of output
  313. @return SOAP_OK or SOAP_SSL_ERROR
  314. */
  315. int
  316. soap_mec_update(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  317. { return soap_mec_upd(soap, data, s, n, 0);
  318. }
  319. /**
  320. @fn int soap_mec_final(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  321. @brief Ends mecevp engine state: encrypt/decrypt remainder from buffers.
  322. @param soap context
  323. @param[in,out] data mecevp engine context
  324. @param[out] s afterwards points to converted remaining data in streaming mode, or entire converted data in buffer mode (SOAP_MEC_STORE option)
  325. @param[out] n afterwards size of remaining data
  326. @return SOAP_OK or SOAP_SSL_ERROR
  327. */
  328. int
  329. soap_mec_final(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  330. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_mec_final()\n"));
  331. *n = 0;
  332. if (!data->ctx)
  333. return SOAP_OK;
  334. if (soap_mec_upd(soap, data, s, n, 1))
  335. return soap->error;
  336. return SOAP_OK;
  337. }
  338. /**
  339. @fn void soap_mec_cleanup(struct soap *soap, struct soap_mec_data *data)
  340. @brief Clean up mecevp engine and deallocate cipher context and buffers.
  341. @param soap context
  342. @param[in,out] data mecevp engine context
  343. @return SOAP_OK or SOAP_SSL_ERROR
  344. */
  345. void
  346. soap_mec_cleanup(struct soap *soap, struct soap_mec_data *data)
  347. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_mec_cleanup()\n"));
  348. if (data->ctx)
  349. { EVP_CIPHER_CTX_cleanup(data->ctx);
  350. EVP_CIPHER_CTX_free( data->ctx );
  351. //SOAP_FREE(soap, data->ctx);
  352. data->ctx = NULL;
  353. }
  354. if (data->buf)
  355. { SOAP_FREE(soap, data->buf);
  356. data->buf = NULL;
  357. }
  358. if (data->rest)
  359. { SOAP_FREE(soap, data->rest);
  360. data->rest = NULL;
  361. }
  362. }
  363. /**
  364. @fn int soap_mec_begin(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen)
  365. @brief Initialize the mecevp engine data and begin encryption or decryption
  366. message sequence using a private/public key or symmetric secret key.
  367. @param soap context
  368. @param[in,out] data mecevp engine context
  369. @param[in] alg encryption/decryption algorithm
  370. @param[in] pkey public/private key or NULL
  371. @param[in,out] key secret key or encrypted ephemeral secret key set with envelope encryption, or NULL
  372. @param[in,out] keylen secret key length
  373. @return SOAP_OK or error code
  374. */
  375. int
  376. soap_mec_begin(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen)
  377. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Begin alg=%x\n", alg));
  378. /* save and set the engine's 'data' field to pass data to the callbacks */
  379. soap->data[1] = (void*)data;
  380. data->ctx = NULL;
  381. data->type = EVP_enc_null();
  382. data->pkey = NULL;
  383. data->key = NULL;
  384. data->buf = NULL;
  385. /* save the mode flag */
  386. data->mode = soap->mode;
  387. if (alg & SOAP_MEC_ENC)
  388. { /* clear the IO flags and DOM flag */
  389. soap->mode &= ~(SOAP_IO | SOAP_IO_LENGTH | SOAP_ENC_ZLIB | SOAP_XML_DOM);
  390. /* clear the XML attribute store */
  391. soap_clr_attr(soap);
  392. /* load the local XML namespaces store */
  393. soap_set_local_namespaces(soap);
  394. if (soap->mode & SOAP_XML_CANONICAL)
  395. soap->ns = 0; /* for in c14n, we must have all xmlns bindings available */
  396. }
  397. else
  398. { /* save and override the callbacks */
  399. data->ffilterrecv = soap->ffilterrecv;
  400. soap->ffilterrecv = soap_mec_filterrecv;
  401. }
  402. /* init the soap_mec engine */
  403. return soap_mec_init(soap, data, alg, pkey, key, keylen);
  404. }
  405. /**
  406. @fn int soap_mec_start_alg(struct soap *soap, int alg, const unsigned char *key)
  407. @brief Start encryption or decryption of current message. If key is non-NULL,
  408. use the symmetric triple DES key. Use soap_mec_start only after soap_mec_begin.
  409. The soap_mec_start should be followed by a soap_mec_stop call.
  410. @param soap context
  411. @param[in] alg algorithm
  412. @param[in] key secret triple DES key or NULL
  413. @return SOAP_OK or error code
  414. */
  415. int
  416. soap_mec_start_alg(struct soap *soap, int alg, const unsigned char *key)
  417. { struct soap_mec_data *data;
  418. int ok = 1;
  419. data = (struct soap_mec_data*)soap->data[1];
  420. if (!data)
  421. return soap->error = SOAP_USER_ERROR;
  422. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Start alg=%x\n", data->alg));
  423. if (key)
  424. data->key = key;
  425. if (alg != SOAP_MEC_NONE)
  426. data->alg = alg;
  427. if (data->alg & SOAP_MEC_ENC)
  428. { unsigned char iv[EVP_MAX_IV_LENGTH];
  429. int ivlen;
  430. /* save and override the callbacks */
  431. data->ffiltersend = soap->ffiltersend;
  432. soap->ffiltersend = soap_mec_filtersend;
  433. data->bufidx = 0;
  434. data->i = 0;
  435. data->m = 0;
  436. ivlen = EVP_CIPHER_iv_length(data->type);
  437. if (ivlen)
  438. { RAND_pseudo_bytes(iv, ivlen);
  439. soap_mec_put_base64(soap, data, (unsigned char*)iv, ivlen);
  440. }
  441. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "IV = "));
  442. DBGHEX(TEST, iv, ivlen);
  443. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  444. ok = EVP_EncryptInit_ex(data->ctx, NULL, NULL, data->key, iv);
  445. }
  446. else
  447. { size_t len;
  448. /* algorithm */
  449. if (data->alg & SOAP_MEC_DES_CBC)
  450. data->type = EVP_des_ede3_cbc(); /* triple DES CBC */
  451. else if (data->alg & SOAP_MEC_AES128_CBC)
  452. data->type = EVP_get_cipherbyname("AES128");
  453. else if (data->alg & SOAP_MEC_AES192_CBC)
  454. data->type = EVP_get_cipherbyname("AES192");
  455. else if (data->alg & SOAP_MEC_AES256_CBC)
  456. data->type = EVP_get_cipherbyname("AES256");
  457. else if (data->alg & SOAP_MEC_AES512_CBC)
  458. data->type = EVP_get_cipherbyname("AES512");
  459. else
  460. data->type = EVP_enc_null();
  461. len = 2 * sizeof(soap->buf) + EVP_CIPHER_block_size(data->type);
  462. if (!data->buf || data->buflen < len)
  463. { if (data->buf)
  464. SOAP_FREE(soap, data->buf);
  465. data->buflen = len;
  466. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  467. }
  468. data->bufidx = soap->buflen - soap->bufidx;
  469. /* copy buf[bufidx..buflen-1] to data buf */
  470. memcpy(data->buf, soap->buf + soap->bufidx, data->bufidx);
  471. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Alloc buf=%lu, copy %lu message bytes\n", (unsigned long)data->buflen, (unsigned long)data->bufidx));
  472. /* trigger ffilterrecv() */
  473. soap->bufidx = soap->buflen;
  474. /* INIT state */
  475. data->i = 0;
  476. data->m = 0;
  477. data->state = SOAP_MEC_STATE_INIT;
  478. }
  479. return soap_mec_check(soap, data, ok, "soap_mec_start() failed");
  480. }
  481. /**
  482. @fn int soap_mec_start(struct soap *soap, const unsigned char *key)
  483. @brief Start encryption or decryption of current message. If key is non-NULL,
  484. use the symmetric triple DES key. Use soap_mec_start only after soap_mec_begin.
  485. The soap_mec_start should be followed by a soap_mec_stop call.
  486. @param soap context
  487. @param[in] key secret triple DES key or NULL
  488. @return SOAP_OK or error code
  489. */
  490. int
  491. soap_mec_start(struct soap *soap, const unsigned char *key)
  492. { return soap_mec_start_alg(soap, SOAP_MEC_NONE, key);
  493. }
  494. /**
  495. @fn int soap_mec_stop(struct soap *soap)
  496. @brief Stops encryption or decryption of current message. Use after
  497. soap_mec_start.
  498. @param soap context
  499. @return SOAP_OK or error code
  500. */
  501. int
  502. soap_mec_stop(struct soap *soap)
  503. { struct soap_mec_data *data;
  504. int err = SOAP_OK;
  505. const char *s = NULL;
  506. size_t n = 0;
  507. data = (struct soap_mec_data*)soap->data[1];
  508. if (!data)
  509. return soap->error = SOAP_USER_ERROR;
  510. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Stop alg=%x\n", data->alg));
  511. err = soap_mec_final(soap, data, &s, &n);
  512. if (data->alg & SOAP_MEC_ENC)
  513. { /* reset callbacks */
  514. if (soap->ffiltersend == soap_mec_filtersend)
  515. soap->ffiltersend = data->ffiltersend;
  516. /* send remaining cipher data */
  517. if (!err && n)
  518. if (soap_send_raw(soap, s, n))
  519. return soap->error;
  520. }
  521. return err;
  522. }
  523. /**
  524. @fn int soap_mec_end(struct soap *soap, struct soap_mec_data *data)
  525. @brief Ends encryption or decryption of a sequence of message parts that began
  526. with soap_mec_begin.
  527. @param soap context
  528. @param[in,out] data mecevp engine context
  529. @return SOAP_OK or error code
  530. */
  531. int
  532. soap_mec_end(struct soap *soap, struct soap_mec_data *data)
  533. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC End alg=%x\n", data->alg));
  534. /* reset callbacks */
  535. if (soap->ffiltersend == soap_mec_filtersend)
  536. soap->ffiltersend = data->ffiltersend;
  537. if (soap->ffilterrecv == soap_mec_filterrecv)
  538. soap->ffilterrecv = data->ffilterrecv;
  539. /* restore the mode flag */
  540. soap->mode = data->mode;
  541. /* cleanup and reset mecevp engine */
  542. soap_mec_cleanup(soap, data);
  543. soap->data[1] = NULL;
  544. return SOAP_OK;
  545. }
  546. /**
  547. @fn size_t soap_mec_size(int alg, SOAP_MEC_KEY_TYPE *pkey)
  548. @brief Returns the number of octets needed to store the public/private key or
  549. the symmetric key, depending on the algorithm.
  550. @param[in] alg is the algorithm to be used
  551. @param[in] pkey is a pointer to an EVP_PKEY object or NULL for symmetric keys
  552. @return size_t number of octets that is needed to hold the key.
  553. */
  554. size_t
  555. soap_mec_size(int alg, SOAP_MEC_KEY_TYPE *pkey)
  556. { if (alg & SOAP_MEC_ENV)
  557. return EVP_PKEY_size(pkey);
  558. switch (alg & SOAP_MEC_MASK & ~SOAP_MEC_ENC)
  559. { case SOAP_MEC_DES_CBC:
  560. return 20; /* triple DES 160 bits */
  561. case SOAP_MEC_AES128_CBC:
  562. return 16;
  563. case SOAP_MEC_AES192_CBC:
  564. return 24;
  565. case SOAP_MEC_AES256_CBC:
  566. return 32;
  567. case SOAP_MEC_AES512_CBC:
  568. return 64;
  569. }
  570. return 0;
  571. }
  572. /******************************************************************************\
  573. *
  574. * Static local functions
  575. *
  576. \******************************************************************************/
  577. /**
  578. @fn int soap_mec_upd(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  579. @brief Update encryption/decryption state depending on the current algorithm
  580. @param soap context
  581. @param[in,out] data mecevp engine context
  582. @param[in,out] s input data to convert, afterwards points to converted data (original content is unchanged)
  583. @param[in,out] n size of input, afterwards size of output
  584. @param[in] final flag to indicate no more input, output is flushed to s
  585. @return SOAP_OK or SOAP_SSL_ERROR
  586. */
  587. static int
  588. soap_mec_upd(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  589. { if (!data || !data->ctx)
  590. return soap->error = SOAP_USER_ERROR;
  591. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- MEC Update alg=%x n=%lu final=%d (%p) --\n", data->alg, (unsigned long)*n, final, data->ctx));
  592. DBGMSG(TEST, *s, *n);
  593. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  594. if (data->alg & SOAP_MEC_ENC)
  595. { if (soap_mec_upd_enc(soap, data, s, n, final))
  596. return soap->error;
  597. }
  598. else
  599. { if (soap_mec_upd_dec(soap, data, s, n, final))
  600. return soap->error;
  601. }
  602. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  603. DBGMSG(TEST, *s, *n);
  604. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  605. return SOAP_OK;
  606. }
  607. /**
  608. @fn int soap_mec_upd_enc(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  609. @brief Update encryption state with input plain text (or raw) data and output
  610. in base64 format.
  611. @param soap context
  612. @param[in,out] data mecevp engine context
  613. @param[in,out] s input plain text, afterwards points to output cipher data
  614. @param[in,out] n size of input text, afterwards size of cipher data
  615. @param[in] final flag to indicate no more input, output is flushed to s
  616. @return SOAP_OK or SOAP_SSL_ERROR
  617. */
  618. static int
  619. soap_mec_upd_enc(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  620. { size_t k;
  621. int m;
  622. int ok = 0;
  623. /* cipher size */
  624. k = *n + EVP_CIPHER_block_size(data->type);
  625. /* scale by base64 size + in-use part + 8 margin */
  626. m = data->bufidx + 8 + (k + 2) / 3 * 4 + 1;
  627. /* fits in buf after bufidx? */
  628. if (m > (int)data->buflen)
  629. { char *t = data->buf;
  630. data->buflen = m; /* + slack? */
  631. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  632. if (t)
  633. { memcpy(data->buf, t, data->bufidx); /* copy in-use part */
  634. SOAP_FREE(soap, t);
  635. }
  636. }
  637. if (!final)
  638. { /* envelope encryption or with shared key? */
  639. if (data->alg & SOAP_MEC_ENV)
  640. ok = EVP_SealUpdate(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m, (unsigned char*)*s, (int)*n);
  641. else
  642. ok = EVP_EncryptUpdate(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m, (unsigned char*)*s, (int)*n);
  643. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  644. /* convert to base64 */
  645. soap_mec_put_base64(soap, data, (unsigned char*)(data->buf + data->buflen - k), m);
  646. *s = data->buf;
  647. *n = data->bufidx;
  648. if (!(data->alg & SOAP_MEC_STORE))
  649. data->bufidx = 0;
  650. }
  651. else
  652. { /* envelope encryption or with shared key? */
  653. if (data->alg & SOAP_MEC_ENV)
  654. ok = EVP_SealFinal(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m);
  655. else
  656. ok = EVP_EncryptFinal(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m);
  657. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  658. /* convert to base64 */
  659. soap_mec_put_base64(soap, data, (unsigned char*)(data->buf + data->buflen - k), m);
  660. soap_mec_end_base64(soap, data);
  661. *s = data->buf;
  662. *n = data->bufidx;
  663. if (!(data->alg & SOAP_MEC_STORE))
  664. data->bufidx = 0;
  665. }
  666. if (m > (int)k)
  667. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Assertion m<=k failed k=%lu m=%lu\n", (unsigned long)k, (unsigned long)m));
  668. return soap->error = SOAP_USER_ERROR;
  669. }
  670. return soap_mec_check(soap, data, ok, "soap_mec_upd_enc() failed");
  671. }
  672. /**
  673. @fn int soap_mec_upd_dec(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  674. @brief Update decryption state with input cipher data in base64 format and output in plain text (or raw) format
  675. @param soap context
  676. @param[in,out] data mecevp engine context
  677. @param[in,out] s input cipher data, afterwards points to output plain text
  678. @param[in,out] n size of input cipher data, afterwards size of plain text
  679. @param[in] final flag to indicate no more input, output is flushed to s
  680. @return SOAP_OK or SOAP_SSL_ERROR
  681. */
  682. static int
  683. soap_mec_upd_dec(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  684. { const char *r = NULL;
  685. size_t k = 0, l = 0, m = 0;
  686. int len = 0;
  687. int ok = 1;
  688. enum SOAP_MEC_STATE state = data->state;
  689. if (final && state == SOAP_MEC_STATE_DECRYPT)
  690. data->state = SOAP_MEC_STATE_FINAL;
  691. /* if flushing the buf, no base64-decode or decryption to do */
  692. if (state == SOAP_MEC_STATE_FLUSH)
  693. { /* old + new fit in buf? */
  694. if (data->bufidx + *n > data->buflen)
  695. { char *t = data->buf;
  696. do
  697. data->buflen += sizeof(soap->buf);
  698. while (data->buflen < data->bufidx + *n);
  699. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging buffer n=%lu\n", (unsigned long)data->buflen));
  700. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  701. if (t)
  702. { memcpy(data->buf, t, data->bufidx); /* copy old */
  703. SOAP_FREE(soap, t);
  704. }
  705. }
  706. /* concat old + new */
  707. memcpy(data->buf + data->bufidx, *s, *n);
  708. *s = data->buf;
  709. *n += data->bufidx;
  710. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Flush state n=%lu\n", (unsigned long)*n));
  711. /* release old + new for next round (assuming consumer fetches all) */
  712. if (!(data->alg & SOAP_MEC_STORE))
  713. data->bufidx = 0;
  714. return SOAP_OK;
  715. }
  716. if (state == SOAP_MEC_STATE_INIT)
  717. { /* at init, base64 is in data->buf[bufidx] copied from buf[] */
  718. data->i = 0;
  719. data->m = 0;
  720. k = (data->bufidx + *n + 3) / 4 * 3; /* decoded size from old + new */
  721. }
  722. else
  723. k = (*n + 3) / 4 * 3; /* base64-decoded size */
  724. m = k + EVP_CIPHER_block_size(data->type); /* decrypted data size */
  725. /* decrypted + base64-decoded will fit in current buf? */
  726. if (data->bufidx + m > data->buflen - k)
  727. { /* no, need to enlarge */
  728. char *t = data->buf;
  729. do
  730. data->buflen += sizeof(soap->buf);
  731. while (data->buflen < data->bufidx + m + k);
  732. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging buffer n=%lu\n", (unsigned long)data->buflen));
  733. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  734. if (t)
  735. { memcpy(data->buf, t, data->bufidx); /* copy old part */
  736. SOAP_FREE(soap, t);
  737. }
  738. }
  739. /* base64 decode */
  740. if (state == SOAP_MEC_STATE_INIT)
  741. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 1 n=%lu\n", (unsigned long)data->bufidx));
  742. /* base64 is in data buf[0..bufidx-1] and *s */
  743. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k, &m, data->buf, data->bufidx, &r, &l))
  744. return soap->error;
  745. /* position 'r' is at a spot that gets overwritten, copy to rest */
  746. if (r && l)
  747. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 2 rest=%lu bytes\n", (unsigned long)l));
  748. }
  749. else
  750. { size_t j;
  751. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 3 n=%lu\n", (unsigned long)*n));
  752. /* base64-decode *s */
  753. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k + m, &j, *s, *n, &r, &l))
  754. return soap->error;
  755. m += j;
  756. }
  757. data->bufidx = 0;
  758. }
  759. else if (state != SOAP_MEC_STATE_FINAL && state != SOAP_MEC_STATE_FLUSH)
  760. { /* base64-decode *s */
  761. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k, &m, *s, *n, &r, &l))
  762. return soap->error;
  763. }
  764. if (r && l)
  765. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Rest=%lu bytes\n", (unsigned long)l));
  766. if (data->restlen < l)
  767. { if (data->rest)
  768. SOAP_FREE(soap, data->rest);
  769. data->rest = (char*)SOAP_MALLOC(soap, l);
  770. }
  771. data->restlen = l;
  772. memcpy(data->rest, r, l);
  773. }
  774. /* debug */
  775. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  776. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  777. /* decryption of data buf[buflen-k] */
  778. switch (data->state)
  779. { case SOAP_MEC_STATE_INIT:
  780. /* move to next state */
  781. state = SOAP_MEC_STATE_IV;
  782. case SOAP_MEC_STATE_IV:
  783. /* get the IV data from buf[buflen-k] */
  784. memmove(data->buf + data->bufidx, data->buf + data->buflen - k, m);
  785. /* add to IV */
  786. data->bufidx += m;
  787. /* got all IV data? */
  788. if (data->bufidx >= (size_t)EVP_CIPHER_iv_length(data->type))
  789. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get IV = "));
  790. DBGHEX(TEST, (unsigned char*)data->buf, EVP_CIPHER_iv_length(data->type));
  791. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\nInitializing alg=%x\n", data->alg));
  792. switch (data->alg & SOAP_MEC_MASK)
  793. { case SOAP_MEC_ENV_DEC_DES_CBC:
  794. case SOAP_MEC_ENV_DEC_AES128_CBC:
  795. case SOAP_MEC_ENV_DEC_AES192_CBC:
  796. case SOAP_MEC_ENV_DEC_AES256_CBC:
  797. case SOAP_MEC_ENV_DEC_AES512_CBC:
  798. ok = EVP_OpenInit(data->ctx, data->type, data->key, data->keylen, (unsigned char*)data->buf, (EVP_PKEY*)data->pkey);
  799. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EVP_OpenInit ok=%d\n", ok));
  800. break;
  801. case SOAP_MEC_DEC_DES_CBC:
  802. case SOAP_MEC_DEC_AES128_CBC:
  803. case SOAP_MEC_DEC_AES192_CBC:
  804. case SOAP_MEC_DEC_AES256_CBC:
  805. case SOAP_MEC_DEC_AES512_CBC:
  806. ok = EVP_DecryptInit_ex(data->ctx, data->type, NULL, data->key, (unsigned char*)data->buf);
  807. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EVP_DecryptInit_ex ok=%d\n", ok));
  808. break;
  809. }
  810. if (ok)
  811. { /* shift rest of data to cipher section */
  812. k = data->bufidx - EVP_CIPHER_iv_length(data->type);
  813. memmove(data->buf + data->buflen - k, data->buf + EVP_CIPHER_iv_length(data->type), k);
  814. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt %lu bytes\n", (unsigned long)k));
  815. /* decrypt to buf */
  816. len = 0;
  817. switch (data->alg & SOAP_MEC_MASK)
  818. { case SOAP_MEC_ENV_DEC_DES_CBC:
  819. case SOAP_MEC_ENV_DEC_AES128_CBC:
  820. case SOAP_MEC_ENV_DEC_AES192_CBC:
  821. case SOAP_MEC_ENV_DEC_AES256_CBC:
  822. case SOAP_MEC_ENV_DEC_AES512_CBC:
  823. ok = EVP_OpenUpdate(data->ctx, (unsigned char*)data->buf, &len, (unsigned char*)(data->buf + data->buflen - k), k);
  824. break;
  825. case SOAP_MEC_DEC_DES_CBC:
  826. case SOAP_MEC_DEC_AES128_CBC:
  827. case SOAP_MEC_DEC_AES192_CBC:
  828. case SOAP_MEC_DEC_AES256_CBC:
  829. case SOAP_MEC_DEC_AES512_CBC:
  830. ok = EVP_DecryptUpdate(data->ctx, (unsigned char*)data->buf, &len, (unsigned char*)(data->buf + data->buflen - k), k);
  831. break;
  832. }
  833. *s = data->buf;
  834. *n = (size_t)len;
  835. }
  836. if (!(data->alg & SOAP_MEC_STORE))
  837. data->bufidx = 0; /* next decoded goes to start of buf */
  838. else
  839. data->bufidx = *n;
  840. /* next state */
  841. state = SOAP_MEC_STATE_DECRYPT;
  842. }
  843. else
  844. { /* nothing to return yet, need more data */
  845. *n = 0;
  846. }
  847. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "IV/decrypt state n=%lu\n", (unsigned long)*n));
  848. break;
  849. case SOAP_MEC_STATE_DECRYPT:
  850. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt %lu bytes\n", (unsigned long)m));
  851. len = 0;
  852. switch (data->alg & SOAP_MEC_MASK)
  853. { case SOAP_MEC_ENV_DEC_DES_CBC:
  854. case SOAP_MEC_ENV_DEC_AES128_CBC:
  855. case SOAP_MEC_ENV_DEC_AES192_CBC:
  856. case SOAP_MEC_ENV_DEC_AES256_CBC:
  857. case SOAP_MEC_ENV_DEC_AES512_CBC:
  858. ok = EVP_OpenUpdate(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len, (unsigned char*)(data->buf + data->buflen - k), m);
  859. break;
  860. case SOAP_MEC_DEC_DES_CBC:
  861. case SOAP_MEC_DEC_AES128_CBC:
  862. case SOAP_MEC_DEC_AES192_CBC:
  863. case SOAP_MEC_DEC_AES256_CBC:
  864. case SOAP_MEC_DEC_AES512_CBC:
  865. ok = EVP_DecryptUpdate(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len, (unsigned char*)data->buf + data->buflen - k, m);
  866. break;
  867. }
  868. *s = data->buf;
  869. *n = data->bufidx + (size_t)len;
  870. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt state n=%lu\n", (unsigned long)*n));
  871. if (!(data->alg & SOAP_MEC_STORE))
  872. data->bufidx = 0; /* next decoded goes to start of buf */
  873. break;
  874. case SOAP_MEC_STATE_FINAL:
  875. { /* we know there is enough space to flush *s and *n through the buf */
  876. const char *t = *s;
  877. k = *n;
  878. len = 0;
  879. switch (data->alg & SOAP_MEC_MASK)
  880. { case SOAP_MEC_ENV_DEC_DES_CBC:
  881. case SOAP_MEC_ENV_DEC_AES128_CBC:
  882. case SOAP_MEC_ENV_DEC_AES192_CBC:
  883. case SOAP_MEC_ENV_DEC_AES256_CBC:
  884. case SOAP_MEC_ENV_DEC_AES512_CBC:
  885. ok = EVP_OpenFinal(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len);
  886. break;
  887. case SOAP_MEC_DEC_DES_CBC:
  888. case SOAP_MEC_DEC_AES128_CBC:
  889. case SOAP_MEC_DEC_AES192_CBC:
  890. case SOAP_MEC_DEC_AES256_CBC:
  891. case SOAP_MEC_DEC_AES512_CBC:
  892. ok = EVP_DecryptFinal(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len);
  893. break;
  894. }
  895. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %d bytes from decrypted\n", len));
  896. *s = data->buf;
  897. *n = data->bufidx + (size_t)len;
  898. if (data->restlen)
  899. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %lu bytes from rest\n", data->restlen));
  900. memcpy(data->buf + *n, data->rest, data->restlen);
  901. *n += data->restlen;
  902. }
  903. if (k)
  904. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %lu bytes from input\n", k));
  905. memmove(data->buf + *n, t, k);
  906. *n += k;
  907. }
  908. if (!(data->alg & SOAP_MEC_STORE))
  909. data->bufidx = 0; /* next decoded goes to start of buf */
  910. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Final len=%lu\n", (unsigned long)*n));
  911. state = SOAP_MEC_STATE_FLUSH; /* flush data buf, if needed */
  912. break;
  913. }
  914. default:
  915. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in decryption state\n"));
  916. return soap->error = SOAP_SSL_ERROR;
  917. }
  918. if (l)
  919. { if (state == SOAP_MEC_STATE_DECRYPT)
  920. { state = SOAP_MEC_STATE_FINAL;
  921. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append rest of stream %lu (%lu <= %lu)\n", data->restlen, *n, data->buflen));
  922. }
  923. else
  924. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in decryption state\n"));
  925. return soap->error = SOAP_SSL_ERROR;
  926. }
  927. }
  928. data->state = state;
  929. return soap_mec_check(soap, data, ok, "soap_mec_upd_dec() failed");
  930. }
  931. /**
  932. @fn int soap_mec_check(struct soap *soap, struct soap_mec_data *data, int ok, const char *msg)
  933. @brief Check result of init/update/final mecevp engine operations.
  934. @param soap context
  935. @param[in,out] data mecevp engine context
  936. @param[in] ok EVP error value
  937. @param[in] msg error message
  938. @return SOAP_OK or SOAP_SSL_ERROR
  939. */
  940. static int
  941. soap_mec_check(struct soap *soap, struct soap_mec_data *data, int ok, const char *msg)
  942. { if (ok <= 0)
  943. { unsigned long r;
  944. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- MEC Error (%d)", ok));
  945. while ((r = ERR_get_error()))
  946. { ERR_error_string_n(r, soap->msgbuf, sizeof(soap->msgbuf));
  947. DBGLOG(TEST, SOAP_MESSAGE(fdebug, " %s: \"%s\";", msg, soap->msgbuf));
  948. }
  949. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  950. /* cleanup and reset mecevp engine */
  951. soap_mec_cleanup(soap, data);
  952. return soap_set_receiver_error(soap, msg, soap->msgbuf, SOAP_SSL_ERROR);
  953. }
  954. return SOAP_OK;
  955. }
  956. /**
  957. @fn void soap_mec_put_base64(struct soap *soap, struct soap_mec_data *data, const unsigned char *s, int n)
  958. @brief Write base64 formatted data stored in s of length n to internal buffer
  959. @param soap context
  960. @param[in,out] data mecevp engine context
  961. @param[in] s data to convert
  962. @param[in] n length of data to convert
  963. */
  964. static void
  965. soap_mec_put_base64(struct soap *soap, struct soap_mec_data *data, const unsigned char *s, int n)
  966. { char *t;
  967. int i;
  968. unsigned long m;
  969. if (!s || !n)
  970. return;
  971. t = data->buf + data->bufidx;
  972. i = data->i;
  973. m = data->m;
  974. while (n--)
  975. { m = (m << 8) | *s++;
  976. if (i++ == 2)
  977. { for (i = 4; i > 0; m >>= 6)
  978. t[--i] = soap_base64o[m & 0x3F];
  979. t += 4;
  980. data->bufidx += 4;
  981. }
  982. }
  983. data->i = i;
  984. data->m = m;
  985. }
  986. /**
  987. @fn void soap_mec_end_base64(struct soap *soap, struct soap_mec_data *data)
  988. @brief End writing base64 formatted data to internal buffer
  989. @param soap context
  990. @param[in,out] data mecevp engine context
  991. */
  992. static void
  993. soap_mec_end_base64(struct soap *soap, struct soap_mec_data *data)
  994. { if (data->i)
  995. { char *t;
  996. int i;
  997. unsigned long m;
  998. t = data->buf + data->bufidx;
  999. i = data->i;
  1000. m = data->m;
  1001. for (; i < 3; i++)
  1002. m <<= 8;
  1003. for (i++; i > 0; m >>= 6)
  1004. t[--i] = soap_base64o[m & 0x3F];
  1005. for (i = 3; i > data->i; i--)
  1006. t[i] = '=';
  1007. data->bufidx += 4;
  1008. }
  1009. data->i = 0;
  1010. data->m = 0;
  1011. }
  1012. /**
  1013. @fn int soap_mec_get_base64(struct soap *soap, struct soap_mec_data *data, char *t, size_t *l, const char *s, size_t n, const char **r, size_t *k)
  1014. @brief Convert base64-formatted data from s[0..n-1] into raw data in t[0..l-1]
  1015. where l is the max size and set equal or lower if data fits in t. If data does
  1016. not fit r points to remainder in s[0..n-1] of size k.
  1017. @param soap context
  1018. @param[in,out] data mecevp engine context
  1019. @param[in] t raw data (converted from base64)
  1020. @param[in,out] l max size of t[], afterwards actual size of data written to t[]
  1021. @param[in] s data in base64 format
  1022. @param[in] n size of base64 data
  1023. @param[out] r if data does not fit in t[], points to s[] remainder to convert
  1024. @param[out] k if data does not fit in t[], size of remainder in r[]
  1025. @return SOAP_OK or SOAP_SSL_ERROR
  1026. */
  1027. static int
  1028. soap_mec_get_base64(struct soap *soap, struct soap_mec_data *data, char *t, size_t *l, const char *s, size_t n, const char **r, size_t *k)
  1029. { int i;
  1030. unsigned long m;
  1031. size_t j;
  1032. int c;
  1033. i = data->i;
  1034. m = data->m;
  1035. j = 0;
  1036. for (;;)
  1037. { do
  1038. { if (!n--)
  1039. { *l = j;
  1040. data->i = i;
  1041. data->m = m;
  1042. *r = NULL;
  1043. *k = 0;
  1044. return SOAP_OK;
  1045. }
  1046. c = *s++;
  1047. if (c == '=' || c == '<')
  1048. { switch (i)
  1049. { case 2:
  1050. *t++ = (char)((m >> 4) & 0xFF);
  1051. j++;
  1052. break;
  1053. case 3:
  1054. *t++ = (char)((m >> 10) & 0xFF);
  1055. *t++ = (char)((m >> 2) & 0xFF);
  1056. j += 2;
  1057. }
  1058. if (c == '<')
  1059. { s--;
  1060. n++;
  1061. }
  1062. else if (n && *s == '=')
  1063. { s++;
  1064. n--;
  1065. }
  1066. *l = j;
  1067. *k = n;
  1068. if (n)
  1069. *r = s;
  1070. else
  1071. *r = NULL;
  1072. return SOAP_OK;
  1073. }
  1074. if (c >= '+' && c <= '+' + 79)
  1075. { register int b = soap_base64i[c - '+'];
  1076. if (b >= 64)
  1077. return soap->error = SOAP_SSL_ERROR;
  1078. m = (m << 6) + b;
  1079. i++;
  1080. }
  1081. else if (c < 0 || c > 32)
  1082. return soap->error = SOAP_SSL_ERROR;
  1083. } while (i < 4);
  1084. *t++ = (char)((m >> 16) & 0xFF);
  1085. *t++ = (char)((m >> 8) & 0xFF);
  1086. *t++ = (char)(m & 0xFF);
  1087. j += 3;
  1088. i = 0;
  1089. m = 0;
  1090. }
  1091. }
  1092. /******************************************************************************\
  1093. *
  1094. * Callbacks registered by plugin
  1095. *
  1096. \******************************************************************************/
  1097. /**
  1098. @fn int soap_mec_filtersend(struct soap *soap, const char **s, size_t *n)
  1099. @brief Callback to modify outbound messages by encrypting through the engine.
  1100. @param soap context
  1101. @param[in,out] s plain text message, afterwards set to encrypted message
  1102. @param[in,out] n plain text message size, afterwards set to encrypted message size
  1103. @return SOAP_OK or SOAP_SSL_ERROR
  1104. */
  1105. static int
  1106. soap_mec_filtersend(struct soap *soap, const char **s, size_t *n)
  1107. { struct soap_mec_data *data = (struct soap_mec_data*)soap->data[1];
  1108. if (!data)
  1109. return SOAP_OK;
  1110. /* encrypt to base64 */
  1111. return soap_mec_upd(soap, data, s, n, 0);
  1112. }
  1113. /**
  1114. @fn int soap_mec_filterrecv(struct soap *soap, char *buf, size_t *len, size_t maxlen)
  1115. @brief Callback to modify inbound messages by decrypting through the engine.
  1116. @param soap context
  1117. @param[in,out] buf encrypted message, afterwards contains decrypted content
  1118. @param[in,out] len encrypted message size, afterwards set to decrypted content size
  1119. @param[in] maxlen max length of allocated buf size to contain decrypted content
  1120. @return SOAP_OK or SOAP_SSL_ERROR
  1121. */
  1122. static int
  1123. soap_mec_filterrecv(struct soap *soap, char *buf, size_t *len, size_t maxlen)
  1124. { struct soap_mec_data *data = (struct soap_mec_data*)soap->data[1];
  1125. const char *s = buf;
  1126. if (!data || (data->alg & SOAP_MEC_MASK) == SOAP_MEC_NONE || (data->alg & SOAP_MEC_ENC))
  1127. return SOAP_OK;
  1128. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filter recv in=%lu\n", (unsigned long)*len));
  1129. /* convert s[len] to new s with len (new s = data->buf) */
  1130. if (soap_mec_upd(soap, data, &s, len, 0))
  1131. return soap->error;
  1132. /* does the result fit in buf[maxlen]? */
  1133. if (*len <= maxlen)
  1134. memcpy(buf, s, *len); /* yes: copy data to buf[] */
  1135. else
  1136. { memcpy(buf, s, maxlen); /* no: copy first part to buf[maxlen] */
  1137. memmove(data->buf, s + maxlen, *len - maxlen); /* shift rest to the left */
  1138. data->bufidx = *len - maxlen; /* keep rest of the data in s (data->buf) */
  1139. *len = maxlen;
  1140. }
  1141. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filter recv out=%lu\n", (unsigned long)*len));
  1142. return SOAP_OK;
  1143. }
  1144. #ifdef __cplusplus
  1145. }
  1146. #endif