Blame src/lib/crypto/crypto_tests/t_fork.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* lib/crypto/crypto_tests/t_fork.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2010 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Test basic libk5crypto behavior across forks.  This is primarily interesting
Packit fd8b60
 * for back ends with PKCS11-based constraints.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
#include <unistd.h>
Packit fd8b60
#include <sys/types.h>
Packit fd8b60
#include <sys/wait.h>
Packit fd8b60
Packit fd8b60
static krb5_context ctx = NULL;
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
t(krb5_error_code code)
Packit fd8b60
{
Packit fd8b60
    if (code != 0) {
Packit fd8b60
        fprintf(stderr, "Failure: %s\n", krb5_get_error_message(ctx, code));
Packit fd8b60
        exit(1);
Packit fd8b60
    }
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
prepare_enc_data(krb5_key key, size_t in_len, krb5_enc_data *enc_data)
Packit fd8b60
{
Packit fd8b60
    size_t out_len;
Packit fd8b60
Packit fd8b60
    t(krb5_c_encrypt_length(ctx, key->keyblock.enctype, in_len, &out_len));
Packit fd8b60
    t(alloc_data(&enc_data->ciphertext, out_len));
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main()
Packit fd8b60
{
Packit fd8b60
    krb5_keyblock kb_aes, kb_rc4;
Packit fd8b60
    krb5_key key_aes, key_rc4;
Packit fd8b60
    krb5_data state_rc4, plain = string2data("plain"), decrypted;
Packit fd8b60
    krb5_enc_data out_aes, out_rc4;
Packit fd8b60
    pid_t pid, wpid;
Packit fd8b60
    int status;
Packit fd8b60
Packit fd8b60
    /* Seed the PRNG instead of creating a context, so we don't need
Packit fd8b60
     * krb5.conf. */
Packit fd8b60
    t(krb5_c_random_seed(ctx, &plain));
Packit fd8b60
Packit fd8b60
    /* Create AES and RC4 ciphertexts with random keys.  Use cipher state for
Packit fd8b60
     * RC4. */
Packit fd8b60
    t(krb5_c_make_random_key(ctx, ENCTYPE_AES256_CTS_HMAC_SHA1_96, &kb_aes));
Packit fd8b60
    t(krb5_c_make_random_key(ctx, ENCTYPE_ARCFOUR_HMAC, &kb_rc4));
Packit fd8b60
    t(krb5_k_create_key(ctx, &kb_aes, &key_aes));
Packit fd8b60
    t(krb5_k_create_key(ctx, &kb_rc4, &key_rc4));
Packit fd8b60
    prepare_enc_data(key_aes, plain.length, &out_aes);
Packit fd8b60
    prepare_enc_data(key_aes, plain.length, &out_rc4);
Packit fd8b60
    t(krb5_c_init_state(ctx, &kb_rc4, 0, &state_rc4));
Packit fd8b60
    t(krb5_k_encrypt(ctx, key_aes, 0, NULL, &plain, &out_aes));
Packit fd8b60
    t(krb5_k_encrypt(ctx, key_rc4, 0, &state_rc4, &plain, &out_rc4));
Packit fd8b60
Packit fd8b60
    /* Fork; continue in both parent and child. */
Packit fd8b60
    pid = fork();
Packit fd8b60
    assert(pid >= 0);
Packit fd8b60
Packit fd8b60
    /* Decrypt the AES message with both key and keyblock. */
Packit fd8b60
    t(alloc_data(&decrypted, plain.length));
Packit fd8b60
    t(krb5_k_decrypt(ctx, key_aes, 0, NULL, &out_aes, &decrypted));
Packit fd8b60
    assert(data_eq(plain, decrypted));
Packit fd8b60
    t(krb5_c_decrypt(ctx, &kb_aes, 0, NULL, &out_aes, &decrypted));
Packit fd8b60
    assert(data_eq(plain, decrypted));
Packit fd8b60
Packit fd8b60
    /* Encrypt another RC4 message. */
Packit fd8b60
    t(krb5_k_encrypt(ctx, key_rc4, 0, &state_rc4, &plain, &out_rc4));
Packit fd8b60
    t(krb5_c_free_state(ctx, &kb_rc4, &state_rc4));
Packit fd8b60
Packit fd8b60
    krb5_free_keyblock_contents(ctx, &kb_aes);
Packit fd8b60
    krb5_free_keyblock_contents(ctx, &kb_rc4);
Packit fd8b60
    krb5_k_free_key(ctx, key_aes);
Packit fd8b60
    krb5_k_free_key(ctx, key_rc4);
Packit fd8b60
    krb5_free_data_contents(ctx, &out_aes.ciphertext);
Packit fd8b60
    krb5_free_data_contents(ctx, &out_rc4.ciphertext);
Packit fd8b60
    krb5_free_data_contents(ctx, &decrypted);
Packit fd8b60
Packit fd8b60
    /* If we're the parent, make sure the child succeeded. */
Packit fd8b60
    if (pid != 0) {
Packit fd8b60
        wpid = wait(&status);
Packit fd8b60
        assert(wpid == pid);
Packit fd8b60
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Packit fd8b60
            fprintf(stderr, "Child failed with status %d\n", status);
Packit fd8b60
            return 1;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return 0;
Packit fd8b60
}