1*** Settings ***
2Documentation  Certificate utilities keywords.
3
4Library        OperatingSystem
5Resource       rest_client.robot
6Resource       resource.robot
7
8*** Variables ***
9
10# Default wait sync time for certificate install and restart services.
11${wait_time}    30
12
13*** Keywords ***
14
15Install Certificate File On BMC
16    [Documentation]  Install certificate file in BMC using POST operation.
17    [Arguments]  ${uri}  ${status}=ok  &{kwargs}
18
19    # Description of argument(s):
20    # uri         URI for installing certificate file via Redfish
21    #             e.g. "/redfish/v1/AccountService/LDAP/Certificates".
22    # status      Expected status of certificate installation via Redfish
23    #             e.g. error, ok.
24    # kwargs      A dictionary of keys/values to be passed directly to
25    #             POST Request.
26
27    Initialize OpenBMC
28
29    ${headers}=  Create Dictionary  Content-Type=application/octet-stream
30    ...  X-Auth-Token=${XAUTH_TOKEN}
31    Set To Dictionary  ${kwargs}  headers  ${headers}
32
33    ${ret}=  Post Request  openbmc  ${uri}  &{kwargs}
34    ${content_json}=  To JSON  ${ret.content}
35    ${cert_id}=  Set Variable If  '${ret.status_code}' == '${HTTP_OK}'  ${content_json["Id"]}  -1
36
37    Run Keyword If  '${status}' == 'ok'
38    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_OK}
39    ...  ELSE IF  '${status}' == 'error'
40    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_INTERNAL_SERVER_ERROR}
41
42    Delete All Sessions
43
44    [Return]  ${cert_id}
45
46
47Get Certificate Content From BMC Via Openssl
48    [Documentation]  Get certificate content from BMC via openssl.
49
50    Check If Openssl Tool Exist
51
52    ${openssl_cmd}=  Catenate
53    ...  timeout 10  openssl s_client -connect ${OPENBMC_HOST}:${HTTPS_PORT} -showcerts
54    ${output}=  Run  ${openssl_cmd}
55
56    ${result}=  Fetch From Left
57    ...  ${output}  -----END CERTIFICATE-----
58    ${result}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
59    [Return]  ${result}
60
61
62Get Certificate File Content From BMC
63    [Documentation]  Get required certificate file content from BMC.
64    [Arguments]  ${cert_type}=Client
65
66    # Description of argument(s):
67    # cert_type      Certificate type (e.g. "Client" or "CA").
68
69    ${certificate}  ${stderr}  ${rc}=  Run Keyword If  '${cert_type}' == 'Client'
70    ...    BMC Execute Command  cat /etc/nslcd/certs/cert.pem
71
72    [Return]  ${certificate}
73
74
75Generate Certificate File Via Openssl
76    [Documentation]  Create certificate file via openssl with required content
77    ...              and returns its path.
78    [Arguments]  ${cert_format}  ${time}=365  ${cert_dir_name}=certificate_dir
79
80    # Description of argument(s):
81    # cert_format          Certificate file format
82    #                      e.g. Valid_Certificate_Empty_Privatekey.
83    # time                 Number of days to certify the certificate for.
84    # cert_dir_name        The name of the sub-directory where the certificate
85    #                      is stored.
86
87    Check If Openssl Tool Exist
88
89    ${openssl_cmd}=  Catenate  openssl req -x509 -sha256 -newkey rsa:2048
90    ...  ${SPACE}-nodes -days ${time}
91    ...  ${SPACE}-keyout ${cert_dir_name}/cert.pem -out ${cert_dir_name}/cert.pem
92    ...  ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com"
93
94    ${rc}  ${output}=  Run And Return RC and Output  ${openssl_cmd}
95    Should Be Equal  ${rc}  ${0}  msg=${output}
96    OperatingSystem.File Should Exist
97    ...  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
98
99    ${file_content}=  OperatingSystem.Get File
100    ...  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
101    ${result}=  Fetch From Left  ${file_content}  -----END CERTIFICATE-----
102    ${cert_content}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
103
104    ${result}=  Fetch From Left  ${file_content}  -----END PRIVATE KEY-----
105    ${private_key_content}=  Fetch From Right  ${result}  -----BEGIN PRIVATE KEY-----
106
107    ${cert_data}=
108    ...  Run Keyword if  '${cert_format}' == 'Valid Certificate Valid Privatekey'
109    ...  OperatingSystem.Get File  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
110    ...  ELSE IF  '${cert_format}' == 'Empty Certificate Valid Privatekey'
111    ...  Remove String  ${file_content}  ${cert_content}
112    ...  ELSE IF  '${cert_format}' == 'Valid Certificate Empty Privatekey'
113    ...  Remove String  ${file_content}  ${private_key_content}
114    ...  ELSE IF  '${cert_format}' == 'Empty Certificate Empty Privatekey'
115    ...  Remove String  ${file_content}  ${cert_content}  ${private_key_content}
116    ...  ELSE IF  '${cert_format}' == 'Expired Certificate' or '${cert_format}' == 'Not Yet Valid Certificate'
117    ...  OperatingSystem.Get File  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
118    ...  ELSE IF  '${cert_format}' == 'Valid Certificate'
119    ...  Remove String  ${file_content}  ${private_key_content}
120    ...  -----BEGIN PRIVATE KEY-----  -----END PRIVATE KEY-----
121    ...  ELSE IF  '${cert_format}' == 'Empty Certificate'
122    ...  Remove String  ${file_content}  ${cert_content}
123    ...  ${private_key_content}  -----BEGIN PRIVATE KEY-----
124    ...  -----END PRIVATE KEY-----
125
126    ${random_name}=  Generate Random String  8
127    ${cert_name}=  Catenate  SEPARATOR=  ${random_name}  .pem
128    Create File  ${cert_dir_name}/${cert_name}  ${cert_data}
129
130    [Return]  ${EXECDIR}${/}${cert_dir_name}${/}${cert_name}
131
132
133Get Certificate Content From File
134    [Documentation]  Get certificate content from certificate file.
135    [Arguments]  ${cert_file_path}
136
137    # Description of argument(s):
138    # cert_file_path  Downloaded certificate file path.
139
140    ${file_content}=  OperatingSystem.Get File  ${cert_file_path}
141    ${result}=  Fetch From Left  ${file_content}  -----END CERTIFICATE-----
142    ${result}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
143    [Return]  ${result}
144
145
146Check If Openssl Tool Exist
147    [Documentation]  Check if openssl tool installed or not.
148
149    ${rc}  ${output}=  Run And Return RC and Output  which openssl
150    Should Not Be Empty  ${output}  msg=Openssl tool not installed.
151
152
153Verify Certificate Visible Via OpenSSL
154    [Documentation]  Checks if given certificate is visible via openssl's showcert command.
155    [Arguments]  ${cert_file_path}
156
157    # Description of argument(s):
158    # cert_file_path           Certificate file path.
159
160    ${cert_file_content}=  OperatingSystem.Get File  ${cert_file_path}
161    ${openssl_cert_content}=  Get Certificate Content From BMC Via Openssl
162    Should Contain  ${cert_file_content}  ${openssl_cert_content}
163
164
165Delete All CA Certificate Via Redfish
166    [Documentation]  Delete all CA certificate via Redfish.
167    ${cert_list}=  Redfish_Utils.Get Member List  /redfish/v1/Managers/bmc/Truststore/Certificates
168    FOR  ${cert}  IN  @{cert_list}
169      Redfish.Delete  ${cert}  valid_status_codes=[${HTTP_NO_CONTENT}]
170    END
171
172
173Delete Certificate Via BMC CLI
174    [Documentation]  Delete certificate via BMC CLI.
175    [Arguments]  ${cert_type}
176
177    # Description of argument(s):
178    # cert_type           Certificate type (e.g. "Client" or "CA").
179
180    ${certificate_file_path}  ${certificate_service}  ${certificate_uri}=
181    ...  Run Keyword If  '${cert_type}' == 'Client'
182    ...    Set Variable  /etc/nslcd/certs/cert.pem  phosphor-certificate-manager@nslcd.service
183    ...    ${REDFISH_LDAP_CERTIFICATE_URI}
184    ...  ELSE IF  '${cert_type}' == 'CA'
185    ...    Set Variable  ${ROOT_CA_FILE_PATH}  phosphor-certificate-manager@authority.service
186    ...    ${REDFISH_CA_CERTIFICATE_URI}
187
188    ${file_status}  ${stderr}  ${rc}=  BMC Execute Command
189    ...  [ -f ${certificate_file_path} ] && echo "Found" || echo "Not Found"
190
191    Return From Keyword If  "${file_status}" != "Found"
192    BMC Execute Command  rm ${certificate_file_path}
193    BMC Execute Command  systemctl restart ${certificate_service}
194    BMC Execute Command  systemctl daemon-reload
195    Wait Until Keyword Succeeds  1 min  10 sec  Redfish.Get  ${certificate_uri}/1
196    ...  valid_status_codes=[${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}]
197
198
199Replace Certificate Via Redfish
200    [Documentation]  Test 'replace certificate' operation in the BMC via Redfish.
201    [Arguments]  ${cert_type}  ${cert_format}  ${expected_status}
202
203    # Description of argument(s):
204    # cert_type           Certificate type (e.g. "Server" or "Client").
205    # cert_format         Certificate file format
206    #                     (e.g. Valid_Certificate_Valid_Privatekey).
207    # expected_status     Expected status of certificate replace Redfish
208    #                     request (i.e. "ok" or "error").
209
210    # Install certificate before replacing client or CA certificate.
211    ${cert_id}=  Run Keyword If  '${cert_type}' == 'Client'
212    ...    Install And Verify Certificate Via Redfish  ${cert_type}  Valid Certificate Valid Privatekey  ok
213    ...  ELSE IF  '${cert_type}' == 'CA'
214    ...    Install And Verify Certificate Via Redfish  ${cert_type}  Valid Certificate  ok
215
216    ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}
217
218    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
219    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
220
221    Run Keyword If  '${cert_format}' == 'Expired Certificate'
222    ...    Modify BMC Date  future
223    ...  ELSE IF  '${cert_format}' == 'Not Yet Valid Certificate'
224    ...    Modify BMC Date  old
225
226
227    ${certificate_uri}=  Set Variable If
228    ...  '${cert_type}' == 'Server'  ${REDFISH_HTTPS_CERTIFICATE_URI}/1
229    ...  '${cert_type}' == 'Client'  ${REDFISH_LDAP_CERTIFICATE_URI}/1
230    ...  '${cert_type}' == 'CA'  ${REDFISH_CA_CERTIFICATE_URI}/${cert_id}
231
232    ${certificate_dict}=  Create Dictionary  @odata.id=${certificate_uri}
233    ${payload}=  Create Dictionary  CertificateString=${file_data}
234    ...  CertificateType=PEM  CertificateUri=${certificate_dict}
235
236    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
237    ...  '${expected_status}' == 'error'  ${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}
238    ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
239    ...  body=${payload}  valid_status_codes=[${expected_resp}]
240
241    ${cert_file_content}=  OperatingSystem.Get File  ${cert_file_path}
242    ${bmc_cert_content}=  redfish_utils.Get Attribute  ${certificate_uri}  CertificateString
243
244    Run Keyword If  '${expected_status}' == 'ok'
245    ...    Should Contain  ${cert_file_content}  ${bmc_cert_content}
246    ...  ELSE
247    ...    Should Not Contain  ${cert_file_content}  ${bmc_cert_content}
248
249
250Install And Verify Certificate Via Redfish
251    [Documentation]  Install and verify certificate using Redfish.
252    [Arguments]  ${cert_type}  ${cert_format}  ${expected_status}  ${delete_cert}=${True}
253
254    # Description of argument(s):
255    # cert_type           Certificate type (e.g. "Client" or "CA").
256    # cert_format         Certificate file format
257    #                     (e.g. "Valid_Certificate_Valid_Privatekey").
258    # expected_status     Expected status of certificate replace Redfish
259    #                     request (i.e. "ok" or "error").
260    # delete_cert         Certificate will be deleted before installing if this True.
261
262    Run Keyword If  '${cert_type}' == 'CA' and '${delete_cert}' == '${True}'
263    ...  Delete All CA Certificate Via Redfish
264    ...  ELSE IF  '${cert_type}' == 'Client' and '${delete_cert}' == '${True}'
265    ...  Delete Certificate Via BMC CLI  ${cert_type}
266
267    ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}
268    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
269    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
270
271    ${certificate_uri}=  Set Variable If
272    ...  '${cert_type}' == 'Client'  ${REDFISH_LDAP_CERTIFICATE_URI}
273    ...  '${cert_type}' == 'CA'  ${REDFISH_CA_CERTIFICATE_URI}
274
275    Run Keyword If  '${cert_format}' == 'Expired Certificate'  Modify BMC Date  future
276    ...  ELSE IF  '${cert_format}' == 'Not Yet Valid Certificate'  Modify BMC Date  old
277
278    ${cert_id}=  Install Certificate File On BMC  ${certificate_uri}  ${expected_status}  data=${file_data}
279    Logging  Installed certificate id: ${cert_id}
280
281    # Adding delay after certificate installation.
282    # Lesser wait timing causes bmcweb to restart quickly and breaks the web services.
283    Log To Console  Wait Time started in seconds ${wait_time}
284    Sleep  ${wait_time}s
285
286    ${cert_file_content}=  OperatingSystem.Get File  ${cert_file_path}
287    ${bmc_cert_content}=  Run Keyword If  '${expected_status}' == 'ok'  redfish_utils.Get Attribute
288    ...  ${certificate_uri}/${cert_id}  CertificateString
289
290    Run Keyword If  '${expected_status}' == 'ok'  Should Contain  ${cert_file_content}  ${bmc_cert_content}
291    [Return]  ${cert_id}
292
293
294Modify BMC Date
295    [Documentation]  Modify date in BMC.
296    [Arguments]  ${date_set_type}=current
297
298    # Description of argument(s):
299    # date_set_type    Set BMC date to a current, future, old date by 375 days.
300    #                  current - Sets date to local system date.
301    #                  future - Sets to a future date from current date.
302    #                  old - Sets to a old date from current date.
303
304    Redfish Power Off  stack_mode=skip
305    ${current_date_time}=  Get Current Date
306    ${new_time}=  Run Keyword If  '${date_set_type}' == 'current'  Set Variable  ${current_date_time}
307    ...  ELSE IF  '${date_set_type}' == 'future'
308    ...  Add Time To Date  ${current_date_time}  375 days
309    ...  ELSE IF  '${date_set_type}' == 'old'
310    ...  Subtract Time From Date  ${current_date_time}  375 days
311
312    # Enable manual mode.
313    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
314    ...  body={'NTP':{'ProtocolEnabled': ${False}}}
315    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
316
317    # NTP network takes few seconds to restart.
318    Wait Until Keyword Succeeds  30 sec  10 sec
319    ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/bmc  body={'DateTime': '${new_time}'}
320    ...  valid_status_codes=[${HTTP_OK}]
321