commit b19c686

default  ·  2023-04-01 01:56:37 +0000 UTC
parent c83b601
Deleted OpenSSL that uses deprecated functions.
3 files changed,  +28, -86
+1, -1
1@@ -295,7 +295,7 @@ int adduser(const char *uid)
2     }
3 
4     printf("\nCreating RSA key...\n");
5-    key = xs_rsa_genkey(4096);
6+    key = xs_evp_genkey(4096);
7     printf("Done.\n");
8 
9     xs *kfn = xs_fmt("%s/key.json", basedir);
+26, -84
  1@@ -11,9 +11,7 @@ xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex
  2 #define xs_sha256_hex(input, size)    _xs_digest(input, size, "sha256", 1)
  3 #define xs_sha256_base64(input, size) _xs_digest(input, size, "sha256", 0)
  4 
  5-xs_dict *xs_rsa_genkey(int bits);
  6-xs_str *xs_rsa_sign(const char *secret, const char *mem, int size);
  7-int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
  8+xs_dict *xs_evp_genkey(int bits);
  9 xs_str *xs_evp_sign(const char *secret, const char *mem, int size);
 10 int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
 11 
 12@@ -47,98 +45,42 @@ xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex
 13 }
 14 
 15 
 16-xs_dict *xs_rsa_genkey(int bits)
 17-/* generates an RSA keypair */
 18+xs_dict *xs_evp_genkey(int bits)
 19+/* generates an RSA keypair using the EVP interface */
 20 {
 21-    BIGNUM *bne;
 22-    RSA *rsa;
 23     xs_dict *keypair = NULL;
 24+    EVP_PKEY_CTX *ctx;
 25+    EVP_PKEY *pkey = NULL;
 26 
 27-    if ((bne = BN_new()) != NULL) {
 28-        if (BN_set_word(bne, RSA_F4) == 1) {
 29-            if ((rsa = RSA_new()) != NULL) {
 30-                if (RSA_generate_key_ex(rsa, bits, bne, NULL) == 1) {
 31-                    BIO *bs = BIO_new(BIO_s_mem());
 32-                    BIO *bp = BIO_new(BIO_s_mem());
 33-                    BUF_MEM *sptr;
 34-                    BUF_MEM *pptr;
 35+    if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL)
 36+        goto end;
 37 
 38-                    PEM_write_bio_RSAPrivateKey(bs, rsa, NULL, NULL, 0, 0, NULL);
 39-                    BIO_get_mem_ptr(bs, &sptr);
 40+    if (EVP_PKEY_keygen_init(ctx) <= 0 ||
 41+        EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0 ||
 42+        EVP_PKEY_keygen(ctx, &pkey) <= 0)
 43+        goto end;
 44 
 45-                    PEM_write_bio_RSA_PUBKEY(bp, rsa);
 46-                    BIO_get_mem_ptr(bp, &pptr);
 47+    BIO *bs = BIO_new(BIO_s_mem());
 48+    BIO *bp = BIO_new(BIO_s_mem());
 49+    BUF_MEM *sptr;
 50+    BUF_MEM *pptr;
 51 
 52-                    keypair = xs_dict_new();
 53+    PEM_write_bio_PrivateKey(bs, pkey, NULL, NULL, 0, 0, NULL);
 54+    BIO_get_mem_ptr(bs, &sptr);
 55 
 56-                    keypair = xs_dict_append(keypair, "secret", sptr->data);
 57-                    keypair = xs_dict_append(keypair, "public", pptr->data);
 58+    PEM_write_bio_PUBKEY(bp, pkey);
 59+    BIO_get_mem_ptr(bp, &pptr);
 60 
 61-                    BIO_free(bs);
 62-                    BIO_free(bp);
 63-                }
 64-            }
 65-        }
 66-    }
 67-
 68-    return keypair;    
 69-}
 70-
 71-
 72-xs_str *xs_rsa_sign(const char *secret, const char *mem, int size)
 73-/* signs a memory block (secret is in PEM format) */
 74-{
 75-    xs_str *signature = NULL;
 76-    BIO *b;
 77-    RSA *rsa;
 78-    unsigned char *sig;
 79-    unsigned int sig_len;
 80-
 81-    /* un-PEM the key */
 82-    b = BIO_new_mem_buf(secret, strlen(secret));
 83-    rsa = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL);
 84-
 85-    /* alloc space */
 86-    sig = xs_realloc(NULL, RSA_size(rsa));
 87-
 88-    if (RSA_sign(NID_sha256, (unsigned char *)mem, size, sig, &sig_len, rsa) == 1)
 89-        signature = xs_base64_enc((char *)sig, sig_len);
 90-
 91-    BIO_free(b);
 92-    RSA_free(rsa);
 93-    xs_free(sig);
 94+    keypair = xs_dict_new();
 95 
 96-    return signature;
 97-}
 98+    keypair = xs_dict_append(keypair, "secret", sptr->data);
 99+    keypair = xs_dict_append(keypair, "public", pptr->data);
100 
101+    BIO_free(bs);
102+    BIO_free(bp);
103 
104-int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig)
105-/* verifies a base64 block, returns non-zero on ok */
106-{
107-    int r = 0;
108-    BIO *b;
109-    RSA *rsa;
110-
111-    /* un-PEM the key */
112-    b = BIO_new_mem_buf(pubkey, strlen(pubkey));
113-    rsa = PEM_read_bio_RSA_PUBKEY(b, NULL, NULL, NULL);
114-
115-    if (rsa != NULL) {
116-        xs *sig = NULL;
117-        int s_size;
118-
119-        /* de-base64 */
120-        sig = xs_base64_dec(b64sig,  &s_size);
121-
122-        if (sig != NULL)
123-            r = RSA_verify(NID_sha256, (unsigned char *)mem, size,
124-                           (unsigned char *)sig, s_size, rsa);
125-    }
126-
127-    BIO_free(b);
128-    RSA_free(rsa);
129-
130-    return r;
131+end:
132+    return keypair;
133 }
134 
135 
+1, -1
1@@ -1 +1 @@
2-/* fe95bda22e514fa188c50438cce0eee0c6919911 */
3+/* b4afa5f823a998a263159ebfe9be67b81a8cc774 */