#include #include #include #include #include #include #include #include #include #include static const unsigned char espneg[] = { 0xc9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2e, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb5, 0x00, 0x08, 0x00, 0x00, 0x00, 0x59, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0xb0, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x11, 0x94, 0x00, 0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x07, 0x00, 0x00, 0x00, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x24, 0x6f, 0xd3, 0x4c, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xfa, 0x60, 0x8c, 0x1e, 0x5a, 0xd6, 0xa7, 0x45, 0x51, 0xd7, 0x1f, 0xa1, 0x52, 0x1f, 0x69, 0x5a, 0x51, 0xf1, 0x93, 0xaf, 0x9b, 0x11, 0x2d, 0xc1, 0x2a, 0x8f, 0x77, 0xed, 0x9f, 0x42, 0xa3, 0x8e, 0x0b, 0x34, 0x85, 0xf1, 0x12, 0x27, 0x1c, 0x0b, 0x40, 0x93, 0x58, 0x55, 0x87, 0x2d, 0x23, 0xe1, 0x63, 0x63, 0xd6, 0x6c, 0x60, 0x39, 0x37, 0xc4, 0x97, 0x0a, 0xc4, 0x48, 0x12, 0xe4, 0x35, 0xd0 }; unsigned char tlvneg[] = { 0x61 + 20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x4d, 0x00, 0x03, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x08, 0x5a, 0xa5, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x05, 0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x08, 0x08, 0x08, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x04, 0x02, 0x02, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x18, 0x6d, 0x61, 0x67, 0x32, 0x36, 0x30, 0x30, 0x2e, 0x61, 0x64, 0x76, 0x69, 0x73, 0x74, 0x61, 0x74, 0x65, 0x63, 0x68, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x59, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0xb0, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x11, 0x94, 0x00, 0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0xc0, 0xa8, 0x14, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0xc0, 0xa8, 0x14, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x24, 0x6f, 0xd3, 0x4c, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xfa, 0x60, 0x8c, 0x1e, 0x5a, 0xd6, 0xa7, 0x45, 0x51, 0xd7, 0x1f, 0xa1, 0x52, 0x1f, 0x69, 0x5a, 0x51, 0xf1, 0x93, 0xaf, 0x9b, 0x11, 0x2d, 0xc1, 0x2a, 0x8f, 0x77, 0xed, 0x9f, 0x42, 0xa3, 0x8e, 0x0b, 0x34, 0x85, 0xf1, 0x12, 0x27, 0x1c, 0x0b, 0x40, 0x93, 0x58, 0x55, 0x87, 0x2d, 0x23, 0xe1, 0x63, 0x63, 0xd6, 0x6c, 0x60, 0x39, 0x37, 0xc4, 0x97, 0x0a, 0xc4, 0x48, 0x12, 0xe4, 0x35, 0xd0, 0x00, 0x03, 0x00, 0x00, 0x00, 14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x08, 0x82, 0xd7, 0x27, 0x12, 0xff, 0xff, 0xff, 0x00 }; unsigned char esp_secrets[0x40] = { 0xfa, 0x60, 0x8c, 0x1e, 0x5a, 0xd6, 0xa7, 0x45, 0x51, 0xd7, 0x1f, 0xa1, 0x52, 0x1f, 0x69, 0x5a, 0x51, 0xf1, 0x93, 0xaf, 0x9b, 0x11, 0x2d, 0xc1, 0x2a, 0x8f, 0x77, 0xed, 0x9f, 0x42, 0xa3, 0x8e, 0x0b, 0x34, 0x85, 0xf1, 0x12, 0x27, 0x1c, 0x0b, 0x40, 0x93, 0x58, 0x55, 0x87, 0x2d, 0x23, 0xe1, 0x63, 0x63, 0xd6, 0x6c, 0x60, 0x39, 0x37, 0xc4, 0x97, 0x0a, 0xc4, 0x48, 0x12, 0xe4, 0x35, 0xd0 }; unsigned char spi[4] = {0x24, 0x6f, 0xd3, 0x4c}; unsigned char *aeskey = &esp_secrets[0]; unsigned char *hmackey = &esp_secrets[16]; int blksize = 16, ivsize = 16; gnutls_cipher_hd_t cipher; gnutls_hmac_hd_t hmac; int setup_cipher(void) { gnutls_datum_t key; key.data = aeskey; key.size = 16; if (gnutls_cipher_init(&cipher, GNUTLS_CIPHER_AES_128_CBC, &key, NULL)) { printf("cipher fail\n"); return -EIO; } if (gnutls_hmac_init(&hmac, GNUTLS_MAC_SHA1, hmackey, 20)) { printf("hmac fail\n"); return -EIO; } printf("hmac key %02x...%02x\n", hmackey[0], hmackey[19]); return 0; } int decrypt_esp(unsigned char *pkt, int len) { unsigned char plain[2048]; int i; unsigned char hmac_buf[20]; if (len < 20 + ivsize) return -EINVAL; if (memcmp(pkt, spi, 4)) { printf("Wrong SPI\n"); return -EINVAL; } gnutls_hmac_fast(GNUTLS_MAC_SHA1, hmackey, 20, pkt, len - 12, hmac_buf); if (memcmp(hmac_buf, pkt + len - 12, 12)) { printf("Invalid HMAC. Does not match "); for (i=0;i<12; i++) printf(" %02x", hmac_buf[i]); printf("\n"); } gnutls_cipher_set_iv(cipher, pkt + 8, ivsize); len -= 20 + ivsize; if (gnutls_cipher_decrypt2(cipher, pkt + 8 + ivsize, len, plain, sizeof(plain))) { printf("decrypt fail\n"); return -EINVAL; } if (plain[len - 1] != 0x04 && plain[len - 1] != 0x29) { printf("Unexpected encapsulation type 0x%02x in ESP\n", plain[len-1]); return -EINVAL; } if (len <= 2 + plain[len - 2]) { printf("Invalid padding length %02x in ESP\n", plain[len - 2]); return -EINVAL; } len -= 2 + plain[len - 2]; printf("ESP packet received:"); for (i=0;i> 8; enc[5] = out_seq >> 16; enc[4] = out_seq >> 24; out_seq++; memset(enc + 8, enc[7], ivsize); printf("Before encrypt:"); for(i=0;i 0) { static int i; printf("Got packet of len %d: %02x %02x %02x...\n", len, pktbuf[0], pktbuf[1], pktbuf[2]); if (decrypt_esp(pktbuf, len) == 1) send_esp(fd, "\0", 1, (void *)&sin, sinlen); else if (p->d) { send_esp(fd, p->d, p->l, (void *)&sin, sinlen); p++; } // if (p->d == pkt5) // gnutls_record_send(session, espneg, sizeof(espneg)); } } void hexdump(const char *title, unsigned char *buf, int len) { int i; printf("%s", title); for (i=0; i