1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2009 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_AUTH_CRYPTO_H
16 #define CEPH_AUTH_CRYPTO_H
18 #include "include/types.h"
19 #include "include/utime.h"
20 #include "include/memory.h"
21 #include "include/buffer.h"
26 class CryptoKeyContext;
27 namespace ceph { class Formatter; }
31 * some per-key context that is specific to a particular crypto backend
33 class CryptoKeyHandler {
37 virtual ~CryptoKeyHandler() {}
39 virtual int encrypt(const bufferlist& in,
40 bufferlist& out, std::string *error) const = 0;
41 virtual int decrypt(const bufferlist& in,
42 bufferlist& out, std::string *error) const = 0;
46 * match encoding of struct ceph_secret
52 bufferptr secret; // must set this via set_secret()!
54 // cache a pointer to the implementation-specific key handler, so we
55 // don't have to create it for every crypto operation.
56 mutable ceph::shared_ptr<CryptoKeyHandler> ckh;
58 int _set_secret(int type, const bufferptr& s);
61 CryptoKey() : type(0) { }
62 CryptoKey(int t, utime_t c, bufferptr& s)
69 void encode(bufferlist& bl) const;
70 void decode(bufferlist::iterator& bl);
72 int get_type() const { return type; }
73 utime_t get_created() const { return created; }
74 void print(std::ostream& out) const;
76 int set_secret(int type, const bufferptr& s, utime_t created);
77 const bufferptr& get_secret() { return secret; }
78 const bufferptr& get_secret() const { return secret; }
80 void encode_base64(string& s) const {
88 string encode_base64() const {
93 void decode_base64(const string& s) {
98 bufferlist::iterator p = bl.begin();
102 void encode_formatted(string label, Formatter *f, bufferlist &bl);
103 void encode_plaintext(bufferlist &bl);
106 int create(CephContext *cct, int type);
107 int encrypt(CephContext *cct, const bufferlist& in, bufferlist& out,
108 std::string *error) const {
109 assert(ckh); // Bad key?
110 return ckh->encrypt(in, out, error);
112 int decrypt(CephContext *cct, const bufferlist& in, bufferlist& out,
113 std::string *error) const {
114 assert(ckh); // Bad key?
115 return ckh->decrypt(in, out, error);
118 void to_str(std::string& s) const;
120 WRITE_CLASS_ENCODER(CryptoKey)
122 static inline ostream& operator<<(ostream& out, const CryptoKey& k)
130 * Driver for a particular algorithm
132 * To use these functions, you need to call ceph::crypto::init(), see
133 * common/ceph_crypto.h. common_init_finish does this for you.
135 class CryptoHandler {
137 virtual ~CryptoHandler() {}
138 virtual int get_type() const = 0;
139 virtual int create(bufferptr& secret) = 0;
140 virtual int validate_secret(const bufferptr& secret) = 0;
141 virtual CryptoKeyHandler *get_key_handler(const bufferptr& secret,
144 static CryptoHandler *create(int type);
147 extern int get_random_bytes(char *buf, int len);
148 extern uint64_t get_random(uint64_t min_val, uint64_t max_val);