| 1 | /** |
| 2 | * Copyright (C) 2011-2012 Juho Vähä-Herttua |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <assert.h> |
| 19 | |
| 20 | #include "raop.h" |
| 21 | #include "raop_rtp.h" |
| 22 | #include "rsakey.h" |
| 23 | #include "digest.h" |
| 24 | #include "httpd.h" |
| 25 | #include "sdp.h" |
| 26 | |
| 27 | #include "global.h" |
| 28 | #include "utils.h" |
| 29 | #include "netutils.h" |
| 30 | #include "logger.h" |
| 31 | #include "compat.h" |
| 32 | |
| 33 | /* Actually 345 bytes for 2048-bit key */ |
| 34 | #define MAX_SIGNATURE_LEN 512 |
| 35 | |
| 36 | /* Let's just decide on some length */ |
| 37 | #define MAX_PASSWORD_LEN 64 |
| 38 | |
| 39 | /* MD5 as hex fits here */ |
| 40 | #define MAX_NONCE_LEN 32 |
| 41 | |
| 42 | struct raop_s { |
| 43 | /* Callbacks for audio */ |
| 44 | raop_callbacks_t callbacks; |
| 45 | |
| 46 | /* Logger instance */ |
| 47 | logger_t *logger; |
| 48 | |
| 49 | /* HTTP daemon and RSA key */ |
| 50 | httpd_t *httpd; |
| 51 | rsakey_t *rsakey; |
| 52 | |
| 53 | /* Hardware address information */ |
| 54 | unsigned char hwaddr[MAX_HWADDR_LEN]; |
| 55 | int hwaddrlen; |
| 56 | |
| 57 | /* Password information */ |
| 58 | char password[MAX_PASSWORD_LEN+1]; |
| 59 | }; |
| 60 | |
| 61 | struct raop_conn_s { |
| 62 | raop_t *raop; |
| 63 | raop_rtp_t *raop_rtp; |
| 64 | |
| 65 | unsigned char *local; |
| 66 | int locallen; |
| 67 | |
| 68 | unsigned char *remote; |
| 69 | int remotelen; |
| 70 | |
| 71 | char nonce[MAX_NONCE_LEN+1]; |
| 72 | }; |
| 73 | typedef struct raop_conn_s raop_conn_t; |
| 74 | |
| 75 | static void * |
| 76 | conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remote, int remotelen) |
| 77 | { |
| 78 | raop_conn_t *conn; |
| 79 | |
| 80 | conn = calloc(1, sizeof(raop_conn_t)); |
| 81 | if (!conn) { |
| 82 | return NULL; |
| 83 | } |
| 84 | conn->raop = opaque; |
| 85 | conn->raop_rtp = NULL; |
| 86 | |
| 87 | if (locallen == 4) { |
| 88 | logger_log(conn->raop->logger, LOGGER_INFO, |
| 89 | "Local: %d.%d.%d.%d", |
| 90 | local[0], local[1], local[2], local[3]); |
| 91 | } else if (locallen == 16) { |
| 92 | logger_log(conn->raop->logger, LOGGER_INFO, |
| 93 | "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", |
| 94 | local[0], local[1], local[2], local[3], local[4], local[5], local[6], local[7], |
| 95 | local[8], local[9], local[10], local[11], local[12], local[13], local[14], local[15]); |
| 96 | } |
| 97 | if (remotelen == 4) { |
| 98 | logger_log(conn->raop->logger, LOGGER_INFO, |
| 99 | "Remote: %d.%d.%d.%d", |
| 100 | remote[0], remote[1], remote[2], remote[3]); |
| 101 | } else if (remotelen == 16) { |
| 102 | logger_log(conn->raop->logger, LOGGER_INFO, |
| 103 | "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", |
| 104 | remote[0], remote[1], remote[2], remote[3], remote[4], remote[5], remote[6], remote[7], |
| 105 | remote[8], remote[9], remote[10], remote[11], remote[12], remote[13], remote[14], remote[15]); |
| 106 | } |
| 107 | |
| 108 | conn->local = malloc(locallen); |
| 109 | assert(conn->local); |
| 110 | memcpy(conn->local, local, locallen); |
| 111 | |
| 112 | conn->remote = malloc(remotelen); |
| 113 | assert(conn->remote); |
| 114 | memcpy(conn->remote, remote, remotelen); |
| 115 | |
| 116 | conn->locallen = locallen; |
| 117 | conn->remotelen = remotelen; |
| 118 | |
| 119 | digest_generate_nonce(conn->nonce, sizeof(conn->nonce)); |
| 120 | return conn; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | conn_request(void *ptr, http_request_t *request, http_response_t **response) |
| 125 | { |
| 126 | raop_conn_t *conn = ptr; |
| 127 | raop_t *raop = conn->raop; |
| 128 | |
| 129 | http_response_t *res; |
| 130 | const char *method; |
| 131 | const char *cseq; |
| 132 | const char *challenge; |
| 133 | int require_auth = 0; |
| 134 | |
| 135 | method = http_request_get_method(request); |
| 136 | cseq = http_request_get_header(request, "CSeq"); |
| 137 | if (!method || !cseq) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | res = http_response_init("RTSP/1.0", 200, "OK"); |
| 142 | if (strlen(raop->password)) { |
| 143 | const char *authorization; |
| 144 | |
| 145 | authorization = http_request_get_header(request, "Authorization"); |
| 146 | if (authorization) { |
| 147 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce); |
| 148 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization); |
| 149 | } |
| 150 | if (!digest_is_valid("AppleTV", raop->password, conn->nonce, method, http_request_get_url(request), authorization)) { |
| 151 | char *authstr; |
| 152 | int authstrlen; |
| 153 | |
| 154 | /* Allocate the authenticate string */ |
| 155 | authstrlen = sizeof("Digest realm=\"AppleTV\", nonce=\"\"") + sizeof(conn->nonce) + 1; |
| 156 | authstr = malloc(authstrlen); |
| 157 | |
| 158 | /* Concatenate the authenticate string */ |
| 159 | memset(authstr, 0, authstrlen); |
| 160 | strcat(authstr, "Digest realm=\"AppleTV\", nonce=\""); |
| 161 | strcat(authstr, conn->nonce); |
| 162 | strcat(authstr, "\""); |
| 163 | |
| 164 | /* Construct a new response */ |
| 165 | require_auth = 1; |
| 166 | http_response_destroy(res); |
| 167 | res = http_response_init("RTSP/1.0", 401, "Unauthorized"); |
| 168 | http_response_add_header(res, "WWW-Authenticate", authstr); |
| 169 | free(authstr); |
| 170 | } else { |
| 171 | logger_log(conn->raop->logger, LOGGER_DEBUG, "AUTHENTICATION SUCCESS!"); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | http_response_add_header(res, "CSeq", cseq); |
| 176 | http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog"); |
| 177 | |
| 178 | challenge = http_request_get_header(request, "Apple-Challenge"); |
| 179 | if (!require_auth && challenge) { |
| 180 | char signature[MAX_SIGNATURE_LEN]; |
| 181 | |
| 182 | memset(signature, 0, sizeof(signature)); |
| 183 | rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge, |
| 184 | conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen); |
| 185 | http_response_add_header(res, "Apple-Response", signature); |
| 186 | |
| 187 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge); |
| 188 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature); |
| 189 | } |
| 190 | |
| 191 | if (require_auth) { |
| 192 | /* Do nothing in case of authentication request */ |
| 193 | } else if (!strcmp(method, "OPTIONS")) { |
| 194 | http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER"); |
| 195 | } else if (!strcmp(method, "ANNOUNCE")) { |
| 196 | const char *data; |
| 197 | int datalen; |
| 198 | |
| 199 | unsigned char aeskey[16]; |
| 200 | unsigned char aesiv[16]; |
| 201 | int aeskeylen, aesivlen; |
| 202 | |
| 203 | data = http_request_get_data(request, &datalen); |
| 204 | if (data) { |
| 205 | sdp_t *sdp; |
| 206 | const char *remotestr, *rtpmapstr, *fmtpstr, *aeskeystr, *aesivstr; |
| 207 | |
| 208 | sdp = sdp_init(data, datalen); |
| 209 | remotestr = sdp_get_connection(sdp); |
| 210 | rtpmapstr = sdp_get_rtpmap(sdp); |
| 211 | fmtpstr = sdp_get_fmtp(sdp); |
| 212 | aeskeystr = sdp_get_rsaaeskey(sdp); |
| 213 | aesivstr = sdp_get_aesiv(sdp); |
| 214 | |
| 215 | logger_log(conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr); |
| 216 | logger_log(conn->raop->logger, LOGGER_DEBUG, "rtpmap: %s", rtpmapstr); |
| 217 | logger_log(conn->raop->logger, LOGGER_DEBUG, "fmtp: %s", fmtpstr); |
| 218 | logger_log(conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s", aeskeystr); |
| 219 | logger_log(conn->raop->logger, LOGGER_DEBUG, "aesiv: %s", aesivstr); |
| 220 | |
| 221 | aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr); |
| 222 | aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr); |
| 223 | logger_log(conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen); |
| 224 | logger_log(conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen); |
| 225 | |
| 226 | if (conn->raop_rtp) { |
| 227 | /* This should never happen */ |
| 228 | raop_rtp_destroy(conn->raop_rtp); |
| 229 | conn->raop_rtp = NULL; |
| 230 | } |
| 231 | conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, rtpmapstr, fmtpstr, aeskey, aesiv); |
| 232 | sdp_destroy(sdp); |
| 233 | } |
| 234 | } else if (!strcmp(method, "SETUP")) { |
| 235 | unsigned short remote_cport=0, remote_tport=0; |
| 236 | unsigned short cport=0, tport=0, dport=0; |
| 237 | const char *transport; |
| 238 | char buffer[1024]; |
| 239 | int use_udp; |
| 240 | |
| 241 | transport = http_request_get_header(request, "Transport"); |
| 242 | assert(transport); |
| 243 | |
| 244 | logger_log(conn->raop->logger, LOGGER_INFO, "Transport: %s", transport); |
| 245 | use_udp = strncmp(transport, "RTP/AVP/TCP", 11); |
| 246 | if (use_udp) { |
| 247 | char *original, *current, *tmpstr; |
| 248 | |
| 249 | current = original = strdup(transport); |
| 250 | if (original) { |
| 251 | while ((tmpstr = utils_strsep(¤t, ";")) != NULL) { |
| 252 | unsigned short value; |
| 253 | int ret; |
| 254 | |
| 255 | ret = sscanf(tmpstr, "control_port=%hu", &value); |
| 256 | if (ret == 1) { |
| 257 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value); |
| 258 | remote_cport = value; |
| 259 | } |
| 260 | ret = sscanf(tmpstr, "timing_port=%hu", &value); |
| 261 | if (ret == 1) { |
| 262 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value); |
| 263 | remote_tport = value; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | free(original); |
| 268 | } |
| 269 | raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport); |
| 270 | |
| 271 | memset(buffer, 0, sizeof(buffer)); |
| 272 | if (use_udp) { |
| 273 | snprintf(buffer, sizeof(buffer)-1, |
| 274 | "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu", |
| 275 | tport, cport, dport); |
| 276 | } else { |
| 277 | snprintf(buffer, sizeof(buffer)-1, |
| 278 | "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u", |
| 279 | dport); |
| 280 | } |
| 281 | logger_log(conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer); |
| 282 | http_response_add_header(res, "Transport", buffer); |
| 283 | http_response_add_header(res, "Session", "DEADBEEF"); |
| 284 | } else if (!strcmp(method, "SET_PARAMETER")) { |
| 285 | const char *content_type; |
| 286 | const char *data; |
| 287 | int datalen; |
| 288 | |
| 289 | content_type = http_request_get_header(request, "Content-Type"); |
| 290 | data = http_request_get_data(request, &datalen); |
| 291 | if (!strcmp(content_type, "text/parameters")) { |
| 292 | char *datastr; |
| 293 | datastr = calloc(1, datalen+1); |
| 294 | if (data && datastr && conn->raop_rtp) { |
| 295 | memcpy(datastr, data, datalen); |
| 296 | if (!strncmp(datastr, "volume: ", 8)) { |
| 297 | float vol = 0.0; |
| 298 | sscanf(datastr+8, "%f", &vol); |
| 299 | raop_rtp_set_volume(conn->raop_rtp, vol); |
| 300 | } |
| 301 | } |
| 302 | free(datastr); |
| 303 | } else if (!strcmp(content_type, "image/jpeg")) { |
| 304 | logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen); |
| 305 | raop_rtp_set_coverart(conn->raop_rtp, data, datalen); |
| 306 | } else if (!strcmp(content_type, "application/x-dmap-tagged")) { |
| 307 | logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen); |
| 308 | raop_rtp_set_metadata(conn->raop_rtp, data, datalen); |
| 309 | } |
| 310 | } else if (!strcmp(method, "FLUSH")) { |
| 311 | const char *rtpinfo; |
| 312 | int next_seq = -1; |
| 313 | |
| 314 | rtpinfo = http_request_get_header(request, "RTP-Info"); |
| 315 | if (rtpinfo) { |
| 316 | logger_log(conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo); |
| 317 | if (!strncmp(rtpinfo, "seq=", 4)) { |
| 318 | next_seq = strtol(rtpinfo+4, NULL, 10); |
| 319 | } |
| 320 | } |
| 321 | if (conn->raop_rtp) { |
| 322 | raop_rtp_flush(conn->raop_rtp, next_seq); |
| 323 | } |
| 324 | } else if (!strcmp(method, "TEARDOWN")) { |
| 325 | http_response_add_header(res, "Connection", "close"); |
| 326 | if (conn->raop_rtp) { |
| 327 | /* Destroy our RTP session */ |
| 328 | raop_rtp_stop(conn->raop_rtp); |
| 329 | raop_rtp_destroy(conn->raop_rtp); |
| 330 | conn->raop_rtp = NULL; |
| 331 | } |
| 332 | } |
| 333 | http_response_finish(res, NULL, 0); |
| 334 | |
| 335 | logger_log(conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s", method, http_request_get_url(request)); |
| 336 | *response = res; |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | conn_destroy(void *ptr) |
| 341 | { |
| 342 | raop_conn_t *conn = ptr; |
| 343 | |
| 344 | if (conn->raop_rtp) { |
| 345 | /* This is done in case TEARDOWN was not called */ |
| 346 | raop_rtp_destroy(conn->raop_rtp); |
| 347 | } |
| 348 | free(conn->local); |
| 349 | free(conn->remote); |
| 350 | free(conn); |
| 351 | } |
| 352 | |
| 353 | raop_t * |
| 354 | raop_init(int max_clients, raop_callbacks_t *callbacks, const char *pemkey, int *error) |
| 355 | { |
| 356 | raop_t *raop; |
| 357 | httpd_t *httpd; |
| 358 | rsakey_t *rsakey; |
| 359 | httpd_callbacks_t httpd_cbs; |
| 360 | |
| 361 | assert(callbacks); |
| 362 | assert(max_clients > 0); |
| 363 | assert(max_clients < 100); |
| 364 | assert(pemkey); |
| 365 | |
| 366 | /* Initialize the network */ |
| 367 | if (netutils_init() < 0) { |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | /* Validate the callbacks structure */ |
| 372 | if (!callbacks->audio_init || |
| 373 | !callbacks->audio_process || |
| 374 | !callbacks->audio_destroy) { |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | /* Allocate the raop_t structure */ |
| 379 | raop = calloc(1, sizeof(raop_t)); |
| 380 | if (!raop) { |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | /* Initialize the logger */ |
| 385 | raop->logger = logger_init(); |
| 386 | |
| 387 | /* Set HTTP callbacks to our handlers */ |
| 388 | memset(&httpd_cbs, 0, sizeof(httpd_cbs)); |
| 389 | httpd_cbs.opaque = raop; |
| 390 | httpd_cbs.conn_init = &conn_init; |
| 391 | httpd_cbs.conn_request = &conn_request; |
| 392 | httpd_cbs.conn_destroy = &conn_destroy; |
| 393 | |
| 394 | /* Initialize the http daemon */ |
| 395 | httpd = httpd_init(raop->logger, &httpd_cbs, max_clients); |
| 396 | if (!httpd) { |
| 397 | free(raop); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | /* Copy callbacks structure */ |
| 402 | memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t)); |
| 403 | |
| 404 | /* Initialize RSA key handler */ |
| 405 | rsakey = rsakey_init_pem(pemkey); |
| 406 | if (!rsakey) { |
| 407 | free(httpd); |
| 408 | free(raop); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | raop->httpd = httpd; |
| 413 | raop->rsakey = rsakey; |
| 414 | |
| 415 | return raop; |
| 416 | } |
| 417 | |
| 418 | raop_t * |
| 419 | raop_init_from_keyfile(int max_clients, raop_callbacks_t *callbacks, const char *keyfile, int *error) |
| 420 | { |
| 421 | raop_t *raop; |
| 422 | char *pemstr; |
| 423 | |
| 424 | if (utils_read_file(&pemstr, keyfile) < 0) { |
| 425 | return NULL; |
| 426 | } |
| 427 | raop = raop_init(max_clients, callbacks, pemstr, error); |
| 428 | free(pemstr); |
| 429 | return raop; |
| 430 | } |
| 431 | |
| 432 | void |
| 433 | raop_destroy(raop_t *raop) |
| 434 | { |
| 435 | if (raop) { |
| 436 | raop_stop(raop); |
| 437 | |
| 438 | httpd_destroy(raop->httpd); |
| 439 | rsakey_destroy(raop->rsakey); |
| 440 | logger_destroy(raop->logger); |
| 441 | free(raop); |
| 442 | |
| 443 | /* Cleanup the network */ |
| 444 | netutils_cleanup(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | int |
| 449 | raop_is_running(raop_t *raop) |
| 450 | { |
| 451 | assert(raop); |
| 452 | |
| 453 | return httpd_is_running(raop->httpd); |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | raop_set_log_level(raop_t *raop, int level) |
| 458 | { |
| 459 | assert(raop); |
| 460 | |
| 461 | logger_set_level(raop->logger, level); |
| 462 | } |
| 463 | |
| 464 | void |
| 465 | raop_set_log_callback(raop_t *raop, raop_log_callback_t callback, void *cls) |
| 466 | { |
| 467 | assert(raop); |
| 468 | |
| 469 | logger_set_callback(raop->logger, callback, cls); |
| 470 | } |
| 471 | |
| 472 | int |
| 473 | raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password) |
| 474 | { |
| 475 | assert(raop); |
| 476 | assert(port); |
| 477 | assert(hwaddr); |
| 478 | |
| 479 | /* Validate hardware address */ |
| 480 | if (hwaddrlen > MAX_HWADDR_LEN) { |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | memset(raop->password, 0, sizeof(raop->password)); |
| 485 | if (password) { |
| 486 | /* Validate password */ |
| 487 | if (strlen(password) > MAX_PASSWORD_LEN) { |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | /* Copy password to the raop structure */ |
| 492 | strncpy(raop->password, password, MAX_PASSWORD_LEN); |
| 493 | } |
| 494 | |
| 495 | /* Copy hwaddr to the raop structure */ |
| 496 | memcpy(raop->hwaddr, hwaddr, hwaddrlen); |
| 497 | raop->hwaddrlen = hwaddrlen; |
| 498 | |
| 499 | return httpd_start(raop->httpd, port); |
| 500 | } |
| 501 | |
| 502 | void |
| 503 | raop_stop(raop_t *raop) |
| 504 | { |
| 505 | assert(raop); |
| 506 | |
| 507 | httpd_stop(raop->httpd); |
| 508 | } |
| 509 | |