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

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 "@(#)Name.C 1.6 96/01/29 Sun Microsystems"
  31. #include <sys/types.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "Time.h"
  36. #include "Bigint.h"
  37. #include "Bstream.h"
  38. #include "ObjId.h"
  39. #include "asn1_der.h"
  40. #include "Name.h"
  41. /*
  42.  * Name class implementation.
  43.  */
  44. #ifndef __GNUC__
  45. Name::Name(int num_elems, ...)
  46. {
  47. Ava elem_val;
  48. va_list ap;
  49. avalist = NULL;
  50. va_start(ap, num_elems);
  51. while (num_elems--) {
  52. elem_val = va_arg(ap, Ava);
  53. add_element(elem_val);
  54. }
  55. va_end(ap);
  56. }
  57. #endif
  58. Name::~Name()
  59. {
  60. Ava *avap = avalist;
  61. while (avap) {
  62. Ava *tmpavap = avap->next;
  63. delete avap;
  64. avap = tmpavap;
  65. }
  66. avalist = NULL;
  67. }
  68. ObjId
  69. get_str_attrtype(Bstream& strname)
  70. {
  71. byte id[4];
  72. bzero(id, sizeof(id));
  73. (void)strname.fetchbyte(id[0]);
  74. (void)strname.fetchbyte(id[1]);
  75. (void)strname.fetchbyte(id[2]);
  76. if (bcmp(id, CON, 3) == 0) {
  77. return (countryName);
  78. } else if (bcmp(id, ORG, 3) == 0) {
  79. return (orgName);
  80. } else if (bcmp(id, STA, 3) == 0) {
  81. return (stateName);
  82. } else if (bcmp(id, LOC, 3) == 0) {
  83. return (localityName);
  84. (void)strname.fetchbyte(id[3]);
  85. if (bcmp(id, IPA, 4) == 0) {
  86. return (ipAddress);
  87. } else if (bcmp(id, OGU, 4) == 0) {
  88. return (orgUnitName);
  89. } else if (bcmp(id, CNM, 4) == 0) {
  90. return (commonName);
  91. } else if (bcmp(id, TIT, 4) == 0) {
  92. return (title);
  93. }
  94. printf("about to return null OIDn");
  95. return (NULL);
  96. }
  97. Bstream
  98. get_str_attrvalue(Bstream& strname)
  99. {
  100. byte tmp = 0;
  101. int len, origlen;
  102. Bstream val = strname;
  103. origlen = strname.getlength();
  104. for (len= 0, tmp = 0; tmp != '/'; len++) {
  105. (void) strname.peekbyte(tmp);
  106. if (len == origlen)
  107. break;
  108. }
  109. if (len != origlen)
  110. len--; // account for /, already seen
  111. val.truncate(origlen - len);
  112. strname.consume(len);
  113. return (val);
  114. }
  115. Name::Name(const char *name)
  116. {
  117. ObjId nulloid;
  118. avalist = NULL;
  119. Bstream charname(strlen(name), (byte *)name);
  120. while (charname.getlength() > 0) {
  121. Ava ava;
  122. ava.attributetype = get_str_attrtype(charname);
  123. if (ava.attributetype == nulloid) {
  124. printf("Invalid Name syntaxn");
  125. // XXX
  126. #ifndef __GNUC__
  127. Name::~Name();
  128. #endif
  129. break;
  130. }
  131. ava.attributevalue = get_str_attrvalue(charname);
  132. ava.next = NULL;
  133. add_element(ava);
  134. }
  135. }
  136. Name::Name(const Name &a)
  137. {
  138. Ava *avap = a.avalist;
  139. Ava *tmpavap, *lastavap;
  140. lastavap = NULL;
  141. while (avap) {
  142. tmpavap = new Ava;
  143. tmpavap->attributetype = avap->attributetype;
  144. tmpavap->attributevalue = avap->attributevalue;
  145. tmpavap->next = NULL;
  146. if (lastavap == NULL) {
  147. avalist = tmpavap;
  148. } else {
  149. lastavap->next = tmpavap;
  150. }
  151. lastavap = tmpavap;
  152. avap = avap->next;
  153. }
  154. return;
  155. }
  156. Name&
  157. Name:: operator =(const Name &a)
  158. {
  159. if (avalist == a.avalist) // check for a = a;
  160. return (*this);
  161. // Release any old name info that may be present
  162. // XXX - TM
  163. #ifndef __GNUC__
  164. Name::~Name();
  165. #endif
  166. Ava *avap = a.avalist;
  167. Ava *tmpavap, *lastavap;
  168. lastavap = NULL;
  169. while (avap) {
  170. tmpavap = new Ava;
  171. tmpavap->attributetype = avap->attributetype;
  172. tmpavap->attributevalue = avap->attributevalue;
  173. tmpavap->next = NULL;
  174. if (lastavap == NULL) {
  175. avalist = tmpavap;
  176. } else {
  177. lastavap->next = tmpavap;
  178. }
  179. lastavap = tmpavap;
  180. avap = avap->next;
  181. }
  182. return (*this);
  183. }
  184. Boolean operator ==(const Name& a, const Name& b)
  185. {
  186. Ava *aavap = a.avalist;
  187. Ava *bavap = b.avalist;
  188. while (aavap) {
  189. if (bavap == NULL)
  190. return (BOOL_FALSE);
  191. if (aavap->attributetype != bavap->attributetype)
  192. return (BOOL_FALSE);
  193. if (aavap->attributevalue != bavap->attributevalue)
  194. return (BOOL_FALSE);
  195. bavap = bavap->next;
  196. aavap = aavap->next;
  197. }
  198. if (bavap != NULL)
  199. return (BOOL_FALSE);
  200. return (BOOL_TRUE);
  201. }
  202. Boolean operator !=(const Name& a, const Name& b)
  203. {
  204. if (a == b)
  205. return (BOOL_FALSE);
  206. return (BOOL_TRUE);
  207. }
  208. void
  209. Name::add_element(const Ava &newval)
  210. {
  211. Ava *avap = avalist;
  212. Ava *lastavap;
  213. Ava *newavap = new Ava;
  214. newavap->next = NULL;
  215. newavap->attributetype = newval.attributetype;
  216. newavap->attributevalue = newval.attributevalue;
  217. if (avap == NULL) {
  218. avalist = newavap;
  219. return;
  220. }
  221. while (avap != NULL) {
  222. lastavap = avap;
  223. avap = avap->next;
  224. }
  225. lastavap->next = newavap;
  226. return;
  227. }
  228. String
  229. Name::getsuffix() const
  230. {
  231. Ava *avap = avalist;
  232. Ava *lastavap;
  233. String  suffix;
  234. if (avap == NULL) {
  235. return suffix;
  236. }
  237. while (avap != NULL) {
  238. lastavap = avap;
  239. avap = avap->next;
  240. }
  241. avap = lastavap;
  242. if (avap->attributetype == ipAddress) {
  243. suffix = "/IP=";
  244. } else if (avap->attributetype == commonName) {
  245. suffix = "/CN=";
  246. } else if (avap->attributetype == countryName) {
  247. suffix = "/C=";
  248. } else if (avap->attributetype == stateName) {
  249. suffix = "/S=";
  250. } else if (avap->attributetype == orgName) {
  251. suffix = "/O=";
  252. } else if (avap->attributetype == orgUnitName) {
  253. suffix = "/OU=";
  254. } else if (avap->attributetype == localityName) {
  255. suffix = "/L=";
  256. } else if (avap->attributetype == title) {
  257. suffix = "/T=";
  258. } else {
  259. suffix = "/OID="; 
  260. suffix += avap->attributetype.getoidstr();
  261. }
  262. suffix += avap->attributevalue.getstr();
  263. return suffix;
  264. }
  265. void
  266. Name::print() const
  267. {
  268. Ava *avap = avalist;
  269. while (avap) {
  270. if (avap->attributetype == ipAddress) {
  271. printf("/IP=");
  272. } else if (avap->attributetype == commonName) {
  273. printf("/CN=");
  274. } else if (avap->attributetype == countryName) {
  275. printf("/C=");
  276. } else if (avap->attributetype == stateName) {
  277. printf("/S=");
  278. } else if (avap->attributetype == orgName) {
  279. printf("/O=");
  280. } else if (avap->attributetype == orgUnitName) {
  281. printf("/OU=");
  282. } else if (avap->attributetype == localityName) {
  283. printf("/L=");
  284. } else if (avap->attributetype == title) {
  285. printf("/T=");
  286. } else {
  287. printf("/OID="); avap->attributetype.print();
  288. }
  289. avap->attributevalue.prints();
  290. avap = avap->next;
  291. }
  292. }
  293. String
  294. Name::getDN() const
  295. {
  296. String dn;
  297. Ava *avap = avalist;
  298. while (avap) {
  299. if (avap->attributetype == ipAddress) {
  300. dn += "/IP=";
  301. } else if (avap->attributetype == commonName) {
  302. dn += "/CN=";
  303. } else if (avap->attributetype == countryName) {
  304. dn += "/C=";
  305. } else if (avap->attributetype == stateName) {
  306. dn += "/S=";
  307. } else if (avap->attributetype == orgName) {
  308. dn += "/O=";
  309. } else if (avap->attributetype == orgUnitName) {
  310. dn += "/OU=";
  311. } else if (avap->attributetype == localityName) {
  312. dn += "/L=";
  313. } else if (avap->attributetype == title) {
  314. dn += "/T=";
  315. } else {
  316. dn += "/OID="; 
  317. dn += avap->attributetype.getoidstr();
  318. }
  319. dn += avap->attributevalue.getstr();
  320. avap = avap->next;
  321. }
  322. return (dn);
  323. }
  324. String
  325. Name::getip() const
  326. {
  327. Ava *avap = avalist;
  328. while (avap) {
  329. if (avap->attributetype == ipAddress) {
  330. Bstream retbstr = avap->attributevalue;
  331.   return (String((const char *)retbstr.getdatap(),
  332.      retbstr.getlength()));
  333. } else {
  334. avap = avap->next;
  335. }
  336. }
  337. return (String(""));
  338. }
  339. String
  340. Name::getcn() const
  341. {
  342. Ava *avap = avalist;
  343. while (avap) {
  344. if (avap->attributetype == commonName) {
  345. Bstream retbstr = avap->attributevalue;
  346.   return (String((const char *)retbstr.getdatap(),
  347.      retbstr.getlength()));
  348. } else {
  349. avap = avap->next;
  350. }
  351. }
  352. return (String(""));
  353. }
  354. String
  355. Name::getou() const
  356. {
  357. Ava *avap = avalist;
  358. while (avap) {
  359. if (avap->attributetype == orgUnitName) {
  360. Bstream retbstr = avap->attributevalue;
  361.   return (String((const char *)retbstr.getdatap(),
  362.      retbstr.getlength()));
  363. } else {
  364. avap = avap->next;
  365. }
  366. }
  367. return (String(""));
  368. }
  369. String
  370. Name::getorg() const
  371. {
  372. Ava *avap = avalist;
  373. while (avap) {
  374. if (avap->attributetype == orgName) {
  375. Bstream retbstr = avap->attributevalue;
  376.   return (String((const char *)retbstr.getdatap(),
  377. retbstr.getlength()));
  378. } else {
  379. avap = avap->next;
  380. }
  381. }
  382. return (String(""));
  383. }
  384. String
  385. Name::getstate() const
  386. {
  387. Ava *avap = avalist;
  388. while (avap) {
  389. if (avap->attributetype == stateName) {
  390. Bstream retbstr = avap->attributevalue;
  391.   return (String((const char *)retbstr.getdatap(),
  392. retbstr.getlength()));
  393. } else {
  394. avap = avap->next;
  395. }
  396. }
  397. return (String(""));
  398. }
  399. String
  400. Name::getcountry() const
  401. {
  402. Ava *avap = avalist;
  403. while (avap) {
  404. if (avap->attributetype == countryName) {
  405. Bstream retbstr = avap->attributevalue;
  406.   return (String((const char *)retbstr.getdatap(),
  407. retbstr.getlength()));
  408. } else {
  409. avap = avap->next;
  410. }
  411. }
  412. return (String(""));
  413. }
  414. /*
  415.  * X.500 Distinguished Name DER decoding
  416.  */
  417. int asn1_der_decode_name(Bstream& der_stream, Name& name)
  418. {
  419. byte tmp = 0;
  420. int  seqoflen, setoflen, seqlen, retval;
  421. int consumed, setoflenwhdr, seqlenwhdr;
  422. Name tmpname;
  423. retval = asn1_der_decode_sequence_of(der_stream, seqoflen);
  424. if (retval < 0) return (retval);
  425. while (seqoflen > 0) {
  426. setoflenwhdr = der_stream.getlength();
  427. retval = asn1_der_decode_set_of(der_stream, setoflen);
  428. if (retval < 0) return (retval);
  429. while (setoflen > 0) {
  430. Ava ava;
  431. seqlenwhdr = der_stream.getlength();
  432. retval = asn1_der_decode_sequence(der_stream, seqlen);
  433. if (retval < 0) return (retval);
  434. retval = asn1_der_decode_obj_id(der_stream, 
  435. ava.attributetype);
  436. if (retval < 0) return (retval);
  437. // switch on OID to figure out attribute value 
  438. // ASN1 type XXX
  439. retval = asn1_der_decode_printable_string(der_stream,
  440. ava.attributevalue);
  441. if (retval < 0) return (retval);
  442. tmpname.add_element(ava);
  443. consumed = seqlenwhdr - der_stream.getlength();
  444. setoflen -= consumed;
  445. }
  446. consumed = setoflenwhdr - der_stream.getlength();
  447. seqoflen -= consumed;
  448. }
  449. name = tmpname;
  450. return (SUCCESS);
  451. }
  452. /*
  453.  * Encode an X.500 Distinguished Name in ASN.1
  454.  */
  455. Bstream
  456. Name::encode()
  457. {
  458. Ava *avap = avalist;
  459. Bstream seqval;
  460. while (avap) {
  461. Bstream oid, ava, rdn, aval;
  462. oid = avap->attributetype.encode();
  463. aval = asn1_der_encode_printable_string(avap->attributevalue);
  464. ava = asn1_der_encode_sequence(oid+aval);
  465. rdn = asn1_der_encode_set_of(ava);
  466. seqval = seqval + rdn;
  467. avap = avap->next;
  468. }
  469. Bstream rdnseq = asn1_der_encode_sequence_of(seqval);
  470. return (rdnseq);
  471. }