blob: 27992ca2decc003fdb01ea3465a16f74b1a63410 [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"
10#include "security/public-key.hpp"
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080011
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080012#include <cryptopp/rsa.h>
13#include <cryptopp/base64.h>
14#include <cryptopp/files.h>
Jeff Thompsonc0573432013-09-19 17:41:36 -070015
16using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080017using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070018
19namespace ndn {
20
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080021static OID RSA_OID("1.2.840.113549.1.1.1");
Jeff Thompsonc0573432013-09-19 17:41:36 -070022
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080023PublicKey::PublicKey()
24{
Jeff Thompsonc0573432013-09-19 17:41:36 -070025}
Jeff Thompsonc0573432013-09-19 17:41:36 -070026
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080027/**
28 * Create a new PublicKey with the given values.
29 * @param algorithm The algorithm of the public key.
30 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
31 */
32PublicKey::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
39PublicKey::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
45 out.Put(key_.buf(), key_.size());
46}
47
48void
49PublicKey::decode(CryptoPP::BufferedTransformation &in)
50{
51 // SubjectPublicKeyInfo ::= SEQUENCE {
52 // algorithm AlgorithmIdentifier
53 // keybits BIT STRING }
54
55 try {
56 std::string out;
57 StringSink sink(out);
58
59 ////////////////////////
60 // part 1: copy as is //
61 ////////////////////////
62 BERSequenceDecoder decoder(in);
63 {
64 assert (decoder.IsDefiniteLength());
65
66 DERSequenceEncoder encoder(sink);
67 decoder.TransferTo(encoder, decoder.RemainingLength());
68 encoder.MessageEnd();
69 }
70 decoder.MessageEnd();
71
72 ////////////////////////
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 (" + algorithm.toString() + " requested");
85 }
86 }
87
88 key_.assign(out.begin(), out.end());
Jeff Thompsonc0573432013-09-19 17:41:36 -070089 }
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080090 catch (CryptoPP::BERDecodeErr &err) {
91 throw Error("PublicKey decoding error");
92 }
93}
94
95// Blob
96// PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
97// {
98// if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
99// uint8_t digest[SHA256_DIGEST_LENGTH];
100// ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
101
102// return Blob(digest, sizeof(digest));
103// }
104// else
105// throw UnrecognizedDigestAlgorithmException("Wrong format!");
106// }
107
108std::ostream &
109operator <<(std::ostream &os, const PublicKey &key)
110{
111 CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
112 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
113
114 return os;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700115}
116
117}