1 /* ykclient_server_response.h --- Server response parsing and validation.
2  *
3  * Written by Sebastien Martini <seb@dbzteam.org>.
4  * Copyright (c) 2011-2013 Yubico AB
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *    * Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *
14  *    * Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #ifndef YKCLIENT_SERVER_RESPONSE_H
34 #define YKCLIENT_SERVER_RESPONSE_H
35 
36 #include <ykclient_errors.h>
37 
38 /* Example:
39      key:   status
40      value: OK
41 */
42 typedef struct ykclient_parameter_st
43 {
44   char *key;
45   char *value;
46 } ykclient_parameter_t;
47 
48 typedef struct ykclient_parameters_st
49 {
50   ykclient_parameter_t *parameter;
51   struct ykclient_parameters_st *next;
52 } ykclient_parameters_t;
53 
54 typedef struct ykclient_server_response_st
55 {
56   ykclient_parameter_t *signature;
57   ykclient_parameters_t *parameters;
58 } ykclient_server_response_t;
59 
60 
61 /* Returns NULL if it fails. */
62 extern ykclient_server_response_t *ykclient_server_response_init (void);
63 
64 /* Frees allocated data structures. */
65 extern void ykclient_server_response_free (ykclient_server_response_t
66 					   * response);
67 
68 /* Parses server's response and builds a list of parameters and isolates
69    the corresponding signature parameter. Returns 0 if it succeeds. */
70 extern ykclient_rc
71 ykclient_server_response_parse (char *response,
72 				ykclient_server_response_t * serv_response);
73 
74 /* Iterates the parameters buils a HMAC-SHA1 and checks it matches the
75    signature returned by the server. This function returns 0 if the signature
76    is valid, 1 otherwise. */
77 extern int
78 ykclient_server_response_verify_signature (const ykclient_server_response_t
79 					   * serv_response,
80 					   const char *key, int key_length);
81 
82 /* Returns value associated to key or NULL if unmatched. The caller doesn't
83    take ownership of the returned value. */
84 extern char *ykclient_server_response_get (const ykclient_server_response_t *
85 					   serv_response, const char *key);
86 
87 #endif /* YKCLIENT_SERVER_RESPONSE_H */
88