|
| 1 | +/* Copyright (c) 2021 Avast Software |
| 2 | +
|
| 3 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | +this software and associated documentation files (the "Software"), to deal |
| 5 | +in the Software without restriction, including without limitation the rights to |
| 6 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 | +of the Software, and to permit persons to whom the Software is furnished to do |
| 8 | +so, subject to the following conditions: |
| 9 | +
|
| 10 | +The above copyright notice and this permission notice shall be included in all |
| 11 | +copies or substantial portions of the Software. |
| 12 | +
|
| 13 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +SOFTWARE. |
| 20 | +*/ |
| 21 | + |
| 22 | +#ifndef AUTHENTICODE_PARSER_AUTHENTICODE_H |
| 23 | +#define AUTHENTICODE_PARSER_AUTHENTICODE_H |
| 24 | + |
| 25 | +#ifdef __cplusplus |
| 26 | +extern "C" { |
| 27 | +#endif |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | +#include <time.h> |
| 31 | + |
| 32 | +/* Signature is valid */ |
| 33 | +#define AUTHENTICODE_VFY_VALID 0 |
| 34 | +/* Parsing error (from OpenSSL functions) */ |
| 35 | +#define AUTHENTICODE_VFY_CANT_PARSE 1 |
| 36 | +/* Signers certificate is missing */ |
| 37 | +#define AUTHENTICODE_VFY_NO_SIGNER_CERT 2 |
| 38 | +/* No digest saved inside the signature */ |
| 39 | +#define AUTHENTICODE_VFY_DIGEST_MISSING 3 |
| 40 | +/* Non verification errors - allocations etc. */ |
| 41 | +#define AUTHENTICODE_VFY_INTERNAL_ERROR 4 |
| 42 | +/* SignerInfo part of PKCS7 is missing */ |
| 43 | +#define AUTHENTICODE_VFY_NO_SIGNER_INFO 5 |
| 44 | +/* PKCS7 doesn't have type of SignedData, can't proceed */ |
| 45 | +#define AUTHENTICODE_VFY_WRONG_PKCS7_TYPE 6 |
| 46 | +/* PKCS7 doesn't have corrent content, can't proceed */ |
| 47 | +#define AUTHENTICODE_VFY_BAD_CONTENT 7 |
| 48 | +/* Contained and calculated digest don't match */ |
| 49 | +#define AUTHENTICODE_VFY_INVALID 8 |
| 50 | +/* Signature hash and file hash doesn't match */ |
| 51 | +#define AUTHENTICODE_VFY_WRONG_FILE_DIGEST 9 |
| 52 | +/* Unknown algorithm, can't proceed with verification */ |
| 53 | +#define AUTHENTICODE_VFY_UNKNOWN_ALGORITHM 10 |
| 54 | + |
| 55 | +/* Countersignature is valid */ |
| 56 | +#define COUNTERSIGNATURE_VFY_VALID 0 |
| 57 | +/* Parsing error (from OpenSSL functions) */ |
| 58 | +#define COUNTERSIGNATURE_VFY_CANT_PARSE 1 |
| 59 | +/* Signers certificate is missing */ |
| 60 | +#define COUNTERSIGNATURE_VFY_NO_SIGNER_CERT 2 |
| 61 | +/* Unknown algorithm, can't proceed with verification */ |
| 62 | +#define COUNTERSIGNATURE_VFY_UNKNOWN_ALGORITHM 3 |
| 63 | +/* Verification failed, digest mismatch */ |
| 64 | +#define COUNTERSIGNATURE_VFY_INVALID 4 |
| 65 | +/* Failed to decrypt countersignature enc_digest for verification */ |
| 66 | +#define COUNTERSIGNATURE_VFY_CANT_DECRYPT_DIGEST 5 |
| 67 | +/* No digest saved inside the countersignature */ |
| 68 | +#define COUNTERSIGNATURE_VFY_DIGEST_MISSING 6 |
| 69 | +/* Message digest inside countersignature doesn't match signature it countersigns */ |
| 70 | +#define COUNTERSIGNATURE_VFY_DOESNT_MATCH_SIGNATURE 7 |
| 71 | +/* Non verification errors - allocations etc. */ |
| 72 | +#define COUNTERSIGNATURE_VFY_INTERNAL_ERROR 8 |
| 73 | +/* Time is missing in the timestamp signature */ |
| 74 | +#define COUNTERSIGNATURE_VFY_TIME_MISSING 9 |
| 75 | + |
| 76 | +typedef struct { |
| 77 | + uint8_t* data; |
| 78 | + int len; |
| 79 | +} ByteArray; |
| 80 | + |
| 81 | +typedef struct { /* Various X509 attributes parsed out in raw bytes*/ |
| 82 | + ByteArray country; |
| 83 | + ByteArray organization; |
| 84 | + ByteArray organizationalUnit; |
| 85 | + ByteArray nameQualifier; |
| 86 | + ByteArray state; |
| 87 | + ByteArray commonName; |
| 88 | + ByteArray serialNumber; |
| 89 | + ByteArray locality; |
| 90 | + ByteArray title; |
| 91 | + ByteArray surname; |
| 92 | + ByteArray givenName; |
| 93 | + ByteArray initials; |
| 94 | + ByteArray pseudonym; |
| 95 | + ByteArray generationQualifier; |
| 96 | + ByteArray emailAddress; |
| 97 | +} Attributes; |
| 98 | + |
| 99 | +typedef struct { |
| 100 | + long version; /* Raw version of X509 */ |
| 101 | + char* issuer; /* Oneline name of Issuer */ |
| 102 | + char* subject; /* Oneline name of Subject */ |
| 103 | + char* serial; /* Serial number in format 00:01:02:03:04... */ |
| 104 | + ByteArray sha1; /* SHA1 of the DER representation of the cert */ |
| 105 | + ByteArray sha256; /* SHA256 of the DER representation of the cert */ |
| 106 | + char* key_alg; /* Name of the key algorithm */ |
| 107 | + char* sig_alg; /* Name of the signature algorithm */ |
| 108 | + char* sig_alg_oid; /* OID of the signature algorithm */ |
| 109 | + time_t not_before; /* NotBefore validity */ |
| 110 | + time_t not_after; /* NotAfter validity */ |
| 111 | + char* key; /* PEM encoded public key */ |
| 112 | + Attributes issuer_attrs; /* Parsed X509 Attributes of Issuer */ |
| 113 | + Attributes subject_attrs; /* Parsed X509 Attributes of Subject */ |
| 114 | +} Certificate; |
| 115 | + |
| 116 | +typedef struct { |
| 117 | + Certificate** certs; |
| 118 | + size_t count; |
| 119 | +} CertificateArray; |
| 120 | + |
| 121 | +typedef struct { |
| 122 | + int verify_flags; /* COUNTERISGNATURE_VFY_ flag */ |
| 123 | + time_t sign_time; /* Signing time of the timestamp countersignature */ |
| 124 | + char* digest_alg; /* Name of the digest algorithm used */ |
| 125 | + ByteArray digest; /* Stored message digest */ |
| 126 | + CertificateArray* chain; /* Certificate chain of the signer */ |
| 127 | +} Countersignature; |
| 128 | + |
| 129 | +typedef struct { |
| 130 | + Countersignature** counters; |
| 131 | + size_t count; |
| 132 | +} CountersignatureArray; |
| 133 | + |
| 134 | +typedef struct { /* Represents SignerInfo structure */ |
| 135 | + ByteArray digest; /* Message Digest of the SignerInfo */ |
| 136 | + char* digest_alg; /* name of the digest algorithm */ |
| 137 | + char* program_name; /* Program name stored in SpcOpusInfo structure of Authenticode */ |
| 138 | + CertificateArray* chain; /* Certificate chain of the signer */ |
| 139 | +} Signer; |
| 140 | + |
| 141 | +typedef struct { |
| 142 | + int verify_flags; /* AUTHENTICODE_VFY_ flag */ |
| 143 | + int version; /* Raw PKCS7 version */ |
| 144 | + char* digest_alg; /* name of the digest algorithm */ |
| 145 | + ByteArray digest; /* File Digest stored in the Signature */ |
| 146 | + ByteArray file_digest; /* Actual calculated file digest */ |
| 147 | + Signer* signer; /* SignerInfo information of the Authenticode */ |
| 148 | + CertificateArray* certs; /* All certificates in the Signature including the ones in timestamp |
| 149 | + countersignatures */ |
| 150 | + CountersignatureArray* countersigs; /* Array of timestamp countersignatures */ |
| 151 | +} Authenticode; |
| 152 | + |
| 153 | +typedef struct { |
| 154 | + Authenticode** signatures; |
| 155 | + size_t count; |
| 156 | +} AuthenticodeArray; |
| 157 | + |
| 158 | +/** |
| 159 | + * @brief Initializes all globals OpenSSl objects we need for parsing, this is not thread-safe and |
| 160 | + * needs to be called only once, before any multithreading environment |
| 161 | + * https://github.com/openssl/openssl/issues/13524 |
| 162 | + */ |
| 163 | +void initialize_authenticode_parser(); |
| 164 | + |
| 165 | +/** |
| 166 | + * @brief Constructs AuthenticodeArray from PE file data. Authenticode can |
| 167 | + * contains nested Authenticode signatures as its unsigned attribute, |
| 168 | + * which can also contain nested signatures. For this reason the function returns |
| 169 | + * an Array of parsed Authenticode signatures. Any field of the parsed out |
| 170 | + * structures can be NULL, depending on the input data. |
| 171 | + * Verification result is stored in verify_flags with the first verification error. |
| 172 | + * |
| 173 | + * @param pe_data PE binary data |
| 174 | + * @param pe_len |
| 175 | + * @return AuthenticodeArray* |
| 176 | + */ |
| 177 | +AuthenticodeArray* parse_authenticode(const uint8_t* pe_data, uint64_t pe_len); |
| 178 | + |
| 179 | +/** |
| 180 | + * @brief Constructs AuthenticodeArray from binary data containing Authenticode |
| 181 | + * signature. Authenticode can contains nested Authenticode signatures |
| 182 | + * as its unsigned attribute, which can also contain nested signatures. |
| 183 | + * For this reason the function return an Array of parsed Authenticode signatures. |
| 184 | + * Any field of the parsed out structures can be NULL, depending on the input data. |
| 185 | + * WARNING: in case of this interface, the file and signature digest comparison is |
| 186 | + * up to the library user, as there is no pe data to calculate file digest from. |
| 187 | + * Verification result is stored in verify_flags with the first verification error |
| 188 | + * |
| 189 | + * @param data Binary data containing Authenticode signature |
| 190 | + * @param len |
| 191 | + * @return AuthenticodeArray* |
| 192 | + */ |
| 193 | +AuthenticodeArray* authenticode_new(const uint8_t* data, long len); |
| 194 | + |
| 195 | +/** |
| 196 | + * @brief Deallocates AuthenticodeArray and all it's allocated members |
| 197 | + * |
| 198 | + * @param auth |
| 199 | + */ |
| 200 | +void authenticode_array_free(AuthenticodeArray* auth); |
| 201 | + |
| 202 | +#ifdef __cplusplus |
| 203 | +} |
| 204 | +#endif |
| 205 | + |
| 206 | +#endif |
0 commit comments