pki_pkcs12.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_pkcs12.cpp,v 1.12 2003/01/06 19:35:51 chris Exp $
  48.  *
  49.  */                           
  50. #include "pki_pkcs12.h"
  51. pki_pkcs12::pki_pkcs12(const string d, pki_x509 *acert, pki_key *akey, pem_password_cb *cb):
  52. pki_base(d)
  53. {
  54. key = new pki_key(akey);
  55. cert = new pki_x509(acert);
  56. certstack = sk_X509_new_null();
  57. pkcs12 = NULL;
  58. passcb = cb;
  59. openssl_error();
  60. className="pki_pkcs12";
  61. }
  62. pki_pkcs12::pki_pkcs12(const string fname, pem_password_cb *cb)
  63. :pki_base(fname)
  64. FILE *fp;
  65. char pass[30];
  66. EVP_PKEY *mykey;
  67. X509 *mycert;
  68. key=NULL; cert=NULL; pkcs12=NULL;
  69. passcb = cb;
  70. certstack = sk_X509_new_null();
  71. PASS_INFO p;
  72. string title = XCA_TITLE;
  73. string description = "Please enter the password to encrypt the PKCS#12 file.";
  74. p.title = &title;
  75. p.description = &description;
  76. fp = fopen(fname.c_str(), "rb");
  77. if (fp) {
  78. pkcs12 = d2i_PKCS12_fp(fp, NULL);
  79. CERR("PK12");
  80. fclose(fp);
  81. openssl_error();
  82. passcb(pass, 30, 0, &p);
  83. CERR("PK12");
  84. PKCS12_parse(pkcs12, pass, &mykey, &mycert, &certstack);
  85. CERR("PK12");
  86. openssl_error();
  87. if (mykey) {
  88. key = new pki_key(mykey);
  89. key->setDescription("pk12-import");
  90. //EVP_PKEY_free(mykey);
  91. }
  92. if (mycert) {
  93. cert = new pki_x509(mycert);
  94. cert->setDescription("pk12-import");
  95. //X509_free(mycert);
  96. }
  97. }
  98. else fopen_error(fname);
  99. className="pki_pkcs12";
  100. }
  101. pki_pkcs12::~pki_pkcs12()
  102. {
  103. CERR("popping free certs");
  104. if (sk_X509_num(certstack)>0)
  105. sk_X509_pop_free(certstack, X509_free); // free the certs itself, because we own a copy of them
  106. CERR("deleting key");
  107. if (key) delete(key); 
  108. CERR( "deleting cert");
  109. if (cert) delete(cert);
  110. CERR("freeing PKCS12");
  111. PKCS12_free(pkcs12);
  112. openssl_error();
  113. }
  114. void pki_pkcs12::addCaCert(pki_x509 *ca)
  115. if (!ca) return;
  116. sk_X509_push(certstack, X509_dup(ca->getCert()));
  117. openssl_error();
  118. }
  119. void pki_pkcs12::writePKCS12(const string fname)
  120. char pass[30];
  121. char desc[100];
  122. strncpy(desc,getDescription().c_str(),100);
  123. PASS_INFO p;
  124. string title = XCA_TITLE;
  125. string description = "Please enter the password to encrypt the PKCS#12 file";
  126. p.title = &title;
  127. p.description = &description;
  128. if (!pkcs12) {
  129. if (cert == NULL || key == NULL) {
  130. openssl_error("No key or no Cert and no pkcs12....");
  131. }
  132. passcb(pass, 30, 0, &p); 
  133. CERR( desc << key->getKey() << cert->getCert() );
  134. CERR("before PKCS12_create....");
  135. pkcs12 = PKCS12_create(pass, desc, key->getKey(), cert->getCert(), certstack, 0, 0, 0, 0, 0);
  136. openssl_error();
  137. CERR("after PKCS12_create....");
  138. }
  139. FILE *fp = fopen(fname.c_str(),"wb");
  140. if (fp != NULL) {
  141.     CERR("writing PKCS#12");
  142.             i2d_PKCS12_fp(fp, pkcs12);
  143.             openssl_error();
  144.     fclose (fp);
  145.         }
  146. else fopen_error(fname);
  147. }
  148. int pki_pkcs12::numCa() {
  149. int n= sk_X509_num(certstack);
  150. openssl_error();
  151. return n;
  152. }
  153. pki_key *pki_pkcs12::getKey() {
  154. return new pki_key(key);
  155. }
  156. pki_x509 *pki_pkcs12::getCert() {
  157. return new pki_x509(cert);
  158. }
  159. pki_x509 *pki_pkcs12::getCa(int x) {
  160. pki_x509 *cert;
  161. cert = new pki_x509(X509_dup(sk_X509_value(certstack, x)));
  162. openssl_error();
  163. cert->setDescription("pk12-import");
  164. return cert;
  165. }