1*** Settings ***
2Documentation    Test certificate in OpenBMC.
3
4Resource         ../../lib/resource.robot
5Resource         ../../lib/bmc_redfish_resource.robot
6Resource         ../../lib/openbmc_ffdc.robot
7Resource         ../../lib/certificate_utils.robot
8Library          String
9
10Force Tags       Certificate_Test
11
12Suite Setup      Suite Setup Execution
13Suite Teardown   Suite Teardown
14Test Teardown    Test Teardown Execution
15
16
17*** Variables ***
18
19${invalid_value}  abc
20${ROOT_CA_FILE_PATH}  /etc/ssl/certs/authority/*
21
22
23** Test Cases **
24
25Verify Server Certificate Replace
26    [Documentation]  Verify server certificate replace.
27    [Tags]  Verify_Server_Certificate_Replace
28    [Template]  Replace Certificate Via Redfish
29
30    # cert_type  cert_format                         expected_status
31    Server       Valid Certificate Valid Privatekey  ok
32    Server       Empty Certificate Valid Privatekey  error
33    Server       Valid Certificate Empty Privatekey  error
34    Server       Empty Certificate Empty Privatekey  error
35
36
37Verify Client Certificate Replace
38    [Documentation]  Verify client certificate replace.
39    [Tags]  Verify_Client_Certificate_Replace
40    [Template]  Replace Certificate Via Redfish
41
42    # cert_type  cert_format                         expected_status
43    Client       Valid Certificate Valid Privatekey  ok
44    Client       Empty Certificate Valid Privatekey  error
45    Client       Valid Certificate Empty Privatekey  error
46    Client       Empty Certificate Empty Privatekey  error
47
48
49Verify CA Certificate Replace
50    [Documentation]  Verify CA certificate replace.
51    [Tags]  Verify_CA_Certificate_Replace
52    [Template]  Replace Certificate Via Redfish
53
54    # cert_type  cert_format        expected_status
55    CA           Valid Certificate  ok
56    CA           Empty Certificate  error
57
58
59Verify Client Certificate Install
60    [Documentation]  Verify client certificate install.
61    [Tags]  Verify_Client_Certificate_Install
62    [Template]  Install And Verify Certificate Via Redfish
63
64    # cert_type  cert_format                         expected_status
65    Client       Valid Certificate Valid Privatekey  ok
66    Client       Empty Certificate Valid Privatekey  error
67    Client       Valid Certificate Empty Privatekey  error
68    Client       Empty Certificate Empty Privatekey  error
69
70
71Verify CA Certificate Install
72    [Documentation]  Verify CA certificate install.
73    [Tags]  Verify_CA_Certificate_Install
74    [Template]  Install And Verify Certificate Via Redfish
75
76    # cert_type  cert_format        expected_status
77    CA           Valid Certificate  ok
78    CA           Empty Certificate  error
79
80
81Verify Maximum CA Certificate Install
82    [Documentation]  Verify maximum CA certificate install.
83    [Tags]  Verify_Maximum_CA_Certificate_Install
84    [Teardown]  Run Keywords  FFDC On Test Case Fail  AND  Delete All CA Certificate Via Redfish
85
86    # Get CA certificate count from BMC.
87    ${cert_list}=  Redfish_Utils.Get Member List  /redfish/v1/Managers/bmc/Truststore/Certificates
88    ${cert_count}=  Get Length  ${cert_list}
89
90    # Install CA certificate to reach maximum count of 10.
91    FOR  ${INDEX}  IN RANGE  ${cert_count}  10
92      Install And Verify Certificate Via Redfish  CA  Valid Certificate  ok  ${FALSE}
93      ${cert_count}=  Evaluate  ${cert_count} + 1
94    END
95
96    # Verify error while installing 11th CA certificate.
97    Install And Verify Certificate Via Redfish  CA  Valid Certificate  error  ${FALSE}
98
99
100Verify Error While Uploding Same CA Certificate
101    [Documentation]  Verify error while uploading same CA certificate two times.
102    [Tags]  Verify_Error_While_Uploding_Same_CA_Certificate
103
104    # Create certificate file for uploading.
105    ${cert_file_path}=  Generate Certificate File Via Openssl  Valid Certificate  365
106    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
107    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
108
109    # Install CA certificate.
110    Install Certificate File On BMC  ${REDFISH_CA_CERTIFICATE_URI}  ok  data=${file_data}
111
112    # Adding delay after certificate installation.
113    Sleep  30s
114
115    # Check error while uploading same certificate.
116    Install Certificate File On BMC  ${REDFISH_CA_CERTIFICATE_URI}  error  data=${file_data}
117
118
119Verify Server Certificate View Via Openssl
120    [Documentation]  Verify server certificate via openssl command.
121    [Tags]  Verify_Server_Certificate_View_Via_Openssl
122
123    ${cert_file_path}=  Generate Certificate File Via Openssl  Valid Certificate Valid Privatekey
124    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
125    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
126
127    ${certificate_dict}=  Create Dictionary
128    ...  @odata.id=/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1
129    ${payload}=  Create Dictionary  CertificateString=${file_data}
130    ...  CertificateType=PEM  CertificateUri=${certificate_dict}
131
132    ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
133    ...  body=${payload}
134
135    Wait Until Keyword Succeeds  2 mins  15 secs  Verify Certificate Visible Via OpenSSL  ${cert_file_path}
136
137
138Verify CSR Generation For Server Certificate
139    [Documentation]  Verify CSR generation for server certificate.
140    [Tags]  Verify_CSR_Generation_For_Server_Certificate
141    [Template]  Generate CSR Via Redfish
142
143    # csr_type  key_pair_algorithm  key_bit_length  key_curv_id  expected_status
144    Server      RSA                 ${2048}         ${EMPTY}     ok
145    Server      EC                  ${EMPTY}        prime256v1   ok
146    Server      EC                  ${EMPTY}        secp521r1    ok
147    Server      EC                  ${EMPTY}        secp384r1    ok
148
149
150Verify CSR Generation For Client Certificate
151    [Documentation]  Verify CSR generation for client certificate.
152    [Tags]  Verify_CSR_Generation_For_Client_Certificate
153    [Template]  Generate CSR Via Redfish
154
155    # csr_type  key_pair_algorithm  key_bit_length  key_curv_id  expected_status
156    Client      RSA                 ${2048}         ${EMPTY}     ok
157    Client      EC                  ${EMPTY}        prime256v1   ok
158    Client      EC                  ${EMPTY}        secp521r1    ok
159    Client      EC                  ${EMPTY}        secp384r1    ok
160
161
162Verify CSR Generation For Server Certificate With Invalid Value
163    [Documentation]  Verify error while generating CSR for server certificate with invalid value.
164    [Tags]  Verify_CSR_Generation_For_Server_Certificate_With_Invalid_Value
165    [Template]  Generate CSR Via Redfish
166
167    # csr_type  key_pair_algorithm  key_bit_length    key_curv_id       expected_status
168    Server      ${invalid_value}    ${2048}           prime256v1        error
169    Server      RAS                 ${invalid_value}  ${EMPTY}          error
170    Server      EC                  ${EMPTY}          ${invalid_value}  error
171
172
173Verify CSR Generation For Client Certificate With Invalid Value
174    [Documentation]  Verify error while generating CSR for client certificate with invalid value.
175    [Tags]  Verify_CSR_Generation_For_Client_Certificate_With_Invalid_Value
176    [Template]  Generate CSR Via Redfish
177
178    Client      ${invalid_value}    ${2048}           prime256v1        error
179    Client      RSA                 ${invalid_value}  ${EMPTY}          error
180    Client      EC                  ${EMPTY}          ${invalid_value}  error
181
182
183Verify Expired Client Certificate Install
184    [Documentation]  Verify installation of expired CA certificate.
185    [Tags]  Verify_Expired_Client_Certificate_Install
186    [Setup]  Get Current BMC Date
187    [Teardown]  Run Keywords  FFDC On Test Case Fail  AND
188    ...  Restore BMC Date
189
190    Modify BMC Date
191    Install And Verify Certificate Via Redfish  Client  Expired Certificate  error
192
193
194Verify Expired CA Certificate Install
195    [Documentation]  Verify installation of expired CA certificate.
196    [Tags]  Verify_Expired_CA_Certificate_Install
197    [Setup]  Get Current BMC Date
198    [Teardown]  Run Keywords  FFDC On Test Case Fail  AND
199    ...  Restore BMC Date
200
201    Modify BMC Date
202    Install And Verify Certificate Via Redfish  CA  Expired Certificate  error
203
204
205*** Keywords ***
206
207Install And Verify Certificate Via Redfish
208    [Documentation]  Install and verify certificate using Redfish.
209    [Arguments]  ${cert_type}  ${cert_format}  ${expected_status}  ${delete_cert}=${True}
210
211    # Description of argument(s):
212    # cert_type           Certificate type (e.g. "Client" or "CA").
213    # cert_format         Certificate file format
214    #                     (e.g. "Valid_Certificate_Valid_Privatekey").
215    # expected_status     Expected status of certificate replace Redfish
216    #                     request (i.e. "ok" or "error").
217    # delete_cert         Certificate will be deleted before installing if this True.
218
219    Run Keyword If  '${cert_type}' == 'CA' and '${delete_cert}' == '${True}'
220    ...  Delete All CA Certificate Via Redfish
221    ...  ELSE IF  '${cert_type}' == 'Client' and '${delete_cert}' == '${True}'
222    ...  Delete Certificate Via BMC CLI  ${cert_type}
223
224    ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}
225    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
226    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
227
228    ${certificate_uri}=  Set Variable If
229    ...  '${cert_type}' == 'Client'  ${REDFISH_LDAP_CERTIFICATE_URI}
230    ...  '${cert_type}' == 'CA'  ${REDFISH_CA_CERTIFICATE_URI}
231
232    Run Keyword If  '${cert_format}' == 'Expired Certificate'  Modify BMC Date  future
233
234    ${cert_id}=  Install Certificate File On BMC  ${certificate_uri}  ${expected_status}  data=${file_data}
235    Logging  Installed certificate id: ${cert_id}
236
237    # Adding delay after certificate installation.
238    Sleep  30s
239
240    ${cert_file_content}=  OperatingSystem.Get File  ${cert_file_path}
241    ${bmc_cert_content}=  Run Keyword If  '${expected_status}' == 'ok'  redfish_utils.Get Attribute
242    ...  ${certificate_uri}/${cert_id}  CertificateString
243
244    Run Keyword If  '${expected_status}' == 'ok'  Should Contain  ${cert_file_content}  ${bmc_cert_content}
245    [Return]  ${cert_id}
246
247Modify BMC Date
248    [Documentation]  Modify date in BMC.
249    [Arguments]  ${date_set_type}=current
250
251    # Description of argument(s):
252    # date_set_type    Set BMC date to a current, future, old date by 375 days.
253    #                  current - Sets date to local system date.
254    #                  future - Sets to a future date from current date.
255    #                  old - Sets to a old date from current date.
256
257    Redfish Power Off  stack_mode=skip
258    ${current_date_time}=  Get Current Date
259    ${new_time}=  Run Keyword If  '${date_set_type}' == 'current'  Set Variable  ${current_date_time}
260    ...  ELSE IF  '${date_set_type}' == 'future'
261    ...  Add Time To Date  ${current_date_time}  375 days
262    ...  ELSE IF  '${date_set_type}' == 'old'
263    ...  Subtract Time From Date  ${current_date_time}  375 days
264
265    # Enable manaual mode.
266    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
267    ...  body={'NTP':{'ProtocolEnabled': ${False}}}
268    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
269    Redfish.Patch  ${REDFISH_BASE_URI}Managers/bmc  body={'DateTime': '${new_time}'}
270    ...  valid_status_codes=[${HTTP_OK}]
271
272Get Current BMC Date
273    [Documentation]  Get current BMC date.
274
275    ${cli_date_time}=  CLI Get BMC DateTime
276    Set Test Variable  ${cli_date_time}
277
278Restore BMC Date
279    [Documentation]  Restore BMC date to its prior value.
280
281    Redfish.Patch  ${REDFISH_BASE_URI}Managers/bmc  body={'DateTime': '${cli_date_time}'}
282    ...  valid_status_codes=[${HTTP_OK}]
283
284Replace Certificate Via Redfish
285    [Documentation]  Test 'replace certificate' operation in the BMC via Redfish.
286    [Arguments]  ${cert_type}  ${cert_format}  ${expected_status}
287
288    # Description of argument(s):
289    # cert_type           Certificate type (e.g. "Server" or "Client").
290    # cert_format         Certificate file format
291    #                     (e.g. Valid_Certificate_Valid_Privatekey).
292    # expected_status     Expected status of certificate replace Redfish
293    #                     request (i.e. "ok" or "error").
294
295    # Install certificate before replacing client or CA certificate.
296    ${cert_id}=  Run Keyword If  '${cert_type}' == 'Client'
297    ...    Install And Verify Certificate Via Redfish  ${cert_type}  Valid Certificate Valid Privatekey  ok
298    ...  ELSE IF  '${cert_type}' == 'CA'
299    ...    Install And Verify Certificate Via Redfish  ${cert_type}  Valid Certificate  ok
300
301    ${time}=  Set Variable If  '${cert_format}' == 'Expired Certificate'  -10  365
302    ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}  ${time}
303
304    ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
305    ${file_data}=  Decode Bytes To String  ${bytes}  UTF-8
306
307    ${certificate_uri}=  Set Variable If
308    ...  '${cert_type}' == 'Server'  ${REDFISH_HTTPS_CERTIFICATE_URI}/1
309    ...  '${cert_type}' == 'Client'  ${REDFISH_LDAP_CERTIFICATE_URI}/1
310    ...  '${cert_type}' == 'CA'  ${REDFISH_CA_CERTIFICATE_URI}/${cert_id}
311
312    ${certificate_dict}=  Create Dictionary  @odata.id=${certificate_uri}
313    ${payload}=  Create Dictionary  CertificateString=${file_data}
314    ...  CertificateType=PEM  CertificateUri=${certificate_dict}
315
316    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
317    ...  '${expected_status}' == 'error'  ${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}
318    ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
319    ...  body=${payload}  valid_status_codes=[${expected_resp}]
320
321    ${cert_file_content}=  OperatingSystem.Get File  ${cert_file_path}
322    ${bmc_cert_content}=  redfish_utils.Get Attribute  ${certificate_uri}  CertificateString
323
324    Run Keyword If  '${expected_status}' == 'ok'
325    ...    Should Contain  ${cert_file_content}  ${bmc_cert_content}
326    ...  ELSE
327    ...    Should Not Contain  ${cert_file_content}  ${bmc_cert_content}
328
329
330Generate CSR Via Redfish
331    [Documentation]  Generate CSR using Redfish.
332    [Arguments]  ${cert_type}  ${key_pair_algorithm}  ${key_bit_length}  ${key_curv_id}  ${expected_status}
333
334    # Description of argument(s):
335    # cert_type           Certificate type ("Server" or "Client").
336    # key_pair_algorithm  CSR key pair algorithm ("EC" or "RSA")
337    # key_bit_length      CSR key bit length ("2048").
338    # key_curv_id         CSR key curv id ("prime256v1" or "secp521r1" or "secp384r1").
339    # expected_status     Expected status of certificate replace Redfish
340    #                     request ("ok" or "error").
341
342    ${certificate_uri}=  Set Variable If
343    ...  '${cert_type}' == 'Server'  ${REDFISH_HTTPS_CERTIFICATE_URI}/
344    ...  '${cert_type}' == 'Client'  ${REDFISH_LDAP_CERTIFICATE_URI}/
345
346    ${certificate_dict}=  Create Dictionary  @odata.id=${certificate_uri}
347    ${payload}=  Create Dictionary  City=Austin  CertificateCollection=${certificate_dict}
348    ...  CommonName=${OPENBMC_HOST}  Country=US  Organization=IBM
349    ...  OrganizationalUnit=ISL  State=AU  KeyBitLength=${key_bit_length}
350    ...  KeyPairAlgorithm=${key_pair_algorithm}  KeyCurveId=${key_curv_id}
351
352    # Remove not applicable field for CSR generation.
353    Run Keyword If  '${key_pair_algorithm}' == 'EC'  Remove From Dictionary  ${payload}  KeyBitLength
354    ...  ELSE IF  '${key_pair_algorithm}' == 'RSA'  Remove From Dictionary  ${payload}  KeyCurveId
355
356    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
357    ...  '${expected_status}' == 'error'  ${HTTP_INTERNAL_SERVER_ERROR}, ${HTTP_BAD_REQUEST}
358    ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR
359    ...  body=${payload}  valid_status_codes=[${expected_resp}]
360
361    # Delay added between two CSR generation request.
362    Sleep  5s
363
364
365Delete Certificate Via BMC CLI
366    [Documentation]  Delete certificate via BMC CLI.
367    [Arguments]  ${cert_type}
368
369    # Description of argument(s):
370    # cert_type           Certificate type (e.g. "Client" or "CA").
371
372    ${certificate_file_path}  ${certificate_service}  ${certificate_uri}=
373    ...  Run Keyword If  '${cert_type}' == 'Client'
374    ...    Set Variable  /etc/nslcd/certs/cert.pem  phosphor-certificate-manager@nslcd.service
375    ...    ${REDFISH_LDAP_CERTIFICATE_URI}
376    ...  ELSE IF  '${cert_type}' == 'CA'
377    ...    Set Variable  ${ROOT_CA_FILE_PATH}  phosphor-certificate-manager@authority.service
378    ...    ${REDFISH_CA_CERTIFICATE_URI}
379
380    ${file_status}  ${stderr}  ${rc}=  BMC Execute Command
381    ...  [ -f ${certificate_file_path} ] && echo "Found" || echo "Not Found"
382
383    Return From Keyword If  "${file_status}" != "Found"
384    BMC Execute Command  rm ${certificate_file_path}
385    BMC Execute Command  systemctl restart ${certificate_service}
386    BMC Execute Command  systemctl daemon-reload
387    Wait Until Keyword Succeeds  1 min  10 sec  Redfish.Get  ${certificate_uri}/1
388    ...  valid_status_codes=[${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}]
389
390
391Delete All CA Certificate Via Redfish
392    [Documentation]  Delete all CA certificate via Redfish.
393
394    ${cert_list}=  Redfish_Utils.Get Member List  /redfish/v1/Managers/bmc/Truststore/Certificates
395    FOR  ${cert}  IN  @{cert_list}
396      Redfish.Delete  ${cert}  valid_status_codes=[${HTTP_NO_CONTENT}]
397    END
398
399
400Suite Setup Execution
401    [Documentation]  Do suite setup tasks.
402
403    # Create certificate sub-directory in current working directory.
404    Create Directory  certificate_dir
405    Redfish.Login
406
407
408Test Teardown Execution
409    [Documentation]  Do the post test teardown.
410
411    FFDC On Test Case Fail
412
413Suite Teardown
414    [Documentation]  Do suite teardown tasks.
415
416    Redfish.Logout