26#include <openssl/pem.h>
27#include <openssl/x509.h>
33#ifndef DEFAULT_CONNECTOR_URL
34#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
41static void print_extension(X509_EXTENSION *extension) {
44 const uint8_t version[] = {0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
45 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x01};
47 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x02};
48 const uint8_t origin[] = {0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
49 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x03};
51 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x04};
53 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x05};
54 const uint8_t id[] = {0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
55 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x06};
56 const uint8_t label[] = {0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
57 0x01, 0x82, 0xc4, 0x0a, 0x04, 0x09};
63 if (i2d_ASN1_OBJECT(a_object, NULL) > 1024) {
64 printf(
"Extension to long.\n");
67 unsigned int object_len = i2d_ASN1_OBJECT(a_object, &ptr);
71 if (i2d_ASN1_OCTET_STRING(a_value, NULL) > 1024) {
72 printf(
"Extension value to long.\n");
75 unsigned int value_len = i2d_ASN1_OCTET_STRING(a_value, &ptr);
77 if (object_len ==
sizeof(version) &&
78 memcmp(
object, version,
sizeof(version)) == 0) {
80 }
else if (object_len ==
sizeof(
serial) &&
83 }
else if (object_len ==
sizeof(origin) &&
84 memcmp(
object, origin,
sizeof(origin)) == 0) {
86 }
else if (object_len ==
sizeof(
domains) &&
91 printf(
"Capabilities:");
92 }
else if (object_len ==
sizeof(
id) && memcmp(
object,
id,
sizeof(
id)) == 0) {
94 }
else if (object_len ==
sizeof(
label) &&
101 for (
unsigned int i = 0; i <
value_len; i++) {
102 printf(
" %02x",
value[i]);
114 const char *connector_url;
116 connector_url = getenv(
"DEFAULT_CONNECTOR_URL");
117 if (connector_url == NULL) {
131 sizeof(password),
false, &
session);
141 printf(
"Successfully established session %02d\n", session_id);
158 printf(
"Generated attesting key with ID %04x\n", attesting_key_id);
163 X509 *attestation_template = PEM_read_X509(fp, NULL, NULL, NULL);
164 assert(attestation_template != NULL);
167 uint8_t attestation_template_buffer[3072];
168 uint16_t attestation_template_buffer_len =
169 i2d_X509(attestation_template, NULL);
170 assert(
sizeof(attestation_template_buffer) >=
171 attestation_template_buffer_len);
173 unsigned char *certptr = attestation_template_buffer;
175 i2d_X509(attestation_template, &certptr);
176 X509_free(attestation_template);
182 attestation_template_buffer,
183 attestation_template_buffer_len);
187 size_t tmpbuf_len =
sizeof(tmpbuf);
190 assert(tmpbuf_len == attestation_template_buffer_len);
191 assert(memcmp(attestation_template_buffer, tmpbuf, tmpbuf_len) == 0);
202 printf(
"Generated attested key with ID %04x\n", attested_key_id);
205 size_t attestation_len =
sizeof(attestation);
208 attesting_key_id, attestation,
212 const unsigned char *ptr = attestation;
214 X509 *x509 = d2i_X509(NULL, &ptr, attestation_len);
215 assert(x509 != NULL);
217 BIO *STDout = BIO_new_fp(stdout, BIO_NOCLOSE);
219 X509_print_ex(STDout, x509, 0, 0);
223 const STACK_OF(X509_EXTENSION) *extensions_list = X509_get0_extensions(x509);
224 assert(sk_X509_EXTENSION_num(extensions_list) >= 6);
226 for (
int i = 0; i < sk_X509_EXTENSION_num(extensions_list); i++) {
227 X509_EXTENSION *extension;
229 extension = sk_X509_EXTENSION_value(extensions_list, i);
231 print_extension(extension);
#define DEFAULT_CONNECTOR_URL
const char attestation_template_file[]
CK_SESSION_HANDLE session
ASN1_OCTET_STRING * X509_EXTENSION_get_data(X509_EXTENSION *ex)
ASN1_OBJECT * X509_EXTENSION_get_object(X509_EXTENSION *ex)
const STACK_OF(X509_EXTENSION)
Capabilities representation.
uint8_t capabilities[YH_CAPABILITIES_LEN]
Capabilities is represented as an 8 byte uint8_t array.
yh_rc yh_util_import_opaque(yh_session *session, uint16_t *object_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *in, size_t in_len)
yh_rc yh_destroy_session(yh_session **session)
yh_rc yh_util_generate_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm)
yh_rc yh_create_session_derived(yh_connector *connector, uint16_t authkey_id, const uint8_t *password, size_t password_len, bool recreate, yh_session **session)
yh_rc yh_util_get_opaque(yh_session *session, uint16_t object_id, uint8_t *out, size_t *out_len)
yh_rc yh_util_sign_attestation_certificate(yh_session *session, uint16_t key_id, uint16_t attest_id, uint8_t *out, size_t *out_len)
yh_rc yh_util_close_session(yh_session *session)
yh_rc yh_authenticate_session(yh_session *session)
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
yh_rc yh_init_connector(const char *url, yh_connector **connector)
yh_rc yh_connect(yh_connector *connector, int timeout)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_disconnect(yh_connector *connector)
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
@ YH_ALGO_OPAQUE_X509_CERTIFICATE
opaque-x509-certificate
#define YH_CAPABILITIES_LEN
Length of capabilities array.
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities
yh_object_descriptor object
memset(pInfo->slotDescription, ' ', 64)