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.

1210 lines
44 KiB

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. if (!data->ctx)
  232. return soap->error = SOAP_EOM;
  233. EVP_CIPHER_CTX_init(data->ctx);
  234. data->alg = alg;
  235. data->state = SOAP_MEC_STATE_NONE;
  236. if (alg & SOAP_MEC_DES_CBC)
  237. data->type = EVP_des_ede3_cbc(); /* triple DES CBC */
  238. else if (alg & SOAP_MEC_AES128_CBC)
  239. data->type = EVP_get_cipherbyname("AES128");
  240. else if (alg & SOAP_MEC_AES192_CBC)
  241. data->type = EVP_get_cipherbyname("AES192");
  242. else if (alg & SOAP_MEC_AES256_CBC)
  243. data->type = EVP_get_cipherbyname("AES256");
  244. else if (alg & SOAP_MEC_AES512_CBC)
  245. data->type = EVP_get_cipherbyname("AES512");
  246. else
  247. data->type = EVP_enc_null();
  248. data->buf = NULL;
  249. data->rest = NULL;
  250. data->restlen = 0;
  251. if (alg & SOAP_MEC_ENC)
  252. { if (!data->type)
  253. return soap_mec_check(soap, data, 0, "soap_mec_init() failed: cannot load cipher");
  254. EVP_EncryptInit_ex(data->ctx, data->type, NULL, NULL, NULL);
  255. }
  256. if (alg & SOAP_MEC_OAEP)
  257. EVP_CIPHER_CTX_set_padding(data->ctx, RSA_PKCS1_OAEP_PADDING);
  258. else
  259. EVP_CIPHER_CTX_set_padding(data->ctx, RSA_PKCS1_PADDING);
  260. switch (alg & SOAP_MEC_MASK)
  261. { case SOAP_MEC_ENV_ENC_AES128_CBC:
  262. case SOAP_MEC_ENV_ENC_AES192_CBC:
  263. case SOAP_MEC_ENV_ENC_AES256_CBC:
  264. case SOAP_MEC_ENV_ENC_AES512_CBC:
  265. case SOAP_MEC_ENV_ENC_DES_CBC:
  266. ok = EVP_CIPHER_CTX_rand_key(data->ctx, data->ekey);
  267. /* generate ephemeral secret key */
  268. #if (OPENSSL_VERSION_NUMBER >= 0x01000000L)
  269. *keylen = EVP_PKEY_encrypt_old(key, data->ekey, EVP_CIPHER_CTX_key_length(data->ctx), pkey);
  270. #else
  271. *keylen = EVP_PKEY_encrypt(key, data->ekey, EVP_CIPHER_CTX_key_length(data->ctx), pkey);
  272. #endif
  273. key = data->ekey;
  274. /* fall through to next arm */
  275. case SOAP_MEC_ENC_DES_CBC:
  276. case SOAP_MEC_ENC_AES128_CBC:
  277. case SOAP_MEC_ENC_AES192_CBC:
  278. case SOAP_MEC_ENC_AES256_CBC:
  279. case SOAP_MEC_ENC_AES512_CBC:
  280. data->bufidx = 0;
  281. data->buflen = 1024; /* > iv in base64 must fit */
  282. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  283. data->key = key;
  284. break;
  285. case SOAP_MEC_ENV_DEC_AES128_CBC:
  286. case SOAP_MEC_ENV_DEC_AES192_CBC:
  287. case SOAP_MEC_ENV_DEC_AES256_CBC:
  288. case SOAP_MEC_ENV_DEC_AES512_CBC:
  289. case SOAP_MEC_ENV_DEC_DES_CBC:
  290. case SOAP_MEC_DEC_DES_CBC:
  291. case SOAP_MEC_DEC_AES128_CBC:
  292. case SOAP_MEC_DEC_AES192_CBC:
  293. case SOAP_MEC_DEC_AES256_CBC:
  294. case SOAP_MEC_DEC_AES512_CBC:
  295. data->pkey = pkey;
  296. data->key = key;
  297. data->keylen = *keylen;
  298. break;
  299. default:
  300. return soap_set_receiver_error(soap, "Unsupported encryption algorithm", NULL, SOAP_SSL_ERROR);
  301. }
  302. return soap_mec_check(soap, data, ok, "soap_mec_init() failed");
  303. }
  304. /**
  305. @fn int soap_mec_update(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  306. @brief Update mecevp engine state: encrypts plain text (or raw data) or
  307. decrypts cipher data in base64 format.
  308. @param soap context
  309. @param[in,out] data mecevp engine context
  310. @param[in,out] s input data to convert, afterwards points to converted data (original content is unchanged)
  311. @param[in,out] n size of input, afterwards size of output
  312. @return SOAP_OK or SOAP_SSL_ERROR
  313. */
  314. int
  315. soap_mec_update(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  316. { return soap_mec_upd(soap, data, s, n, 0);
  317. }
  318. /**
  319. @fn int soap_mec_final(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  320. @brief Ends mecevp engine state: encrypt/decrypt remainder from buffers.
  321. @param soap context
  322. @param[in,out] data mecevp engine context
  323. @param[out] s afterwards points to converted remaining data in streaming mode, or entire converted data in buffer mode (SOAP_MEC_STORE option)
  324. @param[out] n afterwards size of remaining data
  325. @return SOAP_OK or SOAP_SSL_ERROR
  326. */
  327. int
  328. soap_mec_final(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n)
  329. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_mec_final()\n"));
  330. *n = 0;
  331. if (!data->ctx)
  332. return SOAP_OK;
  333. if (soap_mec_upd(soap, data, s, n, 1))
  334. return soap->error;
  335. return SOAP_OK;
  336. }
  337. /**
  338. @fn void soap_mec_cleanup(struct soap *soap, struct soap_mec_data *data)
  339. @brief Clean up mecevp engine and deallocate cipher context and buffers.
  340. @param soap context
  341. @param[in,out] data mecevp engine context
  342. @return SOAP_OK or SOAP_SSL_ERROR
  343. */
  344. void
  345. soap_mec_cleanup(struct soap *soap, struct soap_mec_data *data)
  346. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_mec_cleanup()\n"));
  347. if (data->ctx)
  348. { EVP_CIPHER_CTX_cleanup(data->ctx);
  349. SOAP_FREE(soap, data->ctx);
  350. data->ctx = NULL;
  351. }
  352. if (data->buf)
  353. { SOAP_FREE(soap, data->buf);
  354. data->buf = NULL;
  355. }
  356. if (data->rest)
  357. { SOAP_FREE(soap, data->rest);
  358. data->rest = NULL;
  359. }
  360. }
  361. /**
  362. @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)
  363. @brief Initialize the mecevp engine data and begin encryption or decryption
  364. message sequence using a private/public key or symmetric secret key.
  365. @param soap context
  366. @param[in,out] data mecevp engine context
  367. @param[in] alg encryption/decryption algorithm
  368. @param[in] pkey public/private key or NULL
  369. @param[in,out] key secret key or encrypted ephemeral secret key set with envelope encryption, or NULL
  370. @param[in,out] keylen secret key length
  371. @return SOAP_OK or error code
  372. */
  373. int
  374. soap_mec_begin(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen)
  375. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Begin alg=%x\n", alg));
  376. /* save and set the engine's 'data' field to pass data to the callbacks */
  377. soap->data[1] = (void*)data;
  378. data->ctx = NULL;
  379. data->type = EVP_enc_null();
  380. data->pkey = NULL;
  381. data->key = NULL;
  382. data->buf = NULL;
  383. /* save the mode flag */
  384. data->mode = soap->mode;
  385. if (alg & SOAP_MEC_ENC)
  386. { /* clear the IO flags and DOM flag */
  387. soap->mode &= ~(SOAP_IO | SOAP_IO_LENGTH | SOAP_ENC_ZLIB | SOAP_XML_DOM);
  388. /* clear the XML attribute store */
  389. soap_clr_attr(soap);
  390. /* load the local XML namespaces store */
  391. soap_set_local_namespaces(soap);
  392. if (soap->mode & SOAP_XML_CANONICAL)
  393. soap->ns = 0; /* for in c14n, we must have all xmlns bindings available */
  394. }
  395. else
  396. { /* save and override the callbacks */
  397. data->ffilterrecv = soap->ffilterrecv;
  398. soap->ffilterrecv = soap_mec_filterrecv;
  399. }
  400. /* init the soap_mec engine */
  401. return soap_mec_init(soap, data, alg, pkey, key, keylen);
  402. }
  403. /**
  404. @fn int soap_mec_start_alg(struct soap *soap, int alg, const unsigned char *key)
  405. @brief Start encryption or decryption of current message. If key is non-NULL,
  406. use the symmetric triple DES key. Use soap_mec_start only after soap_mec_begin.
  407. The soap_mec_start should be followed by a soap_mec_stop call.
  408. @param soap context
  409. @param[in] alg algorithm
  410. @param[in] key secret triple DES key or NULL
  411. @return SOAP_OK or error code
  412. */
  413. int
  414. soap_mec_start_alg(struct soap *soap, int alg, const unsigned char *key)
  415. { struct soap_mec_data *data;
  416. int ok = 1;
  417. data = (struct soap_mec_data*)soap->data[1];
  418. if (!data)
  419. return soap->error = SOAP_USER_ERROR;
  420. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Start alg=%x\n", data->alg));
  421. if (key)
  422. data->key = key;
  423. if (alg != SOAP_MEC_NONE)
  424. data->alg = alg;
  425. if (data->alg & SOAP_MEC_ENC)
  426. { unsigned char iv[EVP_MAX_IV_LENGTH];
  427. int ivlen;
  428. /* save and override the callbacks */
  429. data->ffiltersend = soap->ffiltersend;
  430. soap->ffiltersend = soap_mec_filtersend;
  431. data->bufidx = 0;
  432. data->i = 0;
  433. data->m = 0;
  434. ivlen = EVP_CIPHER_iv_length(data->type);
  435. if (ivlen)
  436. { RAND_pseudo_bytes(iv, ivlen);
  437. soap_mec_put_base64(soap, data, (unsigned char*)iv, ivlen);
  438. }
  439. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "IV = "));
  440. DBGHEX(TEST, iv, ivlen);
  441. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  442. ok = EVP_EncryptInit_ex(data->ctx, NULL, NULL, data->key, iv);
  443. }
  444. else
  445. { size_t len;
  446. /* algorithm */
  447. if (data->alg & SOAP_MEC_DES_CBC)
  448. data->type = EVP_des_ede3_cbc(); /* triple DES CBC */
  449. else if (data->alg & SOAP_MEC_AES128_CBC)
  450. data->type = EVP_get_cipherbyname("AES128");
  451. else if (data->alg & SOAP_MEC_AES192_CBC)
  452. data->type = EVP_get_cipherbyname("AES192");
  453. else if (data->alg & SOAP_MEC_AES256_CBC)
  454. data->type = EVP_get_cipherbyname("AES256");
  455. else if (data->alg & SOAP_MEC_AES512_CBC)
  456. data->type = EVP_get_cipherbyname("AES512");
  457. else
  458. data->type = EVP_enc_null();
  459. len = 2 * sizeof(soap->buf) + EVP_CIPHER_block_size(data->type);
  460. if (!data->buf || data->buflen < len)
  461. { if (data->buf)
  462. SOAP_FREE(soap, data->buf);
  463. data->buflen = len;
  464. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  465. }
  466. data->bufidx = soap->buflen - soap->bufidx;
  467. /* copy buf[bufidx..buflen-1] to data buf */
  468. memcpy(data->buf, soap->buf + soap->bufidx, data->bufidx);
  469. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Alloc buf=%lu, copy %lu message bytes\n", (unsigned long)data->buflen, (unsigned long)data->bufidx));
  470. /* trigger ffilterrecv() */
  471. soap->bufidx = soap->buflen;
  472. /* INIT state */
  473. data->i = 0;
  474. data->m = 0;
  475. data->state = SOAP_MEC_STATE_INIT;
  476. }
  477. return soap_mec_check(soap, data, ok, "soap_mec_start() failed");
  478. }
  479. /**
  480. @fn int soap_mec_start(struct soap *soap, const unsigned char *key)
  481. @brief Start encryption or decryption of current message. If key is non-NULL,
  482. use the symmetric triple DES key. Use soap_mec_start only after soap_mec_begin.
  483. The soap_mec_start should be followed by a soap_mec_stop call.
  484. @param soap context
  485. @param[in] key secret triple DES key or NULL
  486. @return SOAP_OK or error code
  487. */
  488. int
  489. soap_mec_start(struct soap *soap, const unsigned char *key)
  490. { return soap_mec_start_alg(soap, SOAP_MEC_NONE, key);
  491. }
  492. /**
  493. @fn int soap_mec_stop(struct soap *soap)
  494. @brief Stops encryption or decryption of current message. Use after
  495. soap_mec_start.
  496. @param soap context
  497. @return SOAP_OK or error code
  498. */
  499. int
  500. soap_mec_stop(struct soap *soap)
  501. { struct soap_mec_data *data;
  502. int err = SOAP_OK;
  503. const char *s = NULL;
  504. size_t n = 0;
  505. data = (struct soap_mec_data*)soap->data[1];
  506. if (!data)
  507. return soap->error = SOAP_USER_ERROR;
  508. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC Stop alg=%x\n", data->alg));
  509. err = soap_mec_final(soap, data, &s, &n);
  510. if (data->alg & SOAP_MEC_ENC)
  511. { /* reset callbacks */
  512. if (soap->ffiltersend == soap_mec_filtersend)
  513. soap->ffiltersend = data->ffiltersend;
  514. /* send remaining cipher data */
  515. if (!err && n)
  516. if (soap_send_raw(soap, s, n))
  517. return soap->error;
  518. }
  519. return err;
  520. }
  521. /**
  522. @fn int soap_mec_end(struct soap *soap, struct soap_mec_data *data)
  523. @brief Ends encryption or decryption of a sequence of message parts that began
  524. with soap_mec_begin.
  525. @param soap context
  526. @param[in,out] data mecevp engine context
  527. @return SOAP_OK or error code
  528. */
  529. int
  530. soap_mec_end(struct soap *soap, struct soap_mec_data *data)
  531. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MEC End alg=%x\n", data->alg));
  532. /* reset callbacks */
  533. if (soap->ffiltersend == soap_mec_filtersend)
  534. soap->ffiltersend = data->ffiltersend;
  535. if (soap->ffilterrecv == soap_mec_filterrecv)
  536. soap->ffilterrecv = data->ffilterrecv;
  537. /* restore the mode flag */
  538. soap->mode = data->mode;
  539. /* cleanup and reset mecevp engine */
  540. soap_mec_cleanup(soap, data);
  541. soap->data[1] = NULL;
  542. return SOAP_OK;
  543. }
  544. /**
  545. @fn size_t soap_mec_size(int alg, SOAP_MEC_KEY_TYPE *pkey)
  546. @brief Returns the number of octets needed to store the public/private key or
  547. the symmetric key, depending on the algorithm.
  548. @param[in] alg is the algorithm to be used
  549. @param[in] pkey is a pointer to an EVP_PKEY object or NULL for symmetric keys
  550. @return size_t number of octets that is needed to hold the key.
  551. */
  552. size_t
  553. soap_mec_size(int alg, SOAP_MEC_KEY_TYPE *pkey)
  554. { if (alg & SOAP_MEC_ENV)
  555. return EVP_PKEY_size(pkey);
  556. switch (alg & SOAP_MEC_MASK & ~SOAP_MEC_ENC)
  557. { case SOAP_MEC_DES_CBC:
  558. return 20; /* triple DES 160 bits */
  559. case SOAP_MEC_AES128_CBC:
  560. return 16;
  561. case SOAP_MEC_AES192_CBC:
  562. return 24;
  563. case SOAP_MEC_AES256_CBC:
  564. return 32;
  565. case SOAP_MEC_AES512_CBC:
  566. return 64;
  567. }
  568. return 0;
  569. }
  570. /******************************************************************************\
  571. *
  572. * Static local functions
  573. *
  574. \******************************************************************************/
  575. /**
  576. @fn int soap_mec_upd(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  577. @brief Update encryption/decryption state depending on the current algorithm
  578. @param soap context
  579. @param[in,out] data mecevp engine context
  580. @param[in,out] s input data to convert, afterwards points to converted data (original content is unchanged)
  581. @param[in,out] n size of input, afterwards size of output
  582. @param[in] final flag to indicate no more input, output is flushed to s
  583. @return SOAP_OK or SOAP_SSL_ERROR
  584. */
  585. static int
  586. soap_mec_upd(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  587. { if (!data || !data->ctx)
  588. return soap->error = SOAP_USER_ERROR;
  589. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- MEC Update alg=%x n=%lu final=%d (%p) --\n", data->alg, (unsigned long)*n, final, data->ctx));
  590. DBGMSG(TEST, *s, *n);
  591. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  592. if (data->alg & SOAP_MEC_ENC)
  593. { if (soap_mec_upd_enc(soap, data, s, n, final))
  594. return soap->error;
  595. }
  596. else
  597. { if (soap_mec_upd_dec(soap, data, s, n, final))
  598. return soap->error;
  599. }
  600. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  601. DBGMSG(TEST, *s, *n);
  602. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  603. return SOAP_OK;
  604. }
  605. /**
  606. @fn int soap_mec_upd_enc(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  607. @brief Update encryption state with input plain text (or raw) data and output
  608. in base64 format.
  609. @param soap context
  610. @param[in,out] data mecevp engine context
  611. @param[in,out] s input plain text, afterwards points to output cipher data
  612. @param[in,out] n size of input text, afterwards size of cipher data
  613. @param[in] final flag to indicate no more input, output is flushed to s
  614. @return SOAP_OK or SOAP_SSL_ERROR
  615. */
  616. static int
  617. soap_mec_upd_enc(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  618. { size_t k;
  619. int m;
  620. int ok = 0;
  621. /* cipher size */
  622. k = *n + EVP_CIPHER_block_size(data->type);
  623. /* scale by base64 size + in-use part + 8 margin */
  624. m = data->bufidx + 8 + (k + 2) / 3 * 4 + 1;
  625. /* fits in buf after bufidx? */
  626. if (m > (int)data->buflen)
  627. { char *t = data->buf;
  628. data->buflen = m; /* + slack? */
  629. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  630. if (t)
  631. { memcpy(data->buf, t, data->bufidx); /* copy in-use part */
  632. SOAP_FREE(soap, t);
  633. }
  634. }
  635. if (!final)
  636. { /* envelope encryption or with shared key? */
  637. if (data->alg & SOAP_MEC_ENV)
  638. ok = EVP_SealUpdate(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m, (unsigned char*)*s, (int)*n);
  639. else
  640. ok = EVP_EncryptUpdate(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m, (unsigned char*)*s, (int)*n);
  641. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  642. /* convert to base64 */
  643. soap_mec_put_base64(soap, data, (unsigned char*)(data->buf + data->buflen - k), m);
  644. *s = data->buf;
  645. *n = data->bufidx;
  646. if (!(data->alg & SOAP_MEC_STORE))
  647. data->bufidx = 0;
  648. }
  649. else
  650. { /* envelope encryption or with shared key? */
  651. if (data->alg & SOAP_MEC_ENV)
  652. ok = EVP_SealFinal(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m);
  653. else
  654. ok = EVP_EncryptFinal(data->ctx, (unsigned char*)data->buf + data->buflen - k, &m);
  655. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  656. /* convert to base64 */
  657. soap_mec_put_base64(soap, data, (unsigned char*)(data->buf + data->buflen - k), m);
  658. soap_mec_end_base64(soap, data);
  659. *s = data->buf;
  660. *n = data->bufidx;
  661. if (!(data->alg & SOAP_MEC_STORE))
  662. data->bufidx = 0;
  663. }
  664. if (m > (int)k)
  665. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Assertion m<=k failed k=%lu m=%lu\n", (unsigned long)k, (unsigned long)m));
  666. return soap->error = SOAP_USER_ERROR;
  667. }
  668. return soap_mec_check(soap, data, ok, "soap_mec_upd_enc() failed");
  669. }
  670. /**
  671. @fn int soap_mec_upd_dec(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  672. @brief Update decryption state with input cipher data in base64 format and output in plain text (or raw) format
  673. @param soap context
  674. @param[in,out] data mecevp engine context
  675. @param[in,out] s input cipher data, afterwards points to output plain text
  676. @param[in,out] n size of input cipher data, afterwards size of plain text
  677. @param[in] final flag to indicate no more input, output is flushed to s
  678. @return SOAP_OK or SOAP_SSL_ERROR
  679. */
  680. static int
  681. soap_mec_upd_dec(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n, int final)
  682. { const char *r = NULL;
  683. size_t k = 0, l = 0, m = 0;
  684. int len = 0;
  685. int ok = 1;
  686. enum SOAP_MEC_STATE state = data->state;
  687. if (final && state == SOAP_MEC_STATE_DECRYPT)
  688. data->state = SOAP_MEC_STATE_FINAL;
  689. /* if flushing the buf, no base64-decode or decryption to do */
  690. if (state == SOAP_MEC_STATE_FLUSH)
  691. { /* old + new fit in buf? */
  692. if (data->bufidx + *n > data->buflen)
  693. { char *t = data->buf;
  694. do
  695. data->buflen += sizeof(soap->buf);
  696. while (data->buflen < data->bufidx + *n);
  697. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging buffer n=%lu\n", (unsigned long)data->buflen));
  698. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  699. if (t)
  700. { memcpy(data->buf, t, data->bufidx); /* copy old */
  701. SOAP_FREE(soap, t);
  702. }
  703. }
  704. /* concat old + new */
  705. memcpy(data->buf + data->bufidx, *s, *n);
  706. *s = data->buf;
  707. *n += data->bufidx;
  708. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Flush state n=%lu\n", (unsigned long)*n));
  709. /* release old + new for next round (assuming consumer fetches all) */
  710. if (!(data->alg & SOAP_MEC_STORE))
  711. data->bufidx = 0;
  712. return SOAP_OK;
  713. }
  714. if (state == SOAP_MEC_STATE_INIT)
  715. { /* at init, base64 is in data->buf[bufidx] copied from buf[] */
  716. data->i = 0;
  717. data->m = 0;
  718. k = (data->bufidx + *n + 3) / 4 * 3; /* decoded size from old + new */
  719. }
  720. else
  721. k = (*n + 3) / 4 * 3; /* base64-decoded size */
  722. m = k + EVP_CIPHER_block_size(data->type); /* decrypted data size */
  723. /* decrypted + base64-decoded will fit in current buf? */
  724. if (data->bufidx + m > data->buflen - k)
  725. { /* no, need to enlarge */
  726. char *t = data->buf;
  727. do
  728. data->buflen += sizeof(soap->buf);
  729. while (data->buflen < data->bufidx + m + k);
  730. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging buffer n=%lu\n", (unsigned long)data->buflen));
  731. data->buf = (char*)SOAP_MALLOC(soap, data->buflen);
  732. if (t)
  733. { memcpy(data->buf, t, data->bufidx); /* copy old part */
  734. SOAP_FREE(soap, t);
  735. }
  736. }
  737. /* base64 decode */
  738. if (state == SOAP_MEC_STATE_INIT)
  739. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 1 n=%lu\n", (unsigned long)data->bufidx));
  740. /* base64 is in data buf[0..bufidx-1] and *s */
  741. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k, &m, data->buf, data->bufidx, &r, &l))
  742. return soap->error;
  743. /* position 'r' is at a spot that gets overwritten, copy to rest */
  744. if (r && l)
  745. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 2 rest=%lu bytes\n", (unsigned long)l));
  746. }
  747. else
  748. { size_t j;
  749. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Init state stage 3 n=%lu\n", (unsigned long)*n));
  750. /* base64-decode *s */
  751. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k + m, &j, *s, *n, &r, &l))
  752. return soap->error;
  753. m += j;
  754. }
  755. data->bufidx = 0;
  756. }
  757. else if (state != SOAP_MEC_STATE_FINAL && state != SOAP_MEC_STATE_FLUSH)
  758. { /* base64-decode *s */
  759. if (soap_mec_get_base64(soap, data, data->buf + data->buflen - k, &m, *s, *n, &r, &l))
  760. return soap->error;
  761. }
  762. if (r && l)
  763. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Rest=%lu bytes\n", (unsigned long)l));
  764. if (data->restlen < l)
  765. { if (data->rest)
  766. SOAP_FREE(soap, data->rest);
  767. data->rest = (char*)SOAP_MALLOC(soap, l);
  768. }
  769. data->restlen = l;
  770. memcpy(data->rest, r, l);
  771. }
  772. /* debug */
  773. DBGHEX(TEST, (unsigned char*)(data->buf + data->buflen - k), m);
  774. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--\n"));
  775. /* decryption of data buf[buflen-k] */
  776. switch (data->state)
  777. { case SOAP_MEC_STATE_INIT:
  778. /* move to next state */
  779. state = SOAP_MEC_STATE_IV;
  780. case SOAP_MEC_STATE_IV:
  781. /* get the IV data from buf[buflen-k] */
  782. memmove(data->buf + data->bufidx, data->buf + data->buflen - k, m);
  783. /* add to IV */
  784. data->bufidx += m;
  785. /* got all IV data? */
  786. if (data->bufidx >= (size_t)EVP_CIPHER_iv_length(data->type))
  787. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get IV = "));
  788. DBGHEX(TEST, (unsigned char*)data->buf, EVP_CIPHER_iv_length(data->type));
  789. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\nInitializing alg=%x\n", data->alg));
  790. switch (data->alg & SOAP_MEC_MASK)
  791. { case SOAP_MEC_ENV_DEC_DES_CBC:
  792. case SOAP_MEC_ENV_DEC_AES128_CBC:
  793. case SOAP_MEC_ENV_DEC_AES192_CBC:
  794. case SOAP_MEC_ENV_DEC_AES256_CBC:
  795. case SOAP_MEC_ENV_DEC_AES512_CBC:
  796. ok = EVP_OpenInit(data->ctx, data->type, data->key, data->keylen, (unsigned char*)data->buf, (EVP_PKEY*)data->pkey);
  797. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EVP_OpenInit ok=%d\n", ok));
  798. break;
  799. case SOAP_MEC_DEC_DES_CBC:
  800. case SOAP_MEC_DEC_AES128_CBC:
  801. case SOAP_MEC_DEC_AES192_CBC:
  802. case SOAP_MEC_DEC_AES256_CBC:
  803. case SOAP_MEC_DEC_AES512_CBC:
  804. ok = EVP_DecryptInit_ex(data->ctx, data->type, NULL, data->key, (unsigned char*)data->buf);
  805. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EVP_DecryptInit_ex ok=%d\n", ok));
  806. break;
  807. }
  808. if (ok)
  809. { /* shift rest of data to cipher section */
  810. k = data->bufidx - EVP_CIPHER_iv_length(data->type);
  811. memmove(data->buf + data->buflen - k, data->buf + EVP_CIPHER_iv_length(data->type), k);
  812. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt %lu bytes\n", (unsigned long)k));
  813. /* decrypt to buf */
  814. len = 0;
  815. switch (data->alg & SOAP_MEC_MASK)
  816. { case SOAP_MEC_ENV_DEC_DES_CBC:
  817. case SOAP_MEC_ENV_DEC_AES128_CBC:
  818. case SOAP_MEC_ENV_DEC_AES192_CBC:
  819. case SOAP_MEC_ENV_DEC_AES256_CBC:
  820. case SOAP_MEC_ENV_DEC_AES512_CBC:
  821. ok = EVP_OpenUpdate(data->ctx, (unsigned char*)data->buf, &len, (unsigned char*)(data->buf + data->buflen - k), k);
  822. break;
  823. case SOAP_MEC_DEC_DES_CBC:
  824. case SOAP_MEC_DEC_AES128_CBC:
  825. case SOAP_MEC_DEC_AES192_CBC:
  826. case SOAP_MEC_DEC_AES256_CBC:
  827. case SOAP_MEC_DEC_AES512_CBC:
  828. ok = EVP_DecryptUpdate(data->ctx, (unsigned char*)data->buf, &len, (unsigned char*)(data->buf + data->buflen - k), k);
  829. break;
  830. }
  831. *s = data->buf;
  832. *n = (size_t)len;
  833. }
  834. if (!(data->alg & SOAP_MEC_STORE))
  835. data->bufidx = 0; /* next decoded goes to start of buf */
  836. else
  837. data->bufidx = *n;
  838. /* next state */
  839. state = SOAP_MEC_STATE_DECRYPT;
  840. }
  841. else
  842. { /* nothing to return yet, need more data */
  843. *n = 0;
  844. }
  845. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "IV/decrypt state n=%lu\n", (unsigned long)*n));
  846. break;
  847. case SOAP_MEC_STATE_DECRYPT:
  848. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt %lu bytes\n", (unsigned long)m));
  849. len = 0;
  850. switch (data->alg & SOAP_MEC_MASK)
  851. { case SOAP_MEC_ENV_DEC_DES_CBC:
  852. case SOAP_MEC_ENV_DEC_AES128_CBC:
  853. case SOAP_MEC_ENV_DEC_AES192_CBC:
  854. case SOAP_MEC_ENV_DEC_AES256_CBC:
  855. case SOAP_MEC_ENV_DEC_AES512_CBC:
  856. ok = EVP_OpenUpdate(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len, (unsigned char*)(data->buf + data->buflen - k), m);
  857. break;
  858. case SOAP_MEC_DEC_DES_CBC:
  859. case SOAP_MEC_DEC_AES128_CBC:
  860. case SOAP_MEC_DEC_AES192_CBC:
  861. case SOAP_MEC_DEC_AES256_CBC:
  862. case SOAP_MEC_DEC_AES512_CBC:
  863. ok = EVP_DecryptUpdate(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len, (unsigned char*)data->buf + data->buflen - k, m);
  864. break;
  865. }
  866. *s = data->buf;
  867. *n = data->bufidx + (size_t)len;
  868. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Decrypt state n=%lu\n", (unsigned long)*n));
  869. if (!(data->alg & SOAP_MEC_STORE))
  870. data->bufidx = 0; /* next decoded goes to start of buf */
  871. break;
  872. case SOAP_MEC_STATE_FINAL:
  873. { /* we know there is enough space to flush *s and *n through the buf */
  874. const char *t = *s;
  875. k = *n;
  876. len = 0;
  877. switch (data->alg & SOAP_MEC_MASK)
  878. { case SOAP_MEC_ENV_DEC_DES_CBC:
  879. case SOAP_MEC_ENV_DEC_AES128_CBC:
  880. case SOAP_MEC_ENV_DEC_AES192_CBC:
  881. case SOAP_MEC_ENV_DEC_AES256_CBC:
  882. case SOAP_MEC_ENV_DEC_AES512_CBC:
  883. ok = EVP_OpenFinal(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len);
  884. break;
  885. case SOAP_MEC_DEC_DES_CBC:
  886. case SOAP_MEC_DEC_AES128_CBC:
  887. case SOAP_MEC_DEC_AES192_CBC:
  888. case SOAP_MEC_DEC_AES256_CBC:
  889. case SOAP_MEC_DEC_AES512_CBC:
  890. ok = EVP_DecryptFinal(data->ctx, (unsigned char*)(data->buf + data->bufidx), &len);
  891. break;
  892. }
  893. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %d bytes from decrypted\n", len));
  894. *s = data->buf;
  895. *n = data->bufidx + (size_t)len;
  896. if (data->restlen)
  897. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %lu bytes from rest\n", data->restlen));
  898. memcpy(data->buf + *n, data->rest, data->restlen);
  899. *n += data->restlen;
  900. }
  901. if (k)
  902. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append %lu bytes from input\n", k));
  903. memmove(data->buf + *n, t, k);
  904. *n += k;
  905. }
  906. if (!(data->alg & SOAP_MEC_STORE))
  907. data->bufidx = 0; /* next decoded goes to start of buf */
  908. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Final len=%lu\n", (unsigned long)*n));
  909. state = SOAP_MEC_STATE_FLUSH; /* flush data buf, if needed */
  910. break;
  911. }
  912. default:
  913. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in decryption state\n"));
  914. return soap->error = SOAP_SSL_ERROR;
  915. }
  916. if (l)
  917. { if (state == SOAP_MEC_STATE_DECRYPT)
  918. { state = SOAP_MEC_STATE_FINAL;
  919. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Append rest of stream %lu (%lu <= %lu)\n", data->restlen, *n, data->buflen));
  920. }
  921. else
  922. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in decryption state\n"));
  923. return soap->error = SOAP_SSL_ERROR;
  924. }
  925. }
  926. data->state = state;
  927. return soap_mec_check(soap, data, ok, "soap_mec_upd_dec() failed");
  928. }
  929. /**
  930. @fn int soap_mec_check(struct soap *soap, struct soap_mec_data *data, int ok, const char *msg)
  931. @brief Check result of init/update/final mecevp engine operations.
  932. @param soap context
  933. @param[in,out] data mecevp engine context
  934. @param[in] ok EVP error value
  935. @param[in] msg error message
  936. @return SOAP_OK or SOAP_SSL_ERROR
  937. */
  938. static int
  939. soap_mec_check(struct soap *soap, struct soap_mec_data *data, int ok, const char *msg)
  940. { if (ok <= 0)
  941. { unsigned long r;
  942. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- MEC Error (%d)", ok));
  943. while ((r = ERR_get_error()))
  944. { ERR_error_string_n(r, soap->msgbuf, sizeof(soap->msgbuf));
  945. DBGLOG(TEST, SOAP_MESSAGE(fdebug, " %s: \"%s\";", msg, soap->msgbuf));
  946. }
  947. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  948. /* cleanup and reset mecevp engine */
  949. soap_mec_cleanup(soap, data);
  950. return soap_set_receiver_error(soap, msg, soap->msgbuf, SOAP_SSL_ERROR);
  951. }
  952. return SOAP_OK;
  953. }
  954. /**
  955. @fn void soap_mec_put_base64(struct soap *soap, struct soap_mec_data *data, const unsigned char *s, int n)
  956. @brief Write base64 formatted data stored in s of length n to internal buffer
  957. @param soap context
  958. @param[in,out] data mecevp engine context
  959. @param[in] s data to convert
  960. @param[in] n length of data to convert
  961. */
  962. static void
  963. soap_mec_put_base64(struct soap *soap, struct soap_mec_data *data, const unsigned char *s, int n)
  964. { char *t;
  965. int i;
  966. unsigned long m;
  967. if (!s || !n)
  968. return;
  969. t = data->buf + data->bufidx;
  970. i = data->i;
  971. m = data->m;
  972. while (n--)
  973. { m = (m << 8) | *s++;
  974. if (i++ == 2)
  975. { for (i = 4; i > 0; m >>= 6)
  976. t[--i] = soap_base64o[m & 0x3F];
  977. t += 4;
  978. data->bufidx += 4;
  979. }
  980. }
  981. data->i = i;
  982. data->m = m;
  983. }
  984. /**
  985. @fn void soap_mec_end_base64(struct soap *soap, struct soap_mec_data *data)
  986. @brief End writing base64 formatted data to internal buffer
  987. @param soap context
  988. @param[in,out] data mecevp engine context
  989. */
  990. static void
  991. soap_mec_end_base64(struct soap *soap, struct soap_mec_data *data)
  992. { if (data->i)
  993. { char *t;
  994. int i;
  995. unsigned long m;
  996. t = data->buf + data->bufidx;
  997. i = data->i;
  998. m = data->m;
  999. for (; i < 3; i++)
  1000. m <<= 8;
  1001. for (i++; i > 0; m >>= 6)
  1002. t[--i] = soap_base64o[m & 0x3F];
  1003. for (i = 3; i > data->i; i--)
  1004. t[i] = '=';
  1005. data->bufidx += 4;
  1006. }
  1007. data->i = 0;
  1008. data->m = 0;
  1009. }
  1010. /**
  1011. @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)
  1012. @brief Convert base64-formatted data from s[0..n-1] into raw data in t[0..l-1]
  1013. where l is the max size and set equal or lower if data fits in t. If data does
  1014. not fit r points to remainder in s[0..n-1] of size k.
  1015. @param soap context
  1016. @param[in,out] data mecevp engine context
  1017. @param[in] t raw data (converted from base64)
  1018. @param[in,out] l max size of t[], afterwards actual size of data written to t[]
  1019. @param[in] s data in base64 format
  1020. @param[in] n size of base64 data
  1021. @param[out] r if data does not fit in t[], points to s[] remainder to convert
  1022. @param[out] k if data does not fit in t[], size of remainder in r[]
  1023. @return SOAP_OK or SOAP_SSL_ERROR
  1024. */
  1025. static int
  1026. 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)
  1027. { int i;
  1028. unsigned long m;
  1029. size_t j;
  1030. int c;
  1031. i = data->i;
  1032. m = data->m;
  1033. j = 0;
  1034. for (;;)
  1035. { do
  1036. { if (!n--)
  1037. { *l = j;
  1038. data->i = i;
  1039. data->m = m;
  1040. *r = NULL;
  1041. *k = 0;
  1042. return SOAP_OK;
  1043. }
  1044. c = *s++;
  1045. if (c == '=' || c == '<')
  1046. { switch (i)
  1047. { case 2:
  1048. *t++ = (char)((m >> 4) & 0xFF);
  1049. j++;
  1050. break;
  1051. case 3:
  1052. *t++ = (char)((m >> 10) & 0xFF);
  1053. *t++ = (char)((m >> 2) & 0xFF);
  1054. j += 2;
  1055. }
  1056. if (c == '<')
  1057. { s--;
  1058. n++;
  1059. }
  1060. else if (n && *s == '=')
  1061. { s++;
  1062. n--;
  1063. }
  1064. *l = j;
  1065. *k = n;
  1066. if (n)
  1067. *r = s;
  1068. else
  1069. *r = NULL;
  1070. return SOAP_OK;
  1071. }
  1072. if (c >= '+' && c <= '+' + 79)
  1073. { register int b = soap_base64i[c - '+'];
  1074. if (b >= 64)
  1075. return soap->error = SOAP_SSL_ERROR;
  1076. m = (m << 6) + b;
  1077. i++;
  1078. }
  1079. else if (c < 0 || c > 32)
  1080. return soap->error = SOAP_SSL_ERROR;
  1081. } while (i < 4);
  1082. *t++ = (char)((m >> 16) & 0xFF);
  1083. *t++ = (char)((m >> 8) & 0xFF);
  1084. *t++ = (char)(m & 0xFF);
  1085. j += 3;
  1086. i = 0;
  1087. m = 0;
  1088. }
  1089. }
  1090. /******************************************************************************\
  1091. *
  1092. * Callbacks registered by plugin
  1093. *
  1094. \******************************************************************************/
  1095. /**
  1096. @fn int soap_mec_filtersend(struct soap *soap, const char **s, size_t *n)
  1097. @brief Callback to modify outbound messages by encrypting through the engine.
  1098. @param soap context
  1099. @param[in,out] s plain text message, afterwards set to encrypted message
  1100. @param[in,out] n plain text message size, afterwards set to encrypted message size
  1101. @return SOAP_OK or SOAP_SSL_ERROR
  1102. */
  1103. static int
  1104. soap_mec_filtersend(struct soap *soap, const char **s, size_t *n)
  1105. { struct soap_mec_data *data = (struct soap_mec_data*)soap->data[1];
  1106. if (!data)
  1107. return SOAP_OK;
  1108. /* encrypt to base64 */
  1109. return soap_mec_upd(soap, data, s, n, 0);
  1110. }
  1111. /**
  1112. @fn int soap_mec_filterrecv(struct soap *soap, char *buf, size_t *len, size_t maxlen)
  1113. @brief Callback to modify inbound messages by decrypting through the engine.
  1114. @param soap context
  1115. @param[in,out] buf encrypted message, afterwards contains decrypted content
  1116. @param[in,out] len encrypted message size, afterwards set to decrypted content size
  1117. @param[in] maxlen max length of allocated buf size to contain decrypted content
  1118. @return SOAP_OK or SOAP_SSL_ERROR
  1119. */
  1120. static int
  1121. soap_mec_filterrecv(struct soap *soap, char *buf, size_t *len, size_t maxlen)
  1122. { struct soap_mec_data *data = (struct soap_mec_data*)soap->data[1];
  1123. const char *s = buf;
  1124. if (!data || (data->alg & SOAP_MEC_MASK) == SOAP_MEC_NONE || (data->alg & SOAP_MEC_ENC))
  1125. return SOAP_OK;
  1126. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filter recv in=%lu\n", (unsigned long)*len));
  1127. /* convert s[len] to new s with len (new s = data->buf) */
  1128. if (soap_mec_upd(soap, data, &s, len, 0))
  1129. return soap->error;
  1130. /* does the result fit in buf[maxlen]? */
  1131. if (*len <= maxlen)
  1132. memcpy(buf, s, *len); /* yes: copy data to buf[] */
  1133. else
  1134. { memcpy(buf, s, maxlen); /* no: copy first part to buf[maxlen] */
  1135. memmove(data->buf, s + maxlen, *len - maxlen); /* shift rest to the left */
  1136. data->bufidx = *len - maxlen; /* keep rest of the data in s (data->buf) */
  1137. *len = maxlen;
  1138. }
  1139. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filter recv out=%lu\n", (unsigned long)*len));
  1140. return SOAP_OK;
  1141. }
  1142. #ifdef __cplusplus
  1143. }
  1144. #endif