Skip to content

Commit

Permalink
Treat SPI as a uint32_t instead of char[]
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Jan 26, 2015
1 parent c43d15b commit 22021bc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
19 changes: 8 additions & 11 deletions gnutls-esp.c
Expand Up @@ -109,18 +109,15 @@ int setup_esp_keys(struct openconnect_info *vpninfo)
default:
return -EINVAL;
}
#if 1
memcpy(vpninfo->esp_in.spi, vpninfo->esp_out.spi, 0x44);
#else
ret = gnutls_rnd(GNUTLS_RND_RANDOM, &vpninfo->esp_in.spi,
sizeof(vpninfo->esp_in.secrets) + sizeof(vpninfo->esp_in.spi));
if (ret) {

if ((ret = gnutls_rnd(GNUTLS_RND_NONCE, &vpninfo->esp_in.spi, sizeof(vpninfo->esp_in.spi))) ||
(ret = gnutls_rnd(GNUTLS_RND_RANDOM, &vpninfo->esp_in.secrets, sizeof(vpninfo->esp_in.secrets)))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to generate random keys for ESP: %s\n"),
gnutls_strerror(ret));
return -EIO;
}
#endif

ret = init_esp_ciphers(vpninfo, &vpninfo->esp_out, macalg, encalg);
if (ret)
return ret;
Expand All @@ -142,10 +139,10 @@ int decrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt)
unsigned char hmac_buf[20];
int err;

if (memcmp(pkt->esp.spi, vpninfo->esp_in.spi, 4)) {
if (pkt->esp.spi != vpninfo->esp_in.spi) {
vpn_progress(vpninfo, PRG_DEBUG,
_("Received ESP packet with invalid SPI %02x%02x%02x%02x\n"),
pkt->esp.spi[0], pkt->esp.spi[1], pkt->esp.spi[2], pkt->esp.spi[3]);
_("Received ESP packet with invalid SPI 0x%08x\n"),
ntohl(pkt->esp.spi));
return -EINVAL;
}

Expand Down Expand Up @@ -185,7 +182,7 @@ int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt)
int err;

/* This gets much more fun if the IV is variable-length */
memcpy(pkt->esp.spi, vpninfo->esp_out.spi, 4);
pkt->esp.spi = vpninfo->esp_out.spi;
pkt->esp.seq = htonl(vpninfo->esp_out.seq++);
err = gnutls_rnd(GNUTLS_RND_NONCE, pkt->esp.iv, sizeof(pkt->esp.iv));
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions oncp.c
Expand Up @@ -838,7 +838,7 @@ static int process_attr(struct openconnect_info *vpninfo, int group, int attr,
case GRP_ATTR(7, 1):
if (attrlen != 4)
goto badlen;
memcpy(vpninfo->esp_out.spi, data, 4);
memcpy(&vpninfo->esp_out.spi, data, 4);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP SPI (outbound): %x\n"),
TLV_BE32(data));
break;
Expand Down Expand Up @@ -1136,7 +1136,7 @@ int oncp_connect(struct openconnect_info *vpninfo)
* *not* to have an oc_text_buf and build it up manually, and since it's
* all fixed size and fairly simple anyway, just hard-code the packet */
buf_append_bytes(reqbuf, esp_kmp_hdr, sizeof(esp_kmp_hdr));
buf_append_bytes(reqbuf, vpninfo->esp_in.spi, sizeof(vpninfo->esp_in.spi));
buf_append_bytes(reqbuf, &vpninfo->esp_in.spi, sizeof(vpninfo->esp_in.spi));
buf_append_bytes(reqbuf, esp_kmp_part2, sizeof(esp_kmp_part2));
buf_append_bytes(reqbuf, vpninfo->esp_in.secrets, sizeof(vpninfo->esp_in.secrets));
if (buf_error(reqbuf))
Expand Down
4 changes: 2 additions & 2 deletions openconnect-internal.h
Expand Up @@ -119,7 +119,7 @@
/****************************************************************************/

struct esp_hdr {
unsigned char spi[4];
uint32_t spi;
uint32_t seq;
unsigned char iv[16];
unsigned char payload[];
Expand Down Expand Up @@ -258,7 +258,7 @@ struct esp {
#endif
uint32_t seq;
uint32_t seq_backlog;
unsigned char spi[4];
uint32_t spi; /* Stored network-endian */
unsigned char secrets[0x40];
};

Expand Down
12 changes: 6 additions & 6 deletions openssl-esp.c
Expand Up @@ -99,8 +99,8 @@ int setup_esp_keys(struct openconnect_info *vpninfo)
return -EINVAL;
}

if (!RAND_bytes((void *)&vpninfo->esp_in.spi,
sizeof(vpninfo->esp_in.secrets) + sizeof(vpninfo->esp_in.spi))) {
if (!RAND_pseudo_bytes((void *)&vpninfo->esp_in.spi, sizeof(vpninfo->esp_in.spi)) ||
!RAND_bytes((void *)&vpninfo->esp_in.secrets, sizeof(vpninfo->esp_in.secrets))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to generate random keys for ESP:\n"));
openconnect_report_ssl_errors(vpninfo);
Expand Down Expand Up @@ -130,10 +130,10 @@ int decrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt)
int crypt_len = pkt->len;
HMAC_CTX hmac_ctx;

if (memcmp(pkt->esp.spi, vpninfo->esp_in.spi, 4)) {
if (pkt->esp.spi != vpninfo->esp_in.spi) {
vpn_progress(vpninfo, PRG_DEBUG,
_("Received ESP packet with invalid SPI %02x%02x%02x%02x\n"),
pkt->esp.spi[0], pkt->esp.spi[1], pkt->esp.spi[2], pkt->esp.spi[3]);
_("Received ESP packet with invalid SPI 0x%08x\n"),
ntohl(pkt->esp.spi));
return -EINVAL;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt)
HMAC_CTX hmac_ctx;

/* This gets much more fun if the IV is variable-length */
memcpy(pkt->esp.spi, vpninfo->esp_out.spi, 4);
pkt->esp.spi = vpninfo->esp_out.spi;
pkt->esp.seq = htonl(vpninfo->esp_out.seq++);
if (!RAND_pseudo_bytes((void *)&pkt->esp.iv, sizeof(pkt->esp.iv))) {
vpn_progress(vpninfo, PRG_ERR,
Expand Down

0 comments on commit 22021bc

Please sign in to comment.