[coop] Refactor/reuse mono_value_box_handle/mono_value_box_checked and reduce raw...
[mono-project.git] / mono / btls / btls-key.c
blobf7e677187b27251f66646eb87ae31b05a55ce665
1 //
2 // btls-key.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 3/7/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
7 //
9 #include "btls-key.h"
11 EVP_PKEY *
12 mono_btls_key_new ()
14 return EVP_PKEY_new ();
17 void
18 mono_btls_key_free (EVP_PKEY *pkey)
20 EVP_PKEY_free (pkey);
23 EVP_PKEY *
24 mono_btls_key_up_ref (EVP_PKEY *pkey)
26 return EVP_PKEY_up_ref (pkey);
29 int
30 mono_btls_key_get_bits (EVP_PKEY *pkey)
32 return EVP_PKEY_bits (pkey);
35 int
36 mono_btls_key_is_rsa (EVP_PKEY *pkey)
38 return pkey->type == EVP_PKEY_RSA;
41 int
42 mono_btls_key_assign_rsa_private_key (EVP_PKEY *pkey, uint8_t *der_data, int der_length)
44 RSA *rsa;
46 rsa = RSA_private_key_from_bytes (der_data, der_length);
47 if (!rsa)
48 return 0;
50 return EVP_PKEY_assign_RSA (pkey, rsa);
53 int
54 mono_btls_key_get_bytes (EVP_PKEY *pkey, uint8_t **buffer, int *size, int include_private_bits)
56 size_t len;
57 RSA *rsa;
58 int ret;
60 *size = 0;
61 *buffer = NULL;
63 if (pkey->type != EVP_PKEY_RSA)
64 return 0;
66 rsa = EVP_PKEY_get1_RSA (pkey);
67 if (!rsa)
68 return 0;
70 if (include_private_bits)
71 ret = RSA_private_key_to_bytes (buffer, &len, rsa);
72 else
73 ret = RSA_public_key_to_bytes (buffer, &len, rsa);
75 RSA_free (rsa);
77 if (ret != 1)
78 return 0;
80 *size = (int)len;
81 return 1;