/security/nss/cmd/signver/signver.c

http://github.com/zpao/v8monkey · C · 347 lines · 260 code · 41 blank · 46 comment · 53 complexity · b536f1170063a6caa468a0732260d771 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #include "secutil.h"
  37. #include "secmod.h"
  38. #include "cert.h"
  39. #include "secoid.h"
  40. #include "nss.h"
  41. /* NSPR 2.0 header files */
  42. #include "prinit.h"
  43. #include "prprf.h"
  44. #include "prsystem.h"
  45. #include "prmem.h"
  46. /* Portable layer header files */
  47. #include "plstr.h"
  48. #include "sechash.h" /* for HASH_GetHashObject() */
  49. static PRBool debugInfo;
  50. static PRBool verbose;
  51. static PRBool doVerify;
  52. static PRBool displayAll;
  53. static const char * const usageInfo[] = {
  54. "signver - verify a detached PKCS7 signature - Version " NSS_VERSION,
  55. "Commands:",
  56. " -A display all information from pkcs #7",
  57. " -V verify the signed object and display result",
  58. "Options:",
  59. " -a signature file is ASCII",
  60. " -d certdir directory containing cert database",
  61. " -i dataFileName input file containing signed data (default stdin)",
  62. " -o outputFileName output file name, default stdout",
  63. " -s signatureFileName input file for signature (default stdin)",
  64. " -v display verbose reason for failure"
  65. };
  66. static int nUsageInfo = sizeof(usageInfo)/sizeof(char *);
  67. extern int SV_PrintPKCS7ContentInfo(FILE *, SECItem *);
  68. static void Usage(char *progName, FILE *outFile)
  69. {
  70. int i;
  71. fprintf(outFile, "Usage: %s [ commands ] options\n", progName);
  72. for (i = 0; i < nUsageInfo; i++)
  73. fprintf(outFile, "%s\n", usageInfo[i]);
  74. exit(-1);
  75. }
  76. static HASH_HashType
  77. AlgorithmToHashType(SECAlgorithmID *digestAlgorithms)
  78. {
  79. SECOidTag tag = SECOID_GetAlgorithmTag(digestAlgorithms);
  80. HASH_HashType hash = HASH_GetHashTypeByOidTag(tag);
  81. return hash;
  82. }
  83. static SECStatus
  84. DigestContent (SECItem * digest, SECItem * content, HASH_HashType hashType)
  85. {
  86. unsigned int maxLen = digest->len;
  87. unsigned int len = HASH_ResultLen(hashType);
  88. SECStatus rv;
  89. if (len > maxLen) {
  90. PORT_SetError(SEC_ERROR_OUTPUT_LEN);
  91. return SECFailure;
  92. }
  93. rv = HASH_HashBuf(hashType, digest->data, content->data, content->len);
  94. if (rv == SECSuccess)
  95. digest->len = len;
  96. return rv;
  97. }
  98. enum {
  99. cmd_DisplayAllPCKS7Info = 0,
  100. cmd_VerifySignedObj
  101. };
  102. enum {
  103. opt_ASCII,
  104. opt_CertDir,
  105. opt_InputDataFile,
  106. opt_ItemNumber,
  107. opt_OutputFile,
  108. opt_InputSigFile,
  109. opt_PrintWhyFailure,
  110. opt_DebugInfo
  111. };
  112. static secuCommandFlag signver_commands[] =
  113. {
  114. { /* cmd_DisplayAllPCKS7Info*/ 'A', PR_FALSE, 0, PR_FALSE },
  115. { /* cmd_VerifySignedObj */ 'V', PR_FALSE, 0, PR_FALSE }
  116. };
  117. static secuCommandFlag signver_options[] =
  118. {
  119. { /* opt_ASCII */ 'a', PR_FALSE, 0, PR_FALSE },
  120. { /* opt_CertDir */ 'd', PR_TRUE, 0, PR_FALSE },
  121. { /* opt_InputDataFile */ 'i', PR_TRUE, 0, PR_FALSE },
  122. { /* opt_OutputFile */ 'o', PR_TRUE, 0, PR_FALSE },
  123. { /* opt_InputSigFile */ 's', PR_TRUE, 0, PR_FALSE },
  124. { /* opt_PrintWhyFailure */ 'v', PR_FALSE, 0, PR_FALSE },
  125. { /* opt_DebugInfo */ 0, PR_FALSE, 0, PR_FALSE, "debug" }
  126. };
  127. int main(int argc, char **argv)
  128. {
  129. PRFileDesc *contentFile = NULL;
  130. PRFileDesc *signFile = PR_STDIN;
  131. FILE * outFile = stdout;
  132. char * progName;
  133. SECStatus rv;
  134. int result = 1;
  135. SECItem pkcs7der, content;
  136. secuCommand signver;
  137. pkcs7der.data = NULL;
  138. content.data = NULL;
  139. signver.numCommands = sizeof(signver_commands) /sizeof(secuCommandFlag);
  140. signver.numOptions = sizeof(signver_options) / sizeof(secuCommandFlag);
  141. signver.commands = signver_commands;
  142. signver.options = signver_options;
  143. #ifdef XP_PC
  144. progName = strrchr(argv[0], '\\');
  145. #else
  146. progName = strrchr(argv[0], '/');
  147. #endif
  148. progName = progName ? progName+1 : argv[0];
  149. rv = SECU_ParseCommandLine(argc, argv, progName, &signver);
  150. if (SECSuccess != rv) {
  151. Usage(progName, outFile);
  152. }
  153. debugInfo = signver.options[opt_DebugInfo ].activated;
  154. verbose = signver.options[opt_PrintWhyFailure ].activated;
  155. doVerify = signver.commands[cmd_VerifySignedObj].activated;
  156. displayAll= signver.commands[cmd_DisplayAllPCKS7Info].activated;
  157. if (!doVerify && !displayAll)
  158. doVerify = PR_TRUE;
  159. /* Set the certdb directory (default is ~/.netscape) */
  160. rv = NSS_Init(SECU_ConfigDirectory(signver.options[opt_CertDir].arg));
  161. if (rv != SECSuccess) {
  162. SECU_PrintPRandOSError(progName);
  163. return result;
  164. }
  165. /* below here, goto cleanup */
  166. SECU_RegisterDynamicOids();
  167. /* Open the input content file. */
  168. if (signver.options[opt_InputDataFile].activated &&
  169. signver.options[opt_InputDataFile].arg) {
  170. if (PL_strcmp("-", signver.options[opt_InputDataFile].arg)) {
  171. contentFile = PR_Open(signver.options[opt_InputDataFile].arg,
  172. PR_RDONLY, 0);
  173. if (!contentFile) {
  174. PR_fprintf(PR_STDERR,
  175. "%s: unable to open \"%s\" for reading.\n",
  176. progName, signver.options[opt_InputDataFile].arg);
  177. goto cleanup;
  178. }
  179. } else
  180. contentFile = PR_STDIN;
  181. }
  182. /* Open the input signature file. */
  183. if (signver.options[opt_InputSigFile].activated &&
  184. signver.options[opt_InputSigFile].arg) {
  185. if (PL_strcmp("-", signver.options[opt_InputSigFile].arg)) {
  186. signFile = PR_Open(signver.options[opt_InputSigFile].arg,
  187. PR_RDONLY, 0);
  188. if (!signFile) {
  189. PR_fprintf(PR_STDERR,
  190. "%s: unable to open \"%s\" for reading.\n",
  191. progName, signver.options[opt_InputSigFile].arg);
  192. goto cleanup;
  193. }
  194. }
  195. }
  196. if (contentFile == PR_STDIN && signFile == PR_STDIN && doVerify) {
  197. PR_fprintf(PR_STDERR,
  198. "%s: cannot read both content and signature from standard input\n",
  199. progName);
  200. goto cleanup;
  201. }
  202. /* Open|Create the output file. */
  203. if (signver.options[opt_OutputFile].activated) {
  204. outFile = fopen(signver.options[opt_OutputFile].arg, "w");
  205. if (!outFile) {
  206. PR_fprintf(PR_STDERR, "%s: unable to open \"%s\" for writing.\n",
  207. progName, signver.options[opt_OutputFile].arg);
  208. goto cleanup;
  209. }
  210. }
  211. /* read in the input files' contents */
  212. rv = SECU_ReadDERFromFile(&pkcs7der, signFile,
  213. signver.options[opt_ASCII].activated);
  214. if (signFile != PR_STDIN)
  215. PR_Close(signFile);
  216. if (rv != SECSuccess) {
  217. SECU_PrintError(progName, "problem reading PKCS7 input");
  218. goto cleanup;
  219. }
  220. if (contentFile) {
  221. rv = SECU_FileToItem(&content, contentFile);
  222. if (contentFile != PR_STDIN)
  223. PR_Close(contentFile);
  224. if (rv != SECSuccess)
  225. content.data = NULL;
  226. }
  227. /* Signature Verification */
  228. if (doVerify) {
  229. SEC_PKCS7ContentInfo *cinfo;
  230. SEC_PKCS7SignedData *signedData;
  231. HASH_HashType digestType;
  232. PRBool contentIsSigned;
  233. cinfo = SEC_PKCS7DecodeItem(&pkcs7der, NULL, NULL, NULL, NULL,
  234. NULL, NULL, NULL);
  235. if (cinfo == NULL) {
  236. PR_fprintf(PR_STDERR, "Unable to decode PKCS7 data\n");
  237. goto cleanup;
  238. }
  239. /* below here, goto done */
  240. contentIsSigned = SEC_PKCS7ContentIsSigned(cinfo);
  241. if (debugInfo) {
  242. PR_fprintf(PR_STDERR, "Content is%s encrypted.\n",
  243. SEC_PKCS7ContentIsEncrypted(cinfo) ? "" : " not");
  244. }
  245. if (debugInfo || !contentIsSigned) {
  246. PR_fprintf(PR_STDERR, "Content is%s signed.\n",
  247. contentIsSigned ? "" : " not");
  248. }
  249. if (!contentIsSigned)
  250. goto done;
  251. signedData = cinfo->content.signedData;
  252. /* assume that there is only one digest algorithm for now */
  253. digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]);
  254. if (digestType == HASH_AlgNULL) {
  255. PR_fprintf(PR_STDERR, "Invalid hash algorithmID\n");
  256. goto done;
  257. }
  258. if (content.data) {
  259. SECCertUsage usage = certUsageEmailSigner;
  260. SECItem digest;
  261. unsigned char digestBuffer[HASH_LENGTH_MAX];
  262. if (debugInfo)
  263. PR_fprintf(PR_STDERR, "contentToVerify=%s\n", content.data);
  264. digest.data = digestBuffer;
  265. digest.len = sizeof digestBuffer;
  266. if (DigestContent(&digest, &content, digestType)) {
  267. SECU_PrintError(progName, "Message digest computation failure");
  268. goto done;
  269. }
  270. if (debugInfo) {
  271. unsigned int i;
  272. PR_fprintf(PR_STDERR, "Data Digest=:");
  273. for (i = 0; i < digest.len; i++)
  274. PR_fprintf(PR_STDERR, "%02x:", digest.data[i]);
  275. PR_fprintf(PR_STDERR, "\n");
  276. }
  277. fprintf(outFile, "signatureValid=");
  278. PORT_SetError(0);
  279. if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage,
  280. &digest, digestType, PR_FALSE)) {
  281. fprintf(outFile, "yes");
  282. } else {
  283. fprintf(outFile, "no");
  284. if (verbose) {
  285. fprintf(outFile, ":%s",
  286. SECU_Strerror(PORT_GetError()));
  287. }
  288. }
  289. fprintf(outFile, "\n");
  290. result = 0;
  291. }
  292. done:
  293. SEC_PKCS7DestroyContentInfo(cinfo);
  294. }
  295. if (displayAll) {
  296. if (SV_PrintPKCS7ContentInfo(outFile, &pkcs7der))
  297. result = 1;
  298. }
  299. cleanup:
  300. SECITEM_FreeItem(&pkcs7der, PR_FALSE);
  301. SECITEM_FreeItem(&content, PR_FALSE);
  302. if (NSS_Shutdown() != SECSuccess) {
  303. result = 1;
  304. }
  305. return result;
  306. }