ObjId.C
上传用户:zbbssh
上传日期:2007-01-08
资源大小:196k
文件大小:7k
源码类别:

CA认证

开发平台:

C/C++

  1. /*
  2. ------------------------------------------------------------------
  3.   Copyright
  4.   Sun Microsystems, Inc.
  5.   Copyright (C) 1994, 1995, 1996 Sun Microsystems, Inc.  All Rights
  6.   Reserved.
  7.   Permission is hereby granted, free of charge, to any person
  8.   obtaining a copy of this software and associated documentation
  9.   files (the "Software"), to deal in the Software without
  10.   restriction, including without limitation the rights to use,
  11.   copy, modify, merge, publish, distribute, sublicense, and/or sell
  12.   copies of the Software or derivatives of the Software, and to 
  13.   permit persons to whom the Software or its derivatives is furnished 
  14.   to do so, subject to the following conditions:
  15.   The above copyright notice and this permission notice shall be
  16.   included in all copies or substantial portions of the Software.
  17.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18.   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19.   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20.   NONINFRINGEMENT.  IN NO EVENT SHALL SUN MICROSYSTEMS, INC., BE LIABLE
  21.   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22.   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.   CONNECTION WITH THE SOFTWARE OR DERIVATES OF THIS SOFTWARE OR 
  24.   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.   Except as contained in this notice, the name of Sun Microsystems, Inc.
  26.   shall not be used in advertising or otherwise to promote
  27.   the sale, use or other dealings in this Software or its derivatives 
  28.   without prior written authorization from Sun Microsystems, Inc.
  29. */
  30. #pragma ident "@(#)ObjId.C 1.4 96/01/29 Sun Microsystems"
  31. #include <sys/types.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include "my_types.h"
  35. #include "Time.h"
  36. #include "Bigint.h"
  37. #include "Bstream.h"
  38. #include "asn1_der.h"
  39. #include "ObjId.h"
  40. /*
  41.  * ObjId class implementation.
  42.  */
  43. #ifndef __GNUC__
  44. ObjId::ObjId(int num_elems, ...)
  45. {
  46. Bigint elem_int;
  47. va_list ap;
  48. obj_id_list = NULL;
  49. va_start(ap, num_elems);
  50. while (num_elems--) {
  51. elem_int = va_arg(ap, Bigint);
  52. add_element(elem_int);
  53. }
  54. va_end(ap);
  55. }
  56. #else
  57. ObjId::ObjId(int num_elems,
  58.                 Bigint b1,Bigint b2, Bigint b3, Bigint b4, Bigint b5, Bigint b6,
  59.                 Bigint b7,Bigint b8, Bigint b9, Bigint b10, Bigint b11,
  60.                 Bigint b12)
  61. {
  62.         obj_id_list = NULL;
  63.         if (num_elems > 0 )
  64.                 add_element(b1);
  65.         if (num_elems > 1)
  66.                 add_element(b2);
  67.         if (num_elems > 2)
  68.                 add_element(b3);
  69.         if (num_elems > 3)
  70.                 add_element(b4);
  71.         if (num_elems > 4)
  72.                 add_element(b5);
  73.         if (num_elems > 5)
  74.                 add_element(b6);
  75.         if (num_elems > 6)
  76.                 add_element(b7);
  77.         if (num_elems > 7)
  78.                 add_element(b8);
  79.         if (num_elems > 8)
  80.                 add_element(b9);
  81.         if (num_elems > 9)
  82.                 add_element(b10);
  83.         if (num_elems > 10)
  84.                 add_element(b11);
  85. }
  86. #endif
  87. ObjId::~ObjId()
  88. {
  89. obj_id_elem *oidp = obj_id_list;
  90. while (oidp != NULL) {
  91. obj_id_elem *tmpoidp = oidp->next;
  92. delete oidp;
  93. oidp = tmpoidp;
  94. }
  95. obj_id_list = NULL;
  96. }
  97. ObjId::ObjId(const ObjId &a)
  98. {
  99. obj_id_elem *oidp = a.obj_id_list;
  100. obj_id_elem *tmpoidp, *lastoidp;
  101. lastoidp = NULL;
  102. while (oidp != NULL) {
  103. tmpoidp = new obj_id_elem;
  104. tmpoidp->elemval = oidp->elemval;
  105. tmpoidp->next = NULL;
  106. if (lastoidp == NULL) {
  107. obj_id_list = tmpoidp;
  108. } else {
  109. lastoidp->next = tmpoidp;
  110. }
  111. lastoidp = tmpoidp;
  112. oidp = oidp->next;
  113. }
  114. return;
  115. }
  116. ObjId&
  117. ObjId:: operator =(const ObjId &a)
  118. {
  119. if (obj_id_list == a.obj_id_list) // Check for a = a;
  120. return (*this);
  121. // Release any old ObjId info that may be present
  122. // XXX memory leak?
  123. #ifndef __GNUC__
  124. ObjId::~ObjId();
  125. #endif
  126. obj_id_elem *oidp = a.obj_id_list;
  127. obj_id_elem *tmpoidp, *lastoidp;
  128. lastoidp = NULL;
  129. while (oidp != NULL) {
  130. tmpoidp = new obj_id_elem;
  131. tmpoidp->elemval = oidp->elemval;
  132. tmpoidp->next = NULL;
  133. if (lastoidp == NULL) {
  134. obj_id_list = tmpoidp;
  135. } else {
  136. lastoidp->next = tmpoidp;
  137. }
  138. lastoidp = tmpoidp;
  139. oidp = oidp->next;
  140. }
  141. return (*this);
  142. }
  143. Boolean operator ==(const ObjId& a, const ObjId& b)
  144. {
  145. obj_id_elem *aoidp = a.obj_id_list;
  146. obj_id_elem *boidp = b.obj_id_list;
  147. while (aoidp) {
  148. if (boidp == NULL)
  149. return (BOOL_FALSE);
  150. if (aoidp->elemval != boidp->elemval)
  151. return (BOOL_FALSE);
  152. boidp = boidp->next;
  153. aoidp = aoidp->next;
  154. }
  155. if (boidp != NULL)
  156. return (BOOL_FALSE);
  157. return (BOOL_TRUE);
  158. }
  159. Boolean operator !=(const ObjId& a, const ObjId& b)
  160. {
  161. if ( a == b ) 
  162. return BOOL_FALSE;
  163. else 
  164. return (BOOL_TRUE);
  165. }
  166. void
  167. ObjId::add_element(const Bigint &newval)
  168. {
  169. obj_id_elem *oidp = obj_id_list;
  170. obj_id_elem *lastoidp;
  171. obj_id_elem *newoidp = new obj_id_elem;
  172. newoidp->next = NULL;
  173. newoidp->elemval = newval;
  174. if (oidp == NULL) {
  175. obj_id_list = newoidp;
  176. return;
  177. }
  178. while (oidp != NULL) {
  179. lastoidp = oidp;
  180. oidp = oidp->next;
  181. }
  182. lastoidp->next = newoidp;
  183. return;
  184. }
  185. // Make integer sub ID into BER encoding of subid.
  186. // Tie together subid bytes using Bit 8 as end-of-subid
  187. Bstream
  188. subid_to_bstr(const Bigint& elem)
  189. {
  190. Bstream subid = Bigint_to_Bstr(elem);
  191. if (elem < 128) {
  192. return (subid);
  193. }
  194. int intlen = subid.getlength();
  195. byte *intstr = subid.getdatap();
  196. int len = intlen + 1 + (intlen/8);
  197. byte *subidber = new byte[len];
  198. byte *deletethis = subidber;
  199. byte carry = 0;
  200. byte hibit = 0;
  201. for (int i = len - 1, j = intlen - 1, k=0; i >= 0; i--, j--, k++) {
  202. byte part;
  203. if (j < 0) {
  204. part = carry | hibit;
  205. } else {
  206. part = ((intstr[j] & ~num_to_mask(k)) << (k%7));
  207. }
  208. subidber[i] = part | carry | hibit;
  209. if (subidber[i] == 0x80 && i == 0) {
  210. len--;
  211. subidber++;
  212. break;
  213. }
  214. carry = ((byte)(intstr[j] & num_to_mask(k)) >> (7-(k%7)));
  215. if (hibit == 0) 
  216. hibit = 0x80;
  217. }
  218. Bstream subidberstr = Bstream(len, subidber);
  219. delete deletethis;
  220. return (subidberstr);
  221. }
  222. // return ASN.1 BER encoding of an OBJECT-IDENTIFIER
  223. Bstream
  224. ObjId::encode() const
  225. {
  226. byte ID = OBJECT_IDENTIFIER_ID;
  227. Bstream IDstr(1, &ID);
  228. Bigint X, Y;
  229. obj_id_elem *oidp = obj_id_list;
  230. X = oidp->elemval;
  231. oidp = oidp->next;
  232. Y = oidp->elemval;
  233. oidp = oidp->next;
  234. Bigint subid = X*40 + Y;
  235. Bstream val = subid_to_bstr(subid);
  236. while (oidp) {
  237. val = val + subid_to_bstr(oidp->elemval);
  238. oidp = oidp->next;
  239. }
  240. Bstream lenstr = asn1_der_set_length(val.getlength());
  241. return (IDstr + lenstr + val);
  242. }
  243. void
  244. ObjId::print() const
  245. {
  246. obj_id_elem *oidp = obj_id_list;
  247. printf("{ ");
  248. while (oidp) {
  249. oidp->elemval.printd();
  250. oidp = oidp->next;
  251. printf(" ");
  252. }
  253. printf("}");
  254. }
  255. String
  256. ObjId::getoidstr() const
  257. {
  258. String oidstr;
  259. obj_id_elem *oidp = obj_id_list;
  260. oidstr += "{ ";
  261. while (oidp) {
  262. oidstr += oidp->elemval.getnumstrd();
  263. oidp = oidp->next;
  264. oidstr += " ";
  265. }
  266. oidstr += "}";
  267. return (oidstr);
  268. }