blob: 4f9f9c858f620d54ca4db3edfba97cea2c24a0e2 [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
Yingdi Yu4f324632014-01-15 18:10:03 -08009#include <ndn-cpp/security/identity-certificate.hpp>
Jeff Thompson173fd432013-10-12 18:16:41 -070010
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");
Yingdi Yu88663af2014-01-15 15:21:38 -080062 bool foundIdString = false;
Jeff Thompson43a57b12013-10-22 16:25:38 -070063 for (; i >= 0; i--) {
64 if (certificateName.get(i).toEscapedString() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -080065 {
66 foundIdString = true;
67 break;
68 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070069 }
Yingdi Yu88663af2014-01-15 15:21:38 -080070
71 if(!foundIdString)
72 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Jeff Thompson43a57b12013-10-22 16:25:38 -070073
74 Name tmpName = certificateName.getSubName(0, i);
75 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -080076 bool foundKeyString = false;
Jeff Thompson43a57b12013-10-22 16:25:38 -070077 for (i = 0; i < tmpName.size(); i++) {
78 if (tmpName.get(i).toEscapedString() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080079 {
80 foundKeyString = true;
81 break;
82 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070083 }
Yingdi Yu88663af2014-01-15 15:21:38 -080084
85 if(!foundKeyString)
86 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Jeff Thompson173fd432013-10-12 18:16:41 -070087
Jeff Thompson74942612013-10-24 16:42:32 -070088 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070089}
90
91}