Ocean
Signature.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_IO_SIGNATURE_H
9 #define META_OCEAN_IO_SIGNATURE_H
10 
11 #include "ocean/io/IO.h"
12 
13 namespace Ocean
14 {
15 
16 namespace IO
17 {
18 
19 /**
20  * This class implements functions allowing to sign data or to provide hash values for data.
21  * @ingroup io
22  */
23 class OCEAN_IO_EXPORT Signature
24 {
25  public:
26 
27  /**
28  * Determines the CRC32 hash for a specified data buffer.
29  * @param buffer The buffer for which the hash will be determined, must be valid
30  * @param bufferSize The size of the buffer in bytes, with range (0, 2^32 - 1)
31  * @return The resulting CRC32 hash value for the given buffer
32  */
33  static unsigned int crc32(const unsigned char* buffer, const size_t bufferSize);
34 
35 #if 0
36 
37  /**
38  * Determines the MD5 hash for a specified data buffer.
39  * @param buffer The buffer for which the hash will be determined, may be nullptr if bufferSize is 0
40  * @param bufferSize The size of the buffer in bytes, with range [0, infinity)
41  * @param hash The resulting MD5 hash value for the given buffer
42  */
43  static void md5(const unsigned char* buffer, const size_t bufferSize, unsigned char hash[16]);
44 
45  /**
46  * Determines the SHA1 hash for a specified data buffer.
47  * @param buffer The buffer for which the hash will be determined, may be nullptr if bufferSize is 0
48  * @param bufferSize The size of the buffer in bytes, with range [0, infinity)
49  * @param hash The resulting SHA1 hash value for the given buffer
50  */
51  static void sha1(const unsigned char* buffer, const size_t bufferSize, unsigned char hash[20]);
52 
53  /**
54  * Determines the keyed hash message authentication code using SHA1 for a specified data buffer and secrete key.
55  * @param buffer The buffer for which the hash will be determined, may be nullptr if bufferSize is 0
56  * @param bufferSize The size of the buffer in bytes, with range [0, infinity)
57  * @param key The key which will be used to determine the hash, may be nullptr if keySize is 0
58  * @param keySize The size of the key in bytes, with range [0, infinity)
59  * @param hash The resulting SHA1 hash value for the given buffer
60  */
61  static void hmacSHA1(const unsigned char* buffer, const size_t bufferSize, const unsigned char* key, const size_t keySize, unsigned char hash[20]);
62 
63 #endif
64 
65 #if defined(OCEAN_PLATFORM_BUILD_APPLE_MACOS) || defined(OCEAN_PLATFORM_BUILD_WINDOWS)
66 
67  /**
68  * Evaluates if the code signature of the specified file exists and is trusted by the platform-specfic certificate store.
69  * On Apple platforms the subject name may contain a team identifier code (e.g., subject (ABCDEFG))
70  * @param filePath File path to signed code file
71  * @param trustedCodeSignature Receives true if the code signature of the file is valid and trusted; otherwise false is returned.
72  * @param subjectName Optional parameter that receives the subject name
73  * @return True if signature evaluation and subject name determination succeeded; otherwise, false is returned.
74  * @sa evaluateCodeSignatureAppleMacos(), evaluateCodeSignatureWindows()
75  */
76  static inline bool evaluateCodeSignature(const std::wstring& filePath, bool& trustedCodeSignature, std::wstring* subjectName);
77 
78 #endif
79 
80  private:
81 
82 #ifdef OCEAN_PLATFORM_BUILD_APPLE_MACOS
83 
84  /**
85  * Evaluates if the code signature of the specified file exists and is trusted by the certificate store on macOS.
86  * The subject name may contain a team identifier code (e.g., subject (ABCDEFG))
87  * @param filePath File path to signed code file
88  * @param trustedCodeSignature Receives true if the code signature of the file is valid and trusted; otherwise false is returned.
89  * @param subjectName Optional parameter that receives the subject name
90  * @return Ture if signature evaluation and subject name determination succeeded; otherwise, false is returned.
91  * @sa evaluateCodeSignature()
92  */
93  static bool evaluateCodeSignatureAppleMacos(const std::wstring& filePath, bool& trustedCodeSignature, std::wstring* subjectName);
94 
95 #endif
96 
97 #ifdef OCEAN_PLATFORM_BUILD_WINDOWS
98 
99  /**
100  * Evaluates if the code signature of the specified file exists and is trusted by the certificate store on Windows.
101  * On Apple platforms the subject name may contain a team identifier code (e.g., subject (ABCDEFG))
102  * @param filePath File path to signed code file
103  * @param trustedCodeSignature Receives true if the code signature of the file is valid and trusted; otherwise false is returned.
104  * @param subjectName Optional parameter that receives the subject name
105  * @return True if signature evaluation and subject name determination succeeded; otherwise, false is returned.
106  * @sa evaluateCodeSignatureAppleMacos()
107  */
108  static bool evaluateCodeSignatureWindows(const std::wstring& filePath, bool& trustedCodeSignature, std::wstring* subjectName);
109 
110  /**
111  * Returns the subject name of the digital signature for the specified file.
112  * The method will fail if the specified file has no digital signature.
113  * @param filePath File path to signed code file
114  * @param subjectName Receives the subject name
115  * @return Ture if succeeded; otherwise, false is returned.
116  */
117  static bool determineSignatureSubjectName(const std::wstring& filePath, std::wstring& subjectName);
118 
119 #endif
120 
121 };
122 
123 #if defined(OCEAN_PLATFORM_BUILD_APPLE_MACOS) || defined(OCEAN_PLATFORM_BUILD_WINDOWS)
124 
125 inline bool Signature::evaluateCodeSignature(const std::wstring& filePath, bool& trustedCodeSignature, std::wstring* subjectName)
126 {
127 #ifdef OCEAN_PLATFORM_BUILD_WINDOWS
128 
129  return Signature::evaluateCodeSignatureWindows(filePath, trustedCodeSignature, subjectName);
130 
131 #elif defined(OCEAN_PLATFORM_BUILD_APPLE_MACOS)
132 
133  return Signature::evaluateCodeSignatureAppleMacos(filePath, trustedCodeSignature, subjectName);
134 
135 #endif
136 }
137 
138 #endif
139 
140 }
141 
142 }
143 
144 #endif // META_OCEAN_IO_SIGNATURE_H
This class implements functions allowing to sign data or to provide hash values for data.
Definition: Signature.h:24
static void hmacSHA1(const unsigned char *buffer, const size_t bufferSize, const unsigned char *key, const size_t keySize, unsigned char hash[20])
Determines the keyed hash message authentication code using SHA1 for a specified data buffer and secr...
static void md5(const unsigned char *buffer, const size_t bufferSize, unsigned char hash[16])
Determines the MD5 hash for a specified data buffer.
static bool evaluateCodeSignatureAppleMacos(const std::wstring &filePath, bool &trustedCodeSignature, std::wstring *subjectName)
Evaluates if the code signature of the specified file exists and is trusted by the certificate store ...
static unsigned int crc32(const unsigned char *buffer, const size_t bufferSize)
Determines the CRC32 hash for a specified data buffer.
static bool determineSignatureSubjectName(const std::wstring &filePath, std::wstring &subjectName)
Returns the subject name of the digital signature for the specified file.
static void sha1(const unsigned char *buffer, const size_t bufferSize, unsigned char hash[20])
Determines the SHA1 hash for a specified data buffer.
static bool evaluateCodeSignatureWindows(const std::wstring &filePath, bool &trustedCodeSignature, std::wstring *subjectName)
Evaluates if the code signature of the specified file exists and is trusted by the certificate store ...
static bool evaluateCodeSignature(const std::wstring &filePath, bool &trustedCodeSignature, std::wstring *subjectName)
Evaluates if the code signature of the specified file exists and is trusted by the platform-specfic c...
Definition: Signature.h:125
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15