+struct sa_list {
+ struct sa_list *next;
+ u_int daddr_version;
+ union inaddr_u daddr;
+ uint32_t spi; /* if == 0, then IKEv2 */
+ int initiator;
+ u_char spii[8]; /* for IKEv2 */
+ u_char spir[8];
+ const EVP_CIPHER *evp;
+ int ivlen;
+ int authlen;
+ u_char authsecret[256];
+ int authsecret_len;
+ u_char secret[256]; /* is that big enough for all secrets? */
+ int secretlen;
+};
+
+/*
+ * this will adjust ndo_packetp and ndo_snapend to new buffer!
+ */
+USES_APPLE_DEPRECATED_API
+int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
+ int initiator,
+ u_char spii[8], u_char spir[8],
+ const u_char *buf, const u_char *end)
+{
+ struct sa_list *sa;
+ const u_char *iv;
+ int len;
+ EVP_CIPHER_CTX ctx;
+
+ /* initiator arg is any non-zero value */
+ if(initiator) initiator=1;
+
+ /* see if we can find the SA, and if so, decode it */
+ for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
+ if (sa->spi == 0
+ && initiator == sa->initiator
+ && memcmp(spii, sa->spii, 8) == 0
+ && memcmp(spir, sa->spir, 8) == 0)
+ break;
+ }
+
+ if(sa == NULL) return 0;
+ if(sa->evp == NULL) return 0;
+
+ /*
+ * remove authenticator, and see if we still have something to
+ * work with
+ */
+ end = end - sa->authlen;
+ iv = buf;
+ buf = buf + sa->ivlen;
+ len = end-buf;
+
+ if(end <= buf) return 0;
+
+ memset(&ctx, 0, sizeof(ctx));
+ if (EVP_CipherInit(&ctx, sa->evp, sa->secret, NULL, 0) < 0)
+ (*ndo->ndo_warning)(ndo, "espkey init failed");
+ EVP_CipherInit(&ctx, NULL, NULL, iv, 0);
+ EVP_Cipher(&ctx, buf, buf, len);
+ EVP_CIPHER_CTX_cleanup(&ctx);
+
+ ndo->ndo_packetp = buf;
+ ndo->ndo_snapend = end;
+
+ return 1;
+
+}
+USES_APPLE_RST
+
+static void esp_print_addsa(netdissect_options *ndo,
+ struct sa_list *sa, int sa_def)
+{
+ /* copy the "sa" */
+
+ struct sa_list *nsa;
+
+ nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
+ if (nsa == NULL)
+ (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
+
+ *nsa = *sa;
+
+ if (sa_def)
+ ndo->ndo_sa_default = nsa;