2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Decode an FKO SPA message after decryption.
10 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.org)
12 * License (GNU Public License):
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 *****************************************************************************
31 #include "fko_common.h"
33 #include "cipher_funcs.h"
37 /* Decode the encoded SPA data.
40 fko_decode_spa_data(fko_ctx_t ctx)
42 char *tbuf, *ndx, *tmp;
45 /* Check for required data.
47 if(ctx->encoded_msg == NULL
48 || strlen(ctx->encoded_msg) < MIN_SPA_ENCODED_MSG_SIZE)
49 return(FKO_ERROR_INVALID_DATA);
51 /* Make sure there are enough fields in the SPA packet
52 * delimited with ':' chars
54 ndx = ctx->encoded_msg;
55 for (i=0; i < MAX_SPA_FIELDS; i++)
57 if ((tmp = strchr(ndx, ':')) == NULL)
64 if (i < MIN_SPA_FIELDS)
65 return(FKO_ERROR_INVALID_DATA);
72 ctx->digest_type = FKO_DIGEST_MD5;
76 ctx->digest_type = FKO_DIGEST_SHA1;
79 case SHA256_B64_LENGTH:
80 ctx->digest_type = FKO_DIGEST_SHA256;
83 case SHA384_B64_LENGTH:
84 ctx->digest_type = FKO_DIGEST_SHA384;
87 case SHA512_B64_LENGTH:
88 ctx->digest_type = FKO_DIGEST_SHA512;
91 default: /* Invalid or unsupported digest */
92 return(FKO_ERROR_INVALID_DIGEST_TYPE);
95 /* Copy the digest into the context and terminate the encoded data
96 * at that point so the original digest is not part of the
99 ctx->digest = strdup(ndx);
100 if(ctx->digest == NULL)
101 return(FKO_ERROR_MEMORY_ALLOCATION);
103 /* Zero out the rest of the encoded_msg bucket...
105 bzero((ndx-1), t_size);
107 /* Make a tmp bucket for processing base64 encoded data and
110 tbuf = malloc(FKO_ENCODE_TMP_BUF_SIZE);
112 return(FKO_ERROR_MEMORY_ALLOCATION);
114 /* Can now verify the digest.
116 switch(ctx->digest_type)
119 md5_base64(tbuf, (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
122 case FKO_DIGEST_SHA1:
123 sha1_base64(tbuf, (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
126 case FKO_DIGEST_SHA256:
127 sha256_base64(tbuf, (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
130 case FKO_DIGEST_SHA384:
131 sha384_base64(tbuf, (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
134 case FKO_DIGEST_SHA512:
135 sha512_base64(tbuf, (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
140 /* We give up here if the computed digest does not match the
141 * digest in the message data.
143 if(strncmp(ctx->digest, tbuf, t_size))
146 return(FKO_ERROR_DIGEST_VERIFICATION_FAILED);
149 /* Now we will work through the encoded data and extract (and base64-
150 * decode where necessary), the SPA data fields and populate the context.
152 ndx = ctx->encoded_msg;
154 /* The rand val data */
155 if((t_size = strcspn(ndx, ":")) < FKO_RAND_VAL_SIZE)
158 return(FKO_ERROR_INVALID_DATA);
161 ctx->rand_val = calloc(1, FKO_RAND_VAL_SIZE+1);
162 if(ctx->rand_val == NULL)
165 return(FKO_ERROR_MEMORY_ALLOCATION);
167 ctx->rand_val = strncpy(ctx->rand_val, ndx, FKO_RAND_VAL_SIZE);
169 /* Jump to the next field (username). We need to use the temp buffer
170 * for the base64 decode step.
173 if((t_size = strcspn(ndx, ":")) < 1)
176 return(FKO_ERROR_INVALID_DATA);
179 if (t_size > MAX_SPA_USERNAME_SIZE)
182 return(FKO_ERROR_INVALID_DATA);
185 strlcpy(tbuf, ndx, t_size+1);
187 ctx->username = malloc(t_size+1); /* Yes, more than we need */
188 if(ctx->username == NULL)
191 return(FKO_ERROR_MEMORY_ALLOCATION);
194 b64_decode(tbuf, (unsigned char*)ctx->username);
196 /* Extract the timestamp value.
199 if((t_size = strcspn(ndx, ":")) < 1)
202 return(FKO_ERROR_INVALID_DATA);
205 if (t_size > MAX_SPA_TIMESTAMP_SIZE)
208 return(FKO_ERROR_INVALID_DATA);
211 strlcpy(tbuf, ndx, t_size+1);
213 ctx->timestamp = (unsigned int)atoi(tbuf);
215 /* Extract the version string.
218 if((t_size = strcspn(ndx, ":")) < 1)
221 return(FKO_ERROR_INVALID_DATA);
224 if (t_size > MAX_SPA_VERSION_SIZE)
227 return(FKO_ERROR_INVALID_DATA);
230 ctx->version = malloc(t_size+1);
231 if(ctx->version == NULL)
234 return(FKO_ERROR_MEMORY_ALLOCATION);
237 strlcpy(ctx->version, ndx, t_size+1);
239 /* Extract the message type value.
242 if((t_size = strcspn(ndx, ":")) < 1)
245 return(FKO_ERROR_INVALID_DATA);
248 if (t_size > MAX_SPA_MESSAGE_TYPE_SIZE)
251 return(FKO_ERROR_INVALID_DATA);
254 strlcpy(tbuf, ndx, t_size+1);
256 ctx->message_type = (unsigned int)atoi(tbuf);
258 /* Extract the SPA message string.
261 if((t_size = strcspn(ndx, ":")) < 1)
264 return(FKO_ERROR_INVALID_DATA);
267 if (t_size > MAX_SPA_MESSAGE_SIZE)
270 return(FKO_ERROR_INVALID_DATA);
273 strlcpy(tbuf, ndx, t_size+1);
275 ctx->message = malloc(t_size+1); /* Yes, more than we need */
276 if(ctx->message == NULL)
279 return(FKO_ERROR_MEMORY_ALLOCATION);
282 b64_decode(tbuf, (unsigned char*)ctx->message);
284 /* Extract nat_access string if the message_type indicates so.
286 if( ctx->message_type == FKO_NAT_ACCESS_MSG
287 || ctx->message_type == FKO_LOCAL_NAT_ACCESS_MSG
288 || ctx->message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG
289 || ctx->message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
292 if((t_size = strcspn(ndx, ":")) < 1)
295 return(FKO_ERROR_INVALID_DATA);
298 if (t_size > MAX_SPA_MESSAGE_SIZE)
301 return(FKO_ERROR_INVALID_DATA);
304 strlcpy(tbuf, ndx, t_size+1);
306 ctx->nat_access = malloc(t_size+1); /* Yes, more than we need */
307 if(ctx->nat_access == NULL)
310 return(FKO_ERROR_MEMORY_ALLOCATION);
313 b64_decode(tbuf, (unsigned char*)ctx->nat_access);
316 /* Now look for a server_auth string.
319 if((t_size = strlen(ndx)) > 0)
321 if (t_size > MAX_SPA_MESSAGE_SIZE)
324 return(FKO_ERROR_INVALID_DATA);
327 /* There is data, but what is it?
328 * If the message_type does not have a timeout, assume it is a
331 if( ctx->message_type != FKO_CLIENT_TIMEOUT_ACCESS_MSG
332 && ctx->message_type != FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG
333 && ctx->message_type != FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
335 strlcpy(tbuf, ndx, t_size+1);
337 ctx->server_auth = malloc(t_size+1); /* Yes, more than we need */
338 if(ctx->server_auth == NULL)
341 return(FKO_ERROR_MEMORY_ALLOCATION);
344 b64_decode(tbuf, (unsigned char*)ctx->server_auth);
346 /* At this point we should be done.
350 /* Call the context initialized.
352 ctx->initval = FKO_CTX_INITIALIZED;
353 FKO_SET_CTX_INITIALIZED(ctx);
358 /* If we are here then we may still have a server_auth string,
359 * or a timeout, or both. So we look for a ':' delimiter. If
360 * it is there we have both, if not we check the message_type
365 t_size = strcspn(ndx, ":");
367 if (t_size > MAX_SPA_MESSAGE_SIZE)
370 return(FKO_ERROR_INVALID_DATA);
373 /* Looks like we have both, so assume this is the
375 strlcpy(tbuf, ndx, t_size+1);
377 ctx->server_auth = malloc(t_size+1); /* Yes, more than we need */
378 if(ctx->server_auth == NULL)
381 return(FKO_ERROR_MEMORY_ALLOCATION);
384 b64_decode(tbuf, (unsigned char*)ctx->server_auth);
389 /* Now we look for a timeout value if one is supposed to be there.
391 if( ctx->message_type == FKO_CLIENT_TIMEOUT_ACCESS_MSG
392 || ctx->message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG
393 || ctx->message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
395 if((t_size = strlen(ndx)) < 1)
398 return(FKO_ERROR_INVALID_DATA);
400 if (t_size > MAX_SPA_MESSAGE_SIZE)
403 return(FKO_ERROR_INVALID_DATA);
406 /* Should be a number only.
408 if(strspn(ndx, "0123456789") != t_size)
411 return(FKO_ERROR_INVALID_DATA);
414 ctx->client_timeout = (unsigned int)atoi(ndx);
418 /* Done with the tmp buffer.
422 /* Call the context initialized.
424 ctx->initval = FKO_CTX_INITIALIZED;
425 FKO_SET_CTX_INITIALIZED(ctx);