Factories honor charon IKEv1/IKEv2 protocol support flags
[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_ECDSA_521, AUTH_DSS,
35 "ECDSA-256 signature",
36 "ECDSA-384 signature",
37 "ECDSA-521 signature");
38 ENUM_NEXT(auth_method_names, AUTH_XAUTH_INIT_PSK, AUTH_HYBRID_RESP_RSA, AUTH_ECDSA_521,
39 "XAuthInitPSK",
40 "XAuthRespPSK",
41 "XAuthInitRSA",
42 "XauthRespRSA",
43 "HybridInitRSA",
44 "HybridRespRSA",
45 );
46 ENUM_END(auth_method_names, AUTH_HYBRID_RESP_RSA);
47
48 #ifdef USE_IKEV2
49
50 /**
51 * Described in header.
52 */
53 authenticator_t *authenticator_create_builder(ike_sa_t *ike_sa, auth_cfg_t *cfg,
54 chunk_t received_nonce, chunk_t sent_nonce,
55 chunk_t received_init, chunk_t sent_init,
56 char reserved[3])
57 {
58 switch ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS))
59 {
60 case AUTH_CLASS_ANY:
61 /* defaults to PUBKEY */
62 case AUTH_CLASS_PUBKEY:
63 return (authenticator_t*)pubkey_authenticator_create_builder(ike_sa,
64 received_nonce, sent_init, reserved);
65 case AUTH_CLASS_PSK:
66 return (authenticator_t*)psk_authenticator_create_builder(ike_sa,
67 received_nonce, sent_init, reserved);
68 case AUTH_CLASS_EAP:
69 return (authenticator_t*)eap_authenticator_create_builder(ike_sa,
70 received_nonce, sent_nonce,
71 received_init, sent_init, reserved);
72 default:
73 return NULL;
74 }
75 }
76
77 /**
78 * Described in header.
79 */
80 authenticator_t *authenticator_create_verifier(
81 ike_sa_t *ike_sa, message_t *message,
82 chunk_t received_nonce, chunk_t sent_nonce,
83 chunk_t received_init, chunk_t sent_init,
84 char reserved[3])
85 {
86 auth_payload_t *auth_payload;
87
88 auth_payload = (auth_payload_t*)message->get_payload(message, AUTHENTICATION);
89 if (auth_payload == NULL)
90 {
91 return (authenticator_t*)eap_authenticator_create_verifier(ike_sa,
92 received_nonce, sent_nonce,
93 received_init, sent_init, reserved);
94 }
95 switch (auth_payload->get_auth_method(auth_payload))
96 {
97 case AUTH_RSA:
98 case AUTH_ECDSA_256:
99 case AUTH_ECDSA_384:
100 case AUTH_ECDSA_521:
101 return (authenticator_t*)pubkey_authenticator_create_verifier(ike_sa,
102 sent_nonce, received_init, reserved);
103 case AUTH_PSK:
104 return (authenticator_t*)psk_authenticator_create_verifier(ike_sa,
105 sent_nonce, received_init, reserved);
106 default:
107 return NULL;
108 }
109 }
110
111 #endif /* USE_IKEV2 */
112
113 #ifdef USE_IKEV1
114
115 /**
116 * Described in header.
117 */
118 authenticator_t *authenticator_create_v1(ike_sa_t *ike_sa, bool initiator,
119 auth_method_t auth_method, diffie_hellman_t *dh,
120 chunk_t dh_value, chunk_t sa_payload,
121 chunk_t id_payload)
122 {
123 switch (auth_method)
124 {
125 case AUTH_PSK:
126 case AUTH_XAUTH_INIT_PSK:
127 case AUTH_XAUTH_RESP_PSK:
128 return (authenticator_t*)psk_v1_authenticator_create(ike_sa,
129 initiator, dh, dh_value, sa_payload,
130 id_payload);
131 case AUTH_RSA:
132 case AUTH_XAUTH_INIT_RSA:
133 case AUTH_XAUTH_RESP_RSA:
134 return (authenticator_t*)pubkey_v1_authenticator_create(ike_sa,
135 initiator, dh, dh_value, sa_payload,
136 id_payload);
137 case AUTH_HYBRID_INIT_RSA:
138 case AUTH_HYBRID_RESP_RSA:
139 return (authenticator_t*)hybrid_authenticator_create(ike_sa,
140 initiator, dh, dh_value, sa_payload,
141 id_payload);
142 default:
143 return NULL;
144 }
145 }
146
147 #endif /* USE_IKEV1 */