static void auth_generate_auth_hmac(struct login_ctx *l) { struct buf* buf = buf_new(); buf_append_data(buf, l->client_parameters->ptr, l->client_parameters->len); buf_append_data(buf, l->server_parameters->ptr, l->server_parameters->len); buf_append_u8(buf, 0); /* random data length */ buf_append_u8(buf, 0); /* unknown */ buf_append_u16(buf, 8); /* puzzle solution length */ buf_append_u32(buf, 0); /* unknown */ /* <-- random data would go here */ buf_append_data(buf, l->puzzle_solution, 8); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_generate_auth_hmac, HMAC message", buf->ptr, buf->len); hexdump8x32 ("auth_generate_auth_hmac, HMAC key", l->key_hmac, sizeof (l->key_hmac)); #endif sha1_hmac(l->key_hmac, sizeof(l->key_hmac), buf->ptr, buf->len, l->auth_hmac); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_generate_auth_hmac, HMAC digest", l->auth_hmac, sizeof(l->auth_hmac)); #endif buf_free(buf); }
void auth_generate_auth_hmac (SESSION * session, unsigned char *auth_hmac, unsigned int mac_len) { (void)mac_len; struct buf* buf = buf_new(); buf_append_data(buf, session->init_client_packet->ptr, session->init_client_packet->len); buf_append_data(buf, session->init_server_packet->ptr, session->init_server_packet->len); buf_append_u8(buf, 0); /* random data length */ buf_append_u8(buf, 0); /* unknown */ buf_append_u16(buf, 8); /* puzzle solution length */ buf_append_u32(buf, 0); /* unknown */ /* <-- random data would go here */ buf_append_data(buf, session->puzzle_solution, 8); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_generate_auth_hmac, HMAC message", buf->ptr, buf->len); hexdump8x32 ("auth_generate_auth_hmac, HMAC key", session->key_hmac, sizeof (session->key_hmac)); #endif sha1_hmac ( session->key_hmac, sizeof (session->key_hmac), buf->ptr, buf->len, auth_hmac); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_generate_auth_hmac, HMAC digest", auth_hmac, mac_len); #endif buf_free(buf); }
int read_server_auth_response (SESSION * session) { unsigned char buf[256]; unsigned char payload_len; int ret; ret = block_read(session->ap_sock, buf, 2); if (ret != 2) { DSFYDEBUG("Failed to read 'status' + length byte, got %d bytes\n", ret); return -1; } if (buf[0] != 0x00) { DSFYDEBUG("Authentication failed with error 0x%02x, bad password?\n", buf[1]); return -1; } /* Payload length + this byte must not be zero(?) */ assert (buf[1] > 0); payload_len = buf[1]; ret = block_read (session->ap_sock, buf, payload_len); if (ret != payload_len) { DSFYDEBUG("Failed to read 'payload', got %d of %u bytes\n", ret, payload_len); return -1; } #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_auth_response, payload", buf, payload_len); #endif return 0; }
int send_client_auth (SESSION * session) { int ret; struct buf* buf = buf_new(); buf_append_data(buf, session->auth_hmac, 20); buf_append_u8(buf, 0); /* random data length */ buf_append_u8(buf, 0); /* unknown */ buf_append_u16(buf, 8); /* puzzle solution length */ buf_append_u32(buf, 0); /* <-- random data would go here */ buf_append_data (buf, session->puzzle_solution, 8); #ifdef DEBUG_LOGIN hexdump8x32 ("send_client_auth, second client packet", buf->ptr, buf->len); #endif ret = send(session->ap_sock, buf->ptr, buf->len, 0); if (ret <= 0) { DSFYDEBUG("send_client_auth(): connection lost\n"); buf_free(buf); return -1; } else if (ret != buf->len) { DSFYDEBUG("send_client_auth(): only wrote %d of %d bytes\n", ret, buf->len); buf_free(buf); return -1; } buf_free(buf); return 0; }
int send_client_initial_packet (SESSION * session) { int ret; unsigned int len_idx; struct buf* b = buf_new(); buf_append_u16 (b, 3); /* protocol version */ len_idx = b->len; buf_append_u16(b, 0); /* packet length - updated later */ buf_append_u32(b, 0x00000300); /* unknown */ buf_append_u32(b, 0x00030c00); /* unknown */ buf_append_u32(b, session->client_revision); buf_append_u32(b, 0); /* unknown */ buf_append_u32(b, 0x01000000); /* unknown */ buf_append_data(b, session->client_id, 4); buf_append_u32(b, 0); /* unknown */ buf_append_data (b, session->client_random_16, 16); buf_append_data (b, session->my_pub_key, 96); BN_bn2bin (session->rsa->n, session->rsa_pub_exp); buf_append_data (b, session->rsa_pub_exp, sizeof(session->rsa_pub_exp)); buf_append_u8 (b, 0); /* length of random data */ buf_append_u8 (b, session->username_len); buf_append_u16(b, 0x0100); /* unknown */ /* <-- random data would go here */ buf_append_data (b, (unsigned char *) session->username, session->username_len); buf_append_u8 (b, 0x40); /* unknown */ /* * Update length bytes * */ b->ptr[len_idx] = (b->len >> 8) & 0xff; b->ptr[len_idx+1] = b->len & 0xff; #ifdef DEBUG_LOGIN hexdump8x32 ("initial client packet", b->ptr, b->len); #endif ret = send (session->ap_sock, b->ptr, b->len, 0); if (ret <= 0) { DSFYDEBUG("connection lost\n"); buf_free(b); return -1; } else if (ret != b->len) { DSFYDEBUG("only wrote %d of %d bytes\n", ret, b->len); buf_free(b); return -1; } /* save initial server packet for auth hmac generation */ session->init_client_packet = b; return 0; }
static void puzzle_solve (struct login_ctx *l) { SHA1_CTX ctx; unsigned char digest[20]; unsigned int *nominator_from_hash; unsigned int denominator; unsigned int seed; int i; /* * Modulus operation by a power of two. * "Most programmers learn this trick early" * Well, f**k me. I'm just here for the party. * */ denominator = 1 << l->puzzle_denominator; denominator--; /* * Compute a hash over random data until * (last dword byteswapped XOR magic number) mod * denominator by server produces zero. * */ seed = get_millisecs() ^ (get_millisecs() << 9); srandom(seed); nominator_from_hash = (unsigned int *) (digest + 16); do { SHA1Init (&ctx); SHA1Update (&ctx, l->server_random_16, 16); /* Let's waste some precious pseudorandomness */ for (i = 0; i < 8; i++) l->puzzle_solution[i] = random (); SHA1Update (&ctx, l->puzzle_solution, 8); SHA1Final (digest, &ctx); /* byteswap (XXX - htonl() won't work on bigendian machines!) */ *nominator_from_hash = htonl (*nominator_from_hash); /* XOR with a fancy magic */ *nominator_from_hash ^= l->puzzle_magic; } while (*nominator_from_hash & denominator); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_solve_puzzle, puzzle_solution", l->puzzle_solution, 8); #endif }
static void auth_generate_auth_hash(struct login_ctx *l) { unsigned char space = ' '; SHA1_CTX ctx; SHA1Init(&ctx); SHA1Update(&ctx, l->salt, 10); SHA1Update(&ctx, &space, 1); SHA1Update(&ctx, (unsigned char *)l->password, strlen(l->password)); SHA1Final(l->auth_hash, &ctx); #ifdef DEBUG_LOGIN hexdump8x32("auth_generate_auth_hash, auth_hash", l->auth_hash, 20); #endif }
void auth_generate_auth_hash (SESSION * session) { SHA1_CTX ctx; SHA1Init (&ctx); SHA1Update (&ctx, (unsigned char *)session->salt, 10); SHA1Update (&ctx, (unsigned char *)" ", 1); SHA1Update (&ctx, (unsigned char *)session->password, strlen (session->password)); SHA1Final (session->auth_hash, &ctx); #ifdef DEBUG_LOGIN hexdump8x32 ("auth_generate_auth_hash, auth_hash", session->auth_hash, 20); #endif }
static int receive_server_auth_response(struct login_ctx *l) { unsigned char buf[256]; unsigned char payload_len; int ret; ret = block_read(l->sock, buf, 2); if (ret != 2) { DSFYDEBUG("Failed to read 'status' + length byte, got %d bytes\n", ret); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } if (buf[0] != 0x00) { DSFYDEBUG("Authentication failed with error 0x%02x, bad password?\n", buf[1]); l->error = SP_LOGIN_ERROR_BAD_PASSWORD; return -1; } /* Payload length + this byte must not be zero(?) */ assert (buf[1] > 0); payload_len = buf[1]; ret = block_read (l->sock, buf, payload_len); if (ret != payload_len) { DSFYDEBUG("Failed to read 'payload', got %d of %u bytes\n", ret, payload_len); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } #ifdef DEBUG_LOGIN hexdump8x32("receive_server_auth_response, payload", buf, payload_len); #endif return 0; }
static int send_client_auth_packet(struct login_ctx *l) { int ret; struct buf* buf = buf_new(); buf_append_data(buf, l->auth_hmac, 20); buf_append_u8(buf, 0); /* random data length */ buf_append_u8(buf, 0); /* unknown */ buf_append_u16(buf, 8); /* puzzle solution length */ buf_append_u32(buf, 0); /* <-- random data would go here */ buf_append_data (buf, l->puzzle_solution, 8); #ifdef DEBUG_LOGIN hexdump8x32("send_client_auth_packet, second client packet", buf->ptr, buf->len); #endif ret = send(l->sock, buf->ptr, buf->len, 0); if (ret <= 0) { DSFYDEBUG("Connection was reset\n"); buf_free(buf); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } else if (ret != buf->len) { DSFYDEBUG("Only wrote %d of %d bytes\n", ret, buf->len); buf_free(buf); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_free(buf); return 0; }
/* * Initialize common crypto keys used for communication * * This step takes place after the initial two packets * have been exchanged. * */ static void key_init(struct login_ctx *l) { BIGNUM *pub_key; unsigned char message[53]; unsigned char hmac_output[20 * 5]; unsigned char *ptr, *hmac_ptr; unsigned int mac_len; int i; /* * Compute DH shared key * It's used in the call to HMAC() below * */ pub_key = BN_bin2bn(l->remote_pub_key, 96, NULL); if((i = DH_compute_key(l->shared_key, pub_key, l->dh)) < 0) { /* XXX */ return; } #ifdef DEBUG_LOGIN hexdump8x32 ("key_init, my private key", l->my_priv_key, 96); hexdump8x32 ("key_init, my public key", l->client_pub_key, 96); hexdump8x32 ("key_init, remote public key", l->remote_pub_key, 96); hexdump8x32 ("key_init, shared key", l->shared_key, 96); #endif BN_free(pub_key); /* * Prepare a message to authenticate. * * Prior to the 19th of December 2008 Spotify happily told clients * (including ours!) almost everything it knew about a particular * user, if they asked for it. * * Legitimate requests for this is for example when you add * someone else's shared playlist. * * This allowed clients to see not only the last four digits of the * credit card used to subscribe to the premium service, whether * the user was a paying customer or preferred commercials, but * also very interesting stuff such as the hash computed from * SHA(salt || " " || password). * * In theory (HE HE!) this allowed any registered user to request * somebody else's user data, get ahold of the hash, and then use * it to authenticate as that user. * * Fortunately, at lest for Spotify and it's users, this is not * the case anymore. (R.I.P poor misfeature) * * However, we urge people to change their passwords for reasons * left as an exercise for the reader to figure out. * */ ptr = message; memcpy (ptr, l->auth_hash, sizeof (l->auth_hash)); ptr += sizeof (l->auth_hash); memcpy (ptr, l->client_random_16, 16); ptr += 16; memcpy (ptr, l->server_random_16, 16); ptr += 16; /* * Run HMAC over the message, using the DH shared key as key * */ hmac_ptr = hmac_output; mac_len = 20; for (i = 1; i <= 5; i++) { /* * Change last byte of message to authenticate * */ *ptr = i; #ifdef DEBUG_LOGIN hexdump8x32 ("key_init, HMAC message", message, sizeof (message)); #endif sha1_hmac(l->shared_key, 96, message, sizeof (message), hmac_ptr); /* * Overwrite the 20 first bytes of the message with output from this round * */ memcpy (message, hmac_ptr, 20); hmac_ptr += 20; } /* * Use computed HMAC to setup keys for the * stream cipher * */ memcpy (l->key_send, hmac_output + 20, 32); memcpy (l->key_recv, hmac_output + 52, 32); /* * The first 20 bytes of the HMAC output is used * to key another HMAC computed for the second * authentication packet sent by the client. * */ memcpy (l->key_hmac, hmac_output, 20); #ifdef DEBUG_LOGIN hexdump8x32 ("key_init, key_hmac", l->key_hmac, 20); hexdump8x32 ("key_init, key_send", l->key_send, 32); hexdump8x32 ("key_init, key_recv", l->key_recv, 32); #endif }
static int receive_server_parameters(struct login_ctx *l) { char buf[512]; unsigned char padlen, username_len; unsigned short chalen[4]; int normalize; int ret; struct buf* save = buf_new(); /* read 2 status bytes */ ret = block_read(l->sock, l->server_random_16, 2); if(ret < 2) { DSFYDEBUG("Failed to read status bytes, return value was %d, errno is %d\n", ret, errno); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } if (l->server_random_16[0] != 0) { DSFYDEBUG("Bad response: %#02x %#02x\n", l->server_random_16[0], l->server_random_16[1]); switch (l->server_random_16[1]) { case 1: /* client upgrade required */ l->error = SP_LOGIN_ERROR_UPGRADE_REQUIRED; return -1; case 3: /* user not found */ l->error = SP_LOGIN_ERROR_USER_NOT_FOUND; return -1; case 4: /* account has been disabled */ l->error = SP_LOGIN_ERROR_USER_BANNED; return -1; case 6: /* you need to complete your account details */ l->error = SP_LOGIN_ERROR_USER_NEED_TO_COMPLETE_DETAILS; return -1; case 9: /* country mismatch */ l->error = SP_LOGIN_ERROR_USER_COUNTRY_MISMATCH; return -1; default: /* unknown error */ l->error = SP_LOGIN_ERROR_OTHER_PERMANENT; return -1; } } /* read remaining 14 random bytes */ ret = block_read(l->sock, l->server_random_16 + 2, 14); if(ret < 14) { DSFYDEBUG("Failed to read server random\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, l->server_random_16, 16); /* read public key */ ret = block_read(l->sock, l->remote_pub_key, 96); if (ret != 96) { DSFYDEBUG("Failed to read 'remote_pub_key'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, l->remote_pub_key, 96); /* read server blob */ ret = block_read(l->sock, buf, 256); if (ret != 256) { DSFYDEBUG("Failed to read 'random_256', got %d of 256 bytes\n", ret); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, buf, 256); /* read salt */ ret = block_read(l->sock, l->salt, 10); if (ret != 10) { DSFYDEBUG("Failed to read 'salt'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, l->salt, 10); /* read padding length */ ret = block_read(l->sock, &padlen, 1); if (ret != 1) { DSFYDEBUG("Failed to read 'padding length'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } assert (padlen > 0); buf_append_u8(save, padlen); /* read username length */ ret = block_read(l->sock, &username_len, 1); if (ret != 1) { DSFYDEBUG("Failed to read 'username_len'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_u8(save, username_len); /* read challenge lengths */ ret = block_read(l->sock, chalen, 8); if (ret != 8) { DSFYDEBUG("Failed to read challenge lengths\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, chalen, 8); /* read packet padding */ ret = block_read(l->sock, buf, padlen); if (ret != padlen) { DSFYDEBUG("Failed to read 'padding'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, buf, padlen); /* read username */ ret = block_read(l->sock, l->username, username_len); if (ret != username_len) { DSFYDEBUG("Failed to read 'username'\n"); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, l->username, username_len); l->username[username_len] = 0; /* read puzzle challenge */ { int puzzle_len = ntohs(chalen[0]); int len1 = ntohs(chalen[1]); int len2 = ntohs(chalen[2]); int len3 = ntohs(chalen[3]); int totlen = puzzle_len + len1 + len2 + len3; struct buf* b = buf_new(); buf_extend(b, totlen); DSFYDEBUG("Reading a total of %d bytes puzzle challenge\n", totlen); ret = block_read(l->sock, b->ptr, totlen); if (ret != totlen) { DSFYDEBUG("Failed to read puzzle\n"); buf_free(b); l->error = SP_LOGIN_ERROR_SOCKET_ERROR; return -1; } buf_append_data(save, b->ptr, totlen); if (b->ptr[0] == 1) { l->puzzle_denominator = b->ptr[1]; memcpy(&normalize, b->ptr + 2, sizeof(int)); l->puzzle_magic = ntohl(normalize); } else { DSFYDEBUG("Unexpected puzzle challenge with first byte 0x%02x\n", b->ptr[0]); hexdump8x32("receive_server_parameters, puzzle", b->ptr, totlen); l->error = SP_LOGIN_ERROR_OTHER_PERMANENT; buf_free(b); return -1; } buf_free(b); } l->server_parameters = save; return 0; }
int read_server_initial_packet (SESSION * session) { char buf[512]; unsigned char padlen; int ret; struct buf* save = buf_new(); /* read 2 status bytes */ ret = block_read(session->ap_sock, session->server_random_16, 2); if (ret < 2) { DSFYDEBUG("Failed to read status bytes\n"); DSFYDEBUG("Remote host was %s:%d\n", session->server_host, session->server_port); if (ret > 0) hexdump8x32 ("read_server_initial_packet, server_random_16", session->server_random_16, ret); return -90; } #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, server_random_16", session->server_random_16, ret); #endif if (session->server_random_16[0] != 0) { DSFYDEBUG("Bad response: %#02x %#02x\n", session->server_random_16[0], session->server_random_16[1]); switch (session->server_random_16[1]) { case 1: /* client upgrade required */ return -11; case 3: /* user not found */ return -13; case 4: /* account has been disabled */ return -14; case 6: /* you need to complete your account details */ return -16; case 9: /* country mismatch */ return -19; default: /* unknown error */ return -91; } } /* read remaining 14 random bytes */ ret = block_read(session->ap_sock, session->server_random_16 + 2, 14); if (ret < 14) { DSFYDEBUG("Failed to read server random\n"); DSFYDEBUG("Remote host was %s:%d\n", session->server_host, session->server_port); if (ret > 0) hexdump8x32("read_server_initial_packet, server_random_16", session->server_random_16+2, ret); return -92; } buf_append_data(save, session->server_random_16, 16); /* read public key */ ret = block_read(session->ap_sock, session->remote_pub_key, 96); if (ret != 96) { DSFYDEBUG("Failed to read 'remote_pub_key'\n"); return -93; } buf_append_data(save, session->remote_pub_key, 96); #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, server pub key", session->remote_pub_key, 96); #endif /* read server blob */ ret = block_read(session->ap_sock, session->random_256, 256); if (ret != 256) { DSFYDEBUG("Failed to read 'random_256', got %d of 256 bytes\n", ret); return -94; } buf_append_data(save, session->random_256, 256); #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, random_256", session->random_256, 256); #endif /* read salt */ ret = block_read(session->ap_sock, session->salt, 10); if (ret != 10) { DSFYDEBUG("Failed to read 'salt'\n"); return -95; } buf_append_data(save, session->salt, 10); #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, salt", session->salt, 10); #endif /* read padding length */ ret = block_read(session->ap_sock, &padlen, 1); if (ret != 1) { DSFYDEBUG("Failed to read 'padding length'\n"); return -96; } assert (padlen > 0); buf_append_u8(save, padlen); /* read username length */ ret = block_read(session->ap_sock, &session->username_len, 1); if (ret != 1) { DSFYDEBUG("Failed to read 'username_len'\n"); return -97; } buf_append_u8(save, session->username_len); /* read challenge lengths */ unsigned short chalen[4]; ret = block_read(session->ap_sock, chalen, 8); if (ret != 8) { DSFYDEBUG("Failed to read challenge lengths\n"); return -98; } buf_append_data(save, chalen, 8); /* read packet padding */ ret = block_read(session->ap_sock, buf, padlen); if (ret != padlen) { DSFYDEBUG("Failed to read 'padding'\n"); return -99; } buf_append_data(save, buf, padlen); #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, padding", buf, padlen); #endif /* read username */ ret = block_read(session->ap_sock, session->username, session->username_len); if (ret != session->username_len) { DSFYDEBUG("Failed to read 'username'\n"); return -100; } buf_append_data(save, session->username, session->username_len); session->username[session->username_len] = 0; #ifdef DEBUG_LOGIN hexdump8x32 ("read_server_initial_packet, username", session->username, session->username_len); #endif /* read puzzle challenge */ { int puzzle_len = ntohs(chalen[0]); int len1 = ntohs(chalen[1]); int len2 = ntohs(chalen[2]); int len3 = ntohs(chalen[3]); int totlen = puzzle_len + len1 + len2 + len3; int normalize = 0; struct buf* b = buf_new(); buf_extend(b, totlen); ret = block_read(session->ap_sock, b->ptr, totlen); if (ret != totlen) { DSFYDEBUG("Failed to read puzzle\n"); buf_free(b); return -101; } buf_append_data(save, b->ptr, totlen); #ifdef DEBUG_LOGIN hexdump8x32("read_server_initial_packet, puzzle", b->ptr, totlen); #endif if (b->ptr[0] == 1) { session->puzzle_denominator = b->ptr[1]; memcpy(&normalize, b->ptr+2, sizeof(int)); session->puzzle_magic = ntohl(normalize); } else { DSFYDEBUG("Unexpected puzzle challenge\n"); hexdump8x32("read_server_initial_packet, puzzle", b->ptr, totlen); buf_free(b); return -102; } buf_free(b); } session->init_server_packet = save; return 0; }