blob: 99779506b6726c0b3dc52a36bd0580a525acb27f [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Jeff Thompsonc0573432013-09-19 17:41:36 -070014 */
15
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080016#include "public-key.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070017
18#include "../encoding/oid.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070019#include "cryptopp.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070020
21using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080022using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070023
24namespace ndn {
25
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080026static OID RSA_OID("1.2.840.113549.1.1.1");
Jeff Thompsonc0573432013-09-19 17:41:36 -070027
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080028PublicKey::PublicKey()
29{
Jeff Thompsonc0573432013-09-19 17:41:36 -070030}
Jeff Thompsonc0573432013-09-19 17:41:36 -070031
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
Jeff Thompsonc0573432013-09-19 17:41:36 -070033{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080034 StringSource src(keyDerBuf, keyDerSize, true);
35 decode(src);
Jeff Thompsonc0573432013-09-19 17:41:36 -070036}
37
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080038void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039PublicKey::encode(CryptoPP::BufferedTransformation& out) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070040{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080041 // SubjectPublicKeyInfo ::= SEQUENCE {
42 // algorithm AlgorithmIdentifier
43 // keybits BIT STRING }
44
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070045 out.Put(m_key.buf(), m_key.size());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080046}
47
48void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049PublicKey::decode(CryptoPP::BufferedTransformation& in)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080050{
51 // SubjectPublicKeyInfo ::= SEQUENCE {
52 // algorithm AlgorithmIdentifier
53 // keybits BIT STRING }
54
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070055 try
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080056 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070057 std::string out;
58 StringSink sink(out);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080059
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070060 ////////////////////////
61 // part 1: copy as is //
62 ////////////////////////
63 BERSequenceDecoder decoder(in);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080064 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070065 assert(decoder.IsDefiniteLength());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080066
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067 DERSequenceEncoder encoder(sink);
68 decoder.TransferTo(encoder, decoder.RemainingLength());
69 encoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080070 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070071 decoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080072
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070073 ////////////////////////
74 // part 2: check if the key is RSA (since it is the only supported for now)
75 ////////////////////////
76 StringSource checkedSource(out, true);
77 BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
78 {
79 BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
80 {
81 OID algorithm;
82 algorithm.decode(algorithmInfo);
83
84 if (algorithm != RSA_OID)
85 throw Error("Only RSA public keys are supported for now (" +
86 algorithm.toString() + " requested)");
87 }
88 }
89
90 m_key.assign(out.begin(), out.end());
91 }
92 catch (CryptoPP::BERDecodeErr& err)
93 {
94 throw Error("PublicKey decoding error");
95 }
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080096}
97
98// Blob
99// PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
100// {
101// if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
102// uint8_t digest[SHA256_DIGEST_LENGTH];
103// ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800105// return Blob(digest, sizeof(digest));
106// }
107// else
108// throw UnrecognizedDigestAlgorithmException("Wrong format!");
109// }
110
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700112operator<<(std::ostream& os, const PublicKey& key)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800113{
114 CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
115 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
116
117 return os;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700118}
119
Yingdi Yufc40d872014-02-18 12:56:04 -0800120} // namespace ndn