| 833 | */ |
| 834 | |
| 835 | int // O - -1 on error, 0 on success |
| 836 | httpSaveCredentials( |
| 837 | const char *path, // I - Keychain/PKCS#12 path |
| 838 | cups_array_t *credentials, // I - Credentials |
| 839 | const char *common_name) // I - Common name for credentials |
| 840 | { |
| 841 | cups_file_t *fp; // Certificate file |
| 842 | char filename[1024], // filename.crt |
| 843 | nfilename[1024],// filename.crt.N |
| 844 | temp[1024], // Temporary string |
| 845 | line[256]; // Base64-encoded line |
| 846 | const unsigned char *ptr; // Pointer into certificate |
| 847 | ssize_t remaining; // Bytes left |
| 848 | http_credential_t *cred; // Current credential |
| 849 | |
| 850 | |
| 851 | if (!credentials || !common_name) |
| 852 | return (-1); |
| 853 | |
| 854 | if (!path) |
| 855 | path = http_default_path(temp, sizeof(temp)); |
| 856 | if (!path) |
| 857 | return (-1); |
| 858 | |
| 859 | http_make_path(filename, sizeof(filename), path, common_name, "crt"); |
| 860 | snprintf(nfilename, sizeof(nfilename), "%s.N", filename); |
| 861 | |
| 862 | if ((fp = cupsFileOpen(nfilename, "w")) == NULL) |
| 863 | return (-1); |
| 864 | |
| 865 | #ifndef _WIN32 |
| 866 | fchmod(cupsFileNumber(fp), 0600); |
| 867 | #endif // !_WIN32 |
| 868 | |
| 869 | for (cred = (http_credential_t *)cupsArrayFirst(credentials); |
| 870 | cred; |
| 871 | cred = (http_credential_t *)cupsArrayNext(credentials)) |
| 872 | { |
| 873 | cupsFilePuts(fp, "-----BEGIN CERTIFICATE-----\n"); |
| 874 | for (ptr = cred->data, remaining = (ssize_t)cred->datalen; remaining > 0; remaining -= 45, ptr += 45) |
| 875 | { |
| 876 | httpEncode64_2(line, sizeof(line), (char *)ptr, remaining > 45 ? 45 : remaining); |
| 877 | cupsFilePrintf(fp, "%s\n", line); |
| 878 | } |
| 879 | cupsFilePuts(fp, "-----END CERTIFICATE-----\n"); |
| 880 | } |
| 881 | |
| 882 | cupsFileClose(fp); |
| 883 | |
| 884 | return (rename(nfilename, filename)); |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /* |
no test coverage detected