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

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 "%w% 96/01/29 Sun Microsystems"
  31. #include <sys/types.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include "Time.h"
  35. #include "Bigint.h"
  36. #include "Bstream.h"
  37. #include "asn1_der.h"
  38. #include "ObjId.h"
  39. #include "Name.h"
  40. #include "X509Cert.h"
  41. #include "Sig.h"
  42. #include "dsa.h"
  43. #include "utils.h"
  44. AlgId&
  45. AlgId::operator =(const AlgId& a)
  46. {
  47. algid = a.algid;
  48. params = a.params;
  49. return (*this);
  50. }
  51. /*
  52.  * X509Cert class implementation.
  53.  */
  54. X509Cert::X509Cert()
  55. {
  56. // Rely on private member constructors
  57. }
  58. X509Cert::~X509Cert()
  59. {
  60. // Rely on private member destructors
  61. }
  62. X509Cert::X509Cert(const X509Cert &a)
  63. {
  64. serialnum = a.serialnum;
  65. sigalg = a.sigalg;
  66. issuer = a.issuer;
  67. notbefore = a.notbefore;
  68. notafter = a.notafter;
  69. subject = a.subject;
  70. pubkey = a.pubkey;
  71. certdata = a.certdata;
  72. certsigdata = a.certsigdata;
  73. sigdata = a.sigdata;
  74. return;
  75. }
  76. X509Cert&
  77. X509Cert:: operator =(const X509Cert &a)
  78. {
  79. serialnum = a.serialnum;
  80. sigalg = a.sigalg;
  81. issuer = a.issuer;
  82. notbefore = a.notbefore;
  83. notafter = a.notafter;
  84. subject = a.subject;
  85. pubkey = a.pubkey;
  86. certdata = a.certdata;
  87. certsigdata = a.certsigdata;
  88. sigdata = a.sigdata;
  89. return (*this);
  90. }
  91. X509Cert::X509Cert(const Bigint& ser, const AlgId& sigalgid,
  92. const Name& issuername, const PCTime& notb,
  93. const PCTime& nota, const Name& subname,
  94. const PubKey& subpub)
  95. {
  96. serialnum = ser;
  97. sigalg = sigalgid;
  98. issuer = issuername;
  99. notbefore = notb;
  100. notafter = nota;
  101. subject = subname;
  102. pubkey = subpub;
  103. return;
  104. }
  105. X509Cert::X509Cert(const Bigint& ser, const AlgId& sigalgid,
  106. const Name& issuername, const PCTime& notb,
  107. const PCTime& nota, const Name& subname,
  108. const PubKey& subpub, const Bstream& cert, 
  109. const Bstream& certsigpart, const Bstream& casig)
  110. {
  111. serialnum = ser;
  112. sigalg = sigalgid;
  113. issuer = issuername;
  114. notbefore = notb;
  115. notafter = nota;
  116. subject = subname;
  117. pubkey = subpub;
  118. certdata = cert;
  119. certsigdata = certsigpart;
  120. sigdata = casig;
  121. return;
  122. }
  123. Boolean operator ==(const X509Cert& a, const X509Cert& b)
  124. {
  125. if (a.certdata == b.certdata)
  126. return BOOL_TRUE;
  127. else
  128. return BOOL_FALSE;
  129. }
  130. VerifyResult
  131. X509Cert::verify(const PubKey& pubca) const
  132. {
  133. VerifyResult v;
  134. v = verify_sig(certsigdata, sigdata, pubca, sigalg);
  135. if (v)
  136. return v;
  137. PCTime now = timenow();
  138. if (now < notbefore)
  139. return (INVALID_NOTBEFORE);
  140. else if (now > notafter)
  141. return (INVALID_EXPIRED);
  142. #ifdef RW
  143. if (cert_in_CRL(*this) == BOOL_TRUE)
  144. return (INVALID_REVOKED);
  145. #endif
  146. return (VALID);
  147. }
  148. Bstream
  149. X509Cert::sign_and_encode(const Bstream& privkey)
  150. {
  151. Bstream val, tmpstr, tmpstr2, algid;
  152. val = asn1_der_encode_integer(serialnum);
  153. tmpstr = asn1_der_encode_null();
  154. tmpstr = algid = // Signature Algorithm ID
  155.    asn1_der_encode_sequence(sigalg.algid.encode() + tmpstr);
  156. val = val + tmpstr;
  157. val = val + issuer.encode();
  158. tmpstr = asn1_der_encode_utctime(notbefore);
  159. tmpstr2 = asn1_der_encode_utctime(notafter);
  160. tmpstr = // Validity
  161.    asn1_der_encode_sequence(tmpstr + tmpstr2);
  162. val = val + tmpstr;
  163. val = val + subject.encode();
  164. tmpstr = pubkey.keytype.algid.encode() + pubkey.keytype.params;
  165.   // Subject Public Key Algorithm ID
  166.     tmpstr = asn1_der_encode_sequence(tmpstr);
  167.     tmpstr2 = asn1_der_encode_bit_string(pubkey.key);
  168. // Subject Public Key Info
  169.     tmpstr = asn1_der_encode_sequence(tmpstr + tmpstr2);
  170. val = val + tmpstr;
  171.     val = asn1_der_encode_sequence(val); // ToBeSigned
  172. certsigdata = val; // Save this for signature
  173. tmpstr = algid;  // Signature Algorithm ID, again
  174. val = val + tmpstr;
  175. // XXX  -- change this to support different sig algs
  176. ObjId md_oid = dsaWithSHA;
  177. tmpstr = sign(certsigdata, privkey, md_oid);
  178. sigdata = tmpstr; // Save the signature
  179. tmpstr = // Encrypted hash, as a BIT-STRING
  180.    asn1_der_encode_bit_string(tmpstr);
  181. val = val + tmpstr;
  182. val = // SIGNED, final certificate
  183.    asn1_der_encode_sequence(val);
  184. certdata = val; // Save the final form
  185. return (val);
  186. }
  187. Bigint
  188. X509Cert::getserialnum() const
  189. {
  190. return (serialnum);
  191. }
  192. Name
  193. X509Cert::getsubject() const
  194. {
  195. return (subject);
  196. }
  197. Name
  198. X509Cert::getissuer() const
  199. {
  200. return (issuer);
  201. }
  202. AlgId
  203. X509Cert::getsigalgid() const
  204. {
  205. return (sigalg);
  206. }
  207. PCTime
  208. X509Cert::getnotbefore() const
  209. {
  210. return (notbefore);
  211. }
  212. PCTime
  213. X509Cert::getnotafter() const
  214. {
  215. return (notafter);
  216. }
  217. PubKey
  218. X509Cert::getpubkey() const
  219. {
  220. return (pubkey);
  221. }
  222. Bstream
  223. X509Cert::getcertdata() const
  224. {
  225. return (certdata);
  226. }
  227. void
  228. X509Cert::print()
  229. {
  230. printf("Serial Number = "); serialnum.print(); printf("n");
  231. printf("X.509 Certificate Signature Algorithm ID:nt");
  232. sigalg.algid.print(); 
  233. if (sigalg.algid == md2WithRSAEncryption)
  234. printf(" == MD2-WITH-RSA-ENCRYPTION");
  235. else if (sigalg.algid == dsaWithSHA)
  236. printf(" == SHA-WITH-DSA-SIGNATURE");
  237. printf("n");
  238. printf("X.509 Certificate Signature Algorithm parameters:");
  239. int retval;
  240. Bstream der_stream = sigalg.params;
  241. retval = asn1_der_decode_null(der_stream);
  242.     if (retval < 0) {
  243. sigalg.params.print(); printf("n");
  244. } else {
  245. printf(" NONEn");
  246. }
  247. printf("Issuer Name:tt"); issuer.print(); printf("n");
  248. printf("Not Valid Before:t"); notbefore.print(); printf("n");
  249. printf("Not Valid After:t"); notafter.print(); printf("n");
  250. printf("Subject Name:tt"); subject.print(); printf("n");
  251. printf("Subject Public Key Algorithm:t"); 
  252. pubkey.keytype.algid.print(); 
  253. if (pubkey.keytype.algid == dsa)
  254. printf(" == DSA");
  255. else if (pubkey.keytype.algid == dhKeyAgreement ||
  256. pubkey.keytype.algid == sundhKeyAgreement)
  257. printf(" == Diffie-Hellman");
  258. else if (pubkey.keytype.algid == rsaEncryption)
  259. printf(" == RSA-ENCRYPTION");
  260. else if (pubkey.keytype.algid == rsa)
  261. printf(" == RSA");
  262. printf("n");
  263. printf("Subject Public Key Algorithm parameters: ");
  264. der_stream = pubkey.keytype.params;
  265. if (pubkey.keytype.algid == rsa) {
  266. Bigint keysize;
  267. printf("Key Size = ");
  268. retval = asn1_der_decode_integer(der_stream, keysize);
  269. if (retval < 0) {
  270. printf("RSA parameters INTEGER decode error");
  271. asn1_perror(retval);
  272. pubkey.keytype.params.print(); printf("n");
  273. } else {
  274. keysize.printd(); printf(" bitsn");
  275. }
  276. } else if (pubkey.keytype.algid == dhKeyAgreement ||
  277. pubkey.keytype.algid == sundhKeyAgreement) {
  278. Bigint Mod, Base;
  279. int retval=asn1_der_decode_dh_params(der_stream, Mod, Base);
  280. printf("nDiffie-Hellman Modulus (p): "); Mod.printd();
  281. printf("nDiffie-Hellman Generator (g): "); Base.printd();
  282. printf("n");
  283. } else if (pubkey.keytype.algid == dsa) {
  284. Bigint prime, subprime, gen;
  285. int retval=asn1_der_decode_dsa_params(der_stream, prime,
  286. subprime, gen);
  287. printf("np: "); prime.printd();
  288. printf("nq: "); subprime.printd();
  289. printf("ng:  "); gen.printd();
  290. printf("n");
  291. } else {
  292. retval = asn1_der_decode_null(der_stream);
  293. if (retval < 0) {
  294. printf("n");
  295. pubkey.keytype.params.print(); printf("n");
  296. } else {
  297. printf("NONEn");
  298. }
  299. }
  300. printf("Subject Public Key: ");
  301. int  seqlen;
  302. Bigint modulus, exponent, pub;
  303. der_stream = pubkey.key;
  304. if (pubkey.keytype.algid == rsa ||
  305. pubkey.keytype.algid == rsaEncryption) {
  306. retval = asn1_der_decode_sequence(der_stream, seqlen);
  307. if (retval < 0) {printf("Bad Key encodingn"); return;}
  308. {
  309. retval = asn1_der_decode_integer(der_stream, modulus);
  310. if (retval < 0) {printf("Bad Key encodingn"); return;}
  311. retval = asn1_der_decode_integer(der_stream, exponent);
  312. if (retval < 0) {printf("Bad Key encodingn"); return;}
  313. printf("nRSA modulus = "); modulus.printd();
  314. printf("nRSA exponent = "); exponent.printd();
  315. printf("n");
  316. } else if (pubkey.keytype.algid == dhKeyAgreement ||
  317. pubkey.keytype.algid == sundhKeyAgreement ||
  318. pubkey.keytype.algid == dsa) {
  319. retval = asn1_der_decode_integer(der_stream, pub);
  320. printf("nDiffie-Hellman public value = "); pub.printd();
  321. printf("n");
  322. } else {
  323. retval = asn1_der_decode_null(der_stream);
  324. if (retval < 0) {
  325. printf("n");
  326. pubkey.key.print(); printf("n");
  327. } else {
  328. printf("NONEn");
  329. }
  330. }
  331. }
  332. /*
  333.  * X.500 X509Certificate DER decoding
  334.  */
  335. int asn1_der_decode_cert(Bstream& der_stream, X509Cert& cert)
  336. {
  337. byte tmp = 0;
  338. int  seqlen, retval;
  339. Bigint serialnum;
  340. AlgId  sigalgid, sigalgid2;
  341. PCTime notbefore, notafter;
  342. Name issuername, subjectname;
  343. PubKey psubject;
  344. Bstream casig, certsigdata, allcert;
  345. Bstream saved_stream = der_stream;
  346. SEQUENCE // SIGNED Macro
  347. {
  348. certsigdata = der_stream;
  349. SEQUENCE // ToBeSigned
  350. {
  351. // version -- Context specific [0]; for the moment,
  352. // we assume only 1988 format, and by DER
  353. // decoding rules, this field is absent.
  354. INTEGER(serialnum);
  355. SEQUENCE // X509Cert signature Algorithm Identifier
  356. {
  357. int begin, end, used, anylen;
  358. MARK_LEN(begin);
  359. OBJECT_IDENTIFIER(sigalgid.algid);
  360. MARK_LEN(end);
  361. used = begin - end;
  362. if ((anylen = seqlen - used) > 0) {
  363. Bstream any(anylen, DATAP);
  364. CONSUME(anylen);
  365. sigalgid.params = any;
  366. }
  367. }
  368. NAME(issuername);
  369. SEQUENCE // Validity
  370. {
  371. UTCTime(notbefore);
  372. UTCTime(notafter);
  373. }
  374. NAME(subjectname);
  375. SEQUENCE // Subject Public Key info
  376. {
  377. SEQUENCE // Subject Public Key Alg ID
  378. {
  379. int begin, end, used, anylen;
  380. MARK_LEN(begin);
  381.      OBJECT_IDENTIFIER(psubject.keytype.algid);
  382. MARK_LEN(end);
  383. used = begin - end;
  384. if ((anylen = seqlen - used) > 0) {
  385. Bstream any(anylen, DATAP);
  386. CONSUME(anylen);
  387. psubject.keytype.params = any;
  388. }
  389. }
  390. //  Subject Public Key
  391. BIT_STRING(psubject.key);
  392. }
  393. }
  394. int tobesignedlen = certsigdata.getlength() - 
  395. der_stream.getlength();
  396. int trunclen = certsigdata.getlength() - tobesignedlen;
  397. certsigdata.truncate(trunclen);
  398. SEQUENCE // Alg ID of cert signing
  399. {
  400. int begin, end, used, anylen;
  401. MARK_LEN(begin);
  402. OBJECT_IDENTIFIER(sigalgid2.algid);
  403. MARK_LEN(end);
  404. used = begin - end;
  405. if ((anylen = seqlen - used) > 0) {
  406. Bstream any(anylen, DATAP);
  407. CONSUME(anylen);
  408. sigalgid2.params = any;
  409. }
  410. }
  411. //  encrypted hash, i.e SIGNATURE
  412. BIT_STRING(casig);
  413. }
  414. int newlen = saved_stream.getlength() - der_stream.getlength();
  415. int trunclen = saved_stream.getlength() - newlen;
  416. saved_stream.truncate(trunclen);
  417. X509Cert tmpcert(serialnum, sigalgid, issuername, notbefore, notafter,
  418.      subjectname, psubject, saved_stream, certsigdata, casig);
  419. cert = tmpcert;
  420. return (SUCCESS);
  421. }
  422. Bstream 
  423. asn1_der_encode_certreq(const char *name, const Bstream& publickey,
  424. const Bstream& privkey)
  425. {
  426. Bstream tmpstr, certreqinfo, signature, nullbstr;
  427. Name nullname;
  428. tmpstr = asn1_der_encode_integer((short)0); // Version #0 (PKCS# 10)
  429. Name subject(name);
  430. if (subject == nullname)
  431. return (nullbstr);
  432. tmpstr = tmpstr + subject.encode();
  433. tmpstr = tmpstr + publickey;
  434. // XXX No attribute encoding for now
  435. certreqinfo = asn1_der_encode_sequence(tmpstr);
  436. tmpstr = sign(certreqinfo, privkey, dsaWithSHA);
  437. // XXX - remove this 
  438. // tmpstr = sign(certreqinfo, privkey, md2WithRSAEncryption);
  439. signature = asn1_der_encode_bit_string(tmpstr);
  440. tmpstr = asn1_der_encode_null();
  441. // XXX - change this
  442. // tmpstr = md2WithRSAEncryption.encode() + tmpstr;
  443. tmpstr = dsaWithSHA.encode() + tmpstr;
  444. tmpstr = asn1_der_encode_sequence(tmpstr);
  445. tmpstr = certreqinfo + tmpstr + signature;
  446. return (asn1_der_encode_sequence(tmpstr));
  447. }
  448. int
  449. asn1_der_decode_algid(Bstream& der_stream, AlgId& algid)
  450. {
  451. int retval, seqlen;
  452. SEQUENCE {
  453. int begin, end, used, anylen;
  454. MARK_LEN(begin);
  455. OBJECT_IDENTIFIER(algid.algid);
  456. MARK_LEN(end);
  457. used = begin - end;
  458. if ((anylen = seqlen - used) > 0) {
  459. Bstream any(anylen, DATAP);
  460. CONSUME(anylen);
  461. algid.params = any;
  462. }
  463. }
  464. }
  465. int
  466. asn1_der_decode_dh_params(Bstream der_stream, Bigint& mod, Bigint& base)
  467. {
  468. byte tmp = 0;
  469.         int seqlen, retval;
  470.         ObjId oid;
  471.         Bigint p, g, pvl;
  472.  
  473.         SEQUENCE
  474.         {
  475.                 OBJECT_IDENTIFIER(oid);
  476.                 SEQUENCE
  477.                 {
  478.                         INTEGER(p);
  479.                         INTEGER(g);
  480.                         INTEGER(pvl);
  481.                 }
  482.         }
  483.         mod = p;
  484.         base = g;
  485.         return (0);
  486. }