blob: 44d658873c40965f42abf86d821d6917f8ed694a [file] [log] [blame]
Jeff Thompson173fd432013-10-12 18:16:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009#include "common.hpp"
10
11#include "identity-certificate.hpp"
Jeff Thompson173fd432013-10-12 18:16:41 -070012
13using namespace std;
14
15namespace ndn {
16
Jeff Thompson173fd432013-10-12 18:16:41 -070017bool
18IdentityCertificate::isCorrectName(const Name& name)
19{
20 int i = name.size() - 1;
21
Jeff Thompson43a57b12013-10-22 16:25:38 -070022 string idString("ID-CERT");
Jeff Thompson173fd432013-10-12 18:16:41 -070023 for (; i >= 0; i--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -070024 if(name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070025 break;
26 }
27
28 if (i < 0)
29 return false;
30
Jeff Thompson43a57b12013-10-22 16:25:38 -070031 int keyIdx = 0;
32 string keyString("KEY");
33 for (; keyIdx < name.size(); keyIdx++) {
34 if(name.get(keyIdx).toEscapedString() == keyString)
35 break;
36 }
37
38 if (keyIdx >= name.size())
39 return false;
40
Jeff Thompson173fd432013-10-12 18:16:41 -070041 return true;
42}
43
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080044void
Jeff Thompson43a57b12013-10-22 16:25:38 -070045IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070046{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080047 if (!isCorrectName(getName()))
48 throw Error("Wrong Identity Certificate Name!");
49
Jeff Thompson74942612013-10-24 16:42:32 -070050 publicKeyName_ = certificateNameToPublicKeyName(getName());
51}
Jeff Thompson173fd432013-10-12 18:16:41 -070052
Jeff Thompson74942612013-10-24 16:42:32 -070053bool
54IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080055{
56 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -070057}
58
59Name
60IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
61{
Jeff Thompson43a57b12013-10-22 16:25:38 -070062 int i = certificateName.size() - 1;
63 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -080064 bool foundIdString = false;
Jeff Thompson43a57b12013-10-22 16:25:38 -070065 for (; i >= 0; i--) {
66 if (certificateName.get(i).toEscapedString() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -080067 {
68 foundIdString = true;
69 break;
70 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070071 }
Yingdi Yu88663af2014-01-15 15:21:38 -080072
73 if(!foundIdString)
74 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Jeff Thompson43a57b12013-10-22 16:25:38 -070075
76 Name tmpName = certificateName.getSubName(0, i);
77 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -080078 bool foundKeyString = false;
Jeff Thompson43a57b12013-10-22 16:25:38 -070079 for (i = 0; i < tmpName.size(); i++) {
80 if (tmpName.get(i).toEscapedString() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080081 {
82 foundKeyString = true;
83 break;
84 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070085 }
Yingdi Yu88663af2014-01-15 15:21:38 -080086
87 if(!foundKeyString)
88 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Jeff Thompson173fd432013-10-12 18:16:41 -070089
Jeff Thompson74942612013-10-24 16:42:32 -070090 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070091}
92
93}