1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2014 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  *    if any, must include the following acknowledgment:
22  *       "This product includes software developed by the
23  *        Kannel Group (http://www.kannel.org/)."
24  *    Alternately, this acknowledgment may appear in the software itself,
25  *    if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  *    endorse or promote products derived from this software without
29  *    prior written permission. For written permission, please
30  *    contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  *    nor may "Kannel" appear in their name, without prior written
34  *    permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group.  For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * wtls_support.c: pack and unpack WTLS packets
59  *
60  * Support functions for packing and unpacking PDUs
61  *
62  * Nikos Balkanas, Inaccess Networks (2009)
63  *
64  */
65 
66 #include "gwlib/gwlib.h"
67 
68 #ifdef HAVE_WTLS_OPENSSL
69 
70 #include "wtls_pdu.h"
71 #include "wtls_pdusupport.h"
72 #include "wtls_statesupport.h"
73 
74 extern PublicKeyAlgorithm public_key_algo;
75 extern SignatureAlgorithm signature_algo;
76 
77 /* Function prototypes */
78 
79 void destroy_octstr(Octstr * data);
80 void destroy_octstr16(Octstr * data);
81 void destroy_octstr_fixed(Octstr * data);
82 void destroy_dhparams(DHParameters * dhparams);
83 void destroy_ecparams(ECParameters * ecparams);
84 void destroy_public_key(PublicKey * key);
85 void destroy_rsa_secret(RSASecret * secret);
86 void destroy_key_exchange_id(KeyExchangeId * keyexid);
87 void destroy_signature(Signature * sig);
88 void dump_void16(char *dbg, int level, int i);
89 
90 /*****************************************************************
91  * PACK functions
92  */
93 
pack_int16(Octstr * data,long charpos,int i)94 int pack_int16(Octstr * data, long charpos, int i)
95 {
96 	octstr_append_char(data, (i & 0xFF00) >> 8);
97 	charpos += 1;
98 	octstr_append_char(data, i & 0x00FF);
99 	charpos += 1;
100 	return charpos;
101 }
102 
pack_int32(Octstr * data,long charpos,long i)103 int pack_int32(Octstr * data, long charpos, long i)
104 {
105 	charpos = pack_int16(data, charpos, (i & 0xFFFF0000) >> 16);
106 	charpos = pack_int16(data, charpos, i & 0xFFFF);
107 	return charpos;
108 }
109 
pack_octstr(Octstr * data,long charpos,Octstr * opaque)110 int pack_octstr(Octstr * data, long charpos, Octstr * opaque)
111 {
112 	octstr_append_char(data, octstr_len(opaque));
113 	charpos += 1;
114 	octstr_insert(data, opaque, octstr_len(data));
115 	charpos += octstr_len(opaque);
116 	return charpos;
117 }
118 
pack_octstr16(Octstr * data,long charpos,Octstr * opaque)119 int pack_octstr16(Octstr * data, long charpos, Octstr * opaque)
120 {
121 	charpos += pack_int16(data, charpos, octstr_len(opaque));
122 	octstr_insert(data, opaque, octstr_len(data));
123 	charpos += octstr_len(opaque);
124 	return charpos;
125 }
126 
pack_octstr_fixed(Octstr * data,long charpos,Octstr * opaque)127 int pack_octstr_fixed(Octstr * data, long charpos, Octstr * opaque)
128 {
129 	octstr_insert(data, opaque, octstr_len(data));
130 	charpos += octstr_len(opaque);
131 	return charpos;
132 }
133 
pack_random(Octstr * data,long charpos,Random * random)134 int pack_random(Octstr * data, long charpos, Random * random)
135 {
136 	charpos = pack_int32(data, charpos, random->gmt_unix_time);
137 	charpos = pack_octstr_fixed(data, charpos, random->random_bytes);
138 	return charpos;
139 }
140 
pack_dhparams(Octstr * data,long charpos,DHParameters * dhparams)141 int pack_dhparams(Octstr * data, long charpos, DHParameters * dhparams)
142 {
143 	octstr_append_char(data, dhparams->dh_e);
144 	charpos += 1;
145 	charpos = pack_octstr16(data, charpos, dhparams->dh_p);
146 	charpos = pack_octstr16(data, charpos, dhparams->dh_g);
147 	return charpos;
148 }
149 
pack_ecparams(Octstr * data,long charpos,ECParameters * ecparams)150 int pack_ecparams(Octstr * data, long charpos, ECParameters * ecparams)
151 {
152 	/* field */
153 	octstr_append_char(data, ecparams->field);
154 	charpos += 1;
155 	switch (ecparams->field) {
156 	case ec_prime_p:
157 		charpos = pack_octstr(data, charpos, ecparams->prime_p);
158 		break;
159 	case ec_characteristic_two:
160 		/* m (16 bits) */
161 		charpos = pack_int16(data, charpos, ecparams->m);
162 		/* basis */
163 		octstr_append_char(data, ecparams->basis);
164 		charpos += 1;
165 		switch (ecparams->basis) {
166 		case ec_basis_onb:
167 			break;
168 		case ec_basis_trinomial:
169 			charpos = pack_int16(data, charpos, ecparams->k);
170 			break;
171 		case ec_basis_pentanomial:
172 			charpos = pack_int16(data, charpos, ecparams->k1);
173 			charpos = pack_int16(data, charpos, ecparams->k2);
174 			charpos = pack_int16(data, charpos, ecparams->k3);
175 			break;
176 		case ec_basis_polynomial:
177          charpos =
178              pack_octstr(data, charpos, ecparams->irreducible);
179 			break;
180 		}
181 		break;
182 	}
183 	/* pack the ECCurve */
184 	charpos = pack_octstr(data, charpos, ecparams->curve->a);
185 	charpos = pack_octstr(data, charpos, ecparams->curve->b);
186 	charpos = pack_octstr(data, charpos, ecparams->curve->seed);
187 	/* pack the ECPoint */
188 	charpos = pack_octstr(data, charpos, ecparams->base->point);
189 	/* order and cofactor */
190 	charpos = pack_octstr(data, charpos, ecparams->order);
191 	charpos = pack_octstr(data, charpos, ecparams->cofactor);
192 
193 	return charpos;
194 }
195 
pack_param_spec(Octstr * data,long charpos,ParameterSpecifier * pspec)196 int pack_param_spec(Octstr * data, long charpos, ParameterSpecifier * pspec)
197 {
198    if (pspec == NULL) {
199 	octstr_append_char(data, 0);
200 	charpos += 1;
201         return charpos;
202         }
203 
204         /* index */
205 	octstr_append_char(data, pspec->param_index);
206 	charpos += 1;
207 	/* ParameterSet struct */
208 	octstr_append_char(data, pspec->param_set->length);
209 	charpos += 1;
210 	switch (public_key_algo) {
211 	case diffie_hellman_pubkey:
212 		pack_dhparams(data, charpos, pspec->param_set->dhparams);
213 		break;
214 	case elliptic_curve_pubkey:
215 		pack_ecparams(data, charpos, pspec->param_set->ecparams);
216 		break;
217    default:
218       break;
219 	}
220 	return charpos;
221 }
222 
pack_public_key(Octstr * data,long charpos,PublicKey * key,PublicKeyType key_type)223 int pack_public_key(Octstr * data, long charpos, PublicKey * key,
224           PublicKeyType key_type)
225 {
226 	switch (key_type) {
227 	case ecdh_key:
228 		charpos = pack_octstr(data, charpos, key->ecdh_pubkey->point);
229 		break;
230 	case ecdsa_key:
231 		charpos = pack_octstr(data, charpos, key->ecdsa_pubkey->point);
232 		break;
233 	case rsa_key:
234 		charpos = pack_rsa_pubkey(data, charpos, key->rsa_pubkey);
235 		break;
236 	}
237 	return charpos;
238 }
239 
pack_rsa_pubkey(Octstr * data,long charpos,RSAPublicKey * key)240 int pack_rsa_pubkey(Octstr * data, long charpos, RSAPublicKey * key)
241 {
242 	charpos = pack_octstr16(data, charpos, key->rsa_exponent);
243 	charpos = pack_octstr16(data, charpos, key->rsa_modulus);
244 	return charpos;
245 }
246 
pack_ec_pubkey(Octstr * data,long charpos,ECPublicKey * key)247 int pack_ec_pubkey(Octstr * data, long charpos, ECPublicKey * key)
248 {
249 	charpos = pack_octstr(data, charpos, key->point);
250 	return charpos;
251 }
252 
pack_dh_pubkey(Octstr * data,long charpos,DHPublicKey * key)253 int pack_dh_pubkey(Octstr * data, long charpos, DHPublicKey * key)
254 {
255 	charpos = pack_octstr16(data, charpos, key->dh_Y);
256 	return charpos;
257 }
258 
pack_rsa_secret(Octstr * data,long charpos,RSASecret * secret)259 int pack_rsa_secret(Octstr * data, long charpos, RSASecret * secret)
260 {
261 	octstr_append_char(data, secret->client_version);
262 	charpos += 1;
263 	charpos = pack_array(data, charpos, secret->random);
264 	return charpos;
265 }
266 
pack_rsa_encrypted_secret(Octstr * data,long charpos,RSAEncryptedSecret * secret)267 int pack_rsa_encrypted_secret(Octstr * data, long charpos,
268     RSAEncryptedSecret * secret)
269 {
270 	charpos = pack_octstr16(data, charpos, secret->encrypted_secret);
271 	return charpos;
272 }
273 
pack_key_exchange_id(Octstr * data,long charpos,KeyExchangeId * keyexid)274 int pack_key_exchange_id(Octstr * data, long charpos, KeyExchangeId * keyexid)
275 {
276 	octstr_set_char(data, charpos, keyexid->key_exchange_suite);
277 	charpos += 1;
278 	charpos = pack_param_spec(data, charpos, keyexid->param_specif);
279 	charpos = pack_identifier(data, charpos, keyexid->identifier);
280 	return charpos;
281 }
282 
pack_array(Octstr * data,long charpos,List * array)283 int pack_array(Octstr * data, long charpos, List * array)
284 {
285 	int i;
286 	long pos = 0;
287 	Octstr *buffer;
288 
289 	/* we need to know the length in bytes of the list
290 	   so we pack everything in a buffer for now. */
291 	buffer = octstr_create("");
292 
293 	/* pack each entry in the buffer */
294    for (i = 0; i < gwlist_len(array); i++) {
295 		pos = pack_octstr(buffer, pos, (Octstr *) gwlist_get(array, i));
296 	}
297 
298 	/* now we know the size of the list */
299 	charpos = pack_int16(data, charpos, pos);
300 
301 	/* append the buffer */
302 	charpos = pack_octstr_fixed(data, charpos, buffer);
303 
304 	return charpos;
305 }
306 
pack_key_list(Octstr * data,long charpos,List * key_list)307 int pack_key_list(Octstr * data, long charpos, List * key_list)
308 {
309 	int i;
310 	long pos = 0;
311 	Octstr *buffer;
312 	KeyExchangeId *keyexid;
313 
314 	/* we need to know the length in bytes of the list
315 	   so we pack everything in a buffer for now. */
316 	buffer = octstr_create("");
317 
318 	/* pack the KeyExchangeIds */
319    for (i = 0; i < gwlist_len(key_list); i++) {
320 		keyexid = (KeyExchangeId *) gwlist_get(key_list, i);
321 
322 		pos = pack_key_exchange_id(buffer, pos, keyexid);
323 	}
324 
325 	/* now we know the size of the list */
326 	charpos = pack_int16(data, charpos, pos);
327 
328 	/* append the buffer */
329 	charpos = pack_octstr_fixed(data, charpos, buffer);
330 
331 	return charpos;
332 }
333 
pack_ciphersuite_list(Octstr * data,long charpos,List * ciphersuites)334 int pack_ciphersuite_list(Octstr * data, long charpos, List * ciphersuites)
335 {
336 	int i;
337 	CipherSuite *cs;
338 
339 	/* vector starts with its length
340 	   Each element uses 2 bytes */
341    octstr_set_char(data, charpos, gwlist_len(ciphersuites) * 2);
342 	charpos += 1;
343 
344 	/* pack the CipherSuites */
345    for (i = 0; i < gwlist_len(ciphersuites); i++) {
346 		cs = (CipherSuite *) gwlist_get(ciphersuites, i);
347 		octstr_set_char(data, charpos, cs->bulk_cipher_algo);
348 		charpos += 1;
349 		octstr_set_char(data, charpos, cs->mac_algo);
350 		charpos += 1;
351 	}
352 
353 	return charpos;
354 }
355 
pack_compression_method_list(Octstr * data,long charpos,List * compmethod_list)356 int pack_compression_method_list(Octstr * data, long charpos,
357              List * compmethod_list)
358 {
359 	int i;
360 
361 	/* vector starts with its length */
362 	octstr_set_char(data, charpos, gwlist_len(compmethod_list));
363 	charpos += 1;
364 
365 	/* pack the CompressionMethods */
366    for (i = 0; i < gwlist_len(compmethod_list); i++) {
367 		octstr_set_char(data, charpos,
368             (CompressionMethod) gwlist_get(compmethod_list,
369                             i));
370 		charpos += 1;
371 	}
372 
373 	return charpos;
374 }
375 
pack_identifier(Octstr * data,long charpos,Identifier * ident)376 int pack_identifier(Octstr * data, long charpos, Identifier * ident)
377 {
378 	switch (ident->id_type) {
379 	case text:
380 		octstr_set_char(data, charpos, ident->charset);
381 		charpos += 1;
382 		charpos = pack_octstr(data, charpos, ident->name);
383 		break;
384 	case binary:
385 		charpos = pack_octstr(data, charpos, ident->identifier);
386 		break;
387 	case key_hash_sha:
388 		charpos = pack_octstr(data, charpos, ident->key_hash);
389 		break;
390 	case x509_name:
391 		charpos = pack_octstr(data, charpos, ident->distinguished_name);
392 		break;
393    default:
394       break;
395 	}
396 	return charpos;
397 }
398 
pack_signature(Octstr * data,long charpos,Signature * sig)399 int pack_signature(Octstr * data, long charpos, Signature * sig)
400 {
401 	switch (signature_algo) {
402 	case ecdsa_sha:
403 	case rsa_sha:
404 		charpos = pack_array(data, charpos, sig->sha_hash);
405 		break;
406    default:
407       break;
408 	}
409 	return charpos;
410 }
411 
pack_wtls_certificate(Octstr * data,long charpos,WTLSCertificate * cert)412 int pack_wtls_certificate(Octstr * data, long charpos, WTLSCertificate * cert)
413 {
414 	/* === pack ToBeSignedCertificate === */
415 	/* version */
416    octstr_set_char(data, charpos,
417          cert->tobesigned_cert->certificate_version);
418 	charpos += 1;
419 	/* sig algo */
420 	octstr_set_char(data, charpos, cert->tobesigned_cert->signature_algo);
421 	charpos += 1;
422 	/* identifier */
423 	octstr_set_char(data, charpos, cert->tobesigned_cert->issuer->id_type);
424 	charpos += 1;
425 	/* issuer Identifier */
426 	charpos = pack_identifier(data, charpos, cert->tobesigned_cert->issuer);
427 	/* validity periods */
428    charpos =
429        pack_int32(data, charpos, cert->tobesigned_cert->valid_not_before);
430    charpos =
431        pack_int32(data, charpos, cert->tobesigned_cert->valid_not_after);
432 	/* subject Identifier */
433    charpos =
434        pack_identifier(data, charpos, cert->tobesigned_cert->subject);
435 	/* public_key_type */
436 	octstr_set_char(data, charpos, cert->tobesigned_cert->pubkey_type);
437 	charpos += 1;
438 	/* parameter specifier */
439    charpos =
440        pack_param_spec(data, charpos, cert->tobesigned_cert->param_spec);
441 	/* public key */
442 	charpos = pack_public_key(data, charpos, cert->tobesigned_cert->pubkey,
443 					cert->tobesigned_cert->pubkey_type);
444 
445 	/* === pack Signature === */
446 	charpos = pack_signature(data, charpos, cert->signature);
447 	return charpos;
448 }
449 
450 /*****************************************************************
451  * UNPACK functions
452  */
453 
unpack_int16(Octstr * data,long * charpos)454 int unpack_int16(Octstr * data, long *charpos)
455 {
456 	int n;
457 
458 	n =  octstr_get_char(data, *charpos) << 8;
459 	*charpos += 1;
460 	n += octstr_get_char(data, *charpos);
461 	*charpos += 1;
462 	return n;
463 }
464 
unpack_int32(Octstr * data,long * charpos)465 long unpack_int32(Octstr * data, long *charpos)
466 {
467 	int n;
468 
469 	n =  octstr_get_char(data, *charpos);
470 	n = n << 8;
471 	*charpos += 1;
472 	n += octstr_get_char(data, *charpos);
473 	n = n << 8;
474 	*charpos += 1;
475 	n += octstr_get_char(data, *charpos);
476 	n = n << 8;
477 	*charpos += 1;
478 	n += octstr_get_char(data, *charpos);
479 	*charpos += 1;
480 	return n;
481 }
482 
unpack_octstr(Octstr * data,long * charpos)483 Octstr *unpack_octstr(Octstr * data, long *charpos)
484 {
485 	int length;
486 	Octstr *opaque;
487 
488 	length = octstr_get_char(data, *charpos);
489 	*charpos += 1;
490 	opaque = octstr_copy(data, *charpos, length);
491 	*charpos += length;
492 	return opaque;
493 }
494 
unpack_octstr16(Octstr * data,long * charpos)495 Octstr *unpack_octstr16(Octstr * data, long *charpos)
496 {
497 	long length;
498 	Octstr *opaque;
499 
500 	length = unpack_int16(data, charpos);
501 	opaque = octstr_copy(data, *charpos, length);
502 	*charpos += length;
503 	return opaque;
504 }
505 
unpack_octstr_fixed(Octstr * data,long * charpos,long length)506 Octstr *unpack_octstr_fixed(Octstr * data, long *charpos, long length)
507 {
508 	Octstr *opaque;
509 
510 	opaque = octstr_copy(data, *charpos, length);
511 	*charpos += length;
512 	return opaque;
513 }
514 
unpack_random(Octstr * data,long * charpos)515 Random *unpack_random(Octstr * data, long *charpos)
516 {
517 	Random *random;
518 	/* create the Random structure */
519    random = (Random *) gw_malloc(sizeof(Random));
520 
521 	random->gmt_unix_time = unpack_int32(data, charpos);
522 	random->random_bytes = unpack_octstr_fixed(data, charpos, 12);
523 	return random;
524 }
525 
unpack_dhparams(Octstr * data,long * charpos)526 DHParameters *unpack_dhparams(Octstr * data, long *charpos)
527 {
528 	DHParameters *dhparams;
529 
530 	/* create the DHParameters */
531    dhparams = (DHParameters *) gw_malloc(sizeof(DHParameters));
532 
533 	dhparams->dh_e = octstr_get_char(data, *charpos);
534 	*charpos += 1;
535 	dhparams->dh_p = unpack_octstr16(data, charpos);
536 	dhparams->dh_g = unpack_octstr16(data, charpos);
537 	return dhparams;
538 }
539 
unpack_ecparams(Octstr * data,long * charpos)540 ECParameters *unpack_ecparams(Octstr * data, long *charpos)
541 {
542 	ECParameters *ecparams;
543 
544 	/* create the ECParameters */
545    ecparams = (ECParameters *) gw_malloc(sizeof(ECParameters));
546 
547 	/* field */
548 	ecparams->field = octstr_get_char(data, *charpos);
549 	*charpos += 1;
550 	switch (ecparams->field) {
551 	case ec_prime_p:
552 		ecparams->prime_p = unpack_octstr(data, charpos);
553 		break;
554 	case ec_characteristic_two:
555 		/* m (16 bits) */
556 		ecparams->m = unpack_int16(data, charpos);
557 		/* basis */
558 		ecparams->basis = octstr_get_char(data, *charpos);
559 		*charpos += 1;
560 		switch (ecparams->basis) {
561 		case ec_basis_onb:
562 			break;
563 		case ec_basis_trinomial:
564 			ecparams->k = unpack_int16(data, charpos);
565 			break;
566 		case ec_basis_pentanomial:
567 			ecparams->k1 = unpack_int16(data, charpos);
568 			ecparams->k2 = unpack_int16(data, charpos);
569 			ecparams->k3 = unpack_int16(data, charpos);
570 			break;
571 		case ec_basis_polynomial:
572 			ecparams->irreducible = unpack_octstr(data, charpos);
573 			break;
574 		}
575 		break;
576 	}
577 	/* pack the ECCurve */
578 	ecparams->curve->a = unpack_octstr(data, charpos);
579 	ecparams->curve->b = unpack_octstr(data, charpos);
580 	ecparams->curve->seed = unpack_octstr(data, charpos);
581 	/* pack the ECPoint */
582 	ecparams->base->point = unpack_octstr(data, charpos);
583 	/* order and cofactor */
584 	ecparams->order = unpack_octstr(data, charpos);
585 	ecparams->cofactor = unpack_octstr(data, charpos);
586 
587 	return ecparams;
588 }
589 
unpack_param_spec(Octstr * data,long * charpos)590 ParameterSpecifier *unpack_param_spec(Octstr * data, long *charpos)
591 {
592 	ParameterSpecifier *pspec;
593 
594 	/* create the ParameterSpecifier */
595    pspec = (ParameterSpecifier *) gw_malloc(sizeof(ParameterSpecifier));
596 
597 	/* index */
598 	pspec->param_index = octstr_get_char(data, *charpos);
599 	*charpos += 1;
600 	/* ParameterSet struct */
601    if (pspec->param_index == 255) {
602       pspec->param_set =
603           (ParameterSet *) gw_malloc(sizeof(ParameterSet));
604 		pspec->param_set->length = octstr_get_char(data, *charpos);
605 		*charpos += 1;
606 		switch (public_key_algo) {
607 		case diffie_hellman_pubkey:
608          pspec->param_set->dhparams =
609              unpack_dhparams(data, charpos);
610 			break;
611 		case elliptic_curve_pubkey:
612          pspec->param_set->ecparams =
613              unpack_ecparams(data, charpos);
614          break;
615       default:
616 			break;
617 		}
618 	}
619 	return pspec;
620 }
621 
unpack_rsa_pubkey(Octstr * data,long * charpos)622 RSAPublicKey *unpack_rsa_pubkey(Octstr * data, long *charpos)
623 {
624 	RSAPublicKey *key;
625 
626 	/* create the RSAPublicKey */
627    key = (RSAPublicKey *) gw_malloc(sizeof(RSAPublicKey));
628    key->rsa_exponent = unpack_octstr16(data, charpos);
629    key->rsa_modulus = unpack_octstr16(data, charpos);
630 	return key;
631 }
632 
unpack_dh_pubkey(Octstr * data,long * charpos)633 DHPublicKey *unpack_dh_pubkey(Octstr * data, long *charpos)
634 {
635 	DHPublicKey *key;
636 
637 	/* create the DHPublicKey */
638    key = (DHPublicKey *) gw_malloc(sizeof(DHPublicKey));
639    key->dh_Y = unpack_octstr16(data, charpos);
640 	return key;
641 }
642 
unpack_ec_pubkey(Octstr * data,long * charpos)643 ECPublicKey *unpack_ec_pubkey(Octstr * data, long *charpos)
644 {
645 	ECPublicKey *key;
646 
647 	/* create the ECPublicKey */
648    key = (ECPublicKey *) gw_malloc(sizeof(ECPublicKey));
649    key->point = unpack_octstr(data, charpos);
650 	return key;
651 }
652 
unpack_rsa_secret(Octstr * data,long * charpos)653 RSASecret *unpack_rsa_secret(Octstr * data, long *charpos)
654 {
655 	RSASecret *secret;
656 
657 	/* create the RSASecret */
658    secret = (RSASecret *) gw_malloc(sizeof(RSASecret));
659 	secret->client_version = octstr_get_char(data, *charpos);
660 	*charpos += 1;
661 	secret->random = unpack_array(data, charpos);
662 
663 	return secret;
664 }
665 
unpack_rsa_encrypted_secret(Octstr * data,long * charpos)666 RSAEncryptedSecret *unpack_rsa_encrypted_secret(Octstr * data, long *charpos)
667 {
668 	RSAEncryptedSecret *secret;
669 
670 	/* create the RSASecret */
671    secret = (RSAEncryptedSecret *) gw_malloc(sizeof(RSAEncryptedSecret));
672 	//secret->encrypted_secret = unpack_octstr16(data, charpos);
673    secret->encrypted_secret =
674        unpack_octstr_fixed(data, charpos, octstr_len(data) - *charpos);
675 	return secret;
676 }
677 
unpack_public_key(Octstr * data,long * charpos,PublicKeyType key_type)678 PublicKey *unpack_public_key(Octstr * data, long *charpos,
679               PublicKeyType key_type)
680 {
681 	PublicKey *key;
682 
683 	/* create the PublicKey */
684    key = (PublicKey *) gw_malloc(sizeof(PublicKey));
685 	switch (key_type) {
686 	case ecdh_key:
687 		key->ecdh_pubkey = unpack_ec_pubkey(data, charpos);
688 		break;
689 	case ecdsa_key:
690 		key->ecdsa_pubkey = unpack_ec_pubkey(data, charpos);
691 		break;
692 	case rsa_key:
693 		key->rsa_pubkey = unpack_rsa_pubkey(data, charpos);
694 		break;
695 	}
696 	return key;
697 }
698 
unpack_key_exchange_id(Octstr * data,long * charpos)699 KeyExchangeId *unpack_key_exchange_id(Octstr * data, long *charpos)
700 {
701 	KeyExchangeId *keyexid;
702 
703 	/* create the KeyExchangeID */
704    keyexid = (KeyExchangeId *) gw_malloc(sizeof(KeyExchangeId));
705 
706 	keyexid->key_exchange_suite = octstr_get_char(data, *charpos);
707 	*charpos += 1;
708 	keyexid->param_specif = unpack_param_spec(data, charpos);
709 	keyexid->identifier = unpack_identifier(data, charpos);
710 	return keyexid;
711 }
712 
unpack_array(Octstr * data,long * charpos)713 List *unpack_array(Octstr * data, long *charpos)
714 {
715 	int i;
716 	int array_length;
717 	List *array;
718 
719 	/* create the list */
720 	array = gwlist_create();
721 
722 	/* get the size of the array */
723 	array_length = octstr_get_char(data, *charpos);
724 	*charpos += 1;
725 
726 	/* store each entry in the list */
727    for (i = 0; i < array_length; i++) {
728 		gwlist_append(array, (void *)unpack_octstr(data, charpos));
729 	}
730 
731 	return array;
732 }
733 
unpack_key_list(Octstr * data,long * charpos)734 List *unpack_key_list(Octstr * data, long *charpos)
735 {
736 	KeyExchangeId *keyexid;
737 	List *key_list;
738 	int gwlist_length;
739 	long endpos;
740 
741 	/* create the list */
742 	key_list = gwlist_create();
743 
744 	/* get the size of the array */
745 	gwlist_length = unpack_int16(data, charpos);
746 	endpos = *charpos + gwlist_length;
747 
748 	/* unpack the KeyExchangeIds */
749    while (*charpos < endpos) {
750 		keyexid = unpack_key_exchange_id(data, charpos);
751 		gwlist_append(key_list, (void *)keyexid);
752 	}
753 	return key_list;
754 }
755 
unpack_ciphersuite_list(Octstr * data,long * charpos)756 List *unpack_ciphersuite_list(Octstr * data, long *charpos)
757 {
758 	List *ciphersuites;
759 	int gwlist_length;
760 	int i;
761 	CipherSuite *cs;
762 
763 	/* create the list */
764 	ciphersuites = gwlist_create();
765 
766    /* get the size of the array (in bytes, not elements) */
767 	gwlist_length = octstr_get_char(data, *charpos);
768 	*charpos += 1;
769 
770 	/* unpack the CipherSuites */
771    for (i = 0; i < gwlist_length; i += 2) {
772       cs = (CipherSuite *) gw_malloc(sizeof(CipherSuite));
773 		cs->bulk_cipher_algo = octstr_get_char(data, *charpos);
774 		*charpos += 1;
775 		cs->mac_algo = octstr_get_char(data, *charpos);
776 		*charpos += 1;
777 		gwlist_append(ciphersuites, (void *)cs);
778 	}
779 
780 	return ciphersuites;
781 }
782 
unpack_compression_method_list(Octstr * data,long * charpos)783 List *unpack_compression_method_list(Octstr * data, long *charpos)
784 {
785 	List *compmethod_list;
786 	int gwlist_length;
787 	int i;
788 	CompressionMethod *cm;
789 
790 	/* create the list */
791 	compmethod_list = gwlist_create();
792 
793 	/* get the size of the array */
794 	gwlist_length = octstr_get_char(data, *charpos);
795 	*charpos += 1;
796 
797 	/* unpack the CompressionMethods */
798    for (i = 0; i < gwlist_length; i++) {
799 		cm = gw_malloc(sizeof(CompressionMethod));
800 		*cm = octstr_get_char(data, *charpos);
801 		gwlist_append(compmethod_list, (void *)cm);
802 	}
803 
804 	return compmethod_list;
805 }
806 
unpack_identifier(Octstr * data,long * charpos)807 Identifier *unpack_identifier(Octstr * data, long *charpos)
808 {
809 	Identifier *ident;
810 
811 	/* create Identifier */
812    ident = (Identifier *) gw_malloc(sizeof(Identifier));
813 
814 	ident->id_type = octstr_get_char(data, *charpos);
815 	*charpos += 1;
816 	switch (ident->id_type) {
817 	case text:
818 		ident->charset = octstr_get_char(data, *charpos);
819 		*charpos += 1;
820 		ident->name = unpack_octstr(data, charpos);
821 		break;
822 	case binary:
823 		ident->identifier = unpack_octstr(data, charpos);
824 		break;
825 	case key_hash_sha:
826 		ident->key_hash = unpack_octstr(data, charpos);
827 		break;
828 	case x509_name:
829 		ident->distinguished_name = unpack_octstr(data, charpos);
830 		break;
831    default:
832       break;
833 	}
834 	return ident;
835 }
836 
unpack_signature(Octstr * data,long * charpos)837 Signature *unpack_signature(Octstr * data, long *charpos)
838 {
839 	Signature *sig;
840 
841 	/* create Signature */
842    sig = (Signature *) gw_malloc(sizeof(Signature));
843 
844 	switch (signature_algo) {
845 	case ecdsa_sha:
846 	case rsa_sha:
847 		sig->sha_hash = unpack_array(data, charpos);
848 		break;
849    default:
850       break;
851 	}
852 	return sig;
853 }
854 
unpack_wtls_certificate(Octstr * data,long * charpos)855 WTLSCertificate *unpack_wtls_certificate(Octstr * data, long *charpos)
856 {
857 	WTLSCertificate *cert;
858 
859 	/* create the Certificate */
860    cert = (WTLSCertificate *) gw_malloc(sizeof(WTLSCertificate));
861 
862 	/* === unpack ToBeSignedCertificate === */
863    cert->tobesigned_cert =
864        (ToBeSignedCertificate *) gw_malloc(sizeof(ToBeSignedCertificate));
865 	/* version */
866    cert->tobesigned_cert->certificate_version =
867        octstr_get_char(data, *charpos);
868 	*charpos += 1;
869 	/* sig algo */
870 	cert->tobesigned_cert->signature_algo = octstr_get_char(data, *charpos);
871 	*charpos += 1;
872 	/* identifier */
873    cert->tobesigned_cert->issuer->id_type =
874        octstr_get_char(data, *charpos);
875 	*charpos += 1;
876 	/* issuer Identifier */
877 	cert->tobesigned_cert->issuer = unpack_identifier(data, charpos);
878 	/* validity periods */
879 	cert->tobesigned_cert->valid_not_before = unpack_int32(data, charpos);
880 	cert->tobesigned_cert->valid_not_after = unpack_int32(data, charpos);
881 	/* subject Identifier */
882 	cert->tobesigned_cert->subject = unpack_identifier(data, charpos);
883 	/* public_key_type */
884 	cert->tobesigned_cert->pubkey_type = octstr_get_char(data, *charpos);
885 	*charpos += 1;
886 	/* parameter specifier */
887 	cert->tobesigned_cert->param_spec = unpack_param_spec(data, charpos);
888 	/* public key */
889 	cert->tobesigned_cert->pubkey = unpack_public_key(data, charpos,
890                        cert->
891                        tobesigned_cert->
892                        pubkey_type);
893 
894 	/* === pack Signature === */
895 	cert->signature = unpack_signature(data, charpos);
896 	return cert;
897 }
898 
899 /*****************************************************************
900  * DESTROY functions
901  */
902 
destroy_octstr(Octstr * data)903 void destroy_octstr(Octstr * data)
904 {
905 	octstr_destroy(data);
906 }
907 
destroy_octstr16(Octstr * data)908 void destroy_octstr16(Octstr * data)
909 {
910 	octstr_destroy(data);
911 }
912 
destroy_octstr_fixed(Octstr * data)913 void destroy_octstr_fixed(Octstr * data)
914 {
915 	octstr_destroy(data);
916 }
917 
destroy_random(Random * random)918 void destroy_random(Random * random)
919 {
920 	octstr_destroy(random->random_bytes);
921 	gw_free(random);
922 }
923 
destroy_dhparams(DHParameters * dhparams)924 void destroy_dhparams(DHParameters * dhparams)
925 {
926 	destroy_octstr16(dhparams->dh_p);
927 	destroy_octstr16(dhparams->dh_g);
928 	gw_free(dhparams);
929 }
930 
destroy_ecparams(ECParameters * ecparams)931 void destroy_ecparams(ECParameters * ecparams)
932 {
933 	/* field */
934 	switch (ecparams->field) {
935 	case ec_prime_p:
936 		octstr_destroy(ecparams->prime_p);
937 		break;
938 	case ec_characteristic_two:
939 		switch (ecparams->basis) {
940 		case ec_basis_onb:
941 			break;
942 		case ec_basis_trinomial:
943 			break;
944 		case ec_basis_pentanomial:
945 			break;
946 		case ec_basis_polynomial:
947 			octstr_destroy(ecparams->irreducible);
948 			break;
949 		}
950 		break;
951 	}
952 	/* pack the ECCurve */
953 	octstr_destroy(ecparams->curve->a);
954 	octstr_destroy(ecparams->curve->b);
955 	octstr_destroy(ecparams->curve->seed);
956 	/* pack the ECPoint */
957 	octstr_destroy(ecparams->base->point);
958 	/* order and cofactor */
959 	octstr_destroy(ecparams->order);
960 	octstr_destroy(ecparams->cofactor);
961 
962 	gw_free(ecparams);
963 }
964 
destroy_param_spec(ParameterSpecifier * pspec)965 void destroy_param_spec(ParameterSpecifier * pspec)
966 {
967  	switch (public_key_algo) {
968 	case diffie_hellman_pubkey:
969 		destroy_dhparams(pspec->param_set->dhparams);
970 		break;
971 	case elliptic_curve_pubkey:
972 		destroy_ecparams(pspec->param_set->ecparams);
973 		break;
974    default:
975       break;
976 	}
977 	gw_free(pspec);
978 }
979 
destroy_public_key(PublicKey * key)980 void destroy_public_key(PublicKey * key)
981 {
982    if (key->ecdh_pubkey) {
983 		octstr_destroy(key->ecdh_pubkey->point);
984 		gw_free(key->ecdh_pubkey);
985 	}
986    if (key->ecdsa_pubkey) {
987 		octstr_destroy(key->ecdsa_pubkey->point);
988 		gw_free(key->ecdsa_pubkey);
989 	}
990    if (key->rsa_pubkey) {
991 		destroy_rsa_pubkey(key->rsa_pubkey);
992 	}
993 	gw_free(key);
994 }
995 
destroy_rsa_pubkey(RSAPublicKey * key)996 void destroy_rsa_pubkey(RSAPublicKey * key)
997 {
998 	octstr_destroy(key->rsa_exponent);
999 	octstr_destroy(key->rsa_modulus);
1000 	gw_free(key);
1001 }
1002 
destroy_ec_pubkey(ECPublicKey * key)1003 void destroy_ec_pubkey(ECPublicKey * key)
1004 {
1005 	octstr_destroy(key->point);
1006 	gw_free(key);
1007 }
1008 
destroy_dh_pubkey(DHPublicKey * key)1009 void destroy_dh_pubkey(DHPublicKey * key)
1010 {
1011 	octstr_destroy(key->dh_Y);
1012 	gw_free(key);
1013 }
1014 
destroy_rsa_secret(RSASecret * secret)1015 void destroy_rsa_secret(RSASecret * secret)
1016 {
1017 	destroy_array(secret->random);
1018 	gw_free(secret);
1019 }
1020 
destroy_rsa_encrypted_secret(RSAEncryptedSecret * secret)1021 void destroy_rsa_encrypted_secret(RSAEncryptedSecret * secret)
1022 {
1023 	octstr_destroy(secret->encrypted_secret);
1024 	gw_free(secret);
1025 }
1026 
destroy_key_exchange_id(KeyExchangeId * keyexid)1027 void destroy_key_exchange_id(KeyExchangeId * keyexid)
1028 {
1029 	destroy_param_spec(keyexid->param_specif);
1030 	destroy_identifier(keyexid->identifier);
1031 	gw_free(keyexid);
1032 }
1033 
destroy_array(List * array)1034 void destroy_array(List * array)
1035 {
1036 	int i;
1037 
1038 	/* pack each entry in the array */
1039    for (i = 0; i < gwlist_len(array); i++) {
1040 		octstr_destroy((Octstr *) gwlist_get(array, i));
1041 	}
1042 
1043 	gwlist_destroy(array, NULL);
1044 }
1045 
destroy_key_list(List * key_list)1046 void destroy_key_list(List * key_list)
1047 {
1048 	int i;
1049 	/* destroy the KeyExchangeIds */
1050    for (i = 0; i < gwlist_len(key_list); i++) {
1051       destroy_key_exchange_id((KeyExchangeId *)
1052                gwlist_get(key_list, i));
1053 	}
1054 	gwlist_destroy(key_list, NULL);
1055 }
1056 
destroy_ciphersuite_list(List * ciphersuites)1057 void destroy_ciphersuite_list(List * ciphersuites)
1058 {
1059 	int i;
1060 
1061 	/* destroy the CipherSuites */
1062    for (i = 0; i < gwlist_len(ciphersuites); i++) {
1063       gw_free((CipherSuite *) gwlist_get(ciphersuites, i));
1064 	}
1065 	gwlist_destroy(ciphersuites, NULL);
1066 }
1067 
destroy_compression_method_list(List * compmethod_list)1068 void destroy_compression_method_list(List * compmethod_list)
1069 {
1070 	int i;
1071 	CompressionMethod *cm;
1072 
1073 	/* destroy the CompressionMethods */
1074    for (i = 0; i < gwlist_len(compmethod_list); i++) {
1075       cm = (CompressionMethod *) gwlist_get(compmethod_list, i);
1076 		gw_free(cm);
1077 	}
1078 
1079 	gw_free(compmethod_list);
1080 }
1081 
destroy_identifier(Identifier * ident)1082 void destroy_identifier(Identifier * ident)
1083 {
1084 	switch (ident->id_type) {
1085 	case text:
1086 		octstr_destroy(ident->name);
1087 		break;
1088 	case binary:
1089 		octstr_destroy(ident->identifier);
1090 		break;
1091 	case key_hash_sha:
1092 		octstr_destroy(ident->key_hash);
1093 		break;
1094 	case x509_name:
1095 		octstr_destroy(ident->distinguished_name);
1096 		break;
1097    default:
1098       break;
1099 	}
1100 	gw_free(ident);
1101 }
1102 
destroy_signature(Signature * sig)1103 void destroy_signature(Signature * sig)
1104 {
1105 	switch (signature_algo) {
1106 	case ecdsa_sha:
1107 	case rsa_sha:
1108 		destroy_array(sig->sha_hash);
1109 		break;
1110    default:
1111       break;
1112 	}
1113 	gw_free(sig);
1114 }
1115 
destroy_wtls_certificate(WTLSCertificate * cert)1116 void destroy_wtls_certificate(WTLSCertificate * cert)
1117 {
1118 	/* === destroy ToBeSignedCertificate === */
1119 	/* issuer Identifier */
1120 	destroy_identifier(cert->tobesigned_cert->issuer);
1121 	/* subject Identifier */
1122 	destroy_identifier(cert->tobesigned_cert->subject);
1123 	/* parameter specifier */
1124 	destroy_param_spec(cert->tobesigned_cert->param_spec);
1125 	/* public key */
1126 	destroy_public_key(cert->tobesigned_cert->pubkey);
1127 
1128 	/* === destroy Signature === */
1129 	destroy_signature(cert->signature);
1130 	gw_free(cert);
1131 }
1132 
1133 /*****************************************************************
1134  * DUMP functions
1135  */
1136 
dump_void16(char * dbg,int level,int i)1137 void dump_void16(char *dbg, int level, int i)
1138 {
1139    debug(dbg, 0, "%*s16 bit Int: %d", level, "", i);
1140 }
1141 
dump_int32(char * dbg,int level,long i)1142 void dump_int32(char *dbg, int level, long i)
1143 {
1144    debug(dbg, 0, "%*s32 bit Int: %ld", level, "", i);
1145 }
1146 
dump_octstr(char * dbg,int level,Octstr * opaque)1147 void dump_octstr(char *dbg, int level, Octstr * opaque)
1148 {
1149 	octstr_dump(opaque, 0);
1150 }
1151 
dump_octstr16(char * dbg,int level,Octstr * opaque)1152 void dump_octstr16(char *dbg, int level, Octstr * opaque)
1153 {
1154 	octstr_dump(opaque, 0);
1155 }
1156 
dump_octstr_fixed(char * dbg,int level,Octstr * opaque)1157 void dump_octstr_fixed(char *dbg, int level, Octstr * opaque)
1158 {
1159 	octstr_dump(opaque, 0);
1160 }
1161 
dump_random(char * dbg,int level,Random * random)1162 void dump_random(char *dbg, int level, Random * random)
1163 {
1164 	debug(dbg, 0, "%*sRandom :", level, "");
1165    debug(dbg, 0, "%*sGMT Unix Time: %ld", level + 1, "",
1166          random->gmt_unix_time);
1167    debug(dbg, 0, "%*sRandom Bytes:", level + 1, "");
1168    dump_octstr_fixed(dbg, level + 2, random->random_bytes);
1169 }
1170 
dump_dhparams(char * dbg,int level,DHParameters * dhparams)1171 void dump_dhparams(char *dbg, int level, DHParameters * dhparams)
1172 {
1173 	debug(dbg, 0, "%*sDH Parameters :", level, "");
1174    debug(dbg, 0, "%*sdh_e: %d", level + 1, "", dhparams->dh_e);
1175    debug(dbg, 0, "%*sdh_p:", level + 1, "");
1176    dump_octstr16(dbg, level + 2, dhparams->dh_p);
1177    debug(dbg, 0, "%*sdh_g:", level + 1, "");
1178    dump_octstr16(dbg, level + 2, dhparams->dh_g);
1179 }
1180 
dump_ecparams(char * dbg,int level,ECParameters * ecparams)1181 void dump_ecparams(char *dbg, int level, ECParameters * ecparams)
1182 {
1183 	debug(dbg, 0, "%*sEC Parameters :", level, "");
1184 	/* field */
1185    debug(dbg, 0, "%*sField: %d", level + 1, "", ecparams->field);
1186 	switch (ecparams->field) {
1187 	case ec_prime_p:
1188       debug(dbg, 0, "%*sprime_p :", level + 1, "");
1189       dump_octstr(dbg, level + 1, ecparams->prime_p);
1190 		break;
1191 	case ec_characteristic_two:
1192 		/* m (16 bits) */
1193       debug(dbg, 0, "%*sM: %d", level + 1, "", ecparams->m);
1194 		/* basis */
1195       debug(dbg, 0, "%*sBasis: %d", level + 1, "", ecparams->basis);
1196 		switch (ecparams->basis) {
1197 		case ec_basis_onb:
1198 			break;
1199 		case ec_basis_trinomial:
1200          debug(dbg, 0, "%*sK: %d", level + 1, "", ecparams->k);
1201 			break;
1202 		case ec_basis_pentanomial:
1203          debug(dbg, 0, "%*sk1: %d", level + 1, "", ecparams->k1);
1204          debug(dbg, 0, "%*sk2: %d", level + 1, "", ecparams->k2);
1205          debug(dbg, 0, "%*sk3: %d", level + 1, "", ecparams->k3);
1206 			break;
1207 		case ec_basis_polynomial:
1208          debug(dbg, 0, "%*sirreducible: 0x%p", level + 1, "",
1209                ecparams->irreducible);
1210          dump_octstr(dbg, level + 1, ecparams->irreducible);
1211 			break;
1212 		}
1213 		break;
1214 	}
1215 	/* pack the ECCurve */
1216    debug(dbg, 0, "%*sEC Curve: 0x%p", level + 1, "", ecparams->curve);
1217    debug(dbg, 0, "%*sa: 0x%p", level + 2, "", ecparams->curve->a);
1218    dump_octstr(dbg, level + 2, ecparams->curve->a);
1219    debug(dbg, 0, "%*sb: 0x%p", level + 2, "", ecparams->curve->b);
1220    dump_octstr(dbg, level + 2, ecparams->curve->b);
1221    debug(dbg, 0, "%*sseed: 0x%p", level + 2, "", ecparams->curve->seed);
1222    dump_octstr(dbg, level + 2, ecparams->curve->seed);
1223 	/* pack the ECPoint */
1224    debug(dbg, 0, "%*spoint: 0x%p", level + 2, "", ecparams->base->point);
1225    dump_octstr(dbg, level + 2, ecparams->base->point);
1226 	/* order and cofactor */
1227    debug(dbg, 0, "%*sorder: 0x%p", level + 2, "", ecparams->order);
1228    dump_octstr(dbg, level + 2, ecparams->order);
1229    debug(dbg, 0, "%*scofactor: 0x%p", level + 2, "", ecparams->cofactor);
1230    dump_octstr(dbg, level + 2, ecparams->cofactor);
1231 }
1232 
dump_param_spec(char * dbg,int level,ParameterSpecifier * pspec)1233 void dump_param_spec(char *dbg, int level, ParameterSpecifier * pspec)
1234 {
1235 	debug(dbg, 0, "%*sParameterSpecifier:", level, "");
1236 	/* index */
1237    debug(dbg, 0, "%*sParameter Index: %d", level + 1, "",
1238          pspec->param_index);
1239 	/* ParameterSet struct */
1240    if (pspec->param_index == 255) {
1241       debug(dbg, 0, "%*sLength: %ld", level + 1, "",
1242             pspec->param_set->length);
1243 		switch (public_key_algo) {
1244 		case diffie_hellman_pubkey:
1245          dump_dhparams(dbg, level + 1,
1246                   pspec->param_set->dhparams);
1247 			break;
1248 		case elliptic_curve_pubkey:
1249          dump_ecparams(dbg, level + 1,
1250                   pspec->param_set->ecparams);
1251          break;
1252       default:
1253 			break;
1254 		}
1255 	}
1256 }
1257 
dump_public_key(char * dbg,int level,PublicKey * key,PublicKeyType key_type)1258 void dump_public_key(char *dbg, int level, PublicKey * key, PublicKeyType
1259            key_type)
1260 {
1261 	switch (key_type) {
1262 	case ecdh_key:
1263       debug(dbg, 0, "%*sPublicKey: 0x%p", level, "",
1264             key->ecdh_pubkey);
1265       debug(dbg, 0, "%*sECDH Point: 0x%p", level + 1, "",
1266             key->ecdh_pubkey->point);
1267       dump_octstr(dbg, level + 1, key->ecdh_pubkey->point);
1268 		break;
1269 	case ecdsa_key:
1270       debug(dbg, 0, "%*sECDSA Point: 0x%p", level + 1, "",
1271             key->ecdsa_pubkey->point);
1272       dump_octstr(dbg, level + 1, key->ecdsa_pubkey->point);
1273 		break;
1274 	case rsa_key:
1275       dump_rsa_pubkey(dbg, level + 1, key->rsa_pubkey);
1276 		break;
1277 	}
1278 }
1279 
dump_rsa_pubkey(char * dbg,int level,RSAPublicKey * key)1280 void dump_rsa_pubkey(char *dbg, int level, RSAPublicKey * key)
1281 {
1282    debug(dbg, 0, "%*sRSA Public Key: 0x%p", level, "", key);
1283    debug(dbg, 0, "%*sRSA Exponent: 0x%p", level + 1, "",
1284          key->rsa_exponent);
1285    dump_octstr(dbg, level + 2, key->rsa_exponent);
1286    debug(dbg, 0, "%*sRSA Modulus: 0x%p", level + 1, "", key->rsa_modulus);
1287    dump_octstr(dbg, level + 2, key->rsa_modulus);
1288 }
1289 
dump_ec_pubkey(char * dbg,int level,ECPublicKey * key)1290 void dump_ec_pubkey(char *dbg, int level, ECPublicKey * key)
1291 {
1292    debug(dbg, 0, "%*sEC Public Key: 0x%p", level, "", key);
1293    debug(dbg, 0, "%*sPoint: 0x%p", level + 1, "", key->point);
1294    dump_octstr(dbg, level + 2, key->point);
1295 }
1296 
dump_dh_pubkey(char * dbg,int level,DHPublicKey * key)1297 void dump_dh_pubkey(char *dbg, int level, DHPublicKey * key)
1298 {
1299    debug(dbg, 0, "%*sDH Public Key: 0x%p", level, "", key->dh_Y);
1300    dump_octstr(dbg, level + 2, key->dh_Y);
1301 }
1302 
dump_rsa_secret(char * dbg,int level,RSASecret * secret)1303 void dump_rsa_secret(char *dbg, int level, RSASecret * secret)
1304 {
1305    debug(dbg, 0, "%*sRSA Secret: 0x%p", level, "", secret);
1306    debug(dbg, 0, "%*sClient Version: %d", level + 1, "",
1307          secret->client_version);
1308    debug(dbg, 0, "%*sRandom: 0x%p", level, "", secret->random);
1309    dump_array(dbg, level + 2, secret->random);
1310 }
1311 
dump_rsa_encrypted_secret(char * dbg,int level,RSAEncryptedSecret * secret)1312 void dump_rsa_encrypted_secret(char *dbg, int level, RSAEncryptedSecret
1313                 * secret)
1314 {
1315    debug(dbg, 0, "%*sRSA Encrypted Secret: %p", level, "",
1316          secret->encrypted_secret);
1317    dump_octstr(dbg, level + 1, secret->encrypted_secret);
1318 }
1319 
dump_key_exchange_id(char * dbg,int level,KeyExchangeId * keyexid)1320 void dump_key_exchange_id(char *dbg, int level, KeyExchangeId * keyexid)
1321 {
1322    debug(dbg, 0, "%*sKey Exchange Id: 0x%p", level, "", keyexid);
1323    debug(dbg, 0, "%*sKey Exch Suite: %d", level + 1, "",
1324          keyexid->key_exchange_suite);
1325    dump_param_spec(dbg, level + 1, keyexid->param_specif);
1326    dump_identifier(dbg, level + 1, keyexid->identifier);
1327 }
1328 
dump_array(char * dbg,int level,List * array)1329 void dump_array(char *dbg, int level, List * array)
1330 {
1331 	int i;
1332 
1333    /*debug(dbg, 0, "%*sOctstr Array: %p", level, ""); */
1334 
1335 	/* dump each entry in the array */
1336    for (i = 0; i < gwlist_len(array); i++) {
1337 		debug(dbg, 0, "%*sElement %d", level, "", i);
1338       dump_octstr(dbg, level + 1, (Octstr *) gwlist_get(array, i));
1339 	}
1340 }
1341 
dump_key_list(char * dbg,int level,List * key_list)1342 void dump_key_list(char *dbg, int level, List * key_list)
1343 {
1344 	int i;
1345 	KeyExchangeId *keyexid;
1346 
1347    debug(dbg, 0, "%*sKey List: 0x%p", level, "", key_list);
1348 
1349 	/* pack the KeyExchangeIds */
1350    for (i = 0; i < gwlist_len(key_list); i++) {
1351 		keyexid = (KeyExchangeId *) gwlist_get(key_list, i);
1352       dump_key_exchange_id(dbg, level + 1, keyexid);
1353 	}
1354 }
1355 
dump_ciphersuite_list(char * dbg,int level,List * ciphersuites)1356 void dump_ciphersuite_list(char *dbg, int level, List * ciphersuites)
1357 {
1358 	int i;
1359 	CipherSuite *cs;
1360 
1361    debug(dbg, 0, "%*sCipherSuite List: 0x%p", level, "", ciphersuites);
1362 
1363 	/* dump the CipherSuites */
1364    for (i = 0; i < gwlist_len(ciphersuites); i++) {
1365 		cs = (CipherSuite *) gwlist_get(ciphersuites, i);
1366       debug(dbg, 0, "%*sBulk Cipher Algo: %d", level, "",
1367             cs->bulk_cipher_algo);
1368       debug(dbg, 0, "%*sMAC Algo: %d", level, "", cs->mac_algo);
1369 	}
1370 }
1371 
dump_compression_method_list(char * dbg,int level,List * compmethod_list)1372 void dump_compression_method_list(char *dbg, int level, List * compmethod_list)
1373 {
1374 	int i;
1375 
1376    debug(dbg, 0, "%*sCompression Method List: 0x%p", level, "",
1377          compmethod_list);
1378 	/* pack the CompressionMethods */
1379    for (i = 0; i < gwlist_len(compmethod_list); i++) {
1380       debug(dbg, 0, "%*sMethod %d: %d", level, "", i,
1381 				(CompressionMethod) gwlist_get(compmethod_list, i));
1382 	}
1383 }
1384 
dump_identifier(char * dbg,int level,Identifier * ident)1385 void dump_identifier(char *dbg, int level, Identifier * ident)
1386 {
1387    debug(dbg, 0, "%*sIdentifier: 0x%p", level, "", ident);
1388    debug(dbg, 0, "%*sIdent type: %d", level + 1, "", ident->id_type);
1389 	switch (ident->id_type) {
1390 	case text:
1391       debug(dbg, 0, "%*sCharset: %d", level + 1, "", ident->charset);
1392       debug(dbg, 0, "%*sNamet: %s", level + 1, "",
1393             octstr_get_cstr(ident->name));
1394 		break;
1395 	case binary:
1396       debug(dbg, 0, "%*sIdentifier: 0x%p", level + 1, "",
1397             ident->identifier);
1398       dump_octstr(dbg, level + 2, ident->identifier);
1399 		break;
1400 	case key_hash_sha:
1401       debug(dbg, 0, "%*sKey Hash: 0x%p", level + 1, "",
1402             ident->key_hash);
1403       dump_octstr(dbg, level + 2, ident->key_hash);
1404 		break;
1405 	case x509_name:
1406       debug(dbg, 0, "%*sDistinguished Name: 0x%p", level + 1, "",
1407             ident->distinguished_name);
1408       dump_octstr(dbg, level + 2, ident->distinguished_name);
1409       break;
1410    default:
1411 		break;
1412 	}
1413 }
1414 
dump_signature(char * dbg,int level,Signature * sig)1415 void dump_signature(char *dbg, int level, Signature * sig)
1416 {
1417    debug(dbg, 0, "%*sSignature: 0x%p", level, "", sig);
1418 	switch (signature_algo) {
1419 	case ecdsa_sha:
1420 	case rsa_sha:
1421       dump_array(dbg, level + 1, sig->sha_hash);
1422       break;
1423 
1424    default:
1425 		break;
1426 	}
1427 }
1428 
dump_wtls_certificate(char * dbg,int level,WTLSCertificate * cert)1429 void dump_wtls_certificate(char *dbg, int level, WTLSCertificate * cert)
1430 {
1431    debug(dbg, 0, "%*sWTLS Certificate: 0x%p", level, "", cert);
1432 	/* === pack ToBeSignedCertificate === */
1433 	/* version */
1434    debug(dbg, 0, "%*sCertificate Version: %d", level + 1, "",
1435          cert->tobesigned_cert->certificate_version);
1436 	/* sig algo */
1437    debug(dbg, 0, "%*sSignature Algo: %d", level + 1, "",
1438          cert->tobesigned_cert->signature_algo);
1439 	/* identifier */
1440    debug(dbg, 0, "%*sID Type: %d", level + 1, "",
1441          cert->tobesigned_cert->issuer->id_type);
1442 	/* issuer Identifier */
1443    dump_identifier(dbg, level + 1, cert->tobesigned_cert->issuer);
1444 	/* validity periods */
1445    debug(dbg, 0, "%*sValid not Before: %ld", level + 1, "",
1446          cert->tobesigned_cert->valid_not_before);
1447    debug(dbg, 0, "%*sValid not After: %ld", level + 1, "",
1448          cert->tobesigned_cert->valid_not_after);
1449 	/* subject Identifier */
1450    dump_identifier(dbg, level + 1, cert->tobesigned_cert->subject);
1451 	/* public_key_type */
1452    debug(dbg, 0, "%*sPublic Key Type: %d", level + 1, "",
1453          cert->tobesigned_cert->pubkey_type);
1454 	/* parameter specifier */
1455    dump_param_spec(dbg, level + 1, cert->tobesigned_cert->param_spec);
1456 	/* public key */
1457    dump_public_key(dbg, level + 1, cert->tobesigned_cert->pubkey,
1458 					cert->tobesigned_cert->pubkey_type);
1459 
1460 	/* === pack Signature === */
1461    dump_signature(dbg, level + 1, cert->signature);
1462 }
1463 #endif /* HAVE_WTLS_OPENSSL */
1464