public void RecvCertificate(Packet pkg) { if (pkg.Next != this.Id) return; //rsu收到obu的证书,进行验证 if (pkg.SrcType != NodeType.OBJECT) { Console.WriteLine("Wrong prev type!"); return; } string key = pkg.VANETCertificate.getStrPubKey(); Certificate c = new Certificate(pkg.VANETCertificate.Id, pkg.VANETCertificate.PubKey, pkg.VANETCertificate.CAId, pkg.VANETCertificate.CAPubKey); float delay = GetCheckCertificateDelay(c); //如果本地缓存中没有证书,则向ca请求;不是由本节点认证的话,直接验证(delay=0) if (delay > 0.0001f) { CertificateArg arg = new CertificateArg(c, CertificateMethod.REMOTE_AUTH); Console.WriteLine("---------------------------------"); Event.AddEvent(new Event(scheduler.currentTime + delay, EventType.CHK_CERT, this, arg)); return; } if(this.CertificateCache[key].authenticatedRSUId != this.Id) { CertificateArg arg = new CertificateArg(c, CertificateMethod.LOCAL); Console.WriteLine("---------------------------------"); Event.AddEvent(new Event(scheduler.currentTime + delay, EventType.CHK_CERT, this, arg)); } else//否则直接通过 { //认证完毕之后删除 float starttime = this.pendingCerterficatingObjects[c.Id]; this.pendingCerterficatingObjects.Remove(c.Id); Packet pkg1 = new Packet(this, global.objects[c.Id], PacketType.DATA_AVAIL); pkg1.Data = starttime; SendPacketDirectly(scheduler.currentTime, pkg1); } if (IsPreFetchCertificate(c)) { CertificateArg arg = new CertificateArg(c, CertificateMethod.REMOTE_RETR); Console.WriteLine("prefetch---------------------------------"); Event.AddEvent(new Event(scheduler.currentTime + global.checkCertDelay, EventType.CHK_CERT, this, arg)); this.prefetchingCertIds.Add(c.Id); } }
public void CheckCertificate(CertificateArg arg) { Certificate c = arg.cert; CertificateMethod method = arg.method; //从CA中获得验证证书的结果 string key = c.getStrPubKey(); c.authedRSUId = this.Id; Console.WriteLine("fetched cert READER{0}---------------------------------{1}", this.Id ,method); if (this.prefetchingCertIds.Contains(c.Id)) this.prefetchingCertIds.Remove(c.Id); //认证 if (method != CertificateMethod.REMOTE_RETR) { //如果缓存本来就有证书,成功 if (this.CertificateCache.ContainsKey(key)) { //认证完毕之后删除 float starttime = this.pendingCerterficatingObjects[c.Id]; this.pendingCerterficatingObjects.Remove(c.Id); Packet pkg1 = new Packet(this, global.objects[c.Id], PacketType.CERTIFICATE_OK); pkg1.Data = starttime; SendPacketDirectly(scheduler.currentTime, pkg1); if (method != CertificateMethod.LOCAL) this.CertificateCache[key].time = (int)scheduler.currentTime; //将该节点标记为已由自己认证 this.CertificateCache[key].authenticatedRSUId = this.Id; } //从ca取回的证书是正确的 else if (c.IsValid()) { this.CertificateCache.Add(key, new CertificateCache(c, (int)scheduler.currentTime, this.Id)); //将该节点标记为已由自己认证 this.CertificateCache[key].authenticatedRSUId = this.Id; //认证完毕之后删除 float starttime = this.pendingCerterficatingObjects[c.Id]; this.pendingCerterficatingObjects.Remove(c.Id); Packet pkg1 = new Packet(this, global.objects[c.Id], PacketType.CERTIFICATE_OK); pkg1.Data = starttime; SendPacketDirectly(scheduler.currentTime, pkg1); } //证书不正确 else { Packet pkg1 = new Packet(this, global.objects[c.Id], PacketType.CERTIFICATE_FAIL); SendPacketDirectly(scheduler.currentTime, pkg1); return; } } else { this.CertificateCache[key].time = (int)scheduler.currentTime; //将该节点标记为已由自己认证 this.CertificateCache[key].authenticatedRSUId = this.Id; } //forward certificate cache if (global.vanetCaForward == true) { Packet pkg2 = new Packet(this, BroadcastNode.Node, PacketType.RSU_CA_FORWARD); pkg2.TTL = 5; pkg2.VANETCaForward = new VANETCAForwardField(this.IssuedCertificate, this.CertificateCache[key].cert, this.CertificateCache[key].time, pkg2.TTL, this.Id); SendPacketDirectly(scheduler.currentTime, pkg2); } }