payload: Use common prefixes for all payload type identifiers
[strongswan.git] / src / libcharon / sa / authenticator.c
1 /*
2 * Copyright (C) 2006-2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <string.h>
18
19 #include "authenticator.h"
20
21 #include <sa/ikev2/authenticators/pubkey_authenticator.h>
22 #include <sa/ikev2/authenticators/psk_authenticator.h>
23 #include <sa/ikev2/authenticators/eap_authenticator.h>
24 #include <sa/ikev1/authenticators/psk_v1_authenticator.h>
25 #include <sa/ikev1/authenticators/pubkey_v1_authenticator.h>
26 #include <sa/ikev1/authenticators/hybrid_authenticator.h>
27 #include <encoding/payloads/auth_payload.h>
28
29
30 ENUM_BEGIN(auth_method_names, AUTH_RSA, AUTH_DSS,
31 "RSA signature",
32 "pre-shared key",
33 "DSS signature");
34 ENUM_NEXT(auth_method_names, AUTH_ECDSA_256, AUTH_GSPM, AUTH_DSS,
35 "ECDSA-256 signature",
36 "ECDSA-384 signature",
37 "ECDSA-521 signature",
38 "secure password method");
39 ENUM_NEXT(auth_method_names, AUTH_XAUTH_INIT_PSK, AUTH_HYBRID_RESP_RSA, AUTH_GSPM,
40 "XAuthInitPSK",
41 "XAuthRespPSK",
42 "XAuthInitRSA",
43 "XauthRespRSA",
44 "HybridInitRSA",
45 "HybridRespRSA",
46 );
47 ENUM_END(auth_method_names, AUTH_HYBRID_RESP_RSA);
48
49 #ifdef USE_IKEV2
50
51 /**
52 * Described in header.
53 */
54 authenticator_t *authenticator_create_builder(ike_sa_t *ike_sa, auth_cfg_t *cfg,
55 chunk_t received_nonce, chunk_t sent_nonce,
56 chunk_t received_init, chunk_t sent_init,
57 char reserved[3])
58 {
59 switch ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS))
60 {
61 case AUTH_CLASS_ANY:
62 /* defaults to PUBKEY */
63 case AUTH_CLASS_PUBKEY:
64 return (authenticator_t*)pubkey_authenticator_create_builder(ike_sa,
65 received_nonce, sent_init, reserved);
66 case AUTH_CLASS_PSK:
67 return (authenticator_t*)psk_authenticator_create_builder(ike_sa,
68 received_nonce, sent_init, reserved);
69 case AUTH_CLASS_EAP:
70 return (authenticator_t*)eap_authenticator_create_builder(ike_sa,
71 received_nonce, sent_nonce,
72 received_init, sent_init, reserved);
73 default:
74 return NULL;
75 }
76 }
77
78 /**
79 * Described in header.
80 */
81 authenticator_t *authenticator_create_verifier(
82 ike_sa_t *ike_sa, message_t *message,
83 chunk_t received_nonce, chunk_t sent_nonce,
84 chunk_t received_init, chunk_t sent_init,
85 char reserved[3])
86 {
87 auth_payload_t *auth_payload;
88
89 auth_payload = (auth_payload_t*)message->get_payload(message, PLV2_AUTH);
90 if (auth_payload == NULL)
91 {
92 return (authenticator_t*)eap_authenticator_create_verifier(ike_sa,
93 received_nonce, sent_nonce,
94 received_init, sent_init, reserved);
95 }
96 switch (auth_payload->get_auth_method(auth_payload))
97 {
98 case AUTH_RSA:
99 case AUTH_ECDSA_256:
100 case AUTH_ECDSA_384:
101 case AUTH_ECDSA_521:
102 return (authenticator_t*)pubkey_authenticator_create_verifier(ike_sa,
103 sent_nonce, received_init, reserved);
104 case AUTH_PSK:
105 return (authenticator_t*)psk_authenticator_create_verifier(ike_sa,
106 sent_nonce, received_init, reserved);
107 default:
108 return NULL;
109 }
110 }
111
112 #endif /* USE_IKEV2 */
113
114 #ifdef USE_IKEV1
115
116 /**
117 * Described in header.
118 */
119 authenticator_t *authenticator_create_v1(ike_sa_t *ike_sa, bool initiator,
120 auth_method_t auth_method, diffie_hellman_t *dh,
121 chunk_t dh_value, chunk_t sa_payload,
122 chunk_t id_payload)
123 {
124 switch (auth_method)
125 {
126 case AUTH_PSK:
127 case AUTH_XAUTH_INIT_PSK:
128 case AUTH_XAUTH_RESP_PSK:
129 return (authenticator_t*)psk_v1_authenticator_create(ike_sa,
130 initiator, dh, dh_value, sa_payload,
131 id_payload, FALSE);
132 case AUTH_RSA:
133 case AUTH_XAUTH_INIT_RSA:
134 case AUTH_XAUTH_RESP_RSA:
135 return (authenticator_t*)pubkey_v1_authenticator_create(ike_sa,
136 initiator, dh, dh_value, sa_payload,
137 id_payload, KEY_RSA);
138 case AUTH_ECDSA_256:
139 case AUTH_ECDSA_384:
140 case AUTH_ECDSA_521:
141 return (authenticator_t*)pubkey_v1_authenticator_create(ike_sa,
142 initiator, dh, dh_value, sa_payload,
143 id_payload, KEY_ECDSA);
144 case AUTH_HYBRID_INIT_RSA:
145 case AUTH_HYBRID_RESP_RSA:
146 return (authenticator_t*)hybrid_authenticator_create(ike_sa,
147 initiator, dh, dh_value, sa_payload,
148 id_payload);
149 default:
150 return NULL;
151 }
152 }
153
154 #endif /* USE_IKEV1 */