aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/minitasn1/decoding.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/minitasn1/decoding.c')
-rw-r--r--src/daemon/https/minitasn1/decoding.c1203
1 files changed, 13 insertions, 1190 deletions
diff --git a/src/daemon/https/minitasn1/decoding.c b/src/daemon/https/minitasn1/decoding.c
index 27586b5e..fcee0797 100644
--- a/src/daemon/https/minitasn1/decoding.c
+++ b/src/daemon/https/minitasn1/decoding.c
@@ -34,7 +34,7 @@
34#include "element.h" 34#include "element.h"
35 35
36 36
37void 37static void
38MHD__asn1_error_description_tag_error (node_asn * node, char *ErrorDescription) 38MHD__asn1_error_description_tag_error (node_asn * node, char *ErrorDescription)
39{ 39{
40 40
@@ -207,7 +207,7 @@ MHD__asn1_get_octet_der (const unsigned char *der, int der_len,
207 207
208/* Returns ASN1_SUCCESS on success or an error code on error. 208/* Returns ASN1_SUCCESS on success or an error code on error.
209 */ 209 */
210int 210static int
211MHD__asn1_get_time_der (const unsigned char *der, int der_len, int *ret_len, 211MHD__asn1_get_time_der (const unsigned char *der, int der_len, int *ret_len,
212 char *str, int str_size) 212 char *str, int str_size)
213{ 213{
@@ -227,7 +227,7 @@ MHD__asn1_get_time_der (const unsigned char *der, int der_len, int *ret_len,
227 227
228 228
229 229
230void 230static void
231MHD__asn1_get_objectid_der (const unsigned char *der, int der_len, int *ret_len, 231MHD__asn1_get_objectid_der (const unsigned char *der, int der_len, int *ret_len,
232 char *str, int str_size) 232 char *str, int str_size)
233{ 233{
@@ -313,7 +313,7 @@ MHD__asn1_get_bit_der (const unsigned char *der, int der_len,
313 313
314 314
315 315
316int 316static int
317MHD__asn1_extract_tag_der (node_asn * node, const unsigned char *der, int der_len, 317MHD__asn1_extract_tag_der (node_asn * node, const unsigned char *der, int der_len,
318 int *ret_len) 318 int *ret_len)
319{ 319{
@@ -506,7 +506,7 @@ MHD__asn1_extract_tag_der (node_asn * node, const unsigned char *der, int der_le
506} 506}
507 507
508 508
509int 509static int
510MHD__asn1_delete_not_used (node_asn * node) 510MHD__asn1_delete_not_used (node_asn * node)
511{ 511{
512 node_asn *p, *p2; 512 node_asn *p, *p2;
@@ -566,7 +566,7 @@ MHD__asn1_delete_not_used (node_asn * node)
566} 566}
567 567
568 568
569MHD__asn1_retCode 569static MHD__asn1_retCode
570MHD__asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len) 570MHD__asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len)
571{ 571{
572 int len2, len3, counter, counter2, counter_end, tot_len, indefinite; 572 int len2, len3, counter, counter2, counter_end, tot_len, indefinite;
@@ -680,7 +680,7 @@ MHD__asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len)
680} 680}
681 681
682 682
683MHD__asn1_retCode 683static MHD__asn1_retCode
684MHD__asn1_get_indefinite_length_string (const unsigned char *der, int *len) 684MHD__asn1_get_indefinite_length_string (const unsigned char *der, int *len)
685{ 685{
686 int len2, len3, counter, indefinite; 686 int len2, len3, counter, indefinite;
@@ -789,7 +789,7 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
789 if (p->type & CONST_SET) 789 if (p->type & CONST_SET)
790 { 790 {
791 p2 = MHD__asn1_find_up (p); 791 p2 = MHD__asn1_find_up (p);
792 len2 = strtol (p2->value, NULL, 10); 792 len2 = strtol ((const char*) p2->value, NULL, 10);
793 if (len2 == -1) 793 if (len2 == -1)
794 { 794 {
795 if (!der[counter] && !der[counter + 1]) 795 if (!der[counter] && !der[counter + 1])
@@ -852,7 +852,7 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
852 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT)) 852 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
853 { 853 {
854 p2 = MHD__asn1_find_up (p); 854 p2 = MHD__asn1_find_up (p);
855 len2 = strtol (p2->value, NULL, 10); 855 len2 = strtol ((const char*) p2->value, NULL, 10);
856 if (counter == len2) 856 if (counter == len2)
857 { 857 {
858 if (p->right) 858 if (p->right)
@@ -917,7 +917,7 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
917 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT)) 917 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
918 { 918 {
919 p2 = MHD__asn1_find_up (p); 919 p2 = MHD__asn1_find_up (p);
920 len2 = strtol (p2->value, NULL, 10); 920 len2 = strtol ((const char*) p2->value, NULL, 10);
921 if ((len2 != -1) && (counter > len2)) 921 if ((len2 != -1) && (counter > len2))
922 ris = ASN1_TAG_ERROR; 922 ris = ASN1_TAG_ERROR;
923 } 923 }
@@ -1045,7 +1045,7 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
1045 case TYPE_SET: 1045 case TYPE_SET:
1046 if (move == UP) 1046 if (move == UP)
1047 { 1047 {
1048 len2 = strtol (p->value, NULL, 10); 1048 len2 = strtol ( (const char*) p->value, NULL, 10);
1049 MHD__asn1_set_value (p, NULL, 0); 1049 MHD__asn1_set_value (p, NULL, 0);
1050 if (len2 == -1) 1050 if (len2 == -1)
1051 { /* indefinite length method */ 1051 { /* indefinite length method */
@@ -1113,7 +1113,7 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
1113 case TYPE_SET_OF: 1113 case TYPE_SET_OF:
1114 if (move == UP) 1114 if (move == UP)
1115 { 1115 {
1116 len2 = strtol (p->value, NULL, 10); 1116 len2 = strtol ( (const char*) p->value, NULL, 10);
1117 if (len2 == -1) 1117 if (len2 == -1)
1118 { /* indefinite length method */ 1118 { /* indefinite length method */
1119 if ((counter + 2) > len) 1119 if ((counter + 2) > len)
@@ -1296,800 +1296,6 @@ MHD__asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
1296} 1296}
1297 1297
1298 1298
1299#define FOUND 1
1300#define SAME_BRANCH 2
1301#define OTHER_BRANCH 3
1302#define EXIT 4
1303
1304/**
1305 * MHD__asn1_der_decoding_element - Fill the element named ELEMENTNAME of the structure STRUCTURE with values of a DER encoding string.
1306 * @structure: pointer to an ASN1 structure
1307 * @elementName: name of the element to fill
1308 * @ider: vector that contains the DER encoding of the whole structure.
1309 * @len: number of bytes of *der: der[0]..der[len-1]
1310 * @errorDescription: null-terminated string contains details when an
1311 * error occurred.
1312 *
1313 * Fill the element named ELEMENTNAME with values of a DER encoding
1314 * string. The sructure must just be created with function
1315 * 'create_stucture'. The DER vector must contain the encoding
1316 * string of the whole STRUCTURE. If an error occurs during the
1317 * decoding procedure, the *STRUCTURE is deleted and set equal to
1318 * %ASN1_TYPE_EMPTY.
1319 *
1320 * Returns:
1321 *
1322 * ASN1_SUCCESS: DER encoding OK.
1323 *
1324 * ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE_EMPTY or
1325 * elementName == NULL.
1326 *
1327 * ASN1_TAG_ERROR,ASN1_DER_ERROR: The der encoding doesn't match
1328 * the structure STRUCTURE. *ELEMENT deleted.
1329 *
1330 **/
1331MHD__asn1_retCode
1332MHD__asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName,
1333 const void *ider, int len, char *errorDescription)
1334{
1335 node_asn *node, *p, *p2, *p3, *nodeFound = ASN1_TYPE_EMPTY;
1336 char temp[128], currentName[MAX_NAME_SIZE * 10], *dot_p, *char_p;
1337 int nameLen = MAX_NAME_SIZE * 10 - 1, state;
1338 int counter, len2, len3, len4, move, ris, tlen;
1339 unsigned char class, *temp2;
1340 unsigned long tag;
1341 int indefinite, result;
1342 const unsigned char *der = ider;
1343
1344 node = *structure;
1345
1346 if (node == ASN1_TYPE_EMPTY)
1347 return ASN1_ELEMENT_NOT_FOUND;
1348
1349 if (elementName == NULL)
1350 {
1351 MHD__asn1_delete_structure (structure);
1352 return ASN1_ELEMENT_NOT_FOUND;
1353 }
1354
1355 if (node->type & CONST_OPTION)
1356 {
1357 MHD__asn1_delete_structure (structure);
1358 return ASN1_GENERIC_ERROR;
1359 }
1360
1361 if ((*structure)->name)
1362 { /* Has *structure got a name? */
1363 nameLen -= strlen ((*structure)->name);
1364 if (nameLen > 0)
1365 strcpy (currentName, (*structure)->name);
1366 else
1367 {
1368 MHD__asn1_delete_structure (structure);
1369 return ASN1_MEM_ERROR;
1370 }
1371 if (!(strcmp (currentName, elementName)))
1372 {
1373 state = FOUND;
1374 nodeFound = *structure;
1375 }
1376 else if (!memcmp (currentName, elementName, strlen (currentName)))
1377 state = SAME_BRANCH;
1378 else
1379 state = OTHER_BRANCH;
1380 }
1381 else
1382 { /* *structure doesn't have a name? */
1383 currentName[0] = 0;
1384 if (elementName[0] == 0)
1385 {
1386 state = FOUND;
1387 nodeFound = *structure;
1388 }
1389 else
1390 {
1391 state = SAME_BRANCH;
1392 }
1393 }
1394
1395 counter = 0;
1396 move = DOWN;
1397 p = node;
1398 while (1)
1399 {
1400
1401 ris = ASN1_SUCCESS;
1402
1403 if (move != UP)
1404 {
1405 if (p->type & CONST_SET)
1406 {
1407 p2 = MHD__asn1_find_up (p);
1408 len2 = strtol (p2->value, NULL, 10);
1409 if (counter == len2)
1410 {
1411 p = p2;
1412 move = UP;
1413 continue;
1414 }
1415 else if (counter > len2)
1416 {
1417 MHD__asn1_delete_structure (structure);
1418 return ASN1_DER_ERROR;
1419 }
1420 p2 = p2->down;
1421 while (p2)
1422 {
1423 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
1424 {
1425 if (type_field (p2->type) != TYPE_CHOICE)
1426 ris =
1427 MHD__asn1_extract_tag_der (p2, der + counter,
1428 len - counter, &len2);
1429 else
1430 {
1431 p3 = p2->down;
1432 while (p3)
1433 {
1434 ris =
1435 MHD__asn1_extract_tag_der (p3, der + counter,
1436 len - counter, &len2);
1437 if (ris == ASN1_SUCCESS)
1438 break;
1439 p3 = p3->right;
1440 }
1441 }
1442 if (ris == ASN1_SUCCESS)
1443 {
1444 p2->type &= ~CONST_NOT_USED;
1445 p = p2;
1446 break;
1447 }
1448 }
1449 p2 = p2->right;
1450 }
1451 if (p2 == NULL)
1452 {
1453 MHD__asn1_delete_structure (structure);
1454 return ASN1_DER_ERROR;
1455 }
1456 }
1457
1458 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1459 {
1460 p2 = MHD__asn1_find_up (p);
1461 len2 = strtol (p2->value, NULL, 10);
1462 if (counter == len2)
1463 {
1464 if (p->right)
1465 {
1466 p2 = p->right;
1467 move = RIGHT;
1468 }
1469 else
1470 move = UP;
1471
1472 if (p->type & CONST_OPTION)
1473 MHD__asn1_delete_structure (&p);
1474
1475 p = p2;
1476 continue;
1477 }
1478 }
1479
1480 if (type_field (p->type) == TYPE_CHOICE)
1481 {
1482 while (p->down)
1483 {
1484 if (counter < len)
1485 ris =
1486 MHD__asn1_extract_tag_der (p->down, der + counter,
1487 len - counter, &len2);
1488 else
1489 ris = ASN1_DER_ERROR;
1490 if (ris == ASN1_SUCCESS)
1491 {
1492 while (p->down->right)
1493 {
1494 p2 = p->down->right;
1495 MHD__asn1_delete_structure (&p2);
1496 }
1497 break;
1498 }
1499 else if (ris == ASN1_ERROR_TYPE_ANY)
1500 {
1501 MHD__asn1_delete_structure (structure);
1502 return ASN1_ERROR_TYPE_ANY;
1503 }
1504 else
1505 {
1506 p2 = p->down;
1507 MHD__asn1_delete_structure (&p2);
1508 }
1509 }
1510
1511 if (p->down == NULL)
1512 {
1513 if (!(p->type & CONST_OPTION))
1514 {
1515 MHD__asn1_delete_structure (structure);
1516 return ASN1_DER_ERROR;
1517 }
1518 }
1519 else
1520 p = p->down;
1521 }
1522
1523 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1524 {
1525 p2 = MHD__asn1_find_up (p);
1526 len2 = strtol (p2->value, NULL, 10);
1527 if (counter > len2)
1528 ris = ASN1_TAG_ERROR;
1529 }
1530
1531 if (ris == ASN1_SUCCESS)
1532 ris =
1533 MHD__asn1_extract_tag_der (p, der + counter, len - counter, &len2);
1534 if (ris != ASN1_SUCCESS)
1535 {
1536 if (p->type & CONST_OPTION)
1537 {
1538 p->type |= CONST_NOT_USED;
1539 move = RIGHT;
1540 }
1541 else if (p->type & CONST_DEFAULT)
1542 {
1543 MHD__asn1_set_value (p, NULL, 0);
1544 move = RIGHT;
1545 }
1546 else
1547 {
1548 if (errorDescription != NULL)
1549 MHD__asn1_error_description_tag_error (p, errorDescription);
1550
1551 MHD__asn1_delete_structure (structure);
1552 return ASN1_TAG_ERROR;
1553 }
1554 }
1555 else
1556 counter += len2;
1557 }
1558
1559 if (ris == ASN1_SUCCESS)
1560 {
1561 switch (type_field (p->type))
1562 {
1563 case TYPE_NULL:
1564 if (der[counter])
1565 {
1566 MHD__asn1_delete_structure (structure);
1567 return ASN1_DER_ERROR;
1568 }
1569
1570 if (p == nodeFound)
1571 state = EXIT;
1572
1573 counter++;
1574 move = RIGHT;
1575 break;
1576 case TYPE_BOOLEAN:
1577 if (der[counter++] != 1)
1578 {
1579 MHD__asn1_delete_structure (structure);
1580 return ASN1_DER_ERROR;
1581 }
1582
1583 if (state == FOUND)
1584 {
1585 if (der[counter++] == 0)
1586 MHD__asn1_set_value (p, "F", 1);
1587 else
1588 MHD__asn1_set_value (p, "T", 1);
1589
1590 if (p == nodeFound)
1591 state = EXIT;
1592
1593 }
1594 else
1595 counter++;
1596
1597 move = RIGHT;
1598 break;
1599 case TYPE_INTEGER:
1600 case TYPE_ENUMERATED:
1601 len2 =
1602 MHD__asn1_get_length_der (der + counter, len - counter, &len3);
1603 if (len2 < 0)
1604 return ASN1_DER_ERROR;
1605 if (state == FOUND)
1606 {
1607 if (len3 + len2 > len - counter)
1608 return ASN1_DER_ERROR;
1609 MHD__asn1_set_value (p, der + counter, len3 + len2);
1610
1611 if (p == nodeFound)
1612 state = EXIT;
1613 }
1614 counter += len3 + len2;
1615 move = RIGHT;
1616 break;
1617 case TYPE_OBJECT_ID:
1618 if (state == FOUND)
1619 {
1620 MHD__asn1_get_objectid_der (der + counter, len - counter, &len2,
1621 temp, sizeof (temp));
1622 tlen = strlen (temp);
1623
1624 if (tlen > 0)
1625 MHD__asn1_set_value (p, temp, tlen + 1);
1626
1627 if (p == nodeFound)
1628 state = EXIT;
1629 }
1630 else
1631 {
1632 len2 =
1633 MHD__asn1_get_length_der (der + counter, len - counter, &len3);
1634 if (len2 < 0)
1635 return ASN1_DER_ERROR;
1636 len2 += len3;
1637 }
1638
1639 counter += len2;
1640 move = RIGHT;
1641 break;
1642 case TYPE_TIME:
1643 if (state == FOUND)
1644 {
1645 result =
1646 MHD__asn1_get_time_der (der + counter, len - counter, &len2,
1647 temp, sizeof (temp) - 1);
1648 if (result != ASN1_SUCCESS)
1649 {
1650 MHD__asn1_delete_structure (structure);
1651 return result;
1652 }
1653
1654 tlen = strlen (temp);
1655 if (tlen > 0)
1656 MHD__asn1_set_value (p, temp, tlen + 1);
1657
1658 if (p == nodeFound)
1659 state = EXIT;
1660 }
1661 else
1662 {
1663 len2 =
1664 MHD__asn1_get_length_der (der + counter, len - counter, &len3);
1665 if (len2 < 0)
1666 return ASN1_DER_ERROR;
1667 len2 += len3;
1668 }
1669
1670 counter += len2;
1671 move = RIGHT;
1672 break;
1673 case TYPE_OCTET_STRING:
1674 len3 = len - counter;
1675 if (state == FOUND)
1676 {
1677 ris = MHD__asn1_get_octet_string (der + counter, p, &len3);
1678 if (p == nodeFound)
1679 state = EXIT;
1680 }
1681 else
1682 ris = MHD__asn1_get_octet_string (der + counter, NULL, &len3);
1683
1684 if (ris != ASN1_SUCCESS)
1685 return ris;
1686 counter += len3;
1687 move = RIGHT;
1688 break;
1689 case TYPE_GENERALSTRING:
1690 len2 =
1691 MHD__asn1_get_length_der (der + counter, len - counter, &len3);
1692 if (len2 < 0)
1693 return ASN1_DER_ERROR;
1694 if (state == FOUND)
1695 {
1696 if (len3 + len2 > len - counter)
1697 return ASN1_DER_ERROR;
1698 MHD__asn1_set_value (p, der + counter, len3 + len2);
1699
1700 if (p == nodeFound)
1701 state = EXIT;
1702 }
1703 counter += len3 + len2;
1704 move = RIGHT;
1705 break;
1706 case TYPE_BIT_STRING:
1707 len2 =
1708 MHD__asn1_get_length_der (der + counter, len - counter, &len3);
1709 if (len2 < 0)
1710 return ASN1_DER_ERROR;
1711 if (state == FOUND)
1712 {
1713 if (len3 + len2 > len - counter)
1714 return ASN1_DER_ERROR;
1715 MHD__asn1_set_value (p, der + counter, len3 + len2);
1716
1717 if (p == nodeFound)
1718 state = EXIT;
1719 }
1720 counter += len3 + len2;
1721 move = RIGHT;
1722 break;
1723 case TYPE_SEQUENCE:
1724 case TYPE_SET:
1725 if (move == UP)
1726 {
1727 len2 = strtol (p->value, NULL, 10);
1728 MHD__asn1_set_value (p, NULL, 0);
1729 if (len2 == -1)
1730 { /* indefinite length method */
1731 if ((der[counter]) || der[counter + 1])
1732 {
1733 MHD__asn1_delete_structure (structure);
1734 return ASN1_DER_ERROR;
1735 }
1736 counter += 2;
1737 }
1738 else
1739 { /* definite length method */
1740 if (len2 != counter)
1741 {
1742 MHD__asn1_delete_structure (structure);
1743 return ASN1_DER_ERROR;
1744 }
1745 }
1746 if (p == nodeFound)
1747 state = EXIT;
1748 move = RIGHT;
1749 }
1750 else
1751 { /* move==DOWN || move==RIGHT */
1752 if (state == OTHER_BRANCH)
1753 {
1754 len3 =
1755 MHD__asn1_get_length_der (der + counter, len - counter,
1756 &len2);
1757 if (len3 < 0)
1758 return ASN1_DER_ERROR;
1759 counter += len2 + len3;
1760 move = RIGHT;
1761 }
1762 else
1763 { /* state==SAME_BRANCH or state==FOUND */
1764 len3 =
1765 MHD__asn1_get_length_der (der + counter, len - counter,
1766 &len2);
1767 if (len3 < 0)
1768 return ASN1_DER_ERROR;
1769 counter += len2;
1770 if (len3 > 0)
1771 {
1772 MHD__asn1_ltostr (counter + len3, temp);
1773 tlen = strlen (temp);
1774
1775 if (tlen > 0)
1776 MHD__asn1_set_value (p, temp, tlen + 1);
1777 move = DOWN;
1778 }
1779 else if (len3 == 0)
1780 {
1781 p2 = p->down;
1782 while (p2)
1783 {
1784 if (type_field (p2->type) != TYPE_TAG)
1785 {
1786 p3 = p2->right;
1787 MHD__asn1_delete_structure (&p2);
1788 p2 = p3;
1789 }
1790 else
1791 p2 = p2->right;
1792 }
1793 move = RIGHT;
1794 }
1795 else
1796 { /* indefinite length method */
1797 MHD__asn1_set_value (p, "-1", 3);
1798 move = DOWN;
1799 }
1800 }
1801 }
1802 break;
1803 case TYPE_SEQUENCE_OF:
1804 case TYPE_SET_OF:
1805 if (move == UP)
1806 {
1807 len2 = strtol (p->value, NULL, 10);
1808 if (len2 > counter)
1809 {
1810 MHD__asn1_append_sequence_set (p);
1811 p = p->down;
1812 while (p->right)
1813 p = p->right;
1814 move = RIGHT;
1815 continue;
1816 }
1817 MHD__asn1_set_value (p, NULL, 0);
1818 if (len2 != counter)
1819 {
1820 MHD__asn1_delete_structure (structure);
1821 return ASN1_DER_ERROR;
1822 }
1823
1824 if (p == nodeFound)
1825 state = EXIT;
1826 }
1827 else
1828 { /* move==DOWN || move==RIGHT */
1829 if (state == OTHER_BRANCH)
1830 {
1831 len3 =
1832 MHD__asn1_get_length_der (der + counter, len - counter,
1833 &len2);
1834 if (len3 < 0)
1835 return ASN1_DER_ERROR;
1836 counter += len2 + len3;
1837 move = RIGHT;
1838 }
1839 else
1840 { /* state==FOUND or state==SAME_BRANCH */
1841 len3 =
1842 MHD__asn1_get_length_der (der + counter, len - counter,
1843 &len2);
1844 if (len3 < 0)
1845 return ASN1_DER_ERROR;
1846 counter += len2;
1847 if (len3)
1848 {
1849 MHD__asn1_ltostr (counter + len3, temp);
1850 tlen = strlen (temp);
1851
1852 if (tlen > 0)
1853 MHD__asn1_set_value (p, temp, tlen + 1);
1854 p2 = p->down;
1855 while ((type_field (p2->type) == TYPE_TAG)
1856 || (type_field (p2->type) == TYPE_SIZE))
1857 p2 = p2->right;
1858 if (p2->right == NULL)
1859 MHD__asn1_append_sequence_set (p);
1860 p = p2;
1861 state = FOUND;
1862 }
1863 }
1864 }
1865
1866 break;
1867 case TYPE_ANY:
1868 if (MHD__asn1_get_tag_der
1869 (der + counter, len - counter, &class, &len2,
1870 &tag) != ASN1_SUCCESS)
1871 return ASN1_DER_ERROR;
1872 if (counter + len2 > len)
1873 return ASN1_DER_ERROR;
1874
1875 len4 =
1876 MHD__asn1_get_length_der (der + counter + len2,
1877 len - counter - len2, &len3);
1878 if (len4 < -1)
1879 return ASN1_DER_ERROR;
1880
1881 if (len4 != -1)
1882 {
1883 len2 += len4;
1884 if (state == FOUND)
1885 {
1886 MHD__asn1_length_der (len2 + len3, NULL, &len4);
1887 temp2 =
1888 (unsigned char *) MHD__asn1_alloca (len2 + len3 + len4);
1889 if (temp2 == NULL)
1890 {
1891 MHD__asn1_delete_structure (structure);
1892 return ASN1_MEM_ALLOC_ERROR;
1893 }
1894
1895 MHD__asn1_octet_der (der + counter, len2 + len3, temp2,
1896 &len4);
1897 MHD__asn1_set_value (p, temp2, len4);
1898 MHD__asn1_afree (temp2);
1899
1900 if (p == nodeFound)
1901 state = EXIT;
1902 }
1903 counter += len2 + len3;
1904 }
1905 else
1906 { /* indefinite length */
1907 /* Check indefinite lenth method in an EXPLICIT TAG */
1908 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1909 indefinite = 1;
1910 else
1911 indefinite = 0;
1912
1913 len2 = len - counter;
1914 ris =
1915 MHD__asn1_get_indefinite_length_string (der + counter, &len2);
1916 if (ris != ASN1_SUCCESS)
1917 {
1918 MHD__asn1_delete_structure (structure);
1919 return ris;
1920 }
1921
1922 if (state == FOUND)
1923 {
1924 MHD__asn1_length_der (len2, NULL, &len4);
1925 temp2 = (unsigned char *) MHD__asn1_alloca (len2 + len4);
1926 if (temp2 == NULL)
1927 {
1928 MHD__asn1_delete_structure (structure);
1929 return ASN1_MEM_ALLOC_ERROR;
1930 }
1931
1932 MHD__asn1_octet_der (der + counter, len2, temp2, &len4);
1933 MHD__asn1_set_value (p, temp2, len4);
1934 MHD__asn1_afree (temp2);
1935
1936 if (p == nodeFound)
1937 state = EXIT;
1938 }
1939
1940 counter += len2;
1941
1942 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1943 an indefinite length method. */
1944 if (indefinite)
1945 {
1946 if (!der[counter] && !der[counter + 1])
1947 {
1948 counter += 2;
1949 }
1950 else
1951 {
1952 MHD__asn1_delete_structure (structure);
1953 return ASN1_DER_ERROR;
1954 }
1955 }
1956 }
1957 move = RIGHT;
1958 break;
1959
1960 default:
1961 move = (move == UP) ? RIGHT : DOWN;
1962 break;
1963 }
1964 }
1965
1966 if ((p == node && move != DOWN) || (state == EXIT))
1967 break;
1968
1969 if (move == DOWN)
1970 {
1971 if (p->down)
1972 {
1973 p = p->down;
1974
1975 if (state != FOUND)
1976 {
1977 nameLen -= strlen (p->name) + 1;
1978 if (nameLen > 0)
1979 {
1980 if (currentName[0])
1981 strcat (currentName, ".");
1982 strcat (currentName, p->name);
1983 }
1984 else
1985 {
1986 MHD__asn1_delete_structure (structure);
1987 return ASN1_MEM_ERROR;
1988 }
1989 if (!(strcmp (currentName, elementName)))
1990 {
1991 state = FOUND;
1992 nodeFound = p;
1993 }
1994 else
1995 if (!memcmp
1996 (currentName, elementName, strlen (currentName)))
1997 state = SAME_BRANCH;
1998 else
1999 state = OTHER_BRANCH;
2000 }
2001 }
2002 else
2003 move = RIGHT;
2004 }
2005
2006 if ((move == RIGHT) && !(p->type & CONST_SET))
2007 {
2008 if (p->right)
2009 {
2010 p = p->right;
2011
2012 if (state != FOUND)
2013 {
2014 dot_p = char_p = currentName;
2015 while ((char_p = strchr (char_p, '.')))
2016 {
2017 dot_p = char_p++;
2018 dot_p++;
2019 }
2020
2021 nameLen += strlen (currentName) - (dot_p - currentName);
2022 *dot_p = 0;
2023
2024 nameLen -= strlen (p->name);
2025 if (nameLen > 0)
2026 strcat (currentName, p->name);
2027 else
2028 {
2029 MHD__asn1_delete_structure (structure);
2030 return ASN1_MEM_ERROR;
2031 }
2032
2033 if (!(strcmp (currentName, elementName)))
2034 {
2035 state = FOUND;
2036 nodeFound = p;
2037 }
2038 else
2039 if (!memcmp
2040 (currentName, elementName, strlen (currentName)))
2041 state = SAME_BRANCH;
2042 else
2043 state = OTHER_BRANCH;
2044 }
2045 }
2046 else
2047 move = UP;
2048 }
2049
2050 if (move == UP)
2051 {
2052 p = MHD__asn1_find_up (p);
2053
2054 if (state != FOUND)
2055 {
2056 dot_p = char_p = currentName;
2057 while ((char_p = strchr (char_p, '.')))
2058 {
2059 dot_p = char_p++;
2060 dot_p++;
2061 }
2062
2063 nameLen += strlen (currentName) - (dot_p - currentName);
2064 *dot_p = 0;
2065
2066 if (!(strcmp (currentName, elementName)))
2067 {
2068 state = FOUND;
2069 nodeFound = p;
2070 }
2071 else
2072 if (!memcmp (currentName, elementName, strlen (currentName)))
2073 state = SAME_BRANCH;
2074 else
2075 state = OTHER_BRANCH;
2076 }
2077 }
2078 }
2079
2080 MHD__asn1_delete_not_used (*structure);
2081
2082 if (counter > len)
2083 {
2084 MHD__asn1_delete_structure (structure);
2085 return ASN1_DER_ERROR;
2086 }
2087
2088 return ASN1_SUCCESS;
2089}
2090
2091
2092
2093/** 1299/**
2094 * MHD__asn1_der_decoding_startEnd - Find the start and end point of an element in a DER encoding string. 1300 * MHD__asn1_der_decoding_startEnd - Find the start and end point of an element in a DER encoding string.
2095 * @element: pointer to an ASN1 element 1301 * @element: pointer to an ASN1 element
@@ -2163,7 +1369,7 @@ MHD__asn1_der_decoding_startEnd (ASN1_TYPE element, const void *ider, int len,
2163 if (p->type & CONST_SET) 1369 if (p->type & CONST_SET)
2164 { 1370 {
2165 p2 = MHD__asn1_find_up (p); 1371 p2 = MHD__asn1_find_up (p);
2166 len2 = strtol (p2->value, NULL, 10); 1372 len2 = strtol ( (const char* ) p2->value, NULL, 10);
2167 if (len2 == -1) 1373 if (len2 == -1)
2168 { 1374 {
2169 if (!der[counter] && !der[counter + 1]) 1375 if (!der[counter] && !der[counter + 1])
@@ -2442,386 +1648,3 @@ MHD__asn1_der_decoding_startEnd (ASN1_TYPE element, const void *ider, int len,
2442} 1648}
2443 1649
2444 1650
2445/**
2446 * MHD__asn1_expand_any_defined_by - Expand "ANY DEFINED BY" fields in structure.
2447 * @definitions: ASN1 definitions
2448 * @element: pointer to an ASN1 structure
2449 *
2450 * Expands every "ANY DEFINED BY" element of a structure created from
2451 * a DER decoding process (MHD__asn1_der_decoding function). The element ANY
2452 * must be defined by an OBJECT IDENTIFIER. The type used to expand
2453 * the element ANY is the first one following the definition of
2454 * the actual value of the OBJECT IDENTIFIER.
2455 *
2456 *
2457 * Returns:
2458 *
2459 * ASN1_SUCCESS: Substitution OK.
2460 *
2461 * ASN1_ERROR_TYPE_ANY: Some "ANY DEFINED BY" element couldn't be
2462 * expanded due to a problem in OBJECT_ID -> TYPE association.
2463 *
2464 * other errors: Result of der decoding process.
2465 **/
2466
2467MHD__asn1_retCode
2468MHD__asn1_expand_any_defined_by (ASN1_TYPE definitions, ASN1_TYPE * element)
2469{
2470 char definitionsName[MAX_NAME_SIZE], name[2 * MAX_NAME_SIZE + 1],
2471 value[MAX_NAME_SIZE];
2472 MHD__asn1_retCode retCode = ASN1_SUCCESS, result;
2473 int len, len2, len3;
2474 ASN1_TYPE p, p2, p3, aux = ASN1_TYPE_EMPTY;
2475 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
2476
2477 if ((definitions == ASN1_TYPE_EMPTY) || (*element == ASN1_TYPE_EMPTY))
2478 return ASN1_ELEMENT_NOT_FOUND;
2479
2480 strcpy (definitionsName, definitions->name);
2481 strcat (definitionsName, ".");
2482
2483 p = *element;
2484 while (p)
2485 {
2486
2487 switch (type_field (p->type))
2488 {
2489 case TYPE_ANY:
2490 if ((p->type & CONST_DEFINED_BY) && (p->value))
2491 {
2492 /* search the "DEF_BY" element */
2493 p2 = p->down;
2494 while ((p2) && (type_field (p2->type) != TYPE_CONSTANT))
2495 p2 = p2->right;
2496
2497 if (!p2)
2498 {
2499 retCode = ASN1_ERROR_TYPE_ANY;
2500 break;
2501 }
2502
2503 p3 = MHD__asn1_find_up (p);
2504
2505 if (!p3)
2506 {
2507 retCode = ASN1_ERROR_TYPE_ANY;
2508 break;
2509 }
2510
2511 p3 = p3->down;
2512 while (p3)
2513 {
2514 if ((p3->name) && !(strcmp (p3->name, p2->name)))
2515 break;
2516 p3 = p3->right;
2517 }
2518
2519 if ((!p3) || (type_field (p3->type) != TYPE_OBJECT_ID) ||
2520 (p3->value == NULL))
2521 {
2522
2523 p3 = MHD__asn1_find_up (p);
2524 p3 = MHD__asn1_find_up (p3);
2525
2526 if (!p3)
2527 {
2528 retCode = ASN1_ERROR_TYPE_ANY;
2529 break;
2530 }
2531
2532 p3 = p3->down;
2533
2534 while (p3)
2535 {
2536 if ((p3->name) && !(strcmp (p3->name, p2->name)))
2537 break;
2538 p3 = p3->right;
2539 }
2540
2541 if ((!p3) || (type_field (p3->type) != TYPE_OBJECT_ID) ||
2542 (p3->value == NULL))
2543 {
2544 retCode = ASN1_ERROR_TYPE_ANY;
2545 break;
2546 }
2547 }
2548
2549 /* search the OBJECT_ID into definitions */
2550 p2 = definitions->down;
2551 while (p2)
2552 {
2553 if ((type_field (p2->type) == TYPE_OBJECT_ID) &&
2554 (p2->type & CONST_ASSIGN))
2555 {
2556 strcpy (name, definitionsName);
2557 strcat (name, p2->name);
2558
2559 len = MAX_NAME_SIZE;
2560 result =
2561 MHD__asn1_read_value (definitions, name, value, &len);
2562
2563 if ((result == ASN1_SUCCESS)
2564 && (!strcmp (p3->value, value)))
2565 {
2566 p2 = p2->right; /* pointer to the structure to
2567 use for expansion */
2568 while ((p2) && (p2->type & CONST_ASSIGN))
2569 p2 = p2->right;
2570
2571 if (p2)
2572 {
2573 strcpy (name, definitionsName);
2574 strcat (name, p2->name);
2575
2576 result =
2577 MHD__asn1_create_element (definitions, name, &aux);
2578 if (result == ASN1_SUCCESS)
2579 {
2580 MHD__asn1_set_name (aux, p->name);
2581 len2 =
2582 MHD__asn1_get_length_der (p->value,
2583 p->value_len, &len3);
2584 if (len2 < 0)
2585 return ASN1_DER_ERROR;
2586
2587 result =
2588 MHD__asn1_der_decoding (&aux, p->value + len3,
2589 len2,
2590 errorDescription);
2591 if (result == ASN1_SUCCESS)
2592 {
2593
2594 MHD__asn1_set_right (aux, p->right);
2595 MHD__asn1_set_right (p, aux);
2596
2597 result = MHD__asn1_delete_structure (&p);
2598 if (result == ASN1_SUCCESS)
2599 {
2600 p = aux;
2601 aux = ASN1_TYPE_EMPTY;
2602 break;
2603 }
2604 else
2605 { /* error with MHD__asn1_delete_structure */
2606 MHD__asn1_delete_structure (&aux);
2607 retCode = result;
2608 break;
2609 }
2610 }
2611 else
2612 { /* error with MHD__asn1_der_decoding */
2613 retCode = result;
2614 break;
2615 }
2616 }
2617 else
2618 { /* error with MHD__asn1_create_element */
2619 retCode = result;
2620 break;
2621 }
2622 }
2623 else
2624 { /* error with the pointer to the structure to exapand */
2625 retCode = ASN1_ERROR_TYPE_ANY;
2626 break;
2627 }
2628 }
2629 }
2630 p2 = p2->right;
2631 } /* end while */
2632
2633 if (!p2)
2634 {
2635 retCode = ASN1_ERROR_TYPE_ANY;
2636 break;
2637 }
2638
2639 }
2640 break;
2641 default:
2642 break;
2643 }
2644
2645
2646 if (p->down)
2647 {
2648 p = p->down;
2649 }
2650 else if (p == *element)
2651 {
2652 p = NULL;
2653 break;
2654 }
2655 else if (p->right)
2656 p = p->right;
2657 else
2658 {
2659 while (1)
2660 {
2661 p = MHD__asn1_find_up (p);
2662 if (p == *element)
2663 {
2664 p = NULL;
2665 break;
2666 }
2667 if (p->right)
2668 {
2669 p = p->right;
2670 break;
2671 }
2672 }
2673 }
2674 }
2675
2676 return retCode;
2677}
2678
2679
2680
2681/**
2682 * MHD__asn1_expand_octet_string - Expand "OCTET STRING" fields in structure.
2683 * @definitions: ASN1 definitions
2684 * @element: pointer to an ASN1 structure
2685 * @octetName: name of the OCTECT STRING field to expand.
2686 * @objectName: name of the OBJECT IDENTIFIER field to use to define
2687 * the type for expansion.
2688 *
2689 * Expands an "OCTET STRING" element of a structure created from a
2690 * DER decoding process (MHD__asn1_der_decoding function). The type used
2691 * for expansion is the first one following the definition of the
2692 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
2693 *
2694 * Returns:
2695 *
2696 * ASN1_SUCCESS: Substitution OK.
2697 *
2698 * ASN1_ELEMENT_NOT_FOUND: OBJECTNAME or OCTETNAME are not correct.
2699 *
2700 * ASN1_VALUE_NOT_VALID: Wasn't possible to find the type to use
2701 * for expansion.
2702 *
2703 * other errors: result of der decoding process.
2704 **/
2705MHD__asn1_retCode
2706MHD__asn1_expand_octet_string (ASN1_TYPE definitions, ASN1_TYPE * element,
2707 const char *octetName, const char *objectName)
2708{
2709 char name[2 * MAX_NAME_SIZE + 1], value[MAX_NAME_SIZE];
2710 MHD__asn1_retCode retCode = ASN1_SUCCESS, result;
2711 int len, len2, len3;
2712 ASN1_TYPE p2, aux = ASN1_TYPE_EMPTY;
2713 ASN1_TYPE octetNode = ASN1_TYPE_EMPTY, objectNode = ASN1_TYPE_EMPTY;
2714 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
2715
2716 if ((definitions == ASN1_TYPE_EMPTY) || (*element == ASN1_TYPE_EMPTY))
2717 return ASN1_ELEMENT_NOT_FOUND;
2718
2719 octetNode = MHD__asn1_find_node (*element, octetName);
2720 if (octetNode == ASN1_TYPE_EMPTY)
2721 return ASN1_ELEMENT_NOT_FOUND;
2722 if (type_field (octetNode->type) != TYPE_OCTET_STRING)
2723 return ASN1_ELEMENT_NOT_FOUND;
2724 if (octetNode->value == NULL)
2725 return ASN1_VALUE_NOT_FOUND;
2726
2727 objectNode = MHD__asn1_find_node (*element, objectName);
2728 if (objectNode == ASN1_TYPE_EMPTY)
2729 return ASN1_ELEMENT_NOT_FOUND;
2730
2731 if (type_field (objectNode->type) != TYPE_OBJECT_ID)
2732 return ASN1_ELEMENT_NOT_FOUND;
2733
2734 if (objectNode->value == NULL)
2735 return ASN1_VALUE_NOT_FOUND;
2736
2737
2738 /* search the OBJECT_ID into definitions */
2739 p2 = definitions->down;
2740 while (p2)
2741 {
2742 if ((type_field (p2->type) == TYPE_OBJECT_ID) &&
2743 (p2->type & CONST_ASSIGN))
2744 {
2745 strcpy (name, definitions->name);
2746 strcat (name, ".");
2747 strcat (name, p2->name);
2748
2749 len = sizeof (value);
2750 result = MHD__asn1_read_value (definitions, name, value, &len);
2751
2752 if ((result == ASN1_SUCCESS)
2753 && (!strcmp (objectNode->value, value)))
2754 {
2755
2756 p2 = p2->right; /* pointer to the structure to
2757 use for expansion */
2758 while ((p2) && (p2->type & CONST_ASSIGN))
2759 p2 = p2->right;
2760
2761 if (p2)
2762 {
2763 strcpy (name, definitions->name);
2764 strcat (name, ".");
2765 strcat (name, p2->name);
2766
2767 result = MHD__asn1_create_element (definitions, name, &aux);
2768 if (result == ASN1_SUCCESS)
2769 {
2770 MHD__asn1_set_name (aux, octetNode->name);
2771 len2 =
2772 MHD__asn1_get_length_der (octetNode->value,
2773 octetNode->value_len, &len3);
2774 if (len2 < 0)
2775 return ASN1_DER_ERROR;
2776
2777 result =
2778 MHD__asn1_der_decoding (&aux, octetNode->value + len3,
2779 len2, errorDescription);
2780 if (result == ASN1_SUCCESS)
2781 {
2782
2783 MHD__asn1_set_right (aux, octetNode->right);
2784 MHD__asn1_set_right (octetNode, aux);
2785
2786 result = MHD__asn1_delete_structure (&octetNode);
2787 if (result == ASN1_SUCCESS)
2788 {
2789 aux = ASN1_TYPE_EMPTY;
2790 break;
2791 }
2792 else
2793 { /* error with MHD__asn1_delete_structure */
2794 MHD__asn1_delete_structure (&aux);
2795 retCode = result;
2796 break;
2797 }
2798 }
2799 else
2800 { /* error with MHD__asn1_der_decoding */
2801 retCode = result;
2802 break;
2803 }
2804 }
2805 else
2806 { /* error with MHD__asn1_create_element */
2807 retCode = result;
2808 break;
2809 }
2810 }
2811 else
2812 { /* error with the pointer to the structure to exapand */
2813 retCode = ASN1_VALUE_NOT_VALID;
2814 break;
2815 }
2816 }
2817 }
2818
2819 p2 = p2->right;
2820
2821 }
2822
2823 if (!p2)
2824 retCode = ASN1_VALUE_NOT_VALID;
2825
2826 return retCode;
2827}