1 /*
2  * libpri: An implementation of Primary Rate ISDN
3  *
4  * Copyright (C) 2009 Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2 as published by the
16  * Free Software Foundation. See the LICENSE file included with
17  * this program for more details.
18  *
19  * In addition, when this program is distributed with Asterisk in
20  * any form that would qualify as a 'combined work' or as a
21  * 'derivative work' (but not mere aggregation), you can redistribute
22  * and/or modify the combination under the terms of the license
23  * provided with that copy of Asterisk, instead of the license
24  * terms granted here.
25  */
26 
27 /*!
28  * \file
29  * \brief ROSE Addressing-Data-Elements
30  *
31  * Addressing-Data-Elements ETS 300 196-1 D.3
32  *
33  * \author Richard Mudgett <rmudgett@digium.com>
34  */
35 
36 
37 #include "compat.h"
38 #include "libpri.h"
39 #include "pri_internal.h"
40 #include "rose.h"
41 #include "rose_internal.h"
42 #include "asn1.h"
43 
44 
45 /* ------------------------------------------------------------------- */
46 
47 /*!
48  * \internal
49  * \brief Encode the public or private network PartyNumber type.
50  *
51  * \param ctrl D channel controller for diagnostic messages or global options.
52  * \param pos Starting position to encode ASN.1 component.
53  * \param end End of ASN.1 encoding data buffer.
54  * \param tag Component tag to identify the encoded component.
55  *   The tag should be ASN1_TAG_SEQUENCE unless the caller implicitly
56  *   tags it otherwise.
57  * \param number
58  * \param length_of_number
59  * \param type_of_number
60  *
61  * \retval Start of the next ASN.1 component to encode on success.
62  * \retval NULL on error.
63  */
rose_enc_NetworkPartyNumber(struct pri * ctrl,unsigned char * pos,unsigned char * end,unsigned tag,const unsigned char * number,size_t length_of_number,u_int8_t type_of_number)64 static unsigned char *rose_enc_NetworkPartyNumber(struct pri *ctrl, unsigned char *pos,
65 	unsigned char *end, unsigned tag, const unsigned char *number,
66 	size_t length_of_number, u_int8_t type_of_number)
67 {
68 	unsigned char *seq_len;
69 
70 	ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, tag);
71 
72 	ASN1_CALL(pos, asn1_enc_int(pos, end, ASN1_TYPE_ENUMERATED, type_of_number));
73 	ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_TYPE_NUMERIC_STRING, number,
74 		length_of_number));
75 
76 	ASN1_CONSTRUCTED_END(seq_len, pos, end);
77 
78 	return pos;
79 }
80 
81 /*!
82  * \brief Encode the PartyNumber type.
83  *
84  * \param ctrl D channel controller for diagnostic messages or global options.
85  * \param pos Starting position to encode ASN.1 component.
86  * \param end End of ASN.1 encoding data buffer.
87  * \param party_number
88  *
89  * \retval Start of the next ASN.1 component to encode on success.
90  * \retval NULL on error.
91  */
rose_enc_PartyNumber(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct rosePartyNumber * party_number)92 unsigned char *rose_enc_PartyNumber(struct pri *ctrl, unsigned char *pos,
93 	unsigned char *end, const struct rosePartyNumber *party_number)
94 {
95 	switch (party_number->plan) {
96 	case 0:	/* Unknown PartyNumber */
97 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 0,
98 			party_number->str, party_number->length));
99 		break;
100 	case 1:	/* Public PartyNumber */
101 		ASN1_CALL(pos, rose_enc_NetworkPartyNumber(ctrl, pos, end,
102 			ASN1_CLASS_CONTEXT_SPECIFIC | 1, party_number->str, party_number->length,
103 			party_number->ton));
104 		break;
105 	case 2:	/* NSAP encoded PartyNumber */
106 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 2,
107 			party_number->str, party_number->length));
108 		break;
109 	case 3:	/* Data PartyNumber (Not used) */
110 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 3,
111 			party_number->str, party_number->length));
112 		break;
113 	case 4:	/* Telex PartyNumber (Not used) */
114 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 4,
115 			party_number->str, party_number->length));
116 		break;
117 	case 5:	/* Private PartyNumber */
118 		ASN1_CALL(pos, rose_enc_NetworkPartyNumber(ctrl, pos, end,
119 			ASN1_CLASS_CONTEXT_SPECIFIC | 5, party_number->str, party_number->length,
120 			party_number->ton));
121 		break;
122 	case 8:	/* National Standard PartyNumber (Not used) */
123 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 8,
124 			party_number->str, party_number->length));
125 		break;
126 	default:
127 		ASN1_ENC_ERROR(ctrl, "Unknown numbering plan");
128 		return NULL;
129 	}
130 
131 	return pos;
132 }
133 
134 /*!
135  * \brief Encode the PartySubaddress type.
136  *
137  * \param ctrl D channel controller for diagnostic messages or global options.
138  * \param pos Starting position to encode ASN.1 component.
139  * \param end End of ASN.1 encoding data buffer.
140  * \param party_subaddress
141  *
142  * \retval Start of the next ASN.1 component to encode on success.
143  * \retval NULL on error.
144  */
rose_enc_PartySubaddress(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct rosePartySubaddress * party_subaddress)145 unsigned char *rose_enc_PartySubaddress(struct pri *ctrl, unsigned char *pos,
146 	unsigned char *end, const struct rosePartySubaddress *party_subaddress)
147 {
148 	unsigned char *seq_len;
149 
150 	switch (party_subaddress->type) {
151 	case 0:	/* UserSpecified */
152 		ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, ASN1_TAG_SEQUENCE);
153 
154 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_TYPE_OCTET_STRING,
155 			party_subaddress->u.user_specified.information, party_subaddress->length));
156 		if (party_subaddress->u.user_specified.odd_count_present) {
157 			ASN1_CALL(pos, asn1_enc_boolean(pos, end, ASN1_TYPE_BOOLEAN,
158 				party_subaddress->u.user_specified.odd_count));
159 		}
160 
161 		ASN1_CONSTRUCTED_END(seq_len, pos, end);
162 		break;
163 	case 1:	/* NSAP */
164 		ASN1_CALL(pos, asn1_enc_string_bin(pos, end, ASN1_TYPE_OCTET_STRING,
165 			party_subaddress->u.nsap, party_subaddress->length));
166 		break;
167 	default:
168 		ASN1_ENC_ERROR(ctrl, "Unknown subaddress type");
169 		return NULL;
170 	}
171 
172 	return pos;
173 }
174 
175 /*!
176  * \brief Encode the Address type.
177  *
178  * \param ctrl D channel controller for diagnostic messages or global options.
179  * \param pos Starting position to encode ASN.1 component.
180  * \param end End of ASN.1 encoding data buffer.
181  * \param tag Component tag to identify the encoded component.
182  *   The tag should be ASN1_TAG_SEQUENCE unless the caller implicitly
183  *   tags it otherwise.
184  * \param address
185  *
186  * \retval Start of the next ASN.1 component to encode on success.
187  * \retval NULL on error.
188  */
rose_enc_Address(struct pri * ctrl,unsigned char * pos,unsigned char * end,unsigned tag,const struct roseAddress * address)189 unsigned char *rose_enc_Address(struct pri *ctrl, unsigned char *pos, unsigned char *end,
190 	unsigned tag, const struct roseAddress *address)
191 {
192 	unsigned char *seq_len;
193 
194 	ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, tag);
195 
196 	ASN1_CALL(pos, rose_enc_PartyNumber(ctrl, pos, end, &address->number));
197 	if (address->subaddress.length) {
198 		ASN1_CALL(pos, rose_enc_PartySubaddress(ctrl, pos, end, &address->subaddress));
199 	}
200 
201 	ASN1_CONSTRUCTED_END(seq_len, pos, end);
202 
203 	return pos;
204 }
205 
206 /*!
207  * \brief Encode the PresentedNumberUnscreened type.
208  *
209  * \param ctrl D channel controller for diagnostic messages or global options.
210  * \param pos Starting position to encode ASN.1 component.
211  * \param end End of ASN.1 encoding data buffer.
212  * \param party
213  *
214  * \retval Start of the next ASN.1 component to encode on success.
215  * \retval NULL on error.
216  */
rose_enc_PresentedNumberUnscreened(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct rosePresentedNumberUnscreened * party)217 unsigned char *rose_enc_PresentedNumberUnscreened(struct pri *ctrl, unsigned char *pos,
218 	unsigned char *end, const struct rosePresentedNumberUnscreened *party)
219 {
220 	unsigned char *seq_len;
221 
222 	switch (party->presentation) {
223 	case 0:	/* presentationAllowedNumber */
224 		/* EXPLICIT tag */
225 		ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 0);
226 		ASN1_CALL(pos, rose_enc_PartyNumber(ctrl, pos, end, &party->number));
227 		ASN1_CONSTRUCTED_END(seq_len, pos, end);
228 		break;
229 	case 1:	/* presentationRestricted */
230 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 1));
231 		break;
232 	case 2:	/* numberNotAvailableDueToInterworking */
233 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 2));
234 		break;
235 	case 3:	/* presentationRestrictedNumber */
236 		/* EXPLICIT tag */
237 		ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 3);
238 		ASN1_CALL(pos, rose_enc_PartyNumber(ctrl, pos, end, &party->number));
239 		ASN1_CONSTRUCTED_END(seq_len, pos, end);
240 		break;
241 	default:
242 		ASN1_ENC_ERROR(ctrl, "Unknown presentation type");
243 		return NULL;
244 	}
245 
246 	return pos;
247 }
248 
249 /*!
250  * \brief Encode the NumberScreened type.
251  *
252  * \param ctrl D channel controller for diagnostic messages or global options.
253  * \param pos Starting position to encode ASN.1 component.
254  * \param end End of ASN.1 encoding data buffer.
255  * \param tag Component tag to identify the encoded component.
256  *   The tag should be ASN1_TAG_SEQUENCE unless the caller implicitly
257  *   tags it otherwise.
258  * \param screened
259  *
260  * \retval Start of the next ASN.1 component to encode on success.
261  * \retval NULL on error.
262  */
rose_enc_NumberScreened(struct pri * ctrl,unsigned char * pos,unsigned char * end,unsigned tag,const struct roseNumberScreened * screened)263 unsigned char *rose_enc_NumberScreened(struct pri *ctrl, unsigned char *pos,
264 	unsigned char *end, unsigned tag, const struct roseNumberScreened *screened)
265 {
266 	unsigned char *seq_len;
267 
268 	ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, tag);
269 
270 	ASN1_CALL(pos, rose_enc_PartyNumber(ctrl, pos, end, &screened->number));
271 	ASN1_CALL(pos, asn1_enc_int(pos, end, ASN1_TYPE_ENUMERATED,
272 		screened->screening_indicator));
273 
274 	ASN1_CONSTRUCTED_END(seq_len, pos, end);
275 
276 	return pos;
277 }
278 
279 /*!
280  * \brief Encode the PresentedNumberScreened type.
281  *
282  * \param ctrl D channel controller for diagnostic messages or global options.
283  * \param pos Starting position to encode ASN.1 component.
284  * \param end End of ASN.1 encoding data buffer.
285  * \param party
286  *
287  * \retval Start of the next ASN.1 component to encode on success.
288  * \retval NULL on error.
289  */
rose_enc_PresentedNumberScreened(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct rosePresentedNumberScreened * party)290 unsigned char *rose_enc_PresentedNumberScreened(struct pri *ctrl, unsigned char *pos,
291 	unsigned char *end, const struct rosePresentedNumberScreened *party)
292 {
293 	switch (party->presentation) {
294 	case 0:	/* presentationAllowedNumber */
295 		ASN1_CALL(pos, rose_enc_NumberScreened(ctrl, pos, end,
296 			ASN1_CLASS_CONTEXT_SPECIFIC | 0, &party->screened));
297 		break;
298 	case 1:	/* presentationRestricted */
299 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 1));
300 		break;
301 	case 2:	/* numberNotAvailableDueToInterworking */
302 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 2));
303 		break;
304 	case 3:	/* presentationRestrictedNumber */
305 		ASN1_CALL(pos, rose_enc_NumberScreened(ctrl, pos, end,
306 			ASN1_CLASS_CONTEXT_SPECIFIC | 3, &party->screened));
307 		break;
308 	default:
309 		ASN1_ENC_ERROR(ctrl, "Unknown presentation type");
310 		return NULL;
311 	}
312 
313 	return pos;
314 }
315 
316 /*!
317  * \brief Encode the AddressScreened type.
318  *
319  * \param ctrl D channel controller for diagnostic messages or global options.
320  * \param pos Starting position to encode ASN.1 component.
321  * \param end End of ASN.1 encoding data buffer.
322  * \param tag Component tag to identify the encoded component.
323  *   The tag should be ASN1_TAG_SEQUENCE unless the caller implicitly
324  *   tags it otherwise.
325  * \param screened
326  *
327  * \retval Start of the next ASN.1 component to encode on success.
328  * \retval NULL on error.
329  */
rose_enc_AddressScreened(struct pri * ctrl,unsigned char * pos,unsigned char * end,unsigned tag,const struct roseAddressScreened * screened)330 unsigned char *rose_enc_AddressScreened(struct pri *ctrl, unsigned char *pos,
331 	unsigned char *end, unsigned tag, const struct roseAddressScreened *screened)
332 {
333 	unsigned char *seq_len;
334 
335 	ASN1_CONSTRUCTED_BEGIN(seq_len, pos, end, tag);
336 
337 	ASN1_CALL(pos, rose_enc_PartyNumber(ctrl, pos, end, &screened->number));
338 	ASN1_CALL(pos, asn1_enc_int(pos, end, ASN1_TYPE_ENUMERATED,
339 		screened->screening_indicator));
340 	if (screened->subaddress.length) {
341 		ASN1_CALL(pos, rose_enc_PartySubaddress(ctrl, pos, end, &screened->subaddress));
342 	}
343 
344 	ASN1_CONSTRUCTED_END(seq_len, pos, end);
345 
346 	return pos;
347 }
348 
349 /*!
350  * \brief Encode the PresentedAddressScreened type.
351  *
352  * \param ctrl D channel controller for diagnostic messages or global options.
353  * \param pos Starting position to encode ASN.1 component.
354  * \param end End of ASN.1 encoding data buffer.
355  * \param party
356  *
357  * \retval Start of the next ASN.1 component to encode on success.
358  * \retval NULL on error.
359  */
rose_enc_PresentedAddressScreened(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct rosePresentedAddressScreened * party)360 unsigned char *rose_enc_PresentedAddressScreened(struct pri *ctrl, unsigned char *pos,
361 	unsigned char *end, const struct rosePresentedAddressScreened *party)
362 {
363 	switch (party->presentation) {
364 	case 0:	/* presentationAllowedAddress */
365 		ASN1_CALL(pos, rose_enc_AddressScreened(ctrl, pos, end,
366 			ASN1_CLASS_CONTEXT_SPECIFIC | 0, &party->screened));
367 		break;
368 	case 1:	/* presentationRestricted */
369 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 1));
370 		break;
371 	case 2:	/* numberNotAvailableDueToInterworking */
372 		ASN1_CALL(pos, asn1_enc_null(pos, end, ASN1_CLASS_CONTEXT_SPECIFIC | 2));
373 		break;
374 	case 3:	/* presentationRestrictedAddress */
375 		ASN1_CALL(pos, rose_enc_AddressScreened(ctrl, pos, end,
376 			ASN1_CLASS_CONTEXT_SPECIFIC | 3, &party->screened));
377 		break;
378 	default:
379 		ASN1_ENC_ERROR(ctrl, "Unknown presentation type");
380 		return NULL;
381 	}
382 
383 	return pos;
384 }
385 
386 /*!
387  * \internal
388  * \brief Decode the NumberDigits PartyNumber argument parameters.
389  *
390  * \param ctrl D channel controller for any diagnostic messages.
391  * \param name Field name
392  * \param tag Component tag that identified this production.
393  * \param pos Starting position of the ASN.1 component length.
394  * \param end End of ASN.1 decoding data buffer.
395  * \param party_number Parameter storage to fill.
396  *
397  * \retval Start of the next ASN.1 component on success.
398  * \retval NULL on error.
399  */
rose_dec_NumberDigits(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartyNumber * party_number)400 static const unsigned char *rose_dec_NumberDigits(struct pri *ctrl, const char *name,
401 	unsigned tag, const unsigned char *pos, const unsigned char *end,
402 	struct rosePartyNumber *party_number)
403 {
404 	size_t str_len;
405 
406 	ASN1_CALL(pos, asn1_dec_string_max(ctrl, name, tag, pos, end,
407 		sizeof(party_number->str), party_number->str, &str_len));
408 	party_number->length = str_len;
409 
410 	return pos;
411 }
412 
413 /*!
414  * \internal
415  * \brief Decode the NSAP PartyNumber argument parameters.
416  *
417  * \param ctrl D channel controller for any diagnostic messages.
418  * \param name Field name
419  * \param tag Component tag that identified this production.
420  * \param pos Starting position of the ASN.1 component length.
421  * \param end End of ASN.1 decoding data buffer.
422  * \param party_number Parameter storage to fill.
423  *
424  * \retval Start of the next ASN.1 component on success.
425  * \retval NULL on error.
426  */
rose_dec_NSAPPartyNumber(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartyNumber * party_number)427 static const unsigned char *rose_dec_NSAPPartyNumber(struct pri *ctrl, const char *name,
428 	unsigned tag, const unsigned char *pos, const unsigned char *end,
429 	struct rosePartyNumber *party_number)
430 {
431 	size_t str_len;
432 
433 	ASN1_CALL(pos, asn1_dec_string_bin(ctrl, name, tag, pos, end,
434 		sizeof(party_number->str), party_number->str, &str_len));
435 	party_number->length = str_len;
436 
437 	return pos;
438 }
439 
440 /*!
441  * \internal
442  * \brief Decode the public or private network PartyNumber argument parameters.
443  *
444  * \param ctrl D channel controller for any diagnostic messages.
445  * \param name Field name
446  * \param tag Component tag that identified this production.
447  * \param pos Starting position of the ASN.1 component length.
448  * \param end End of ASN.1 decoding data buffer.
449  * \param party_number Parameter storage to fill.
450  *
451  * \retval Start of the next ASN.1 component on success.
452  * \retval NULL on error.
453  */
rose_dec_NetworkPartyNumber(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartyNumber * party_number)454 static const unsigned char *rose_dec_NetworkPartyNumber(struct pri *ctrl,
455 	const char *name, unsigned tag, const unsigned char *pos, const unsigned char *end,
456 	struct rosePartyNumber *party_number)
457 {
458 	int32_t value;
459 	int length;
460 	int seq_offset;
461 	const unsigned char *seq_end;
462 
463 	if (ctrl->debug & PRI_DEBUG_APDU) {
464 		pri_message(ctrl, "  %s %s\n", name, asn1_tag2str(tag));
465 	}
466 	ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
467 	ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
468 
469 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
470 	ASN1_CHECK_TAG(ctrl, tag, tag, ASN1_TYPE_ENUMERATED);
471 	ASN1_CALL(pos, asn1_dec_int(ctrl, "typeOfNumber", tag, pos, seq_end, &value));
472 	party_number->ton = value;
473 
474 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
475 	ASN1_CHECK_TAG(ctrl, tag, tag & ~ASN1_PC_MASK, ASN1_TYPE_NUMERIC_STRING);
476 	ASN1_CALL(pos, rose_dec_NumberDigits(ctrl, "numberDigits", tag, pos, seq_end,
477 		party_number));
478 
479 	ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
480 
481 	return pos;
482 }
483 
484 /*!
485  * \brief Decode the PartyNumber argument parameters.
486  *
487  * \param ctrl D channel controller for any diagnostic messages.
488  * \param name Field name
489  * \param tag Component tag that identified this production.
490  * \param pos Starting position of the ASN.1 component length.
491  * \param end End of ASN.1 decoding data buffer.
492  * \param party_number Parameter storage to fill.
493  *
494  * \retval Start of the next ASN.1 component on success.
495  * \retval NULL on error.
496  */
rose_dec_PartyNumber(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartyNumber * party_number)497 const unsigned char *rose_dec_PartyNumber(struct pri *ctrl, const char *name,
498 	unsigned tag, const unsigned char *pos, const unsigned char *end,
499 	struct rosePartyNumber *party_number)
500 {
501 	if (ctrl->debug & PRI_DEBUG_APDU) {
502 		pri_message(ctrl, "  %s PartyNumber\n", name);
503 	}
504 	party_number->ton = 0;	/* unknown */
505 	switch (tag & ~ASN1_PC_MASK) {
506 	case ASN1_CLASS_CONTEXT_SPECIFIC | 0:
507 		party_number->plan = 0;	/* Unknown PartyNumber */
508 		ASN1_CALL(pos, rose_dec_NumberDigits(ctrl, "unknownPartyNumber", tag, pos, end,
509 			party_number));
510 		break;
511 	case ASN1_CLASS_CONTEXT_SPECIFIC | 1:
512 		/* Must be constructed but we will not check for it for simplicity. */
513 		party_number->plan = 1;	/* Public PartyNumber */
514 		ASN1_CALL(pos, rose_dec_NetworkPartyNumber(ctrl, "publicPartyNumber", tag, pos,
515 			end, party_number));
516 		break;
517 	case ASN1_CLASS_CONTEXT_SPECIFIC | 2:
518 		party_number->plan = 2;	/* NSAP encoded PartyNumber */
519 		ASN1_CALL(pos, rose_dec_NSAPPartyNumber(ctrl, "nsapEncodedPartyNumber", tag, pos,
520 			end, party_number));
521 		break;
522 	case ASN1_CLASS_CONTEXT_SPECIFIC | 3:
523 		party_number->plan = 3;	/* Data PartyNumber (Not used) */
524 		ASN1_CALL(pos, rose_dec_NumberDigits(ctrl, "dataPartyNumber", tag, pos, end,
525 			party_number));
526 		break;
527 	case ASN1_CLASS_CONTEXT_SPECIFIC | 4:
528 		party_number->plan = 4;	/* Telex PartyNumber (Not used) */
529 		ASN1_CALL(pos, rose_dec_NumberDigits(ctrl, "telexPartyNumber", tag, pos, end,
530 			party_number));
531 		break;
532 	case ASN1_CLASS_CONTEXT_SPECIFIC | 5:
533 		/* Must be constructed but we will not check for it for simplicity. */
534 		party_number->plan = 5;	/* Private PartyNumber */
535 		ASN1_CALL(pos, rose_dec_NetworkPartyNumber(ctrl, "privatePartyNumber", tag, pos,
536 			end, party_number));
537 		break;
538 	case ASN1_CLASS_CONTEXT_SPECIFIC | 8:
539 		party_number->plan = 8;	/* National Standard PartyNumber (Not used) */
540 		ASN1_CALL(pos, rose_dec_NumberDigits(ctrl, "nationalStandardPartyNumber", tag,
541 			pos, end, party_number));
542 		break;
543 	default:
544 		ASN1_DID_NOT_EXPECT_TAG(ctrl, tag);
545 		return NULL;
546 	}
547 
548 	return pos;
549 }
550 
551 /*!
552  * \internal
553  * \brief Decode the User PartySubaddress argument parameters.
554  *
555  * \param ctrl D channel controller for any diagnostic messages.
556  * \param name Field name
557  * \param tag Component tag that identified this production.
558  * \param pos Starting position of the ASN.1 component length.
559  * \param end End of ASN.1 decoding data buffer.
560  * \param party_subaddress Parameter storage to fill.
561  *
562  * \retval Start of the next ASN.1 component on success.
563  * \retval NULL on error.
564  */
rose_dec_UserSubaddress(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartySubaddress * party_subaddress)565 static const unsigned char *rose_dec_UserSubaddress(struct pri *ctrl, const char *name,
566 	unsigned tag, const unsigned char *pos, const unsigned char *end,
567 	struct rosePartySubaddress *party_subaddress)
568 {
569 	size_t str_len;
570 	int32_t odd_count;
571 	int length;
572 	int seq_offset;
573 	const unsigned char *seq_end;
574 
575 	party_subaddress->type = 0;	/* UserSpecified */
576 
577 	if (ctrl->debug & PRI_DEBUG_APDU) {
578 		pri_message(ctrl, "  %s UserSpecified %s\n", name, asn1_tag2str(tag));
579 	}
580 	ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
581 	ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
582 
583 	/* SubaddressInformation */
584 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
585 	ASN1_CHECK_TAG(ctrl, tag, tag & ~ASN1_PC_MASK, ASN1_TYPE_OCTET_STRING);
586 	ASN1_CALL(pos, asn1_dec_string_bin(ctrl, "subaddressInformation", tag, pos, seq_end,
587 		sizeof(party_subaddress->u.user_specified.information),
588 		party_subaddress->u.user_specified.information, &str_len));
589 	party_subaddress->length = str_len;
590 
591 	if (pos < seq_end && *pos != ASN1_INDEF_TERM) {
592 		/*
593 		 * The optional odd count indicator must be present since there
594 		 * is something left.
595 		 */
596 		ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
597 		ASN1_CHECK_TAG(ctrl, tag, tag, ASN1_TYPE_BOOLEAN);
598 		ASN1_CALL(pos, asn1_dec_boolean(ctrl, "oddCount", tag, pos, seq_end,
599 			&odd_count));
600 		party_subaddress->u.user_specified.odd_count = odd_count;
601 		party_subaddress->u.user_specified.odd_count_present = 1;
602 	} else {
603 		party_subaddress->u.user_specified.odd_count = 0;
604 		party_subaddress->u.user_specified.odd_count_present = 0;
605 	}
606 
607 	ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
608 
609 	return pos;
610 }
611 
612 /*!
613  * \internal
614  * \brief Decode the NSAP PartySubaddress argument parameters.
615  *
616  * \param ctrl D channel controller for any diagnostic messages.
617  * \param name Field name
618  * \param tag Component tag that identified this production.
619  * \param pos Starting position of the ASN.1 component length.
620  * \param end End of ASN.1 decoding data buffer.
621  * \param party_subaddress Parameter storage to fill.
622  *
623  * \retval Start of the next ASN.1 component on success.
624  * \retval NULL on error.
625  */
rose_dec_NSAPSubaddress(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartySubaddress * party_subaddress)626 static const unsigned char *rose_dec_NSAPSubaddress(struct pri *ctrl, const char *name,
627 	unsigned tag, const unsigned char *pos, const unsigned char *end,
628 	struct rosePartySubaddress *party_subaddress)
629 {
630 	size_t str_len;
631 
632 	party_subaddress->type = 1;	/* NSAP */
633 
634 	ASN1_CALL(pos, asn1_dec_string_bin(ctrl, name, tag, pos, end,
635 		sizeof(party_subaddress->u.nsap), party_subaddress->u.nsap, &str_len));
636 	party_subaddress->length = str_len;
637 
638 	return pos;
639 }
640 
641 /*!
642  * \brief Decode the PartySubaddress argument parameters.
643  *
644  * \param ctrl D channel controller for any diagnostic messages.
645  * \param name Field name
646  * \param tag Component tag that identified this production.
647  * \param pos Starting position of the ASN.1 component length.
648  * \param end End of ASN.1 decoding data buffer.
649  * \param party_subaddress Parameter storage to fill.
650  *
651  * \retval Start of the next ASN.1 component on success.
652  * \retval NULL on error.
653  */
rose_dec_PartySubaddress(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePartySubaddress * party_subaddress)654 const unsigned char *rose_dec_PartySubaddress(struct pri *ctrl, const char *name,
655 	unsigned tag, const unsigned char *pos, const unsigned char *end,
656 	struct rosePartySubaddress *party_subaddress)
657 {
658 	if (ctrl->debug & PRI_DEBUG_APDU) {
659 		pri_message(ctrl, "  %s PartySubaddress\n", name);
660 	}
661 	switch (tag) {
662 	case ASN1_TAG_SEQUENCE:
663 		ASN1_CALL(pos, rose_dec_UserSubaddress(ctrl, "user", tag, pos, end,
664 			party_subaddress));
665 		break;
666 	case ASN1_TYPE_OCTET_STRING:
667 	case ASN1_TYPE_OCTET_STRING | ASN1_PC_CONSTRUCTED:
668 		ASN1_CALL(pos, rose_dec_NSAPSubaddress(ctrl, "nsap", tag, pos, end,
669 			party_subaddress));
670 		break;
671 	default:
672 		ASN1_DID_NOT_EXPECT_TAG(ctrl, tag);
673 		return NULL;
674 	}
675 
676 	return pos;
677 }
678 
679 /*!
680  * \brief Decode the Address argument parameters.
681  *
682  * \param ctrl D channel controller for any diagnostic messages.
683  * \param name Field name
684  * \param tag Component tag that identified this production.
685  * \param pos Starting position of the ASN.1 component length.
686  * \param end End of ASN.1 decoding data buffer.
687  * \param address Parameter storage to fill.
688  *
689  * \retval Start of the next ASN.1 component on success.
690  * \retval NULL on error.
691  */
rose_dec_Address(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct roseAddress * address)692 const unsigned char *rose_dec_Address(struct pri *ctrl, const char *name, unsigned tag,
693 	const unsigned char *pos, const unsigned char *end, struct roseAddress *address)
694 {
695 	int length;
696 	int seq_offset;
697 	const unsigned char *seq_end;
698 
699 	if (ctrl->debug & PRI_DEBUG_APDU) {
700 		pri_message(ctrl, "  %s Address %s\n", name, asn1_tag2str(tag));
701 	}
702 	ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
703 	ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
704 
705 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
706 	ASN1_CALL(pos, rose_dec_PartyNumber(ctrl, "partyNumber", tag, pos, seq_end,
707 		&address->number));
708 
709 	if (pos < seq_end && *pos != ASN1_INDEF_TERM) {
710 		/* The optional subaddress must be present since there is something left. */
711 		ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
712 		ASN1_CALL(pos, rose_dec_PartySubaddress(ctrl, "partySubaddress", tag, pos,
713 			seq_end, &address->subaddress));
714 	} else {
715 		address->subaddress.length = 0;	/* Subaddress not present */
716 	}
717 
718 	ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
719 
720 	return pos;
721 }
722 
723 /*!
724  * \brief Decode the PresentedNumberUnscreened argument parameters.
725  *
726  * \param ctrl D channel controller for any diagnostic messages.
727  * \param name Field name
728  * \param tag Component tag that identified this production.
729  * \param pos Starting position of the ASN.1 component length.
730  * \param end End of ASN.1 decoding data buffer.
731  * \param party Parameter storage to fill.
732  *
733  * \retval Start of the next ASN.1 component on success.
734  * \retval NULL on error.
735  */
rose_dec_PresentedNumberUnscreened(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePresentedNumberUnscreened * party)736 const unsigned char *rose_dec_PresentedNumberUnscreened(struct pri *ctrl,
737 	const char *name, unsigned tag, const unsigned char *pos, const unsigned char *end,
738 	struct rosePresentedNumberUnscreened *party)
739 {
740 	int length;
741 	int seq_offset;
742 	const unsigned char *seq_end;
743 
744 	if (ctrl->debug & PRI_DEBUG_APDU) {
745 		pri_message(ctrl, "  %s PresentedNumberUnscreened\n", name);
746 	}
747 	switch (tag) {
748 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 0:
749 		if (ctrl->debug & PRI_DEBUG_APDU) {
750 			pri_message(ctrl, "  Explicit %s\n", asn1_tag2str(tag));
751 		}
752 		ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
753 		ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
754 
755 		ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
756 		party->presentation = 0;	/* presentationAllowedNumber */
757 		ASN1_CALL(pos, rose_dec_PartyNumber(ctrl, "presentationAllowedNumber", tag, pos,
758 			seq_end, &party->number));
759 
760 		ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
761 		break;
762 	case ASN1_CLASS_CONTEXT_SPECIFIC | 1:
763 		party->presentation = 1;	/* presentationRestricted */
764 		ASN1_CALL(pos, asn1_dec_null(ctrl, "presentationRestricted", tag, pos, end));
765 		break;
766 	case ASN1_CLASS_CONTEXT_SPECIFIC | 2:
767 		party->presentation = 2;	/* numberNotAvailableDueToInterworking */
768 		ASN1_CALL(pos, asn1_dec_null(ctrl, "numberNotAvailableDueToInterworking", tag,
769 			pos, end));
770 		break;
771 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 3:
772 		if (ctrl->debug & PRI_DEBUG_APDU) {
773 			pri_message(ctrl, "  Explicit %s\n", asn1_tag2str(tag));
774 		}
775 		ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
776 		ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
777 
778 		ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
779 		party->presentation = 3;	/* presentationRestrictedNumber */
780 		ASN1_CALL(pos, rose_dec_PartyNumber(ctrl, "presentationRestrictedNumber", tag,
781 			pos, seq_end, &party->number));
782 
783 		ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
784 		break;
785 	default:
786 		ASN1_DID_NOT_EXPECT_TAG(ctrl, tag);
787 		return NULL;
788 	}
789 
790 	return pos;
791 }
792 
793 /*!
794  * \brief Decode the NumberScreened argument parameters.
795  *
796  * \param ctrl D channel controller for any diagnostic messages.
797  * \param name Field name
798  * \param tag Component tag that identified this production.
799  * \param pos Starting position of the ASN.1 component length.
800  * \param end End of ASN.1 decoding data buffer.
801  * \param screened Parameter storage to fill.
802  *
803  * \retval Start of the next ASN.1 component on success.
804  * \retval NULL on error.
805  */
rose_dec_NumberScreened(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct roseNumberScreened * screened)806 const unsigned char *rose_dec_NumberScreened(struct pri *ctrl, const char *name,
807 	unsigned tag, const unsigned char *pos, const unsigned char *end,
808 	struct roseNumberScreened *screened)
809 {
810 	int32_t value;
811 	int length;
812 	int seq_offset;
813 	const unsigned char *seq_end;
814 
815 	if (ctrl->debug & PRI_DEBUG_APDU) {
816 		pri_message(ctrl, "  %s NumberScreened %s\n", name, asn1_tag2str(tag));
817 	}
818 	ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
819 	ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
820 
821 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
822 	ASN1_CALL(pos, rose_dec_PartyNumber(ctrl, "partyNumber", tag, pos, seq_end,
823 		&screened->number));
824 
825 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
826 	ASN1_CHECK_TAG(ctrl, tag, tag, ASN1_TYPE_ENUMERATED);
827 	ASN1_CALL(pos, asn1_dec_int(ctrl, "screeningIndicator", tag, pos, seq_end, &value));
828 	screened->screening_indicator = value;
829 
830 	ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
831 
832 	return pos;
833 }
834 
835 /*!
836  * \brief Decode the PresentedNumberScreened argument parameters.
837  *
838  * \param ctrl D channel controller for any diagnostic messages.
839  * \param name Field name
840  * \param tag Component tag that identified this production.
841  * \param pos Starting position of the ASN.1 component length.
842  * \param end End of ASN.1 decoding data buffer.
843  * \param party Parameter storage to fill.
844  *
845  * \retval Start of the next ASN.1 component on success.
846  * \retval NULL on error.
847  */
rose_dec_PresentedNumberScreened(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePresentedNumberScreened * party)848 const unsigned char *rose_dec_PresentedNumberScreened(struct pri *ctrl, const char *name,
849 	unsigned tag, const unsigned char *pos, const unsigned char *end,
850 	struct rosePresentedNumberScreened *party)
851 {
852 	if (ctrl->debug & PRI_DEBUG_APDU) {
853 		pri_message(ctrl, "  %s PresentedNumberScreened\n", name);
854 	}
855 	switch (tag) {
856 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 0:
857 		party->presentation = 0;	/* presentationAllowedNumber */
858 		ASN1_CALL(pos, rose_dec_NumberScreened(ctrl, "presentationAllowedNumber", tag,
859 			pos, end, &party->screened));
860 		break;
861 	case ASN1_CLASS_CONTEXT_SPECIFIC | 1:
862 		party->presentation = 1;	/* presentationRestricted */
863 		ASN1_CALL(pos, asn1_dec_null(ctrl, "presentationRestricted", tag, pos, end));
864 		break;
865 	case ASN1_CLASS_CONTEXT_SPECIFIC | 2:
866 		party->presentation = 2;	/* numberNotAvailableDueToInterworking */
867 		ASN1_CALL(pos, asn1_dec_null(ctrl, "numberNotAvailableDueToInterworking", tag,
868 			pos, end));
869 		break;
870 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 3:
871 		party->presentation = 3;	/* presentationRestrictedNumber */
872 		ASN1_CALL(pos, rose_dec_NumberScreened(ctrl, "presentationRestrictedNumber", tag,
873 			pos, end, &party->screened));
874 		break;
875 	default:
876 		ASN1_DID_NOT_EXPECT_TAG(ctrl, tag);
877 		return NULL;
878 	}
879 
880 	return pos;
881 }
882 
883 /*!
884  * \brief Decode the AddressScreened argument parameters.
885  *
886  * \param ctrl D channel controller for any diagnostic messages.
887  * \param name Field name
888  * \param tag Component tag that identified this production.
889  * \param pos Starting position of the ASN.1 component length.
890  * \param end End of ASN.1 decoding data buffer.
891  * \param screened Parameter storage to fill.
892  *
893  * \retval Start of the next ASN.1 component on success.
894  * \retval NULL on error.
895  */
rose_dec_AddressScreened(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct roseAddressScreened * screened)896 const unsigned char *rose_dec_AddressScreened(struct pri *ctrl, const char *name,
897 	unsigned tag, const unsigned char *pos, const unsigned char *end,
898 	struct roseAddressScreened *screened)
899 {
900 	int32_t value;
901 	int length;
902 	int seq_offset;
903 	const unsigned char *seq_end;
904 
905 	if (ctrl->debug & PRI_DEBUG_APDU) {
906 		pri_message(ctrl, "  %s AddressScreened %s\n", name, asn1_tag2str(tag));
907 	}
908 	ASN1_CALL(pos, asn1_dec_length(pos, end, &length));
909 	ASN1_END_SETUP(seq_end, seq_offset, length, pos, end);
910 
911 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
912 	ASN1_CALL(pos, rose_dec_PartyNumber(ctrl, "partyNumber", tag, pos, seq_end,
913 		&screened->number));
914 
915 	ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
916 	ASN1_CHECK_TAG(ctrl, tag, tag, ASN1_TYPE_ENUMERATED);
917 	ASN1_CALL(pos, asn1_dec_int(ctrl, "screeningIndicator", tag, pos, seq_end, &value));
918 	screened->screening_indicator = value;
919 
920 	if (pos < seq_end && *pos != ASN1_INDEF_TERM) {
921 		/* The optional subaddress must be present since there is something left. */
922 		ASN1_CALL(pos, asn1_dec_tag(pos, seq_end, &tag));
923 		ASN1_CALL(pos, rose_dec_PartySubaddress(ctrl, "partySubaddress", tag, pos,
924 			seq_end, &screened->subaddress));
925 	} else {
926 		screened->subaddress.length = 0;	/* Subaddress not present */
927 	}
928 
929 	ASN1_END_FIXUP(ctrl, pos, seq_offset, seq_end, end);
930 
931 	return pos;
932 }
933 
934 /*!
935  * \brief Decode the PresentedAddressScreened argument parameters.
936  *
937  * \param ctrl D channel controller for any diagnostic messages.
938  * \param name Field name
939  * \param tag Component tag that identified this production.
940  * \param pos Starting position of the ASN.1 component length.
941  * \param end End of ASN.1 decoding data buffer.
942  * \param party Parameter storage to fill.
943  *
944  * \retval Start of the next ASN.1 component on success.
945  * \retval NULL on error.
946  */
rose_dec_PresentedAddressScreened(struct pri * ctrl,const char * name,unsigned tag,const unsigned char * pos,const unsigned char * end,struct rosePresentedAddressScreened * party)947 const unsigned char *rose_dec_PresentedAddressScreened(struct pri *ctrl,
948 	const char *name, unsigned tag, const unsigned char *pos, const unsigned char *end,
949 	struct rosePresentedAddressScreened *party)
950 {
951 	if (ctrl->debug & PRI_DEBUG_APDU) {
952 		pri_message(ctrl, "  %s PresentedAddressScreened\n", name);
953 	}
954 	switch (tag) {
955 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 0:
956 		party->presentation = 0;	/* presentationAllowedAddress */
957 		ASN1_CALL(pos, rose_dec_AddressScreened(ctrl, "presentationAllowedAddress", tag,
958 			pos, end, &party->screened));
959 		break;
960 	case ASN1_CLASS_CONTEXT_SPECIFIC | 1:
961 		party->presentation = 1;	/* presentationRestricted */
962 		ASN1_CALL(pos, asn1_dec_null(ctrl, "presentationRestricted", tag, pos, end));
963 		break;
964 	case ASN1_CLASS_CONTEXT_SPECIFIC | 2:
965 		party->presentation = 2;	/* numberNotAvailableDueToInterworking */
966 		ASN1_CALL(pos, asn1_dec_null(ctrl, "numberNotAvailableDueToInterworking", tag,
967 			pos, end));
968 		break;
969 	case ASN1_CLASS_CONTEXT_SPECIFIC | ASN1_PC_CONSTRUCTED | 3:
970 		party->presentation = 3;	/* presentationRestrictedAddress */
971 		ASN1_CALL(pos, rose_dec_AddressScreened(ctrl, "presentationRestrictedAddress",
972 			tag, pos, end, &party->screened));
973 		break;
974 	default:
975 		ASN1_DID_NOT_EXPECT_TAG(ctrl, tag);
976 		return NULL;
977 	}
978 
979 	return pos;
980 }
981 
982 /* ------------------------------------------------------------------- */
983 /* end rose_address.c */
984