certinit.c
上传用户:lyxiangda
上传日期:2007-01-12
资源大小:3042k
文件大小:13k
源码类别:

CA认证

开发平台:

WINDOWS

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is the Netscape security libraries.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are 
  16.  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s):
  20.  * 
  21.  * Alternatively, the contents of this file may be used under the
  22.  * terms of the GNU General Public License Version 2 or later (the
  23.  * "GPL"), in which case the provisions of the GPL are applicable 
  24.  * instead of those above.  If you wish to allow use of your 
  25.  * version of this file only under the terms of the GPL and not to
  26.  * allow others to use your version of this file under the MPL,
  27.  * indicate your decision by deleting the provisions above and
  28.  * replace them with the notice and other provisions required by
  29.  * the GPL.  If you do not delete the provisions above, a recipient
  30.  * may use your version of this file under either the MPL or the
  31.  * GPL.
  32.  */
  33. #include "cert.h"
  34. #include "base64.h"
  35. #include "mcom_db.h"
  36. #include "certdb.h"
  37. #ifdef STATIC_CERT_INIT
  38. static char example_com_server_ca[] =
  39. "MIICBTCCAW6gAwIBAgIBATANBgkqhkiG9w0BAQQFADA+MREwDwYICZIm9ZgeZAET"
  40. "A2NvbTEVMBMGCAmSJvWYHmQBEwdFeGFtcGxlMRIwEAYDVQQDEwlTZXJ2ZXIgQ0Ew"
  41. "HhcNMDAwMjAzMjIyMDA3WhcNMTAwNTAzMjIyMDA3WjA+MREwDwYICZIm9ZgeZAET"
  42. "A2NvbTEVMBMGCAmSJvWYHmQBEwdFeGFtcGxlMRIwEAYDVQQDEwlTZXJ2ZXIgQ0Ew"
  43. "gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALGiKEvTd2k4ZJbdAVWokfFlB6Hz"
  44. "WJXveXm8+IgmFlgtAnicZI11z5wAutFRvDpun7WmRLgHxvEhU3tLoiACGYdGJXPw"
  45. "+lI2pzHzFSd63B0qcA/NVAW3EOBJeaEFwy0jkUaCIki8qQV06g8RosNX/zv6a+OF"
  46. "d5NMpS0fecK4fEvdAgMBAAGjEzARMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcN"
  47. "AQEEBQADgYEAi5rFiG6afWS1PHigssk2LwAJws5cszPbVIeIMHCBbtu259V7uWts"
  48. "gNxUPJRjeQBsK0ItAfinC0xxLeuMbRfIdZoRYv/OYDxCwGW7hUcNLi+fHlGnJNXH"
  49. "TWaCRdOwkljnws4v8ABas2DYA/k7xUFAygkIJd9NtE29ZrdrWpfSavI=";
  50. static char example_com_individual_ca[] =
  51. "MIICDTCCAXagAwIBAgIBAjANBgkqhkiG9w0BAQQFADBCMREwDwYICZIm9ZgeZAET"
  52. "A2NvbTEVMBMGCAmSJvWYHmQBEwdFeGFtcGxlMRYwFAYDVQQDEw1JbmRpdmlkdWFs"
  53. "IENBMB4XDTAwMDIwMzIyMjE1NFoXDTEwMDUwMzIyMjE1NFowQjERMA8GCAmSJvWY"
  54. "HmQBEwNjb20xFTATBggJkib1mB5kARMHRXhhbXBsZTEWMBQGA1UEAxMNSW5kaXZp"
  55. "ZHVhbCBDQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAu5syfboe93MOkGec"
  56. "dOuJholyX42wcaH/RgnL3C/8NnZp9WWaTaguvn7KrbCj4TAMzu0pabUN8apB3J60"
  57. "9C/FlixjXF7r73OzbyTCM5ja6/bPfmHMPmDl9l/9tKqhh+loFvRizXDaWSFRViDS"
  58. "XvKNeQztwwAOpEAqnJwyTkn4FjECAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zAN"
  59. "BgkqhkiG9w0BAQQFAAOBgQB1XK+5pXdXYq3O3TC/ZY5LWlZ7zuoWUO75OpuMY7XF"
  60. "iW/jeXbVT5IYZXoRGXJFGGaDmnAuK1/m6FTDhjSTG0XUmd5tg4aFieI+LY4rkYEv"
  61. "mbJElxKabXl5hVD4mg2bwYlFY7XBmifTa1Ll3HDX3VZM0DC1bm4KCHBnY0qXjSYq"
  62. "PA==";
  63. static char example_com_objsign_ca[] =
  64. "MIICETCCAXqgAwIBAgIBAzANBgkqhkiG9w0BAQQFADBEMREwDwYICZIm9ZgeZAET"
  65. "A2NvbTEVMBMGCAmSJvWYHmQBEwdFeGFtcGxlMRgwFgYDVQQDEw9Db2RlIFNpZ25p"
  66. "bmcgQ0EwHhcNMDAwMjAzMjIyMzEzWhcNMTAwNTAzMjIyMzEzWjBEMREwDwYICZIm"
  67. "9ZgeZAETA2NvbTEVMBMGCAmSJvWYHmQBEwdFeGFtcGxlMRgwFgYDVQQDEw9Db2Rl"
  68. "IFNpZ25pbmcgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALcy76InmpM9"
  69. "S9K2MlNSjusx6nkYWWbx7eDRTV+xhRPeDxW4t8jtKPqDF5LTusyM9WCI/nneqsIP"
  70. "7iTSHpxlGx37J1VbqKX5fZsfJ3wKv6ZIylzeRuFY9MFypPA2UmVd1ACDOUB3YDvY"
  71. "mrCVkOPEhjnZKbq4FfCpf8KNL2A5EBcZAgMBAAGjEzARMA8GA1UdEwEB/wQFMAMB"
  72. "Af8wDQYJKoZIhvcNAQEEBQADgYEAI0IXzwgBRXvow3JQi8Y4YdG2wZc4BWRGW87x"
  73. "2zOD7GOA0CWN149vb6rEchECykDsJj9LoBl6o1aRxk9WkIFnXmMOJSuJA+ilCe//"
  74. "81a5OhKbe0p7ym6rh190BLwh2VePFeyabq6NipfZlN6qgWUzoepf+jVblufW/2EI"
  75. "fbMSylc=";
  76. #endif
  77. /* This is the cert->certKey (serial number and issuer name) of
  78.  * the cert that we want to revoke.
  79.  */
  80. static unsigned char revoked_system_principal_key[] = {
  81. 0x40, 0x18, 0xf2, 0x35, 0x86, 0x06, 0x78, 0xce, 0x87, 0x89,
  82. 0x0c, 0x5d, 0x68, 0x67, 0x33, 0x09, 0x30, 0x81, 0xc1, 0x31,
  83. 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x16,
  84. 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x54,
  85. 0x72, 0x75, 0x73, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77, 0x6f,
  86. 0x72, 0x6b, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
  87. 0x0b, 0x13, 0x0e, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
  88. 0x6e, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x3a, 0x30,
  89. 0x38, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x31, 0x56, 0x65,
  90. 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x4f, 0x62, 0x6a,
  91. 0x65, 0x63, 0x74, 0x20, 0x53, 0x69, 0x67, 0x6e, 0x69, 0x6e,
  92. 0x67, 0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x43, 0x6c, 0x61,
  93. 0x73, 0x73, 0x20, 0x33, 0x20, 0x4f, 0x72, 0x67, 0x61, 0x6e,
  94. 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x31, 0x49, 0x30,
  95. 0x47, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x40, 0x77, 0x77,
  96. 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e,
  97. 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x50, 0x53, 0x20, 0x49,
  98. 0x6e, 0x63, 0x6f, 0x72, 0x70, 0x2e, 0x62, 0x79, 0x20, 0x52,
  99. 0x65, 0x66, 0x2e, 0x20, 0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c,
  100. 0x49, 0x54, 0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x28, 0x63,
  101. 0x29, 0x39, 0x37, 0x20, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69,
  102. 0x67, 0x6e
  103. };
  104. SECStatus
  105. CERT_CheckForEvilCert(CERTCertificate *cert)
  106. {
  107.     if ( cert->certKey.len == sizeof(revoked_system_principal_key) ) {
  108.         if ( PORT_Memcmp(cert->certKey.data,
  109.                          revoked_system_principal_key,
  110.                          sizeof(revoked_system_principal_key)) == 0 ) {
  111.             return(SECFailure);
  112.         }
  113.     }
  114.     return(SECSuccess);
  115. }
  116. #ifdef STATIC_CERT_INIT
  117. #define DEFAULT_TRUST_FLAGS (CERTDB_VALID_CA | 
  118.                              CERTDB_TRUSTED_CA | 
  119.                              CERTDB_NS_TRUSTED_CA)
  120. typedef enum {
  121.     certUpdateNone,
  122.     certUpdateAdd,
  123.     certUpdateDelete,
  124.     certUpdateAddTrust,
  125.     certUpdateRemoveTrust,
  126.     certUpdateSetTrust
  127. } certUpdateOp;
  128. typedef struct {
  129.     char *cert;
  130.     char *nickname;
  131.     CERTCertTrust trust;
  132.     int updateVersion;
  133.     certUpdateOp op;
  134.     CERTCertTrust trustDelta;
  135. } certInitEntry;
  136. static certInitEntry initialcerts[] = {
  137.   {
  138.     example_com_server_ca,
  139.     "Example.com Server CA",
  140.     { DEFAULT_TRUST_FLAGS | CERTDB_GOVT_APPROVED_CA, 0, 0 },
  141.     1,
  142.     certUpdateAdd,
  143.     { 0, 0, 0 }
  144.   },
  145.   {
  146.     example_com_server_ca,
  147.     "Example.com Server CA",
  148.     { DEFAULT_TRUST_FLAGS | CERTDB_GOVT_APPROVED_CA, 0, 0 },
  149.     2,
  150.     certUpdateAddTrust,
  151.     { CERTDB_GOVT_APPROVED_CA, 0, 0 }
  152.   },
  153.   {
  154.     example_com_individual_ca,
  155.     "Example.com Individual CA",
  156.     { 0, DEFAULT_TRUST_FLAGS, 0 },
  157.     1,
  158.     certUpdateAdd,
  159.     { 0, 0, 0 }
  160.   },
  161.   {
  162.     example_com_individual_ca,
  163.     "Example.com Individual CA",
  164.     { 0, DEFAULT_TRUST_FLAGS, 0 },
  165.     2,
  166.     certUpdateRemoveTrust,
  167.     { 0, 0, DEFAULT_TRUST_FLAGS }
  168.   },
  169.   {
  170.     example_com_objsign_ca,
  171.     "Example.com Code Signing CA",
  172.     { 0, 0, DEFAULT_TRUST_FLAGS },
  173.     2,
  174.     certUpdateAdd,
  175.     { 0, 0, 0 }
  176.   },
  177.   {
  178.         0, 0
  179.   }
  180. };
  181. static SECStatus
  182. ConvertAndCheckCertificate(CERTCertDBHandle *handle, char *asciicert,
  183.                            char *nickname, CERTCertTrust *trust)
  184. {
  185.     SECItem sdder;
  186.     SECStatus rv;
  187.     CERTCertificate *cert;
  188.     PRBool conflict;
  189.     SECItem derSubject;
  190.     /* First convert ascii to binary */
  191.     rv = ATOB_ConvertAsciiToItem (&sdder, asciicert);
  192.     if (rv != SECSuccess) {
  193.         return(rv);
  194.     }
  195.     /*
  196.     ** Inside the ascii is a Signed Certificate.
  197.     */
  198.     cert = NULL;
  199.     /* make sure that no conflicts exist */
  200.     conflict = SEC_CertDBKeyConflict(&sdder, handle);
  201.     if ( conflict ) {
  202.         goto done;
  203.     }
  204.     rv = CERT_NameFromDERCert(&sdder, &derSubject);
  205.     if ( rv != SECSuccess ) {
  206.         goto loser;
  207.     }
  208.     conflict = SEC_CertNicknameConflict(nickname, &derSubject, handle);
  209.     if ( conflict ) {
  210.         goto done;
  211.     }
  212.     cert = CERT_NewTempCertificate(handle, &sdder, NULL, PR_FALSE, PR_TRUE);
  213.     if ( cert == NULL ) {
  214.         goto loser;
  215.     }
  216.     rv = CERT_AddTempCertToPerm(cert, nickname, trust);
  217.     CERT_DestroyCertificate(cert);
  218.     if (rv == SECSuccess) {
  219.         /*
  220.         ** XXX should verify signatures too, if we have the certificate for
  221.         ** XXX its issuer...
  222.         */
  223.     }
  224. done:
  225.     PORT_Free(sdder.data);
  226.     return(rv);
  227. loser:
  228.     return(SECFailure);
  229. }
  230. #endif
  231. extern void certdb_InitDBLock(void);
  232. SECStatus
  233. CERT_InitCertDB(CERTCertDBHandle *handle)
  234. {
  235. #ifdef STATIC_CERT_INIT
  236.     SECStatus rv;
  237.     certInitEntry *entry;
  238.     certdb_InitDBLock();
  239.     entry = initialcerts;
  240.     while ( entry->cert != NULL) {
  241.         if ( entry->op != certUpdateDelete ) {
  242.             rv = ConvertAndCheckCertificate(handle, entry->cert,
  243.                                             entry->nickname, &entry->trust);
  244.             /* keep going */
  245.         }
  246.         entry++;
  247.     }
  248. done:
  249.     CERT_SetDBContentVersion(CERT_DB_CONTENT_VERSION, handle);
  250.     return(rv);
  251. #else
  252.     certdb_InitDBLock();
  253.     CERT_SetDBContentVersion(0, handle);
  254.     return(SECSuccess);
  255. #endif
  256. }
  257. #ifdef STATIC_CERT_INIT
  258. static CERTCertificate *
  259. CertFromEntry(CERTCertDBHandle *handle, char *asciicert)
  260. {
  261.     SECItem sdder;
  262.     SECStatus rv;
  263.     CERTCertificate *cert;
  264.     /* First convert ascii to binary */
  265.     rv = ATOB_ConvertAsciiToItem (&sdder, asciicert);
  266.     if (rv != SECSuccess) {
  267.         return(NULL);
  268.     }
  269.     /*
  270.     ** Inside the ascii is a Signed Certificate.
  271.     */
  272.     cert = CERT_NewTempCertificate(handle, &sdder, NULL, PR_FALSE, PR_TRUE);
  273.     return(cert);
  274. }
  275. #endif
  276. SECStatus
  277. CERT_AddNewCerts(CERTCertDBHandle *handle)
  278. {
  279. #ifdef STATIC_CERT_INIT
  280.     int oldversion;
  281.     int newversion;
  282.     certInitEntry *entry;
  283.     CERTCertTrust tmptrust;
  284.     SECStatus rv;
  285.     CERTCertificate *cert;
  286.     newversion = CERT_DB_CONTENT_VERSION;
  287.     oldversion = CERT_GetDBContentVersion(handle);
  288.     if ( newversion > oldversion ) {
  289.         entry = initialcerts;
  290.         while ( entry->cert != NULL ) {
  291.             if ( entry->updateVersion > oldversion ) {
  292.                 switch ( entry->op ) {
  293.                   default:
  294.                     break;
  295.                   case certUpdateAdd:
  296.                     rv = ConvertAndCheckCertificate(handle, entry->cert,
  297.                                                     entry->nickname,
  298.                                                     &entry->trust);
  299.                     break;
  300.                   case certUpdateDelete:
  301.                     cert = CertFromEntry(handle, entry->cert);
  302.                     if ( cert != NULL ) {
  303.                         if ( cert->isperm ) {
  304.                             rv = SEC_DeletePermCertificate(cert);
  305.                         }
  306.                         CERT_DestroyCertificate(cert);
  307.                     }
  308.                     break;
  309.                   case certUpdateAddTrust:
  310.                     cert = CertFromEntry(handle, entry->cert);
  311.                     if ( cert != NULL ) {
  312.                         if ( cert->isperm ) {
  313.                             tmptrust = *cert->trust;
  314.                             tmptrust.sslFlags |= entry->trustDelta.sslFlags;
  315.                             tmptrust.emailFlags |=
  316.                                 entry->trustDelta.emailFlags;
  317.                             tmptrust.objectSigningFlags |=
  318.                                 entry->trustDelta.objectSigningFlags;
  319.                             rv = CERT_ChangeCertTrust(handle, cert,
  320. &tmptrust);
  321.                         }
  322.                         CERT_DestroyCertificate(cert);
  323.                     }
  324.                     break;
  325.                   case certUpdateRemoveTrust:
  326.                     cert = CertFromEntry(handle, entry->cert);
  327.                     if ( cert != NULL ) {
  328.                         if ( cert->isperm ) {
  329.                             tmptrust = *cert->trust;
  330.                             tmptrust.sslFlags &=
  331.                                 (~entry->trustDelta.sslFlags);
  332.                             tmptrust.emailFlags &=
  333.                                 (~entry->trustDelta.emailFlags);
  334.                             tmptrust.objectSigningFlags &=
  335.                                 (~entry->trustDelta.objectSigningFlags);
  336.                             rv = CERT_ChangeCertTrust(handle, cert,
  337. &tmptrust);
  338.                         }
  339.                         CERT_DestroyCertificate(cert);
  340.                     }
  341.                     break;
  342.                   case certUpdateSetTrust:
  343.                     cert = CertFromEntry(handle, entry->cert);
  344.                     if ( cert != NULL ) {
  345.                         if ( cert->isperm ) {
  346.                             tmptrust = *cert->trust;
  347.                             tmptrust.sslFlags = entry->trustDelta.sslFlags;
  348.                             tmptrust.emailFlags =
  349.                                 entry->trustDelta.emailFlags;
  350.                             tmptrust.objectSigningFlags =
  351.                                 entry->trustDelta.objectSigningFlags;
  352.                             rv = CERT_ChangeCertTrust(handle, cert,
  353. &tmptrust);
  354.                         }
  355.                         CERT_DestroyCertificate(cert);
  356.                     }
  357.                     break;
  358.                 }
  359.             }
  360.             entry++;
  361.         }
  362.         CERT_SetDBContentVersion(newversion, handle);
  363.     }
  364. #endif
  365.     return(SECSuccess);
  366. }