blob: c8362af05ca9ba27ac133d954468c2d29decd940 [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08009#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080010
11#include "public-key.hpp"
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080012
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080013#include <cryptopp/rsa.h>
14#include <cryptopp/base64.h>
15#include <cryptopp/files.h>
Jeff Thompsonc0573432013-09-19 17:41:36 -070016
17using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080018using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070019
20namespace ndn {
21
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080022static OID RSA_OID("1.2.840.113549.1.1.1");
Jeff Thompsonc0573432013-09-19 17:41:36 -070023
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080024PublicKey::PublicKey()
25{
Jeff Thompsonc0573432013-09-19 17:41:36 -070026}
Jeff Thompsonc0573432013-09-19 17:41:36 -070027
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080028/**
29 * Create a new PublicKey with the given values.
30 * @param algorithm The algorithm of the public key.
31 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
32 */
33PublicKey::PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize)
Jeff Thompsonc0573432013-09-19 17:41:36 -070034{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035 StringSource src(keyDerBuf, keyDerSize, true);
36 decode(src);
Jeff Thompsonc0573432013-09-19 17:41:36 -070037}
38
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080039void
40PublicKey::encode(CryptoPP::BufferedTransformation &out) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070041{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080042 // SubjectPublicKeyInfo ::= SEQUENCE {
43 // algorithm AlgorithmIdentifier
44 // keybits BIT STRING }
45
46 out.Put(key_.buf(), key_.size());
47}
48
49void
50PublicKey::decode(CryptoPP::BufferedTransformation &in)
51{
52 // SubjectPublicKeyInfo ::= SEQUENCE {
53 // algorithm AlgorithmIdentifier
54 // keybits BIT STRING }
55
56 try {
57 std::string out;
58 StringSink sink(out);
59
60 ////////////////////////
61 // part 1: copy as is //
62 ////////////////////////
63 BERSequenceDecoder decoder(in);
64 {
65 assert (decoder.IsDefiniteLength());
66
67 DERSequenceEncoder encoder(sink);
68 decoder.TransferTo(encoder, decoder.RemainingLength());
69 encoder.MessageEnd();
70 }
71 decoder.MessageEnd();
72
73 ////////////////////////
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 (" + algorithm.toString() + " requested");
86 }
87 }
88
89 key_.assign(out.begin(), out.end());
Jeff Thompsonc0573432013-09-19 17:41:36 -070090 }
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080091 catch (CryptoPP::BERDecodeErr &err) {
92 throw Error("PublicKey decoding error");
93 }
94}
95
96// Blob
97// PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
98// {
99// if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
100// uint8_t digest[SHA256_DIGEST_LENGTH];
101// ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
102
103// return Blob(digest, sizeof(digest));
104// }
105// else
106// throw UnrecognizedDigestAlgorithmException("Wrong format!");
107// }
108
109std::ostream &
110operator <<(std::ostream &os, const PublicKey &key)
111{
112 CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
113 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
114
115 return os;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700116}
117
Yingdi Yufc40d872014-02-18 12:56:04 -0800118} // namespace ndn