#include #include #include #include #include #include /* * Supplied string can contain up to 32 hex chars. If it contains less, the * rest are assumed to be zero. */ int str_to_key(uint8_t *key, const char *str) { unsigned int len, i; unsigned int byte_num; unsigned int nibble_num; char c; uint8_t val; len = strlen(str); if (len > 32) return (0); memset(key, 0, 16); byte_num = 0; nibble_num = 0; for (i = 0; i < len; i++) { c = tolower(str[i]); if (c >= '0' && c <= '9') val = c - '0'; else if (c >= 'a' && c <= 'f') val = c - 'a' + 10; else return (0); key[byte_num] |= val << ((1 - nibble_num) * 4); nibble_num = (nibble_num + 1) % 2; if (nibble_num == 0) byte_num++; } return (1); } int main(int argc, char **argv) { int i; int set_psk; uint8_t key[16]; const char *which_sysctl; if (!(((argc == 3) && (strcmp(argv[1], "-p") == 0)) || (argc == 2))) { printf("usage: %s [-p] \n", argv[0]); printf(" -p Set pre-shared key, otherwise set server key\n"); printf(" Up to 32 hex digits\n"); return (1); } set_psk = (argc == 3); if (!str_to_key(key, argv[argc - 1])) { printf("Bad key format\n"); return (1); } which_sysctl = set_psk ? "net.inet.tcp.fastopen.setpsk" : "net.inet.tcp.fastopen.setkey"; printf("Setting %s to ", which_sysctl); for (i = 0; i < 16; i++) printf("%02x", key[i]); printf("\n"); if (-1 == sysctlbyname(which_sysctl, NULL, NULL, key, sizeof(key))) { perror("Setting key failed"); return (1); } return (0); }