pki_base.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_base.cpp,v 1.20 2003/01/06 19:35:51 chris Exp $
  48.  *
  49.  */                           
  50. #include "pki_base.h"
  51. pki_base::pki_base(const string d)
  52. {
  53. error = "";
  54. desc = d;
  55. className = "pki_base";
  56. pointer = NULL;
  57. }
  58. pki_base::pki_base()
  59. {
  60. error = "";
  61. desc = "";
  62. className = "pki_base";
  63. pointer=NULL;
  64. }
  65. pki_base::~pki_base(void)
  66. {}
  67. string pki_base::getDescription()
  68. {
  69. string x = desc;
  70. return x;
  71. }
  72. string pki_base::getError()
  73. {
  74. string x = error;
  75. error = "";
  76. return x;
  77. }
  78. string pki_base::getClassName()
  79. {
  80. return className;
  81. }
  82. void pki_base::setDescription(const string d)
  83. {
  84. desc = d;
  85. }
  86. void pki_base::fopen_error(const string fname)
  87. {
  88. string txt = "Error opening file: '" + fname + "'";
  89. openssl_error(txt);
  90. }
  91. void pki_base::openssl_error(const string myerr)
  92. {
  93. string errtxt = "";
  94. error = "";
  95. if (myerr != "") {
  96. CERR("PKI ERROR: " << myerr);
  97. error += myerr + "n";
  98. }
  99. while (int i = ERR_get_error() ) {
  100.    errtxt = ERR_error_string(i ,NULL);
  101.    CERR("OpenSSL: " << errtxt);
  102.    error += errtxt + "n";
  103. }
  104. if (!error.empty()) {
  105. throw errorEx(error, className);
  106. }
  107. }
  108. bool pki_base::ign_openssl_error()
  109. {
  110. // ignore openssl errors
  111. string errtxt;
  112. while (int i = ERR_get_error() ) {
  113.    errtxt = ERR_error_string(i ,NULL);
  114.    CERR("IGNORE -> OpenSSL: " << errtxt << " <- IGNORE");
  115. }
  116. return !errtxt.empty();
  117. }
  118. int pki_base::intToData(unsigned char **p, const int val)
  119. {
  120. int s = sizeof(int);
  121. memcpy(*p, &val, s);
  122. *p += s;
  123. return s;
  124. }
  125. int pki_base::intFromData(unsigned char **p)
  126. {
  127. int s = sizeof(int);
  128. int ret;
  129. memcpy(&ret, *p, s);
  130. *p += s;
  131. return ret;
  132. }
  133. int pki_base::boolToData(unsigned char **p, const bool val)
  134. {
  135. int s = sizeof(bool);
  136. memcpy(*p, &val, s);
  137. *p += s;
  138. return s;
  139. }
  140. bool pki_base::boolFromData(unsigned char **p)
  141. {
  142. int s = sizeof(bool);
  143. bool ret;
  144. memcpy(&ret, *p, s);
  145. *p += s;
  146. return ret;
  147. }
  148. int pki_base::stringToData(unsigned char **p, const string val)
  149. {
  150. int s = (val.length() +1) * sizeof(char);
  151. memcpy(*p, val.c_str(), s);
  152. *p += s;
  153. return s;
  154. }
  155. string pki_base::stringFromData(unsigned char **p)
  156. {
  157. string ret="";
  158. while(**p) {
  159. ret +=(char)**p;
  160. *p += sizeof(char);
  161. }
  162. *p += sizeof(char);
  163. return ret;
  164. }