* Decrypt the string given as per the current config. * * Returns APR_SUCCESS if successful. */
| 342 | * Returns APR_SUCCESS if successful. |
| 343 | */ |
| 344 | static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f, |
| 345 | session_crypto_dir_conf *dconf, const char *in, char **out) |
| 346 | { |
| 347 | apr_status_t res; |
| 348 | apr_crypto_key_t *key = NULL; |
| 349 | apr_size_t ivSize = 0; |
| 350 | apr_crypto_block_t *block = NULL; |
| 351 | unsigned char *decrypted = NULL; |
| 352 | apr_size_t decryptedlen, tlen; |
| 353 | apr_size_t decodedlen; |
| 354 | char *decoded; |
| 355 | apr_size_t blockSize = 0; |
| 356 | apr_crypto_block_key_type_e *cipher; |
| 357 | unsigned char auth[AP_SIPHASH_DSIZE]; |
| 358 | int i = 0; |
| 359 | |
| 360 | /* strip base64 from the string */ |
| 361 | decoded = apr_palloc(r->pool, apr_base64_decode_len(in)); |
| 362 | decodedlen = apr_base64_decode(decoded, in); |
| 363 | decoded[decodedlen] = '\0'; |
| 364 | |
| 365 | /* sanity check - decoded too short? */ |
| 366 | if (decodedlen < (AP_SIPHASH_DSIZE + sizeof(apr_uuid_t))) { |
| 367 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(10005) |
| 368 | "too short to decrypt, aborting"); |
| 369 | return APR_ECRYPT; |
| 370 | } |
| 371 | |
| 372 | res = crypt_init(r, f, &cipher, dconf); |
| 373 | if (res != APR_SUCCESS) { |
| 374 | return res; |
| 375 | } |
| 376 | |
| 377 | res = APR_ECRYPT; /* in case we exhaust all passphrases */ |
| 378 | |
| 379 | /* try each passphrase in turn */ |
| 380 | for (; i < dconf->passphrases->nelts; i++) { |
| 381 | const char *passphrase = APR_ARRAY_IDX(dconf->passphrases, i, char *); |
| 382 | apr_size_t passlen = strlen(passphrase); |
| 383 | apr_size_t len = decodedlen - AP_SIPHASH_DSIZE; |
| 384 | unsigned char *slider = (unsigned char *)decoded + AP_SIPHASH_DSIZE; |
| 385 | |
| 386 | /* Verify authentication of the whole salt+IV+ciphertext by computing |
| 387 | * the MAC and comparing it (timing safe) with the one in the payload. |
| 388 | */ |
| 389 | compute_auth(slider, len, passphrase, passlen, auth); |
| 390 | if (!ap_memeq_timingsafe(auth, decoded, AP_SIPHASH_DSIZE)) { |
| 391 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(10006) |
| 392 | "auth does not match, skipping"); |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | /* encrypt using the first passphrase in the list */ |
| 397 | res = apr_crypto_passphrase(&key, &ivSize, passphrase, passlen, |
| 398 | slider, sizeof(apr_uuid_t), |
| 399 | *cipher, APR_MODE_CBC, 1, 4096, |
| 400 | f, r->pool); |
| 401 | if (APR_STATUS_IS_ENOKEY(res)) { |
no test coverage detected