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