source: creddy/creddy.hh @ d56e51b

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since d56e51b was d56e51b, checked in by Mike Ryan <mikeryan@…>, 14 years ago

raise an error (either return value or exception) when trying to write a
private key when it hasn't been loaded

  • Property mode set to 100644
File size: 4.7 KB
Line 
1#ifndef __CREDDY_HH__
2#define __CREDDY_HH__
3
4#include <cstdio>
5#include <stdexcept>
6
7namespace Creddy {
8    extern "C" {
9        #include <creddy.h>
10    }
11
12    class ID {
13        public:
14            ID() : m_id(NULL) { } // do not use: required by swig
15
16            // load an ID from a file
17            ID(char *filename) : m_id(NULL) {
18                m_id = creddy_id_from_file(filename);
19                if (m_id == NULL)
20                    throw std::invalid_argument("Could not load ID cert");
21            }
22
23            // generate an ID with a given CN and validity
24            ID(char *cn, int validity) : m_id(NULL) {
25                int ret = creddy_id_generate(&m_id, cn, validity);
26                if (ret == CREDDY_GENERATE_INVALID_CN)
27                    throw std::invalid_argument("CN must be alphanumeric and start with a letter");
28                if (ret == CREDDY_GENERATE_INVALID_VALIDITY)
29                    throw std::invalid_argument("Validity must be > 0 days");
30            }
31
32            ~ID() { creddy_id_free(m_id); }
33
34            // load private key from a file
35            void load_privkey(char *filename) {
36                int ret = creddy_id_load_privkey(m_id, filename);
37                if (!ret)
38                    throw std::invalid_argument("Could not load private key");
39            }
40
41            char *keyid() { return creddy_id_keyid(m_id); }
42            char *cert_filename() { return creddy_id_cert_filename(m_id); }
43            void write_cert(std::FILE *out) { creddy_id_write_cert(m_id, out); }
44            char *privkey_filename() { return creddy_id_privkey_filename(m_id); }
45
46            // write the private ket
47            // throws a std::logic_error if no private key is loaded
48            void write_privkey(std::FILE *out) {
49                int ret = creddy_id_write_privkey(m_id, out);
50                if (!ret) throw new std::logic_error("No private key loaded");
51            }
52
53            abac_chunk_t cert_chunk() { return creddy_id_cert_chunk(m_id); }
54
55            friend class Attribute;
56
57        private:
58            creddy_id_t *m_id;
59    };
60
61    class Attribute {
62        public:
63            Attribute() : m_attr(NULL) { } // do not use: required by swig
64
65            // create a cert
66            Attribute(ID &issuer, char *role, int validity) : m_attr(NULL) {
67                int ret = creddy_attribute_create(&m_attr, issuer.m_id, role, validity);
68                if (ret == CREDDY_ATTRIBUTE_ISSUER_NOKEY)
69                    throw std::invalid_argument("Issuer has no private key");
70                if (ret == CREDDY_ATTRIBUTE_INVALID_ROLE)
71                    throw std::invalid_argument("Role name must be alphanumeric");
72                if (ret == CREDDY_ATTRIBUTE_INVALID_VALIDITY)
73                    throw std::invalid_argument("Validity must be > 0 days");
74            }
75
76            ~Attribute() { creddy_attribute_free(m_attr); }
77
78            // these return false if there's a bad keyid or role name
79            // they'll throw a std::logic_error if the cert's been baked
80            bool principal(char *keyid) {
81                if (baked()) throw std::logic_error("Cert is already baked");
82                return creddy_attribute_principal(m_attr, keyid);
83            }
84            bool role(char *keyid, char *role) {
85                if (baked()) throw std::logic_error("Cert is already baked");
86                return creddy_attribute_role(m_attr, keyid, role);
87            }
88            bool linking_role(char *keyid, char *role, char *linked) {
89                if (baked()) throw std::logic_error("Cert is already baked");
90                return creddy_attribute_linking_role(m_attr, keyid, role, linked);
91            }
92
93            // returns false if there are no subjects or libstrongswan fails
94            // throws a std::logic_error if the cert's already been baked
95            bool bake() {
96                if (baked()) throw std::logic_error("Cert is already baked");
97                return creddy_attribute_bake(m_attr);
98            }
99
100            // returns true iff the cert's been baked
101            bool baked() { return creddy_attribute_baked(m_attr); }
102
103            // throws a std::logic_error if the cert isn't baked
104            void write(std::FILE *out) {
105                int ret = creddy_attribute_write(m_attr, out);
106                if (!ret) throw std::logic_error("Cert is not baked");
107            }
108
109            // throws a std::logic_error if the cert isn't baked
110            abac_chunk_t cert_chunk() {
111                abac_chunk_t ret;
112                if (!baked()) throw new std::logic_error("Cert is not baked");
113                creddy_attribute_cert_chunk(m_attr, &ret);
114                return ret;
115            }
116
117        private:
118            creddy_attribute_t *m_attr;
119    };
120}
121
122#endif /* __CREDDY_HH__ */
Note: See TracBrowser for help on using the repository browser.