test_aes_encryptor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2017, Open Source Robotics Foundation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <cstdio>
36 
37 #include <gtest/gtest.h>
38 
39 #include <gpgme.h>
40 
41 #include "std_msgs/String.h"
42 
43 #include "rosbag/bag.h"
44 #include "rosbag/aes_encryptor.h"
45 #include "rosbag/gpgme_utils.h"
46 #include "rosbag/view.h"
47 
48 const char *GPG_KEY_USER = "Foo00";
49 const char *GPG_PRIVATE_SUBKEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"
50  "Version: GnuPG v1\n\n"
51  "lQEVBFn3acoBCACwhq4iofk2V3/4yQy9++pHa3D4SPJdt1G/h83D+t9m95FoTHZl"
52  "zIY5bjKAKQ+NI6u5eQKNndAA7QLg8UGML6VqO7wmlxYSMOqRc4i0QMuUTA87hK4u"
53  "ozcHnjwaRhQzapwhnAMUu4058DIUyTus7ugD81C6y0nNT2PPwQzKifMmMcIgBvKm"
54  "vio7IK1A2tOsyQJD3jo99ZQhxq/eOQIwCs/BZfxu0OWnSJkx98Rsf7w3GC8tqhWK"
55  "avt/rBaFVoS3eZMkgQfT/ep92dYSLi/3/1pMtAt3kEVw3ZrvrCi47KDhpuaIV0kI"
56  "WlfHJF/YBNFbe4rRNwmQ5PkZYW1IwR14A+QFABEBAAH+B2UCR05VAbQqRm9vMDAg"
57  "KEEgR1BHIGtleSBmb3IgdGVzdCBjYXNlcykgPGJhckBiYXo+iQE4BBMBAgAiBQJZ"
58  "92nKAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRCvPh8IgO8IzWTJB/9d"
59  "PO8BrpXtm/tosaGJFHT8FDt4PerCoKD6SqKj6HDrSU8tgUh+qLd+S1SkZ9Zg+yLq"
60  "ccCTeUg40XEvcKeTob8hWbTMKdl1cijM1jJCAUTe9zZd7URHoEkNWdEgA/saCZUj"
61  "TDeImYNcvv93SisKldf4gd67vkBETB4kEY7v7EE2YTge12S1vd9/9Ra6ZQn/qJSh"
62  "aPfZ5RfL1FEexQW+h2+bfpxW+ej/s2uM05AdEMmmGDaSVpimpPDkfF2YX4ESM7cl"
63  "fQzn1sCyVeHQWYvvEFTChdPlIR+gMZUhW+KicEqKsCDleh/jnSG/OoVZQkzec6Kt"
64  "zls7NO+fqIqFDLrNBWMTnQOXBFn3ac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  "RVGr1yx2xSZnVOeXQRSfp9yevAixcC8ATa8GUx9K8OSP/0NtmwP/Z5nW5s0WoG1H"
81  "Yna4s5bhNJ1y6Jue7bhw0gKXhx/9RNilTmq7NU6vTc1vqj0nw5e45WpojqZvSt0f"
82  "+bEL5eW+YnFb0MLJ9gIey4QZbOJeHB1xOaqz54Fnf8t9MmV/efLKvTHdj/WFM3fL"
83  "Z2iesAjK4hCyB2raeD9SOCP2ofztcmpIGYkBHwQYAQIACQUCWfdpygIbDAAKCRCv"
84  "Ph8IgO8Izd2IB/sFNTx/3l4mX9NUvyTpEhXKseti7JncjxZblUTV5MJ154nrfb2w"
85  "Xc81OmPmEDtE0PxTjYVasBD3lZNyLglw9kPpS5qZXU8kO4D6kNjRhdgKd/GqNHSo"
86  "U1LqutU/nbJc5H3AtGMLhg4Afa9xsYIRcszgtyWBbYCJ1MhkBaROAY42VzOyOi6z"
87  "pHOsnQH3xGlOdBYLeIvStCVP4XmHw/Mcx7LVnIfx48MFxVDw1iDiZej0K3cO24Ne"
88  "dSCgzJUVZQ5K4fTSu2xq1jD5zgWZ/Iu8PvAl5skKzgB2HpDVxIMVAELvxaMgKh3S"
89  "LYxADpgShsNtJiU/Dbn8jA+BlK/77fGnkvnc"
90  "=YcIw"
91  "-----END PGP PRIVATE KEY BLOCK-----";
92 const char *MESSAGE = "message foo";
93 const char *TOPIC_NAME = "topic_bar";
94 
95 void importGpgKey(gpgme_ctx_t &ctx) {
96  gpgme_data_t key_data;
97  gpgme_error_t err = gpgme_data_new_from_mem(&key_data, GPG_PRIVATE_SUBKEY, std::strlen(GPG_PRIVATE_SUBKEY), 1);
98  if (err) {
99  gpgme_release(ctx);
100  FAIL() << "gpgme_data_new_from_mem returned " << gpgme_strerror(err);
101  }
102  err = gpgme_op_import(ctx, key_data);
103  if (err) {
104  gpgme_data_release(key_data);
105  gpgme_release(ctx);
106  FAIL() << "gpgme_op_import returned " << gpgme_strerror(err);
107  }
108  gpgme_import_result_t res = gpgme_op_import_result(ctx);
109  EXPECT_TRUE(res->imports);
110  gpgme_data_release(key_data);
111 }
112 
113 TEST(AesCbcEncryptor, EncryptAndDecryptBag) {
114  // Import key
116  gpgme_ctx_t ctx;
117  gpgme_error_t err = gpgme_new(&ctx);
118  if (err) {
119  FAIL() << "Failed to create a GPG context: " << gpgme_strerror(err);
120  }
121  importGpgKey(ctx);
122 
123  // Test if the key has been imported
124  gpgme_key_t key;
125  try {
126  rosbag::getGpgKey(ctx, GPG_KEY_USER, key);
127  }
128  catch (rosbag::BagException const& e) {
129  gpgme_release(ctx);
130  FAIL() << "Failed to get the imported GPG key: " << e.what();
131  }
132 
133  // Write a message to an encrypted bag file
134  char temp_dir_templ[] = "/tmp/bagXXXXXX";
135  char *temp_dir = mkdtemp(temp_dir_templ);
136  std::string bag_file_name = std::string(temp_dir) + "/foo.bag";
137  rosbag::Bag bag(bag_file_name, rosbag::bagmode::Write);
138  bag.setEncryptorPlugin("rosbag/AesCbcEncryptor", GPG_KEY_USER);
139  std_msgs::String msg;
140  msg.data = MESSAGE;
141  bag.write(TOPIC_NAME, ros::TIME_MIN, msg);
142  bag.close();
143 
144  // Test the message decrypted from the bag file
145  bag.open(bag_file_name, rosbag::bagmode::Read);
146  rosbag::View view(bag);
147  EXPECT_EQ(view.size(), 1u);
148  EXPECT_EQ(TOPIC_NAME, view.begin()->getTopic());
149  EXPECT_EQ(MESSAGE, view.begin()->instantiate<std_msgs::String>()->data);
150  bag.close();
151 
152  // Remove the bag file
153  boost::filesystem::remove(bag_file_name);
154  // Delete the key
155  gpgme_op_delete(ctx, key, 1);
156  // Release GPG context
157  gpgme_release(ctx);
158 }
159 
160 int main(int argc, char **argv) {
161  testing::InitGoogleTest(&argc, argv);
162  return RUN_ALL_TESTS();
163 }
const char * TOPIC_NAME
void open(std::string const &filename, uint32_t mode=bagmode::Read)
Open a bag file.
Definition: bag.cpp:103
const Time TIME_MIN(0, 1)
const char * MESSAGE
Base class for rosbag exceptions.
Definition: exceptions.h:43
void close()
Close the bag file.
Definition: bag.cpp:163
TEST(AesCbcEncryptor, EncryptAndDecryptBag)
uint32_t size()
Definition: view.cpp:226
const char * GPG_PRIVATE_SUBKEY
void initGpgme()
Initialize GPGME library.
Definition: gpgme_utils.cpp:42
const char * GPG_KEY_USER
iterator begin()
Simply copy the merge_queue state into the iterator.
Definition: view.cpp:218
int main(int argc, char **argv)
void setEncryptorPlugin(const std::string &plugin_name, const std::string &plugin_param=std::string())
Set encryptor of the bag file.
Definition: bag.cpp:217
void getGpgKey(gpgme_ctx_t &ctx, std::string const &user, gpgme_key_t &key)
Get GPG key.
Definition: gpgme_utils.cpp:53
void write(std::string const &topic, ros::MessageEvent< T > const &event)
Write a message into the bag file.
Definition: bag.h:369
void importGpgKey(gpgme_ctx_t &ctx)


rosbag_storage
Author(s): Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:55