blob: b81d461cf384291b8fc4e919185c6bf80ece6f86 [file] [log] [blame]
James Kuszmaul4a42b182021-01-17 11:32:46 -08001#pragma once
2#include <rawrtcc/code.h>
3#include <re.h>
4
5/*
6 * Certificate private key types.
7 */
8enum rawrtc_certificate_key_type {
9 // An RSA private key.
10 RAWRTC_CERTIFICATE_KEY_TYPE_RSA = TLS_KEYTYPE_RSA,
11 // An elliptic curve private key.
12 RAWRTC_CERTIFICATE_KEY_TYPE_EC = TLS_KEYTYPE_EC,
13};
14
15/*
16 * Certificate signing hash algorithms.
17 */
18enum rawrtc_certificate_sign_algorithm {
19 // Sign algorithm not set.
20 // Note: When passing this as an argument, a sensible default signing
21 // algorithm shall be used.
22 RAWRTC_CERTIFICATE_SIGN_ALGORITHM_NONE = 0,
23 // SHA-256 sign algorithm.
24 RAWRTC_CERTIFICATE_SIGN_ALGORITHM_SHA256 = TLS_FINGERPRINT_SHA256,
25 // SHA-384 sign algorithm.
26 RAWRTC_CERTIFICATE_SIGN_ALGORITHM_SHA384,
27 // SHA-512 sign algorithm.
28 RAWRTC_CERTIFICATE_SIGN_ALGORITHM_SHA512,
29};
30
31/*
32 * Certificate encoding.
33 */
34enum rawrtc_certificate_encode {
35 // Only encode the certificate.
36 RAWRTC_CERTIFICATE_ENCODE_CERTIFICATE,
37 // Only encode the private key.
38 RAWRTC_CERTIFICATE_ENCODE_PRIVATE_KEY,
39 // Encode both the certificate and the private key.
40 RAWRTC_CERTIFICATE_ENCODE_BOTH,
41};
42
43/*
44 * Certificate options.
45 */
46struct rawrtc_certificate_options;
47
48/*
49 * Certificate.
50 */
51struct rawrtc_certificate;
52
53/*
54 * Certificates.
55 * Note: Inherits `struct rawrtc_array_container`.
56 */
57struct rawrtc_certificates {
58 size_t n_certificates;
59 struct rawrtc_certificate* certificates[];
60};
61
62/*
63 * Create certificate options.
64 *
65 * All arguments but `key_type` are optional. Sane and safe default
66 * values will be applied, don't worry!
67 *
68 * `*optionsp` must be unreferenced.
69 *
70 * If `common_name` is `NULL` the default common name will be applied.
71 * If `valid_until` is `0` the default certificate lifetime will be
72 * applied.
73 * If the key type is `ECC` and `named_curve` is `NULL`, the default
74 * named curve will be used.
75 * If the key type is `RSA` and `modulus_length` is `0`, the default
76 * amount of bits will be used. The same applies to the
77 * `sign_algorithm` if it has been set to `NONE`.
78 */
79enum rawrtc_code rawrtc_certificate_options_create(
80 struct rawrtc_certificate_options** const optionsp, // de-referenced
81 enum rawrtc_certificate_key_type const key_type,
82 char* common_name, // nullable, copied
83 uint_fast32_t valid_until,
84 enum rawrtc_certificate_sign_algorithm sign_algorithm,
85 char* named_curve, // nullable, copied, ignored for RSA
86 uint_fast32_t modulus_length // ignored for ECC
87);
88
89/*
90 * Create and generate a self-signed certificate.
91 *
92 * Sane and safe default options will be applied if `options` is
93 * `NULL`.
94 *
95 * `*certificatep` must be unreferenced.
96 */
97enum rawrtc_code rawrtc_certificate_generate(
98 struct rawrtc_certificate** const certificatep,
99 struct rawrtc_certificate_options* options // nullable
100);
101
102/*
103 * TODO http://draft.ortc.org/#dom-rtccertificate
104 * rawrtc_certificate_from_bytes
105 * rawrtc_certificate_get_expires
106 * rawrtc_certificate_get_fingerprint
107 * rawrtc_certificate_get_algorithm
108 */
109
110/*
111 * Translate a certificate sign algorithm to str.
112 */
113char const* rawrtc_certificate_sign_algorithm_to_str(
114 enum rawrtc_certificate_sign_algorithm const algorithm);
115
116/*
117 * Translate a str to a certificate sign algorithm (case-insensitive).
118 */
119enum rawrtc_code rawrtc_str_to_certificate_sign_algorithm(
120 enum rawrtc_certificate_sign_algorithm* const algorithmp, // de-referenced
121 char const* const str);