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

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_pkcs7.cpp,v 1.6 2003/01/06 19:35:51 chris Exp $
  48.  *
  49.  */                           
  50. #include "pki_pkcs7.h"
  51. pki_pkcs7::pki_pkcs7(const string d )
  52. :pki_base(d)
  53. p7 = NULL;
  54. certstack = NULL;
  55. className = "pki_pkcs7";
  56. }
  57. pki_pkcs7::~pki_pkcs7()
  58. {
  59. if (p7) PKCS7_free(p7);
  60. if (certstack) sk_X509_free(certstack);
  61. }
  62. void pki_pkcs7::encryptFile(pki_x509 *crt, string filename)
  63. {
  64. BIO *bio = NULL;
  65. if (!crt) return;
  66. bio = BIO_new_file(filename.c_str(), "r");
  67.         openssl_error();
  68. if (certstack) sk_X509_free(certstack);
  69. certstack = sk_X509_new_null();
  70. sk_X509_push(certstack, crt->getCert());
  71. openssl_error();
  72. if (p7) PKCS7_free(p7);
  73. p7 = PKCS7_encrypt(certstack, bio, EVP_des_ede3_cbc(), PKCS7_BINARY);
  74. openssl_error();
  75. sk_X509_free(certstack);
  76. certstack = NULL;
  77. }
  78. void pki_pkcs7::signBio(pki_x509 *crt, BIO *bio)
  79. {
  80. pki_key *privkey;
  81. if (!crt) return;
  82. privkey = crt->getKey();
  83. if (!privkey) throw errorEx("No private key for signing found", className);
  84. if (certstack) sk_X509_free(certstack);
  85. certstack = sk_X509_new_null();
  86. pki_x509 *signer = crt->getSigner();
  87. while (signer != NULL && signer != signer->getSigner()) {
  88. sk_X509_push(certstack, signer->getCert());
  89.         openssl_error();
  90. signer = signer->getSigner();
  91. CERR("SIGNER: "<<signer->getDescription())
  92. }
  93. if (p7) PKCS7_free(p7);
  94. p7 = PKCS7_sign(crt->getCert(), privkey->getKey(), certstack, bio, PKCS7_BINARY);
  95. openssl_error();
  96. sk_X509_free(certstack);
  97. certstack = NULL;
  98. }
  99. void pki_pkcs7::signFile(pki_x509 *crt, string filename)
  100. {
  101. BIO *bio = NULL;
  102. if (!crt) return;
  103. bio = BIO_new_file(filename.c_str(), "r");
  104.         openssl_error();
  105. signBio(crt, bio);
  106. BIO_free(bio);
  107. }
  108. void pki_pkcs7::signCert(pki_x509 *crt, pki_x509 *contCert)
  109. {
  110. BIO *bio = NULL;
  111. if (!crt) return;
  112. bio = BIO_new(BIO_s_mem());
  113.         openssl_error();
  114. i2d_X509_bio(bio, contCert->getCert());
  115. signBio(crt, bio);
  116. BIO_free(bio);
  117. }
  118. void pki_pkcs7::writeP7(string fname,bool PEM)
  119. {
  120. FILE *fp;
  121.         fp = fopen(fname.c_str(),"w");
  122.         if (fp != NULL) {
  123.            if (p7){
  124.                 if (PEM)
  125.                    PEM_write_PKCS7(fp, p7);
  126.                 else
  127.                    i2d_PKCS7_fp(fp, p7);
  128.                 openssl_error();
  129.            }
  130.         }
  131.         else fopen_error(fname);
  132.         fclose(fp);
  133. }
  134. pki_x509 *pki_pkcs7::getCert(int x) {
  135. if (!certstack) return NULL;
  136. pki_x509 *cert;
  137. cert = new pki_x509(X509_dup(sk_X509_value(certstack, x)));
  138. openssl_error();
  139. cert->setDescription("pk7-import");
  140. return cert;
  141. }
  142. int pki_pkcs7::numCert() {
  143. if (!certstack) return NULL;
  144. int n= sk_X509_num(certstack);
  145. openssl_error();
  146. return n;
  147. }
  148. void pki_pkcs7::readP7(string fname)
  149. {
  150. FILE *fp;
  151. fp = fopen(fname.c_str(), "rb");
  152.         if (fp) {
  153. p7 = PEM_read_PKCS7(fp, NULL, NULL, NULL);
  154.                 if (!p7) {
  155. ign_openssl_error();
  156. CERR("Fallback to DER encoded PKCS#7");
  157. p7 = d2i_PKCS7_fp(fp, &p7);
  158. }
  159. CERR("PK7");
  160. fclose(fp);
  161. openssl_error();
  162. }
  163. certstack = PKCS7_get0_signers(p7, NULL, 0);
  164. openssl_error();
  165. }