convert PEM file to DER x509 type
| 899 | |
| 900 | // convert PEM file to DER x509 type |
| 901 | x509* PemToDer(FILE* file, CertType type, EncryptedInfo* info) |
| 902 | { |
| 903 | using namespace TaoCrypt; |
| 904 | |
| 905 | char header[80]; |
| 906 | char footer[80]; |
| 907 | |
| 908 | if (type == Cert) { |
| 909 | strncpy(header, "-----BEGIN CERTIFICATE-----", sizeof(header)); |
| 910 | strncpy(footer, "-----END CERTIFICATE-----", sizeof(footer)); |
| 911 | } else { |
| 912 | strncpy(header, "-----BEGIN RSA PRIVATE KEY-----", sizeof(header)); |
| 913 | strncpy(footer, "-----END RSA PRIVATE KEY-----", sizeof(header)); |
| 914 | } |
| 915 | |
| 916 | long begin = -1; |
| 917 | long end = 0; |
| 918 | bool foundEnd = false; |
| 919 | |
| 920 | char line[80]; |
| 921 | |
| 922 | while(fgets(line, sizeof(line), file)) |
| 923 | if (strncmp(header, line, strlen(header)) == 0) { |
| 924 | begin = ftell(file); |
| 925 | break; |
| 926 | } |
| 927 | |
| 928 | // remove encrypted header if there |
| 929 | if (fgets(line, sizeof(line), file)) { |
| 930 | char encHeader[] = "Proc-Type"; |
| 931 | if (strncmp(encHeader, line, strlen(encHeader)) == 0 && |
| 932 | fgets(line,sizeof(line), file)) { |
| 933 | |
| 934 | char* start = strstr(line, "DES"); |
| 935 | char* finish = strstr(line, ","); |
| 936 | if (!start) |
| 937 | start = strstr(line, "AES"); |
| 938 | |
| 939 | if (!info) return 0; |
| 940 | |
| 941 | if ( start && finish && (start < finish)) { |
| 942 | memcpy(info->name, start, finish - start); |
| 943 | info->name[finish - start] = 0; |
| 944 | memcpy(info->iv, finish + 1, sizeof(info->iv)); |
| 945 | |
| 946 | char* newline = strstr(line, "\r"); |
| 947 | if (!newline) newline = strstr(line, "\n"); |
| 948 | if (newline && (newline > finish)) { |
| 949 | info->ivSz = newline - (finish + 1); |
| 950 | info->set = true; |
| 951 | } |
| 952 | } |
| 953 | begin = ftell(file); |
| 954 | if (fgets(line,sizeof(line), file)) // get blank line |
| 955 | begin = ftell(file); |
| 956 | } |
| 957 | |
| 958 | } |
no test coverage detected