|
| 1 | +/* |
| 2 | + * Copyright 2026 Aurora Operations, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0 |
| 5 | + * |
| 6 | + * This work is dual licensed. |
| 7 | + * You may use it under Apache-2.0 or GPL-2.0 at your option. |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * |
| 21 | + * OR |
| 22 | + * |
| 23 | + * This program is free software; you can redistribute it and/or |
| 24 | + * modify it under the terms of the GNU General Public License |
| 25 | + * as published by the Free Software Foundation; either version 2 |
| 26 | + * of the License, or (at your option) any later version. |
| 27 | + * |
| 28 | + * This program is distributed in the hope that it will be useful, |
| 29 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | + * GNU General Public License for more details. |
| 32 | + * |
| 33 | + * You should have received a copy of the GNU General Public License |
| 34 | + * along with this program; if not, see |
| 35 | + * <https://www.gnu.org/licenses/>. |
| 36 | + */ |
| 37 | + |
| 38 | +#include <nat20/cbor.h> |
| 39 | +#include <nat20/cose.h> |
| 40 | +#include <nat20/crypto.h> |
| 41 | +#include <nat20/cwt.h> |
| 42 | +#include <nat20/error.h> |
| 43 | +#include <nat20/stream.h> |
| 44 | +#include <nat20/types.h> |
| 45 | + |
| 46 | +static void n20_open_dice_write_name_as_hex(n20_stream_t *const s, n20_slice_t const name) { |
| 47 | + if (name.size != 0 && name.buffer == NULL) { |
| 48 | + n20_cbor_write_null(s); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + for (size_t i = 0; i < name.size; ++i) { |
| 53 | + uint8_t byte = name.buffer[name.size - (i + 1)]; |
| 54 | + uint8_t hex[2] = {(byte >> 4) + '0', (byte & 0x0f) + '0'}; |
| 55 | + if (hex[0] > '9') { |
| 56 | + hex[0] += 39; // Convert to 'a' - 'f' |
| 57 | + } |
| 58 | + if (hex[1] > '9') { |
| 59 | + hex[1] += 39; // Convert to 'a' - 'f' |
| 60 | + } |
| 61 | + n20_stream_prepend(s, hex, sizeof(hex)); |
| 62 | + } |
| 63 | + n20_cbor_write_header(s, n20_cbor_type_string_e, name.size * 2); |
| 64 | +} |
| 65 | + |
| 66 | +void n20_cwt_key_info_to_cose(n20_cose_key_t *const cose_key, |
| 67 | + n20_open_dice_public_key_info_t const *const key_info) { |
| 68 | + if (key_info == NULL || cose_key == NULL) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + switch (key_info->algorithm) { |
| 73 | + case n20_crypto_key_type_ed25519_e: |
| 74 | + cose_key->algorithm_id = n20_cose_algorithm_id_eddsa_e; |
| 75 | + cose_key->x = key_info->key; |
| 76 | + cose_key->y = N20_SLICE_NULL; |
| 77 | + cose_key->d = N20_SLICE_NULL; |
| 78 | + return; |
| 79 | + case n20_crypto_key_type_secp256r1_e: |
| 80 | + cose_key->algorithm_id = n20_cose_algorithm_id_es256_e; |
| 81 | + break; |
| 82 | + case n20_crypto_key_type_secp384r1_e: |
| 83 | + cose_key->algorithm_id = n20_cose_algorithm_id_es384_e; |
| 84 | + break; |
| 85 | + default: |
| 86 | + /* Unsupported algorithm -> don't touch the COSE key structure. */ |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + /* For NIST curves P-256 and P-384 the key is split into |
| 91 | + * X and Y coordinates. */ |
| 92 | + size_t coordinate_size = key_info->key.size / 2; |
| 93 | + |
| 94 | + /* If the key size is odd skip the first byte to swallow |
| 95 | + * the format header. */ |
| 96 | + cose_key->x.buffer = key_info->key.buffer + (key_info->key.size & 1); |
| 97 | + cose_key->x.size = coordinate_size; |
| 98 | + cose_key->y.buffer = cose_key->x.buffer + coordinate_size; |
| 99 | + cose_key->y.size = coordinate_size; |
| 100 | + cose_key->d = N20_SLICE_NULL; |
| 101 | +} |
| 102 | + |
| 103 | +void n20_open_dice_cwt_write(n20_stream_t *const s, n20_open_dice_cert_info_t const *const cwt) { |
| 104 | + uint32_t pairs = 0; |
| 105 | + |
| 106 | + // Write Profile Name |
| 107 | + if (cwt->open_dice_input.profile_name.size > 0) { |
| 108 | + n20_cbor_write_text_string(s, cwt->open_dice_input.profile_name); |
| 109 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_PROFILE); |
| 110 | + pairs++; |
| 111 | + } |
| 112 | + |
| 113 | + // Write Key Usage |
| 114 | + n20_cbor_write_byte_string( |
| 115 | + s, |
| 116 | + (n20_slice_t){.buffer = (uint8_t *)cwt->key_usage, |
| 117 | + .size = (cwt->key_usage[1] != 0 ? 2 : (cwt->key_usage[0] != 0))}); |
| 118 | + |
| 119 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_KEY_USAGE); |
| 120 | + ++pairs; // Key Usage |
| 121 | + |
| 122 | + size_t mark = n20_stream_byte_count(s); |
| 123 | + |
| 124 | + n20_cose_key_t subject_public_key = {0}; |
| 125 | + n20_cose_key_ops_set(&subject_public_key.key_ops, n20_cose_key_op_verify_e); |
| 126 | + |
| 127 | + n20_cwt_key_info_to_cose(&subject_public_key, &cwt->subject_public_key); |
| 128 | + |
| 129 | + // Subject public key |
| 130 | + n20_cose_write_key(s, &subject_public_key); |
| 131 | + n20_cbor_write_header(s, n20_cbor_type_bytes_e, n20_stream_byte_count(s) - mark); |
| 132 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_SUBJECT_PUBLIC_KEY); |
| 133 | + ++pairs; |
| 134 | + |
| 135 | + // Convert mode to uint8_t |
| 136 | + uint8_t mode = (uint8_t)cwt->open_dice_input.mode; |
| 137 | + |
| 138 | + n20_cbor_write_byte_string(s, (n20_slice_t){.buffer = &mode, .size = 1}); |
| 139 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_MODE); |
| 140 | + ++pairs; |
| 141 | + |
| 142 | + if (cwt->open_dice_input.authority_descriptor.size > 0) { |
| 143 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.authority_descriptor); |
| 144 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_AUTHORITY_DESCRIPTOR); |
| 145 | + ++pairs; |
| 146 | + } |
| 147 | + |
| 148 | + if (cwt->open_dice_input.authority_hash.size > 0) { |
| 149 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.authority_hash); |
| 150 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_AUTHORITY_HASH); |
| 151 | + ++pairs; |
| 152 | + } |
| 153 | + |
| 154 | + if (cwt->open_dice_input.configuration_descriptor.size > 0) { |
| 155 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.configuration_descriptor); |
| 156 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_CONFIGURATION_DESCRIPTOR); |
| 157 | + ++pairs; |
| 158 | + } |
| 159 | + |
| 160 | + if (cwt->open_dice_input.configuration_hash.size > 0) { |
| 161 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.configuration_hash); |
| 162 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_CONFIGURATION_HASH); |
| 163 | + ++pairs; |
| 164 | + } |
| 165 | + if (cwt->open_dice_input.code_descriptor.size > 0) { |
| 166 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.code_descriptor); |
| 167 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_CODE_DESCRIPTOR); |
| 168 | + ++pairs; |
| 169 | + } |
| 170 | + if (cwt->open_dice_input.code_hash.size > 0) { |
| 171 | + n20_cbor_write_byte_string(s, cwt->open_dice_input.code_hash); |
| 172 | + n20_cbor_write_int(s, N20_OPEN_DICE_CWT_LABEL_CODE_HASH); |
| 173 | + ++pairs; |
| 174 | + } |
| 175 | + |
| 176 | + if (cwt->subject.size > 0) { |
| 177 | + n20_open_dice_write_name_as_hex(s, cwt->subject); |
| 178 | + n20_cbor_write_int(s, N20_CWT_LABEL_SUBJECT); |
| 179 | + ++pairs; |
| 180 | + } |
| 181 | + |
| 182 | + if (cwt->issuer.size > 0) { |
| 183 | + n20_open_dice_write_name_as_hex(s, cwt->issuer); |
| 184 | + n20_cbor_write_int(s, N20_CWT_LABEL_ISSUER); |
| 185 | + ++pairs; |
| 186 | + } |
| 187 | + |
| 188 | + // Write the map header with the number of pairs. |
| 189 | + n20_cbor_write_map_header(s, pairs); |
| 190 | +} |
0 commit comments