[coop] Refactor/reuse mono_value_box_handle/mono_value_box_checked and reduce raw...
[mono-project.git] / mono / btls / btls-util.c
blob645656032409b16755411b13cb7f190fbcdf6c75
1 //
2 // btls-util.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 3/23/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
7 //
9 #include "btls-util.h"
10 #include <assert.h>
11 // #include <time.h>
13 extern int asn1_generalizedtime_to_tm (struct tm *tm, const ASN1_GENERALIZEDTIME *d);
15 extern int64_t btls_timegm64 (const struct tm *date);
17 void
18 mono_btls_free (void *data)
20 OPENSSL_free (data);
23 int64_t
24 mono_btls_util_asn1_time_to_ticks (ASN1_TIME *time)
26 ASN1_GENERALIZEDTIME *gtime;
27 struct tm tm;
28 int64_t epoch;
29 int ret;
31 memset (&tm, 0, sizeof (tm));
33 gtime = ASN1_TIME_to_generalizedtime (time, NULL);
34 ret = asn1_generalizedtime_to_tm (&tm, gtime);
35 ASN1_GENERALIZEDTIME_free (gtime);
37 /* FIXME: check the return value in managed code */
38 if (ret == 0) {
39 return 0;
42 epoch = btls_timegm64 (&tm);
44 return epoch;
47 // Copied from crypto/bio/printf.c, takes va_list
48 int
49 mono_btls_debug_printf (BIO *bio, const char *format, va_list args)
51 char buf[256], *out, out_malloced = 0;
52 int out_len, ret;
54 out_len = vsnprintf (buf, sizeof(buf), format, args);
55 if (out_len < 0) {
56 return -1;
59 if ((size_t) out_len >= sizeof(buf)) {
60 const int requested_len = out_len;
61 /* The output was truncated. Note that vsnprintf's return value
62 * does not include a trailing NUL, but the buffer must be sized
63 * for it. */
64 out = OPENSSL_malloc (requested_len + 1);
65 out_malloced = 1;
66 if (out == NULL) {
67 OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
68 return -1;
70 out_len = vsnprintf (out, requested_len + 1, format, args);
71 assert(out_len == requested_len);
72 } else {
73 out = buf;
76 ret = BIO_write(bio, out, out_len);
77 if (out_malloced) {
78 OPENSSL_free(out);
81 return ret;