pki_crl.cpp
上传用户:stc1860
上传日期:2007-01-12
资源大小:234k
文件大小:4k
源码类别:

CA认证

开发平台:

MultiPlatform

  1. /*
  2.  * Copyright (C) 2001 Christian Hohnstaedt.
  3.  *
  4.  *  All rights reserved.
  5.  *
  6.  *
  7.  *  Redistribution and use in source and binary forms, with or without 
  8.  *  modification, are permitted provided that the following conditions are met:
  9.  *
  10.  *  - Redistributions of source code must retain the above copyright notice,
  11.  *    this list of conditions and the following disclaimer.
  12.  *  - Redistributions in binary form must reproduce the above copyright notice,
  13.  *    this list of conditions and the following disclaimer in the documentation
  14.  *    and/or other materials provided with the distribution.
  15.  *  - Neither the name of the author nor the names of its contributors may be 
  16.  *    used to endorse or promote products derived from this software without
  17.  *    specific prior written permission.
  18.  *
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  *
  33.  * This program links to software with different licenses from:
  34.  *
  35.  * http://www.openssl.org which includes cryptographic software
  36.  *  written by Eric Young (eay@cryptsoft.com)"
  37.  *
  38.  * http://www.sleepycat.com
  39.  *
  40.  * http://www.trolltech.com
  41.  * 
  42.  *
  43.  *
  44.  * http://www.hohnstaedt.de/xca
  45.  * email: christian@hohnstaedt.de
  46.  *
  47.  * $Id: pki_crl.cpp,v 1.7 2003/01/06 19:35:51 chris Exp $
  48.  *
  49.  */                           
  50. #include "pki_crl.h"
  51. pki_crl::pki_crl(const string d, pki_x509 *iss )
  52. :pki_base(d)
  53. issuer = iss;
  54. crl = NULL;
  55. if (!iss) openssl_error("no issuer");
  56. crl = X509_CRL_new();
  57. X509V3_set_ctx(&ctx, issuer->cert, NULL, NULL, crl, 0);
  58. X509_CRL_INFO *ci = crl->crl;
  59. openssl_error();
  60. ci->issuer = X509_NAME_dup(issuer->cert->cert_info->subject);
  61. ci->lastUpdate = ASN1_UTCTIME_new();
  62. X509_gmtime_adj(ci->lastUpdate,0);
  63. ci->nextUpdate=ASN1_UTCTIME_new();
  64. X509_gmtime_adj(ci->nextUpdate, (issuer->getCrlDays())*24*60*60);
  65. ci->version = ASN1_INTEGER_new();
  66. ASN1_INTEGER_set(ci->version,1); /* version 2 CRL */
  67. openssl_error();
  68. className="pki_crl";
  69. }
  70. pki_crl::~pki_crl()
  71. {
  72. X509_CRL_free(crl);
  73. }
  74. void pki_crl::addRevoked(const pki_x509 *client)
  75. {
  76. X509_REVOKED *rev = NULL;
  77. if (!crl) openssl_error("crl disappeared");
  78. X509_CRL_INFO *ci = crl->crl;
  79. if (!client || !client->revoked) return;
  80. if (client->psigner != issuer) return;
  81. rev = X509_REVOKED_new();
  82. openssl_error();
  83. rev->revocationDate = M_ASN1_TIME_dup(client->revoked);
  84. rev->serialNumber = ASN1_INTEGER_dup(X509_get_serialNumber(client->cert));
  85. sk_X509_REVOKED_push(ci->revoked,rev);
  86. openssl_error();
  87. }
  88. void pki_crl::addV3ext(int nid, string exttext)
  89. X509_EXTENSION *ext;
  90. int len; 
  91. char *c = NULL;
  92. if ((len = exttext.length()) == 0) return;
  93. len++;
  94. c = (char *)OPENSSL_malloc(len);
  95. openssl_error();
  96. strncpy(c, exttext.c_str(), len);
  97. ext =  X509V3_EXT_conf_nid(NULL, &ctx, nid, c);
  98. OPENSSL_free(c);
  99. if (!ext) {
  100. string x="CRL v3 Extension: " + exttext;
  101. openssl_error(x);
  102. return;
  103. }
  104. X509_CRL_add_ext(crl, ext, -1);
  105. X509_EXTENSION_free(ext);
  106. openssl_error();
  107. }
  108. void pki_crl::sign(pki_key *key)
  109. {
  110. if (!key || key->isPubKey()) return;
  111. X509_CRL_sign(crl,key->key, EVP_md5());
  112. openssl_error();
  113. }
  114. void pki_crl::writeCrl(const string fname)
  115. {
  116. FILE *fp = fopen(fname.c_str(),"w");
  117. if (fp != NULL) {
  118.    if (crl){
  119. CERR("writing CRL");
  120. PEM_write_X509_CRL(fp, crl);
  121. openssl_error();
  122.    }
  123. }
  124. else fopen_error(fname);
  125. fclose(fp);
  126. }
  127. pki_x509 *pki_crl::getIssuer() { return issuer; }
  128. ASN1_TIME *pki_crl::getDate()
  129. {
  130. if (!crl || !crl->crl) return NULL;
  131. return crl->crl->lastUpdate;
  132. }