openssl-esp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include <openssl/evp.h>
  20. #include <openssl/rand.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  26. #define EVP_CIPHER_CTX_free(c) do { \
  27. EVP_CIPHER_CTX_cleanup(c); \
  28. free(c); } while (0)
  29. #define HMAC_CTX_free(c) do { \
  30. HMAC_CTX_cleanup(c); \
  31. free(c); } while (0)
  32. static inline HMAC_CTX *HMAC_CTX_new(void)
  33. {
  34. HMAC_CTX *ret = malloc(sizeof(*ret));
  35. if (ret)
  36. HMAC_CTX_init(ret);
  37. return ret;
  38. }
  39. #endif
  40. void destroy_esp_ciphers(struct esp *esp)
  41. {
  42. if (esp->cipher) {
  43. EVP_CIPHER_CTX_free(esp->cipher);
  44. esp->cipher = NULL;
  45. }
  46. if (esp->hmac) {
  47. HMAC_CTX_free(esp->hmac);
  48. esp->hmac = NULL;
  49. }
  50. }
  51. static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp,
  52. const EVP_MD *macalg, const EVP_CIPHER *encalg, int decrypt)
  53. {
  54. int ret;
  55. destroy_esp_ciphers(esp);
  56. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  57. esp->cipher = malloc(sizeof(*esp->cipher));
  58. if (!esp->cipher)
  59. return -ENOMEM;
  60. EVP_CIPHER_CTX_init(esp->cipher);
  61. #else
  62. esp->cipher = EVP_CIPHER_CTX_new();
  63. if (!esp->cipher)
  64. return -ENOMEM;
  65. #endif
  66. if (decrypt)
  67. ret = EVP_DecryptInit_ex(esp->cipher, encalg, NULL, esp->enc_key, NULL);
  68. else {
  69. ret = EVP_EncryptInit_ex(esp->cipher, encalg, NULL, esp->enc_key, esp->iv);
  70. }
  71. if (!ret) {
  72. vpn_progress(vpninfo, PRG_ERR,
  73. _("Failed to initialise ESP cipher:\n"));
  74. openconnect_report_ssl_errors(vpninfo);
  75. return -EIO;
  76. }
  77. EVP_CIPHER_CTX_set_padding(esp->cipher, 0);
  78. esp->hmac = HMAC_CTX_new();
  79. if (!esp->hmac) {
  80. destroy_esp_ciphers(esp);
  81. return -ENOMEM;
  82. }
  83. if (!HMAC_Init_ex(esp->hmac, esp->hmac_key,
  84. EVP_MD_size(macalg), macalg, NULL)) {
  85. vpn_progress(vpninfo, PRG_ERR,
  86. _("Failed to initialize ESP HMAC\n"));
  87. openconnect_report_ssl_errors(vpninfo);
  88. destroy_esp_ciphers(esp);
  89. }
  90. return 0;
  91. }
  92. int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, struct esp *esp_in)
  93. {
  94. const EVP_CIPHER *encalg;
  95. const EVP_MD *macalg;
  96. int ret;
  97. switch (vpninfo->esp_enc) {
  98. case ENC_AES_128_CBC:
  99. encalg = EVP_aes_128_cbc();
  100. break;
  101. case ENC_AES_256_CBC:
  102. encalg = EVP_aes_256_cbc();
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. switch (vpninfo->esp_hmac) {
  108. case HMAC_MD5:
  109. macalg = EVP_md5();
  110. break;
  111. case HMAC_SHA1:
  112. macalg = EVP_sha1();
  113. break;
  114. case HMAC_SHA256:
  115. macalg = EVP_sha256();
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. ret = init_esp_cipher(vpninfo, &vpninfo->esp_out, macalg, encalg, 0);
  121. if (ret)
  122. return ret;
  123. ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg, 1);
  124. if (ret) {
  125. destroy_esp_ciphers(&vpninfo->esp_out);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. /* pkt->len shall be the *payload* length. Omitting the header and the 12-byte HMAC */
  131. int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct pkt *pkt)
  132. {
  133. unsigned char hmac_buf[MAX_HMAC_SIZE];
  134. unsigned int hmac_len = sizeof(hmac_buf);
  135. int crypt_len = pkt->len;
  136. HMAC_Init_ex(esp->hmac, NULL, 0, NULL, NULL);
  137. HMAC_Update(esp->hmac, (void *)&pkt->esp, sizeof(pkt->esp) + pkt->len);
  138. HMAC_Final(esp->hmac, hmac_buf, &hmac_len);
  139. if (memcmp(hmac_buf, pkt->data + pkt->len, vpninfo->hmac_out_len)) {
  140. vpn_progress(vpninfo, PRG_DEBUG,
  141. _("Received ESP packet with invalid HMAC\n"));
  142. return -EINVAL;
  143. }
  144. if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq)))
  145. return -EINVAL;
  146. if (!EVP_DecryptInit_ex(esp->cipher, NULL, NULL, NULL,
  147. pkt->esp.iv)) {
  148. vpn_progress(vpninfo, PRG_ERR,
  149. _("Failed to set up decryption context for ESP packet:\n"));
  150. openconnect_report_ssl_errors(vpninfo);
  151. return -EINVAL;
  152. }
  153. if (!EVP_DecryptUpdate(esp->cipher, pkt->data, &crypt_len,
  154. pkt->data, pkt->len)) {
  155. vpn_progress(vpninfo, PRG_ERR,
  156. _("Failed to decrypt ESP packet:\n"));
  157. openconnect_report_ssl_errors(vpninfo);
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int crypt_len)
  163. {
  164. int blksize = 16;
  165. unsigned int hmac_len = vpninfo->hmac_out_len;
  166. if (!EVP_EncryptUpdate(vpninfo->esp_out.cipher, pkt->data, &crypt_len,
  167. pkt->data, crypt_len)) {
  168. vpn_progress(vpninfo, PRG_ERR,
  169. _("Failed to encrypt ESP packet:\n"));
  170. openconnect_report_ssl_errors(vpninfo);
  171. return -EINVAL;
  172. }
  173. HMAC_Init_ex(vpninfo->esp_out.hmac, NULL, 0, NULL, NULL);
  174. HMAC_Update(vpninfo->esp_out.hmac, (void *)&pkt->esp, sizeof(pkt->esp) + crypt_len);
  175. HMAC_Final(vpninfo->esp_out.hmac, pkt->data + crypt_len, &hmac_len);
  176. EVP_EncryptUpdate(vpninfo->esp_out.cipher, vpninfo->esp_out.iv, &blksize,
  177. pkt->data + crypt_len + hmac_len - blksize, blksize);
  178. return 0;
  179. }