xref: /openbmc/openbmc-test-automation/lib/certificate_utils.robot (revision 665bc61e1efab6f903983b44efdf1f5cc1b4d3b0)
1*** Settings ***
2Documentation  Certificate utilities keywords.
3
4Library        OperatingSystem
5Resource       rest_client.robot
6Resource       resource.txt
7
8
9*** Keywords ***
10
11Install Certificate File On BMC
12    [Documentation]  Install certificate file in BMC using REST PUT operation.
13    [Arguments]  ${uri}  ${status}=ok  ${quiet}=${1}  &{kwargs}
14
15    # Description of argument(s):
16    # uri         URI for installing certificate file via REST
17    #             e.g. "/xyz/openbmc_project/certs/server/https".
18    # status      Expected status of certificate installation via REST
19    #             e.g. error, ok.
20    # quiet       If enabled, turns off logging to console.
21    # kwargs      A dictionary of keys/values to be passed directly to
22    #             PUT Request.
23
24    Initialize OpenBMC  quiet=${quiet}
25
26    ${headers}=  Create Dictionary  Content-Type=application/octet-stream
27    Set To Dictionary  ${kwargs}  headers  ${headers}
28
29    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Put
30    ...  base_uri=${uri}  args=&{kwargs}
31
32    ${ret}=  Put Request  openbmc  ${uri}  &{kwargs}
33    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
34
35    Run Keyword If  '${status}' == 'ok'
36    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_OK}
37    ...  ELSE IF  '${status}' == 'error'
38    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_BAD_REQUEST}
39
40    Delete All Sessions
41
42
43Get Certificate Content From BMC Via Openssl
44    [Documentation]  Get certificate content from BMC via openssl.
45
46    Check If Openssl Tool Exist
47
48    ${openssl_cmd}=  Catenate
49    ...  openssl s_client -connect ${OPENBMC_HOST}:443 -showcerts
50    ${rc}  ${output}=  Run And Return RC and Output  ${openssl_cmd}
51    Should Be Equal  ${rc}  ${0}  msg=${output}
52
53    ${result}=  Fetch From Left
54    ...  ${output}  -----END CERTIFICATE-----
55    ${result}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
56    [Return]  ${result}
57
58
59Get Client Certificate File Content From BMC
60    [Documentation]  Get client certificate file content from BMC.
61
62    ${certificate}  ${stderr}  ${rc}=  BMC Execute Command
63    ...  cat /etc/nslcd/certs/cert.pem
64    Should Be Equal  ${rc}  ${0}  msg=${stderr}
65
66    [Return]  ${certificate}
67
68
69Generate Certificate File Via Openssl
70    [Documentation]  Create certificate file via openssl with required content
71    ...              and returns its path.
72    [Arguments]  ${cert_format}  ${time}=365  ${cert_dir_name}=certificate_dir
73
74    # Description of argument(s):
75    # cert_format          Certificate file format
76    #                      e.g. Valid_Certificate_Empty_Privatekey.
77    # time                 Number of days to certify the certificate for.
78    # cert_dir_name        The name of the sub-directory where the certificate
79    #                      is stored.
80
81    Check If Openssl Tool Exist
82
83    ${openssl_cmd}=  Catenate  openssl req -x509 -sha256 -newkey rsa:2048
84    ...  ${SPACE}-nodes -days ${time}
85    ...  ${SPACE}-keyout ${cert_dir_name}/cert.pem -out ${cert_dir_name}/cert.pem
86    ...  ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com"
87
88    ${rc}  ${output}=  Run And Return RC and Output  ${openssl_cmd}
89    Should Be Equal  ${rc}  ${0}  msg=${output}
90    OperatingSystem.File Should Exist
91    ...  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
92
93    ${file_content}=  OperatingSystem.Get File
94    ...  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
95    ${result}=  Fetch From Left  ${file_content}  -----END CERTIFICATE-----
96    ${cert_content}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
97
98    ${result}=  Fetch From Left  ${file_content}  -----END PRIVATE KEY-----
99    ${private_key_content}=  Fetch From Right  ${result}  -----BEGIN PRIVATE KEY-----
100
101    ${cert_data}=
102    ...  Run Keyword if  '${cert_format}' == 'Valid Certificate Valid Privatekey'
103    ...  OperatingSystem.Get File  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
104    ...  ELSE IF  '${cert_format}' == 'Empty Certificate Valid Privatekey'
105    ...  Remove String  ${file_content}  ${cert_content}
106    ...  ELSE IF  '${cert_format}' == 'Valid Certificate Empty Privatekey'
107    ...  Remove String  ${file_content}  ${private_key_content}
108    ...  ELSE IF  '${cert_format}' == 'Empty Certificate Empty Privatekey'
109    ...  Remove String  ${file_content}  ${cert_content}  ${private_key_content}
110    ...  ELSE IF  '${cert_format}' == 'Expired Certificate'
111    ...  OperatingSystem.Get File  ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
112
113    ${random_name}=  Generate Random String  8
114    ${cert_name}=  Catenate  SEPARATOR=  ${random_name}  .pem
115    Create File  ${cert_dir_name}/${cert_name}  ${cert_data}
116
117    [Return]  ${EXECDIR}${/}${cert_dir_name}${/}${cert_name}
118
119
120Get Certificate Content From File
121    [Documentation]  Get certificate content from certificate file.
122    [Arguments]  ${cert_file_path}
123
124    # Description of argument(s):
125    # cert_file_path  Downloaded certificate file path.
126
127    ${file_content}=  OperatingSystem.Get File  ${cert_file_path}
128    ${result}=  Fetch From Left  ${file_content}  -----END CERTIFICATE-----
129    ${result}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
130    [Return]  ${result}
131
132
133Check If Openssl Tool Exist
134    [Documentation]  Check if openssl tool installed or not.
135
136    ${rc}  ${output}=  Run And Return RC and Output  which openssl
137    Should Not Be Empty  ${output}  msg=Openssl tool not installed.
138
139