Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keychain support public #229

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions include/tlsuv/keychain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#ifndef TLSUV_KEYCHAIN_H
#define TLSUV_KEYCHAIN_H

#if __cplusplus
#include <cstddef>
#include <cstdint>
#else
#include <stddef.h>
#include <stdint.h>
#endif

enum keychain_key_type {
keychain_key_invalid,
keychain_key_ec,
keychain_key_rsa,
};

typedef void* keychain_key_t;

// generic keychain API
typedef struct keychain_s keychain_t;
struct keychain_s {
int (*gen_key)(keychain_key_t *pk, enum keychain_key_type type, const char *name);
int (*load_key)(keychain_key_t*, const char *name);
int (*rem_key)(const char *name);

enum keychain_key_type (*key_type)(keychain_key_t k);
int (*key_public)(keychain_key_t k, char *buf, size_t *len);
int (*key_sign)(keychain_key_t k, const uint8_t * data, size_t datalen,
uint8_t *sig, size_t *siglen, int p);

void (*free_key)(keychain_key_t k);
};

#if __cplusplus
extern "C" {
#endif
const keychain_t *tlsuv_keychain();
void tlsuv_set_keychain(keychain_t *);
#if __cplusplus
}
#endif

#endif //TLSUV_KEYCHAIN_H
32 changes: 4 additions & 28 deletions src/keychain.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TLSUV_KEYCHAIN_H
#define TLSUV_KEYCHAIN_H
#ifndef TLSUV_SRC_KEYCHAIN_H
#define TLSUV_SRC_KEYCHAIN_H

enum keychain_key_type {
keychain_key_invalid,
keychain_key_ec,
keychain_key_rsa,
};

typedef void* keychain_key_t;

// generic keychain API
typedef struct keychain_s keychain_t;
struct keychain_s {
int (*gen_key)(keychain_key_t *pk, enum keychain_key_type type, const char *name);
int (*load_key)(keychain_key_t*, const char *name);
int (*rem_key)(const char *name);

enum keychain_key_type (*key_type)(keychain_key_t k);
int (*key_public)(keychain_key_t k, char *buf, size_t *len);
int (*key_sign)(keychain_key_t k, const uint8_t * data, size_t datalen,
uint8_t *sig, size_t *siglen, int p);

void (*free_key)(keychain_key_t k);
};

const keychain_t* tlsuv_keychain();
void tlsuv_set_keychain(keychain_t *);
#include <tlsuv/keychain.h>

int keychain_gen_key(keychain_key_t *pk, enum keychain_key_type type, const char *name);
int keychain_load_key(keychain_key_t*, const char *name);
Expand All @@ -52,4 +28,4 @@ int keychain_key_sign(keychain_key_t k, const uint8_t * data, size_t datalen,

void keychain_free_key(keychain_key_t k);

#endif //TLSUV_KEYCHAIN_H
#endif //TLSUV_SRC_KEYCHAIN_H
26 changes: 26 additions & 0 deletions src/openssl/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,32 @@
goto error;
}

// check if pub key is ASN.1 SubjectPublicKeyInfo format
// https://docs.openssl.org/3.3/man3/X509_PUBKEY_new/#synopsis
const uint8_t *p = pub;

Check warning on line 516 in src/openssl/keys.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

initializing 'const uint8_t *' (aka 'const unsigned char *') with an expression of type 'char[1024]' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]
X509_PUBKEY *x509_pub = d2i_X509_PUBKEY(NULL, &p, (long)publen);
if (x509_pub != NULL) {
EVP_PKEY *pk1 = X509_PUBKEY_get(x509_pub);
X509_PUBKEY_free(x509_pub);
int key_type = EVP_PKEY_get_base_id(pk1);
if (key_type == EVP_PKEY_EC) {
EC_KEY *key = EVP_PKEY_get1_EC_KEY(pk1);
EC_KEY_set_ex_data(key, kc_ec_idx, k);
EC_KEY_set_method(key, ext_ec_method);
EVP_PKEY_set1_EC_KEY(pk1, key);
EC_KEY_free(key); // decrease refcount
} else if (key_type == EVP_PKEY_RSA) {
RSA *rsa = EVP_PKEY_get0_RSA(pk1);

Check warning on line 529 in src/openssl/keys.c

View workflow job for this annotation

GitHub Actions / build (ubuntu, openssl)

initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

Check warning on line 529 in src/openssl/keys.c

View workflow job for this annotation

GitHub Actions / build (macOS, openssl)

initializing 'RSA *' (aka 'struct rsa_st *') with an expression of type 'const struct rsa_st *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
RSA_set_ex_data(rsa, kc_rsa_idx, k);
RSA_set_method(rsa, ext_rsa_method);
} else {
EVP_PKEY_free(pk1);
return -1;
}
*pkey = pk1;
return 0;
}

if (keychain_key_type(k) == keychain_key_ec) {

const char *group = NULL;
Expand Down
Loading