blob: aa2ad9ceeef25110f1809ceba506369043be5247 [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
Jeff Thompson173fd432013-10-12 18:16:41 -07009#include <ndn-cpp/security/certificate/identity-certificate.hpp>
10
11using namespace std;
12
13namespace ndn {
14
Jeff Thompson173fd432013-10-12 18:16:41 -070015bool
16IdentityCertificate::isCorrectName(const Name& name)
17{
18 int i = name.size() - 1;
19
Jeff Thompson43a57b12013-10-22 16:25:38 -070020 string idString("ID-CERT");
Jeff Thompson173fd432013-10-12 18:16:41 -070021 for (; i >= 0; i--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -070022 if(name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070023 break;
24 }
25
26 if (i < 0)
27 return false;
28
Jeff Thompson43a57b12013-10-22 16:25:38 -070029 int keyIdx = 0;
30 string keyString("KEY");
31 for (; keyIdx < name.size(); keyIdx++) {
32 if(name.get(keyIdx).toEscapedString() == keyString)
33 break;
34 }
35
36 if (keyIdx >= name.size())
37 return false;
38
Jeff Thompson173fd432013-10-12 18:16:41 -070039 return true;
40}
41
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080042void
Jeff Thompson43a57b12013-10-22 16:25:38 -070043IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070044{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080045 if (!isCorrectName(getName()))
46 throw Error("Wrong Identity Certificate Name!");
47
Jeff Thompson74942612013-10-24 16:42:32 -070048 publicKeyName_ = certificateNameToPublicKeyName(getName());
49}
Jeff Thompson173fd432013-10-12 18:16:41 -070050
Jeff Thompson74942612013-10-24 16:42:32 -070051bool
52IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080053{
54 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -070055}
56
57Name
58IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
59{
Jeff Thompson43a57b12013-10-22 16:25:38 -070060 int i = certificateName.size() - 1;
61 string idString("ID-CERT");
62 for (; i >= 0; i--) {
63 if (certificateName.get(i).toEscapedString() == idString)
64 break;
65 }
66
67 Name tmpName = certificateName.getSubName(0, i);
68 string keyString("KEY");
69 for (i = 0; i < tmpName.size(); i++) {
70 if (tmpName.get(i).toEscapedString() == keyString)
71 break;
72 }
Jeff Thompson173fd432013-10-12 18:16:41 -070073
Jeff Thompson74942612013-10-24 16:42:32 -070074 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070075}
76
77}