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    ...  X-Auth-Token=${XAUTH_TOKEN}
28    Set To Dictionary  ${kwargs}  headers  ${headers}
29
30    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Put
31    ...  base_uri=${uri}  args=&{kwargs}
32
33    ${ret}=  Put Request  openbmc  ${uri}  &{kwargs}
34    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
35
36    Run Keyword If  '${status}' == 'ok'
37    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_OK}
38    ...  ELSE IF  '${status}' == 'error'
39    ...  Should Be Equal As Strings  ${ret.status_code}  ${HTTP_BAD_REQUEST}
40
41    Delete All Sessions
42
43
44Get Certificate Content From BMC Via Openssl
45    [Documentation]  Get certificate content from BMC via openssl.
46
47    Check If Openssl Tool Exist
48
49    ${openssl_cmd}=  Catenate
50    ...  openssl s_client -connect ${OPENBMC_HOST}:443 -showcerts
51    ${rc}  ${output}=  Run And Return RC and Output  ${openssl_cmd}
52    Should Be Equal  ${rc}  ${0}  msg=${output}
53
54    ${result}=  Fetch From Left
55    ...  ${output}  -----END CERTIFICATE-----
56    ${result}=  Fetch From Right  ${result}  -----BEGIN CERTIFICATE-----
57    [Return]  ${result}
58
59
60Get Certificate File Content From BMC
61    [Documentation]  Get required certificate file content from BMC.
62    [Arguments]  ${cert_type}=Client
63
64    # Description of argument(s):
65    # cert_type      Certificate type (e.g. "Client" or "CA").
66
67    ${certificate}  ${stderr}  ${rc}=  Run Keyword If  '${cert_type}' == 'Client'
68    ...    BMC Execute Command  cat /etc/nslcd/certs/cert.pem
69    ...  ELSE IF  '${cert_type}' == 'CA'
70    ...    BMC Execute Command  cat /etc/ssl/certs/Root-CA.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'
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