gpgme_utils.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 "rosbag/gpgme_utils.h"
36 
37 #include <boost/format.hpp>
38 
39 namespace rosbag
40 {
41 
42 void initGpgme() {
43  // Check version method must be called before en/decryption
44  gpgme_check_version(0);
45  // Set locale
46  setlocale(LC_ALL, "");
47  gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
48 #ifdef LC_MESSAGES
49  gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
50 #endif
51 }
52 
53 void getGpgKey(gpgme_ctx_t& ctx, std::string const& user, gpgme_key_t& key) {
54  gpgme_error_t err;
55  // Asterisk means an arbitrary user.
56  if (user == std::string("*")) {
57  err = gpgme_op_keylist_start(ctx, 0, 0);
58  } else {
59  err = gpgme_op_keylist_start(ctx, user.c_str(), 0);
60  }
61  if (err) {
62  throw BagException((boost::format("gpgme_op_keylist_start returned %1%") % gpgme_strerror(err)).str());
63  }
64  while (true) {
65  err = gpgme_op_keylist_next(ctx, &key);
66  if (!err) {
67  if (user == std::string("*") || strcmp(key->uids->name, user.c_str()) == 0) {
68  break;
69  }
70  gpgme_key_release(key);
71  } else if (gpg_err_code(err) == GPG_ERR_EOF) {
72  if (user == std::string("*")) {
73  // A method throws an exception (instead of returning a specific value) if the key is not found
74  // This allows rosbag client applications to work without modifying their source code
75  throw BagException("GPG key not found");
76  } else {
77  throw BagException((boost::format("GPG key not found for a user %1%") % user.c_str()).str());
78  }
79  } else {
80  throw BagException((boost::format("gpgme_op_keylist_next returned %1%") % err).str());
81  }
82  }
83  err = gpgme_op_keylist_end(ctx);
84  if (err) {
85  throw BagException((boost::format("gpgme_op_keylist_end returned %1%") % gpgme_strerror(err)).str());
86  }
87 }
88 
89 
90 } // namespace rosbag
gpgme_utils.h
rosbag::initGpgme
void initGpgme()
Initialize GPGME library.
Definition: gpgme_utils.cpp:74
rosbag::BagException
Base class for rosbag exceptions.
Definition: exceptions.h:75
rosbag::getGpgKey
void getGpgKey(gpgme_ctx_t &ctx, std::string const &user, gpgme_key_t &key)
Get GPG key.
Definition: gpgme_utils.cpp:85
rosbag
Definition: aes_encryptor.h:43


rosbag_storage
Author(s): Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:52