gnutls-esp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <gnutls/gnutls.h>
  20. #include <gnutls/crypto.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. void destroy_esp_ciphers(struct esp *esp)
  26. {
  27. if (esp->cipher) {
  28. gnutls_cipher_deinit(esp->cipher);
  29. esp->cipher = NULL;
  30. }
  31. if (esp->hmac) {
  32. gnutls_hmac_deinit(esp->hmac, NULL);
  33. esp->hmac = NULL;
  34. }
  35. }
  36. static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp,
  37. gnutls_mac_algorithm_t macalg, gnutls_cipher_algorithm_t encalg)
  38. {
  39. gnutls_datum_t enc_key;
  40. int err;
  41. destroy_esp_ciphers(esp);
  42. enc_key.size = gnutls_cipher_get_key_size(encalg);
  43. enc_key.data = esp->enc_key;
  44. err = gnutls_cipher_init(&esp->cipher, encalg, &enc_key, NULL);
  45. if (err) {
  46. vpn_progress(vpninfo, PRG_ERR,
  47. _("Failed to initialise ESP cipher: %s\n"),
  48. gnutls_strerror(err));
  49. return -EIO;
  50. }
  51. err = gnutls_hmac_init(&esp->hmac, macalg,
  52. esp->hmac_key,
  53. gnutls_hmac_get_len(macalg));
  54. if (err) {
  55. vpn_progress(vpninfo, PRG_ERR,
  56. _("Failed to initialize ESP HMAC: %s\n"),
  57. gnutls_strerror(err));
  58. destroy_esp_ciphers(esp);
  59. }
  60. return 0;
  61. }
  62. int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, struct esp *esp_in)
  63. {
  64. gnutls_mac_algorithm_t macalg;
  65. gnutls_cipher_algorithm_t encalg;
  66. int ret;
  67. switch (vpninfo->esp_enc) {
  68. case ENC_AES_128_CBC:
  69. encalg = GNUTLS_CIPHER_AES_128_CBC;
  70. break;
  71. case ENC_AES_256_CBC:
  72. encalg = GNUTLS_CIPHER_AES_256_CBC;
  73. break;
  74. default:
  75. return -EINVAL;
  76. }
  77. switch (vpninfo->esp_hmac) {
  78. case HMAC_MD5:
  79. macalg = GNUTLS_MAC_MD5;
  80. break;
  81. case HMAC_SHA1:
  82. macalg = GNUTLS_MAC_SHA1;
  83. break;
  84. case HMAC_SHA256:
  85. macalg = GNUTLS_MAC_SHA256;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. ret = init_esp_cipher(vpninfo, esp_out, macalg, encalg);
  91. if (ret)
  92. return ret;
  93. gnutls_cipher_set_iv(esp_out->cipher, esp_out->iv, sizeof(esp_out->iv));
  94. ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg);
  95. if (ret) {
  96. destroy_esp_ciphers(esp_out);
  97. return ret;
  98. }
  99. return 0;
  100. }
  101. /* pkt->len shall be the *payload* length. Omitting the header and the 12-byte HMAC */
  102. int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct pkt *pkt)
  103. {
  104. unsigned char hmac_buf[MAX_HMAC_SIZE];
  105. int err;
  106. err = gnutls_hmac(esp->hmac, &pkt->esp, sizeof(pkt->esp) + pkt->len);
  107. if (err) {
  108. vpn_progress(vpninfo, PRG_ERR,
  109. _("Failed to calculate HMAC for ESP packet: %s\n"),
  110. gnutls_strerror(err));
  111. return -EIO;
  112. }
  113. gnutls_hmac_output(esp->hmac, hmac_buf);
  114. if (memcmp(hmac_buf, pkt->data + pkt->len, vpninfo->hmac_out_len)) {
  115. vpn_progress(vpninfo, PRG_DEBUG,
  116. _("Received ESP packet with invalid HMAC\n"));
  117. return -EINVAL;
  118. }
  119. if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq)))
  120. return -EINVAL;
  121. gnutls_cipher_set_iv(esp->cipher, pkt->esp.iv, sizeof(pkt->esp.iv));
  122. err = gnutls_cipher_decrypt(esp->cipher, pkt->data, pkt->len);
  123. if (err) {
  124. vpn_progress(vpninfo, PRG_ERR,
  125. _("Decrypting ESP packet failed: %s\n"),
  126. gnutls_strerror(err));
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int crypt_len)
  132. {
  133. const int blksize = 16;
  134. int err;
  135. err = gnutls_cipher_encrypt(vpninfo->esp_out.cipher, pkt->data, crypt_len);
  136. if (err) {
  137. vpn_progress(vpninfo, PRG_ERR,
  138. _("Failed to encrypt ESP packet: %s\n"),
  139. gnutls_strerror(err));
  140. return -EIO;
  141. }
  142. err = gnutls_hmac(vpninfo->esp_out.hmac, &pkt->esp, sizeof(pkt->esp) + crypt_len);
  143. if (err) {
  144. vpn_progress(vpninfo, PRG_ERR,
  145. _("Failed to calculate HMAC for ESP packet: %s\n"),
  146. gnutls_strerror(err));
  147. return -EIO;
  148. }
  149. gnutls_hmac_output(vpninfo->esp_out.hmac, pkt->data + crypt_len);
  150. memcpy(vpninfo->esp_out.iv, pkt->data + crypt_len, blksize);
  151. gnutls_cipher_encrypt(vpninfo->esp_out.cipher, vpninfo->esp_out.iv, blksize);
  152. return 0;
  153. }