blob: 17f204bf2e47d2fe2925e029fae63cb186b815b8 [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 Afanasyev09c613f2014-01-29 00:23:58 -080016#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080017#include "public-key.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070018#include "cryptopp.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070019
20using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080021using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070022
23namespace ndn {
24
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080025static OID RSA_OID("1.2.840.113549.1.1.1");
Jeff Thompsonc0573432013-09-19 17:41:36 -070026
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080027PublicKey::PublicKey()
28{
Jeff Thompsonc0573432013-09-19 17:41:36 -070029}
Jeff Thompsonc0573432013-09-19 17:41:36 -070030
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
Jeff Thompsonc0573432013-09-19 17:41:36 -070032{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080033 StringSource src(keyDerBuf, keyDerSize, true);
34 decode(src);
Jeff Thompsonc0573432013-09-19 17:41:36 -070035}
36
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080037void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038PublicKey::encode(CryptoPP::BufferedTransformation& out) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070039{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080040 // SubjectPublicKeyInfo ::= SEQUENCE {
41 // algorithm AlgorithmIdentifier
42 // keybits BIT STRING }
43
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070044 out.Put(m_key.buf(), m_key.size());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080045}
46
47void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048PublicKey::decode(CryptoPP::BufferedTransformation& in)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080049{
50 // SubjectPublicKeyInfo ::= SEQUENCE {
51 // algorithm AlgorithmIdentifier
52 // keybits BIT STRING }
53
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 try
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080055 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070056 std::string out;
57 StringSink sink(out);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080058
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070059 ////////////////////////
60 // part 1: copy as is //
61 ////////////////////////
62 BERSequenceDecoder decoder(in);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080063 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 assert(decoder.IsDefiniteLength());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080065
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 DERSequenceEncoder encoder(sink);
67 decoder.TransferTo(encoder, decoder.RemainingLength());
68 encoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080069 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070070 decoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080071
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070072 ////////////////////////
73 // part 2: check if the key is RSA (since it is the only supported for now)
74 ////////////////////////
75 StringSource checkedSource(out, true);
76 BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
77 {
78 BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
79 {
80 OID algorithm;
81 algorithm.decode(algorithmInfo);
82
83 if (algorithm != RSA_OID)
84 throw Error("Only RSA public keys are supported for now (" +
85 algorithm.toString() + " requested)");
86 }
87 }
88
89 m_key.assign(out.begin(), out.end());
90 }
91 catch (CryptoPP::BERDecodeErr& err)
92 {
93 throw Error("PublicKey decoding error");
94 }
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080095}
96
97// Blob
98// PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
99// {
100// if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
101// uint8_t digest[SHA256_DIGEST_LENGTH];
102// ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800104// return Blob(digest, sizeof(digest));
105// }
106// else
107// throw UnrecognizedDigestAlgorithmException("Wrong format!");
108// }
109
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700111operator<<(std::ostream& os, const PublicKey& key)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800112{
113 CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
114 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
115
116 return os;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700117}
118
Yingdi Yufc40d872014-02-18 12:56:04 -0800119} // namespace ndn