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
];
9 A2C_CONTEXT
* pcxt
= NULL
;
10 A2C_ERROR err
= A2C_ERROR_Success
;
14 return_code
= fread(buffer
, 1, sizeof(buffer
), file
);
18 return A2C_ERROR_malformedEncoding
;
34 if ((err
< A2C_ERROR_Success
) && (err
!= A2C_ERROR_needMoreData
))
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
;
51 file
= fopen(filename
, "rb");
58 err
= _A2C_DecodeBerFile(
59 (PVOID
*) &certificate
,
60 &Cert_Certificate_descriptor
,
67 if (err
< A2C_ERROR_Success
)
75 static void print_relative_distinguished_name_email_addresses(Cert_RelativeDistinguishedName
* rdn
)
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
)
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
)
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;
112 &Cert_GeneralNames_descriptor
,
115 extensions
->array
[counter
].extnValue
.data
,
116 extensions
->array
[counter
].extnValue
.length
118 if (err
< A2C_ERROR_Success
)
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
);