7e53fd2627b9b31661fb0b85c82fde4c5a412cc3
[gssweb.git] / json_gssapi / src / datamodel / GSSOID.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  */
7
8 #include "GSSOID.h"
9 #include "../GSSException.h"
10
11 GSSOID::GSSOID(std::string oid_str)
12 {
13   init( GSSBuffer(oid_str) );
14 }
15
16 GSSOID::GSSOID(char *oid_str)
17 {
18   init( GSSBuffer(oid_str) );
19 }
20   
21 GSSOID::GSSOID(GSSBuffer oid_str)
22 {
23   init(oid_str);
24 }
25
26 GSSOID::GSSOID(gss_OID gssoid)
27 {
28   oid = gssoid;
29   this->gssInternal = true;
30 }
31
32 GSSOID::GSSOID ( const GSSOID &gssoid )
33 {
34   this->oid = gssoid.oid;
35   this->gssInternal = true;
36 }
37
38 void GSSOID::release()
39 {
40   OM_uint32 major, minor;
41   if ( (!this->gssInternal) && (oid != GSS_C_NO_OID) )
42   {
43     major = gss_release_oid(&minor, &(this->oid));
44     if (GSS_ERROR(major))
45     {
46       throw GSSException("Error in releasing a GSS OID", major, minor);
47     }
48   }
49 }
50
51 GSSOID::~GSSOID()
52 {
53   this->release();
54 }
55   
56 void GSSOID::init(GSSBuffer oid_str)
57 {
58   /* Variables */
59   OM_uint32 major, minor;
60   
61   /* Error checking */
62   /* Setup */
63   
64   /* Main */
65   major = gss_str_to_oid(&minor, oid_str.toGss(), &(this->oid));
66   if (GSS_ERROR(major))
67   {
68     throw GSSException("Error creating GSS OID", major, minor);
69   }
70   this->gssInternal = false;
71   
72   /* Cleanup */
73   /* Return */
74 }
75
76 std::string GSSOID::toString() const
77 {
78   /* Variables */
79   OM_uint32 major, minor;
80   gss_buffer_desc buf;
81   std::string ret;
82   
83   /* Error checking */
84   if (oid->length == 0)
85     return std::string("");
86   /* Setup */
87   /* Main */ 
88   major = gss_oid_to_str(&minor, this->oid, &buf);
89   if (GSS_ERROR(major))
90   {
91     throw GSSException("Error in creating a string representation of a GSS OID", 
92                        major, minor);
93   }
94   ret = std::string((char *)buf.value);
95   
96   /* Cleanup */
97   major = gss_release_buffer(&minor, &buf);
98   if (GSS_ERROR(major))
99   {
100     throw GSSException("Error releasing a gss_buffer", 
101                        major, minor);
102   }
103   
104   /* Return */ 
105   return(ret);
106 }
107
108 JSONObject *GSSOID::toJSONValue() const
109
110   std::string val = this->toString();
111   JSONObject *ret = new JSONObject( val.c_str() );
112   return(ret); 
113 }
114
115 bool GSSOID::setValue ( GSSBuffer buf )
116 {
117   init(buf);
118   return true;
119 }
120
121 bool GSSOID::setValue ( gss_OID gssOID )
122 {
123   oid = gssOID;
124   this->gssInternal = true;
125   return true;
126 }