|
| 1 | +/* |
| 2 | + * Portions Copyright (c) Microsoft Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <sys/ioctl.h> |
| 18 | +#include <sys/types.h> |
| 19 | +#include <sys/stat.h> |
| 20 | +#include <fcntl.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <unistd.h> |
| 24 | +#include <string.h> |
| 25 | +#include <stdbool.h> |
| 26 | +#include <stdlib.h> |
| 27 | + |
| 28 | +#include "snp-attestation.h" |
| 29 | +#include "snp-ioctl5.h" |
| 30 | + |
| 31 | +#include "helpers.h" |
| 32 | + |
| 33 | +bool supportsDevSev() |
| 34 | +{ |
| 35 | + return access("/dev/sev", W_OK) == 0; |
| 36 | +} |
| 37 | + |
| 38 | +bool fetchAttestationReport5(const char* report_data_hexstring, void **snp_report) |
| 39 | +{ |
| 40 | + msg_report_req msg_report_in; |
| 41 | + msg_response_resp msg_report_out; |
| 42 | + |
| 43 | + int fd, rc; |
| 44 | + |
| 45 | + struct sev_snp_guest_request payload = { |
| 46 | + .req_msg_type = SNP_MSG_REPORT_REQ, |
| 47 | + .rsp_msg_type = SNP_MSG_REPORT_RSP, |
| 48 | + .msg_version = 1, |
| 49 | + .request_len = sizeof(msg_report_in), |
| 50 | + .request_uaddr = (uint64_t) (void*) &msg_report_in, |
| 51 | + .response_len = sizeof(msg_report_out), |
| 52 | + .response_uaddr = (uint64_t) (void*) &msg_report_out, |
| 53 | + .error = 0 |
| 54 | + }; |
| 55 | + |
| 56 | + memset((void*) &msg_report_in, 0, sizeof(msg_report_in)); |
| 57 | + memset((void*) &msg_report_out, 0, sizeof(msg_report_out)); |
| 58 | + |
| 59 | + // the report data is passed as a hexstring which needs to be decoded into an array of |
| 60 | + // unsigned bytes |
| 61 | + // MAA expects a SHA-256. So we use left align the bytes in the report data |
| 62 | + |
| 63 | + uint8_t *reportData = decodeHexString(report_data_hexstring, sizeof(msg_report_in.report_data)); |
| 64 | + memcpy(msg_report_in.report_data, reportData, sizeof(msg_report_in.report_data)); |
| 65 | + |
| 66 | + // open the file descriptor of the PSP |
| 67 | + fd = open("/dev/sev", O_RDWR | O_CLOEXEC); |
| 68 | + |
| 69 | + if (fd < 0) { |
| 70 | + fprintf(stderr, "Failed to open /dev/sev\n"); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // issue the custom SEV_SNP_GUEST_MSG_REPORT sys call to the sev driver |
| 75 | + rc = ioctl(fd, SEV_SNP_GUEST_MSG_REPORT, &payload); |
| 76 | + |
| 77 | + if (rc < 0) { |
| 78 | + fprintf(stderr, "Failed to issue ioctl SEV_SNP_GUEST_MSG_REPORT\n"); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + #ifdef DEBUG_OUTPUT |
| 83 | + fprintf(stderr, "Response header:\n"); |
| 84 | + uint8_t *hdr = (uint8_t*) &msg_report_out; |
| 85 | + |
| 86 | + for (size_t i = 0; i < 32; i++) { |
| 87 | + fprintf(stderr, "%02x", hdr[i]); |
| 88 | + if (i % 16 == 15) |
| 89 | + fprintf(stderr, "\n"); |
| 90 | + else |
| 91 | + fprintf(stderr, " "); |
| 92 | + } |
| 93 | + fprintf(stderr, "Attestation report:\n"); |
| 94 | + printReport(&msg_report_out.report); |
| 95 | + #endif |
| 96 | + |
| 97 | + *snp_report = (snp_attestation_report *) malloc (sizeof(snp_attestation_report)); |
| 98 | + memcpy(*snp_report, &msg_report_out.report, sizeof(snp_attestation_report)); |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
0 commit comments