53ef75f22cb28813bef19c49f74e39128a53eab4
[public/netxms.git] / src / libnxcl / cert.cpp
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** Client Library
5 ** Copyright (C) 2004, 2005, 2006, 2007 Victor Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 ** File: cert.cpp
22 **
23 **/
24
25 #include "libnxcl.h"
26
27
28 //
29 // Add trusted CA certificate
30 //
31
32 DWORD LIBNXCL_EXPORTABLE NXCAddCACertificate(NXC_SESSION hSession, DWORD dwCertLen,
33 BYTE *pCert, TCHAR *pszComments)
34 {
35 CSCPMessage msg;
36 DWORD dwRqId;
37
38 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
39
40 msg.SetCode(CMD_ADD_CA_CERTIFICATE);
41 msg.SetId(dwRqId);
42 msg.SetVariable(VID_CERTIFICATE, pCert, dwCertLen);
43 msg.SetVariable(VID_COMMENTS, pszComments);
44 ((NXCL_Session *)hSession)->SendMsg(&msg);
45
46 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
47 }
48
49
50 //
51 // Update certificate's comments
52 //
53
54 DWORD LIBNXCL_EXPORTABLE NXCUpdateCertificateComments(NXC_SESSION hSession, DWORD dwCertId,
55 TCHAR *pszComments)
56 {
57 CSCPMessage msg;
58 DWORD dwRqId;
59
60 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
61
62 msg.SetCode(CMD_UPDATE_CERT_COMMENTS);
63 msg.SetId(dwRqId);
64 msg.SetVariable(VID_CERTIFICATE_ID, dwCertId);
65 msg.SetVariable(VID_COMMENTS, pszComments);
66 ((NXCL_Session *)hSession)->SendMsg(&msg);
67
68 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
69 }
70
71
72 //
73 // Delete certificate
74 //
75
76 DWORD LIBNXCL_EXPORTABLE NXCDeleteCertificate(NXC_SESSION hSession, DWORD dwCertId)
77 {
78 CSCPMessage msg;
79 DWORD dwRqId;
80
81 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
82
83 msg.SetCode(CMD_DELETE_CERTIFICATE);
84 msg.SetId(dwRqId);
85 msg.SetVariable(VID_CERTIFICATE_ID, dwCertId);
86 ((NXCL_Session *)hSession)->SendMsg(&msg);
87
88 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
89 }
90
91
92 //
93 // Get list of installed certificates
94 //
95
96 DWORD LIBNXCL_EXPORTABLE NXCGetCertificateList(NXC_SESSION hSession, NXC_CERT_LIST **ppList)
97 {
98 CSCPMessage msg, *pResponse;
99 DWORD i, dwId, dwRetCode, dwRqId;
100
101 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
102 *ppList = NULL;
103
104 msg.SetCode(CMD_GET_CERT_LIST);
105 msg.SetId(dwRqId);
106 ((NXCL_Session *)hSession)->SendMsg(&msg);
107
108 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
109 if (pResponse != NULL)
110 {
111 dwRetCode = pResponse->GetVariableLong(VID_RCC);
112 if (dwRetCode == RCC_SUCCESS)
113 {
114 *ppList = (NXC_CERT_LIST *)malloc(sizeof(NXC_CERT_LIST));
115 (*ppList)->dwNumElements = pResponse->GetVariableLong(VID_NUM_CERTIFICATES);
116 if ((*ppList)->dwNumElements > 0)
117 {
118 (*ppList)->pElements = (NXC_CERT_INFO *)malloc(sizeof(NXC_CERT_INFO) * (*ppList)->dwNumElements);
119 for(i = 0, dwId = VID_CERT_LIST_BASE; i < (*ppList)->dwNumElements; i++, dwId += 6)
120 {
121 (*ppList)->pElements[i].dwId = pResponse->GetVariableLong(dwId++);
122 (*ppList)->pElements[i].nType = (int)pResponse->GetVariableShort(dwId++);
123 (*ppList)->pElements[i].pszComments = pResponse->GetVariableStr(dwId++);
124 (*ppList)->pElements[i].pszSubject = pResponse->GetVariableStr(dwId++);
125 }
126 }
127 else
128 {
129 (*ppList)->pElements = NULL;
130 }
131 }
132 delete pResponse;
133 }
134 else
135 {
136 dwRetCode = RCC_TIMEOUT;
137 }
138 return dwRetCode;
139 }
140
141
142 //
143 // Destroy certificate list
144 //
145
146 void LIBNXCL_EXPORTABLE NXCDestroyCertificateList(NXC_CERT_LIST *pList)
147 {
148 DWORD i;
149
150 if (pList == NULL)
151 return;
152
153 for(i = 0; i < pList->dwNumElements; i++)
154 {
155 safe_free(pList->pElements[i].pszComments);
156 safe_free(pList->pElements[i].pszSubject);
157 }
158 safe_free(pList->pElements);
159 free(pList);
160 }