Reworked file I/O to use chunked A2C_DecodeBer.
[bcr-a2csamples.git] / printcert / printcert.c
blob814225f966e75dbb8903d71b1a092d149e55350f
1 #include "rfc3280.h"
3 #define BUFFER_LENGTH 4096
5 static A2C_ERROR _A2C_DecodeBerFile(PVOID * ppv, PC_A2C_DESCRIPTOR pdesc, int flags, FILE* file)
7 unsigned char buffer[BUFFER_LENGTH];
8 int return_code = 0;
9 A2C_CONTEXT* pcxt = NULL;
10 A2C_ERROR err = A2C_ERROR_Success;
12 while (1)
14 return_code = fread(buffer, 1, sizeof(buffer), file);
16 if (return_code < 0)
18 return A2C_ERROR_malformedEncoding;
21 if (return_code == 0)
23 break;
26 err = A2C_DecodeBer(
27 ppv,
28 pdesc,
29 flags,
30 &pcxt,
31 buffer,
32 return_code
34 if ((err < A2C_ERROR_Success) && (err != A2C_ERROR_needMoreData))
36 return err;
39 flags |= A2C_FLAGS_MORE_DATA;
42 return A2C_ERROR_Success;
45 static Cert_Certificate* read_certificate_from_file(const char* filename)
47 A2C_ERROR err = A2C_ERROR_Success;
48 Cert_Certificate* certificate = NULL;
49 FILE* file = NULL;
51 file = fopen(filename, "rb");
52 if (file == NULL)
54 perror(filename);
55 return NULL;
58 err = _A2C_DecodeBerFile(
59 (PVOID*) &certificate,
60 &Cert_Certificate_descriptor,
62 file
65 (void) fclose(file);
67 if (err < A2C_ERROR_Success)
69 return NULL;
72 return certificate;
75 static void print_relative_distinguished_name_email_addresses(Cert_RelativeDistinguishedName* rdn)
77 int counter = 0;
79 for (counter = 0;counter < rdn->count;++counter)
81 if (A2C_Compare(&(rdn->array[counter].type), Cert_emailAddress.id, &A2C_OBJECT_IDENTIFIER_descriptor) == 0)
83 printf("%s\n", ((Cert_emailAddress__Type*) rdn->array[counter].value.objectPtr)->string);
88 static void print_name_email_addresses(Cert_Name* name)
90 int counter = 0;
92 for (counter = 0;counter < name->rdnSequence.count;++counter)
94 print_relative_distinguished_name_email_addresses(&name->rdnSequence.array[counter]);
98 static void print_subject_alt_name_email_addresses(Cert_Extensions* extensions)
100 int counter = 0;
102 for (counter = 0;counter < extensions->count;++counter)
104 if (A2C_Compare(&extensions->array[counter].extnID, &Cert_id_ce_subjectAltName, &A2C_OBJECT_IDENTIFIER_descriptor) == 0)
106 Cert_GeneralNames* names = NULL;
107 A2C_ERROR err = A2C_ERROR_Success;
108 int namesCounter = 0;
110 err = A2C_DecodeBer(
111 (PVOID*) &names,
112 &Cert_GeneralNames_descriptor,
114 NULL,
115 extensions->array[counter].extnValue.data,
116 extensions->array[counter].extnValue.length
118 if (err < A2C_ERROR_Success)
120 return;
123 for (namesCounter = 0;namesCounter < names->count;++namesCounter)
125 if (names->array[namesCounter].index == Cert_GeneralName_rfc822Name_index)
127 printf("%s\n", names->array[namesCounter].rfc822Name.string);
134 int main(int argc, char** argv)
136 Cert_Certificate* certificate = NULL;
138 certificate = read_certificate_from_file(argv[1]);
139 if (certificate != NULL)
141 print_name_email_addresses(&certificate->toBeSigned.subject);
142 print_subject_alt_name_email_addresses(&certificate->toBeSigned.extensions);