| 708 | */ |
| 709 | |
| 710 | int /* O - 0 on success, -1 on error */ |
| 711 | httpLoadCredentials( |
| 712 | const char *path, /* I - Keychain/PKCS#12 path */ |
| 713 | cups_array_t **credentials, /* IO - Credentials */ |
| 714 | const char *common_name) /* I - Common name for credentials */ |
| 715 | { |
| 716 | cups_file_t *fp; /* Certificate file */ |
| 717 | char filename[1024], /* filename.crt */ |
| 718 | temp[1024], /* Temporary string */ |
| 719 | line[256]; /* Base64-encoded line */ |
| 720 | unsigned char *data = NULL; /* Buffer for cert data */ |
| 721 | size_t alloc_data = 0, /* Bytes allocated */ |
| 722 | num_data = 0; /* Bytes used */ |
| 723 | int decoded; /* Bytes decoded */ |
| 724 | int in_certificate = 0; |
| 725 | /* In a certificate? */ |
| 726 | |
| 727 | |
| 728 | if (!credentials || !common_name) |
| 729 | return (-1); |
| 730 | |
| 731 | if (!path) |
| 732 | path = http_gnutls_default_path(temp, sizeof(temp)); |
| 733 | if (!path) |
| 734 | return (-1); |
| 735 | |
| 736 | http_gnutls_make_path(filename, sizeof(filename), path, common_name, "crt"); |
| 737 | |
| 738 | if ((fp = cupsFileOpen(filename, "r")) == NULL) |
| 739 | return (-1); |
| 740 | |
| 741 | while (cupsFileGets(fp, line, sizeof(line))) |
| 742 | { |
| 743 | if (!strcmp(line, "-----BEGIN CERTIFICATE-----")) |
| 744 | { |
| 745 | if (in_certificate) |
| 746 | { |
| 747 | /* |
| 748 | * Missing END CERTIFICATE... |
| 749 | */ |
| 750 | |
| 751 | httpFreeCredentials(*credentials); |
| 752 | *credentials = NULL; |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | in_certificate = 1; |
| 757 | } |
| 758 | else if (!strcmp(line, "-----END CERTIFICATE-----")) |
| 759 | { |
| 760 | if (!in_certificate || !num_data) |
| 761 | { |
| 762 | /* |
| 763 | * Missing data... |
| 764 | */ |
| 765 | |
| 766 | httpFreeCredentials(*credentials); |
| 767 | *credentials = NULL; |
no test coverage detected