attributes: Move the configuration attributes framework to libcharon
[strongswan.git] / src / libcharon / plugins / unit_tester / tests / test_agent.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <library.h>
17 #include <daemon.h>
18
19 /*******************************************************************************
20 * SSH agent signature creation and verification
21 ******************************************************************************/
22 bool test_agent()
23 {
24 char *path;
25 chunk_t sig, data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08);
26 private_key_t *private;
27 public_key_t *public;
28
29 path = getenv("SSH_AUTH_SOCK");
30 if (!path)
31 {
32 DBG1(DBG_CFG, "ssh-agent not found.");
33 return FALSE;
34 }
35
36 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
37 BUILD_AGENT_SOCKET, path, BUILD_END);
38 if (!private)
39 {
40 return FALSE;
41 }
42 if (!private->sign(private, SIGN_RSA_EMSA_PKCS1_SHA1, data, &sig))
43 {
44 return FALSE;
45 }
46 public = private->get_public_key(private);
47 if (!public)
48 {
49 return FALSE;;
50 }
51 if (!public->verify(public, SIGN_RSA_EMSA_PKCS1_SHA1, data, sig))
52 {
53 return FALSE;
54 }
55 free(sig.ptr);
56 data.ptr[1] = 0x01; /* fake it */
57 if (public->verify(public, SIGN_RSA_EMSA_PKCS1_SHA1, data, sig))
58 {
59 return FALSE;
60 }
61
62 private->destroy(private);
63 public->destroy(public);
64
65 return TRUE;
66 }
67