36 #ifdef HAVE_XKEY_PROVIDER
38 #include <openssl/provider.h>
39 #include <openssl/params.h>
40 #include <openssl/core_dispatch.h>
41 #include <openssl/core_object.h>
42 #include <openssl/core_names.h>
43 #include <openssl/store.h>
44 #include <openssl/evp.h>
45 #include <openssl/err.h>
47 static const char *
const props = XKEY_PROV_PROPS;
49 XKEY_EXTERNAL_SIGN_fn xkey_management_sign;
52 print_openssl_errors(
void)
55 while ((e = ERR_get_error()))
57 msg(
M_WARN,
"OpenSSL error %lu: %s\n", e, ERR_error_string(e, NULL));
63 xkey_digest(
const unsigned char *src,
size_t srclen,
unsigned char *buf,
64 size_t *buflen,
const char *mdname)
70 msg(
M_WARN,
"WARN: xkey_digest: MD_fetch failed for <%s>", mdname);
74 unsigned int len = (
unsigned int) *buflen;
75 if (EVP_Digest(src, srclen, buf, &len, md, NULL) != 1)
77 msg(
M_WARN,
"WARN: xkey_digest: EVP_Digest failed");
86 #ifdef ENABLE_MANAGEMENT
95 xkey_load_management_key(
OSSL_LIB_CTX *libctx, EVP_PKEY *pubkey)
103 void *dummy = &
"dummy";
105 XKEY_EXTERNAL_SIGN_fn *sign_op = xkey_management_sign;
107 return xkey_load_generic_key(libctx, dummy, pubkey, sign_op, NULL);
117 xkey_load_generic_key(
OSSL_LIB_CTX *libctx,
void *handle, EVP_PKEY *pubkey,
118 XKEY_EXTERNAL_SIGN_fn *sign_op, XKEY_PRIVKEY_FREE_fn *free_op)
120 EVP_PKEY *pkey = NULL;
121 const char *origin =
"external";
124 OSSL_PARAM params[] = {
125 {
"xkey-origin", OSSL_PARAM_UTF8_STRING, (
char *) origin, 0, 0},
126 {
"pubkey", OSSL_PARAM_OCTET_STRING, &pubkey,
sizeof(pubkey), 0},
127 {
"handle", OSSL_PARAM_OCTET_PTR, &handle,
sizeof(handle), 0},
128 {
"sign_op", OSSL_PARAM_OCTET_PTR, (
void **) &sign_op,
sizeof(sign_op), 0},
129 {
"free_op", OSSL_PARAM_OCTET_PTR, (
void **) &free_op,
sizeof(free_op), 0},
130 {NULL, 0, NULL, 0, 0}
134 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, EVP_PKEY_get0_type_name(pubkey), props);
136 || EVP_PKEY_fromdata_init(ctx) != 1
137 || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1)
139 print_openssl_errors();
140 msg(
M_FATAL,
"OpenSSL error: failed to load key into ovpn.xkey provider");
144 EVP_PKEY_CTX_free(ctx);
150 #ifdef ENABLE_MANAGEMENT
165 xkey_management_sign(
void *unused,
unsigned char *sig,
size_t *siglen,
166 const unsigned char *tbs,
size_t tbslen, XKEY_SIGALG alg)
168 dmsg(
D_XKEY,
"In xkey_management_sign with keytype = %s, op = %s",
169 alg.keytype, alg.op);
173 unsigned char buf[EVP_MAX_MD_SIZE];
174 size_t buflen =
sizeof(buf);
176 unsigned char enc[EVP_MAX_MD_SIZE + 32];
177 size_t enc_len =
sizeof(enc);
180 bool is_message = !strcmp(alg.op,
"DigestSign");
184 && strcmp(alg.mdname,
"none"))
186 dmsg(
D_XKEY,
"xkey_management_sign: computing digest");
187 if (xkey_digest(tbs, tbslen, buf, &buflen, alg.mdname))
200 if (!strcmp(alg.keytype,
"EC"))
202 if (!strcmp(alg.op,
"Sign"))
204 strncpynt(alg_str,
"ECDSA",
sizeof(alg_str));
208 snprintf(alg_str,
sizeof(alg_str),
"ECDSA,hashalg=%s", alg.mdname);
211 else if (!strcmp(alg.keytype,
"ED448") || !strcmp(alg.keytype,
"ED25519"))
213 strncpynt(alg_str, alg.keytype,
sizeof(alg_str));
219 if (!strcmp(alg.op,
"Sign"))
221 if (!encode_pkcs1(enc, &enc_len, alg.mdname, tbs, tbslen))
227 strncpynt(alg_str,
"RSA_PKCS1_PADDING",
sizeof(alg_str));
232 snprintf(alg_str,
sizeof(alg_str),
"%s,hashalg=%s",
233 "RSA_PKCS1_PADDING", alg.mdname);
237 && !strcmp(alg.op,
"Sign"))
239 strncpynt(alg_str,
"RSA_NO_PADDING",
sizeof(alg_str));
243 snprintf(alg_str,
sizeof(alg_str),
"%s,hashalg=%s,saltlen=%s",
244 "RSA_PKCS1_PSS_PADDING", alg.mdname, alg.saltlen);
248 msg(
M_NONFATAL,
"RSA padding mode not supported by management-client <%s>",
255 strncat(alg_str,
",data=message",
sizeof(alg_str) - strlen(alg_str) - 1);
258 dmsg(
D_LOW,
"xkey management_sign: requesting sig with algorithm <%s>", alg_str);
261 char *out_b64 = NULL;
277 *siglen = (len > 0) ? len : 0;
279 return (*siglen > 0);
299 encode_pkcs1(
unsigned char *enc,
size_t *enc_len,
const char *mdname,
300 const unsigned char *tbs,
size_t tbslen)
313 const unsigned char sha1[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b,
314 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
315 const unsigned char sha256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
316 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
317 const unsigned char sha384[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
318 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30};
319 const unsigned char sha512[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
320 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
321 const unsigned char sha224[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
322 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c};
323 const unsigned char sha512_224[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
324 0x01, 0x65, 0x03, 0x04, 0x02, 0x05, 0x05, 0x00, 0x04, 0x1c};
325 const unsigned char sha512_256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
326 0x01, 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20};
330 const unsigned char *header;
334 #define MAKE_DI(x) {NID_ ## x, x, sizeof(x)}
336 DIG_INFO dinfo[] = {MAKE_DI(sha1), MAKE_DI(sha256), MAKE_DI(sha384),
337 MAKE_DI(sha512), MAKE_DI(sha224), MAKE_DI(sha512_224),
338 MAKE_DI(sha512_256), {0, NULL, 0}};
343 int nid = OBJ_sn2nid(mdname);
344 if (nid == NID_undef)
347 nid = EVP_MD_type(EVP_get_digestbyname(mdname));
348 if (nid == NID_undef)
350 msg(
M_WARN,
"Error: encode_pkcs11: invalid digest name <%s>", mdname);
355 if (tbslen != EVP_MD_size(EVP_get_digestbyname(mdname)))
357 msg(
M_WARN,
"Error: encode_pkcs11: invalid input length <%d>", (
int)tbslen);
361 if (nid == NID_md5_sha1)
363 if (enc && (*enc_len >= tbslen))
365 memcpy(enc, tbs, tbslen);
373 DIG_INFO *di = dinfo;
374 while ((di->nid != nid) && (di->nid != 0))
380 msg(
M_WARN,
"Error: encode_pkcs11: unsupported hash algorithm <%s>", mdname);
384 out_len = tbslen + di->sz;
386 if (enc && (out_len <= (
int) *enc_len))
389 memcpy(enc, di->header, di->sz);
390 memcpy(enc + di->sz, tbs, tbslen);
391 dmsg(
D_XKEY,
"encode_pkcs1: digest length = %d encoded length = %d",
392 (
int) tbslen, (
int) out_len);
409 ecdsa_bin2der(
unsigned char *buf,
int len,
size_t capacity)
411 ECDSA_SIG *ecsig = NULL;
413 BIGNUM *r = BN_bin2bn(buf, rlen, NULL);
414 BIGNUM *s = BN_bin2bn(buf+rlen, rlen, NULL);
419 ecsig = ECDSA_SIG_new();
424 if (!ECDSA_SIG_set0(ecsig, r, s))
426 ECDSA_SIG_free(ecsig);
430 int derlen = i2d_ECDSA_SIG(ecsig, NULL);
431 if (derlen > (
int) capacity)
433 ECDSA_SIG_free(ecsig);
434 msg(
M_NONFATAL,
"Error: DER encoded ECDSA signature is too long (%d)\n", derlen);
437 derlen = i2d_ECDSA_SIG(ecsig, &buf);
438 ECDSA_SIG_free(ecsig);