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