/security/nss/cmd/pp/pp.c

http://github.com/zpao/v8monkey · C · 193 lines · 131 code · 19 blank · 43 comment · 39 complexity · 36a33e019967825ae108c41b8b174228 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. /*
  37. * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
  38. * keys, pkcs7)
  39. *
  40. * $Id: pp.c,v 1.10 2010/09/03 19:25:02 nelson%bolyard.com Exp $
  41. */
  42. #include "secutil.h"
  43. #if defined(__sun) && !defined(SVR4)
  44. extern int fprintf(FILE *, char *, ...);
  45. #endif
  46. #include "plgetopt.h"
  47. #include "pk11func.h"
  48. #include "nspr.h"
  49. #include "nss.h"
  50. static void Usage(char *progName)
  51. {
  52. fprintf(stderr,
  53. "Usage: %s -t type [-a] [-i input] [-o output]\n",
  54. progName);
  55. fprintf(stderr, "%-20s Specify the input type (must be one of %s,\n",
  56. "-t type", SEC_CT_PRIVATE_KEY);
  57. fprintf(stderr, "%-20s %s, %s, %s,\n", "", SEC_CT_PUBLIC_KEY,
  58. SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
  59. fprintf(stderr, "%-20s %s, %s or %s)\n", "", SEC_CT_PKCS7, SEC_CT_CRL,
  60. SEC_CT_NAME);
  61. fprintf(stderr, "%-20s Input is in ascii encoded form (RFC1113)\n",
  62. "-a");
  63. fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
  64. "-i input");
  65. fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
  66. "-o output");
  67. exit(-1);
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. int rv, ascii;
  72. char *progName;
  73. FILE *outFile;
  74. PRFileDesc *inFile;
  75. SECItem der, data;
  76. char *typeTag;
  77. PLOptState *optstate;
  78. progName = strrchr(argv[0], '/');
  79. progName = progName ? progName+1 : argv[0];
  80. ascii = 0;
  81. inFile = 0;
  82. outFile = 0;
  83. typeTag = 0;
  84. optstate = PL_CreateOptState(argc, argv, "at:i:o:");
  85. while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
  86. switch (optstate->option) {
  87. case '?':
  88. Usage(progName);
  89. break;
  90. case 'a':
  91. ascii = 1;
  92. break;
  93. case 'i':
  94. inFile = PR_Open(optstate->value, PR_RDONLY, 0);
  95. if (!inFile) {
  96. fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
  97. progName, optstate->value);
  98. return -1;
  99. }
  100. break;
  101. case 'o':
  102. outFile = fopen(optstate->value, "w");
  103. if (!outFile) {
  104. fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
  105. progName, optstate->value);
  106. return -1;
  107. }
  108. break;
  109. case 't':
  110. typeTag = strdup(optstate->value);
  111. break;
  112. }
  113. }
  114. PL_DestroyOptState(optstate);
  115. if (!typeTag) Usage(progName);
  116. if (!inFile) inFile = PR_STDIN;
  117. if (!outFile) outFile = stdout;
  118. PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
  119. rv = NSS_NoDB_Init(NULL);
  120. if (rv != SECSuccess) {
  121. fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
  122. progName, SECU_Strerror(PORT_GetError()));
  123. exit(1);
  124. }
  125. SECU_RegisterDynamicOids();
  126. rv = SECU_ReadDERFromFile(&der, inFile, ascii);
  127. if (rv != SECSuccess) {
  128. fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
  129. exit(1);
  130. }
  131. /* Data is untyped, using the specified type */
  132. data.data = der.data;
  133. data.len = der.len;
  134. /* Pretty print it */
  135. if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0) {
  136. rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
  137. SECU_PrintCertificate);
  138. } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0) {
  139. rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
  140. SECU_PrintCertificateRequest);
  141. } else if (PORT_Strcmp (typeTag, SEC_CT_CRL) == 0) {
  142. rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
  143. #ifdef HAVE_EPV_TEMPLATE
  144. } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0) {
  145. rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
  146. #endif
  147. } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0) {
  148. rv = SECU_PrintSubjectPublicKeyInfo(outFile, &data, "Public Key", 0);
  149. } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0) {
  150. rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
  151. "PKCS #7 Content Info", 0);
  152. } else if (PORT_Strcmp(typeTag, SEC_CT_NAME) == 0) {
  153. rv = SECU_PrintDERName(outFile, &data, "Name", 0);
  154. } else {
  155. fprintf(stderr, "%s: don't know how to print out '%s' files\n",
  156. progName, typeTag);
  157. SECU_PrintAny(outFile, &data, "File contains", 0);
  158. return -1;
  159. }
  160. if (inFile != PR_STDIN)
  161. PR_Close(inFile);
  162. PORT_Free(der.data);
  163. if (rv) {
  164. fprintf(stderr, "%s: problem converting data (%s)\n",
  165. progName, SECU_Strerror(PORT_GetError()));
  166. }
  167. if (NSS_Shutdown() != SECSuccess) {
  168. fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
  169. progName, SECU_Strerror(PORT_GetError()));
  170. rv = SECFailure;
  171. }
  172. PR_Cleanup();
  173. return rv;
  174. }