blob: ff44dd242cd717821f3ee60e138db1d36a41e040 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonc0573432013-09-19 17:41:36 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Jeff Thompsonc0573432013-09-19 17:41:36 -070023 */
24
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "public-key.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026
27#include "../encoding/oid.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070028#include "cryptopp.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070029
30using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080031using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070032
33namespace ndn {
34
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035static OID RSA_OID("1.2.840.113549.1.1.1");
Jeff Thompsonc0573432013-09-19 17:41:36 -070036
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080037PublicKey::PublicKey()
38{
Jeff Thompsonc0573432013-09-19 17:41:36 -070039}
Jeff Thompsonc0573432013-09-19 17:41:36 -070040
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
Jeff Thompsonc0573432013-09-19 17:41:36 -070042{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080043 StringSource src(keyDerBuf, keyDerSize, true);
44 decode(src);
Jeff Thompsonc0573432013-09-19 17:41:36 -070045}
46
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080047void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048PublicKey::encode(CryptoPP::BufferedTransformation& out) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070049{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080050 // SubjectPublicKeyInfo ::= SEQUENCE {
51 // algorithm AlgorithmIdentifier
52 // keybits BIT STRING }
53
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 out.Put(m_key.buf(), m_key.size());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080055}
56
57void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058PublicKey::decode(CryptoPP::BufferedTransformation& in)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080059{
60 // SubjectPublicKeyInfo ::= SEQUENCE {
61 // algorithm AlgorithmIdentifier
62 // keybits BIT STRING }
63
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 try
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080065 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 std::string out;
67 StringSink sink(out);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080068
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070069 ////////////////////////
70 // part 1: copy as is //
71 ////////////////////////
72 BERSequenceDecoder decoder(in);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080073 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070074 assert(decoder.IsDefiniteLength());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080075
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070076 DERSequenceEncoder encoder(sink);
77 decoder.TransferTo(encoder, decoder.RemainingLength());
78 encoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080079 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070080 decoder.MessageEnd();
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080081
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070082 ////////////////////////
83 // part 2: check if the key is RSA (since it is the only supported for now)
84 ////////////////////////
85 StringSource checkedSource(out, true);
86 BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
87 {
88 BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
89 {
90 OID algorithm;
91 algorithm.decode(algorithmInfo);
92
93 if (algorithm != RSA_OID)
94 throw Error("Only RSA public keys are supported for now (" +
95 algorithm.toString() + " requested)");
96 }
97 }
98
99 m_key.assign(out.begin(), out.end());
100 }
101 catch (CryptoPP::BERDecodeErr& err)
102 {
103 throw Error("PublicKey decoding error");
104 }
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800105}
106
107// Blob
108// PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
109// {
110// if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
111// uint8_t digest[SHA256_DIGEST_LENGTH];
112// ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800114// return Blob(digest, sizeof(digest));
115// }
116// else
117// throw UnrecognizedDigestAlgorithmException("Wrong format!");
118// }
119
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700121operator<<(std::ostream& os, const PublicKey& key)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800122{
123 CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
124 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
125
126 return os;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700127}
128
Yingdi Yufc40d872014-02-18 12:56:04 -0800129} // namespace ndn