Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1995-96 Microsoft Corporation
  3. Module Name:
  4. certify.cxx
  5. Abstract:
  6. This is the command line tool to manipulate certificates on an executable image.
  7. Author:
  8. Revision History:
  9. --*/
  10. #define UNICODE 1
  11. #define _UNICODE 1
  12. #include <private.h>
  13. #if 1
  14. #define TEST 1
  15. #else
  16. #define TEST 0
  17. #endif
  18. void
  19. PrintUsage(
  20. VOID)
  21. {
  22. fputs("usage: CERTIFY [switches] image-names... \n"
  23. " [-?] display this message\n"
  24. " [-l] list the certificates in an image\n"
  25. " [-a:<Filename>] add a certificate file to an image\n"
  26. " [-r:<index>] remove certificate <index> from an image\n"
  27. " [-g:<Filename>] update any associated .DBG file\n"
  28. " [-s:<Filename>] used with -r to save the removed certificate\n",
  29. stderr
  30. );
  31. exit(-1);
  32. }
  33. #if TEST
  34. // Test routine
  35. BOOL fAllDataReturned;
  36. PVOID pvDataRefTest;
  37. DWORD FileSize;
  38. DWORD DataRead;
  39. BOOL
  40. WINAPI
  41. DigestRoutine (
  42. DIGEST_HANDLE DataReference,
  43. PBYTE pData,
  44. DWORD dwLength
  45. )
  46. {
  47. if (DataReference != pvDataRefTest) {
  48. return(FALSE);
  49. }
  50. // Attempt to read the range
  51. if (IsBadReadPtr(pData, dwLength)) {
  52. return(FALSE);
  53. }
  54. DataRead += dwLength;
  55. if (DataRead > FileSize) {
  56. return(FALSE);
  57. }
  58. return(TRUE);
  59. }
  60. #endif
  61. int __cdecl
  62. main(
  63. int argc,
  64. char *argv[],
  65. char *envp[]
  66. )
  67. {
  68. char c, *p;
  69. if (argc < 2) {
  70. PrintUsage();
  71. }
  72. while (--argc) {
  73. p = *++argv;
  74. if (*p == '/' || *p == '-') {
  75. while (c = *++p)
  76. switch (toupper( c )) {
  77. case '?':
  78. PrintUsage();
  79. break;
  80. case 'A':
  81. c = *++p;
  82. if (c != ':') {
  83. PrintUsage();
  84. } else {
  85. // Add a certificate file to an image.
  86. #if TEST
  87. // Test code
  88. WIN_CERTIFICATE wc;
  89. HANDLE Handle;
  90. DWORD Index;
  91. wc.dwLength = sizeof(WIN_CERTIFICATE);
  92. wc.wCertificateType = WIN_CERT_TYPE_X509;
  93. if ((Handle = CreateFile(TEXT("test.exe"),
  94. GENERIC_WRITE | GENERIC_READ,
  95. 0,
  96. 0,
  97. OPEN_EXISTING,
  98. FILE_ATTRIBUTE_NORMAL,
  99. NULL)) == INVALID_HANDLE_VALUE)
  100. {
  101. fputs("Unable to open test.exe", stderr);
  102. exit(1);
  103. }
  104. printf("ImageAddCertificate on test.exe returned: %d\n",
  105. ImageAddCertificate(Handle, &wc, &Index));
  106. printf("Index #: %d\n", Index);
  107. CloseHandle(Handle);
  108. exit(0);
  109. #else
  110. // The real code
  111. #endif
  112. }
  113. break;
  114. case 'L':
  115. // List the certificates in an image.
  116. #if TEST
  117. // Test code
  118. WIN_CERTIFICATE wc;
  119. HANDLE Handle;
  120. DWORD Index;
  121. if ((Handle = CreateFileA("test.exe",
  122. GENERIC_READ,
  123. 0,
  124. 0,
  125. OPEN_EXISTING,
  126. FILE_ATTRIBUTE_NORMAL,
  127. NULL)) == INVALID_HANDLE_VALUE)
  128. {
  129. fputs("Unable to open test.exe", stderr);
  130. exit(1);
  131. }
  132. ImageEnumerateCertificates(Handle, CERT_SECTION_TYPE_ANY, &Index, NULL, 0);
  133. printf("Enumerate lists: %d\n", Index);
  134. Index--;
  135. while (ImageGetCertificateHeader(Handle, Index, &wc)) {
  136. printf("Index: %d\n", Index);
  137. Index--;
  138. }
  139. CloseHandle(Handle);
  140. exit(0);
  141. #else
  142. // The real code
  143. #endif
  144. break;
  145. case 'R':
  146. c = *++p;
  147. if (c != ':') {
  148. PrintUsage();
  149. } else {
  150. // Remove a specific certificate from an image.
  151. #if TEST
  152. // Test code
  153. HANDLE Handle;
  154. if ((Handle = CreateFile(TEXT("test.exe"),
  155. GENERIC_WRITE | GENERIC_READ,
  156. 0,
  157. 0,
  158. OPEN_EXISTING,
  159. FILE_ATTRIBUTE_NORMAL,
  160. NULL)) == INVALID_HANDLE_VALUE)
  161. {
  162. fputs("Unable to open test.exe", stderr);
  163. exit(1);
  164. }
  165. printf("ImageRemoveCertificate(0) on test.exe returned: %d\n",
  166. ImageRemoveCertificate(Handle, 0));
  167. exit(0);
  168. #else
  169. // The real code
  170. #endif
  171. }
  172. break;
  173. case 'G':
  174. c = *++p;
  175. if (c != ':') {
  176. PrintUsage();
  177. } else {
  178. p++;
  179. // Generate a certificate from an image.
  180. #if TEST
  181. // Test code
  182. HANDLE Handle;
  183. LPSTR FileName=p;
  184. if ((Handle = CreateFileA(FileName,
  185. GENERIC_READ,
  186. 0,
  187. 0,
  188. OPEN_EXISTING,
  189. FILE_ATTRIBUTE_NORMAL,
  190. NULL)) == INVALID_HANDLE_VALUE)
  191. {
  192. fprintf(stderr, "Unable to open %s", FileName);
  193. exit(1);
  194. }
  195. FileSize = GetFileSize(Handle, NULL);
  196. DataRead = 0;
  197. pvDataRefTest = (PVOID) 1;
  198. printf("ImageGetDigestStream debug w/o resources on %s returned: %s\tGetLastError(): %d\n",
  199. FileName,
  200. ImageGetDigestStream(Handle,
  201. CERT_PE_IMAGE_DIGEST_DEBUG_INFO,
  202. DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
  203. GetLastError());
  204. printf("Message Stream Size: %d\n", DataRead);
  205. DataRead = 0;
  206. pvDataRefTest = (PVOID) 2;
  207. printf("ImageGetDigestStream debug w/ resources %s returned: %s\tGetLastError(): %d\n",
  208. FileName,
  209. ImageGetDigestStream(Handle,
  210. CERT_PE_IMAGE_DIGEST_DEBUG_INFO | CERT_PE_IMAGE_DIGEST_RESOURCES,
  211. DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
  212. GetLastError());
  213. printf("Message Stream Size: %d\n", DataRead);
  214. DataRead = 0;
  215. pvDataRefTest = (PVOID) 3;
  216. printf("ImageGetDigestStream w/o debug w/o resources on %s returned: %s\tGetLastError(): %d\n",
  217. FileName,
  218. ImageGetDigestStream(Handle,
  219. 0,
  220. DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
  221. GetLastError());
  222. printf("Message Stream Size: %d\n", DataRead);
  223. DataRead = 0;
  224. pvDataRefTest = (PVOID) 4;
  225. printf("ImageGetDigestStream w/o debug w/ resources %s returned: %s\tGetLastError(): %d\n",
  226. FileName,
  227. ImageGetDigestStream(Handle,
  228. CERT_PE_IMAGE_DIGEST_RESOURCES,
  229. DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
  230. GetLastError());
  231. printf("Message Stream Size: %d\n", DataRead);
  232. exit(0);
  233. #else
  234. // Real code
  235. #endif
  236. }
  237. break;
  238. case 'S':
  239. c = *++p;
  240. if (c != ':') {
  241. PrintUsage();
  242. } else {
  243. // Save the certificate in some file.
  244. }
  245. break;
  246. default:
  247. fprintf( stderr, "CERTIFY: Invalid switch - /%c\n", c );
  248. PrintUsage();
  249. break;
  250. }
  251. }
  252. }
  253. return 0;
  254. }