c8b545c58187d3aadb429ebd8f7bcbb747c2f7d0
[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()
33 {
34   OM_uint32 major, minor;
35   if (! this->gssInternal)
36   {
37     major = gss_release_oid(&minor, &(this->oid));
38     if (GSS_ERROR(major))
39     {
40       throw GSSException("Error in releasing a GSS OID", major, minor);
41     }
42   }
43 }
44   
45 void GSSOID::init(GSSBuffer oid_str)
46 {
47   /* Variables */
48   OM_uint32 major, minor;
49   
50   /* Error checking */
51   /* Setup */
52   
53   /* Main */
54   major = gss_str_to_oid(&minor, oid_str.toGss(), &(this->oid));
55   if (GSS_ERROR(major))
56   {
57     throw GSSException("Error creating GSS OID", major, minor);
58   }
59   this->gssInternal = false;
60   
61   /* Cleanup */
62   /* Return */
63 }
64
65 std::string GSSOID::toString()
66 {
67   /* Variables */
68   OM_uint32 major, minor;
69   gss_buffer_desc buf;
70   std::string ret;
71   
72   /* Error checking */
73   /* Setup */
74   /* Main */ 
75   major = gss_oid_to_str(&minor, this->oid, &buf);
76   if (GSS_ERROR(major))
77   {
78     throw GSSException("Error in creating a string representation of a GSS OID", 
79                        major, minor);
80   }
81   ret = std::string((char *)buf.value);
82   
83   /* Cleanup */
84   major = gss_release_buffer(&minor, &buf);
85   if (GSS_ERROR(major))
86   {
87     throw GSSException("Error releasing a gss_buffer", 
88                        major, minor);
89   }
90   
91   /* Return */ 
92   return(ret);
93 }