5 // Created by Martin Baulig on 14/11/15.
6 // Copyright (c) 2015 Xamarin. All rights reserved.
10 #include <openssl/x509v3.h>
11 #include <openssl/pkcs12.h>
14 mono_btls_x509_from_data (const void *buf
, int len
, MonoBtlsX509Format format
)
19 bio
= BIO_new_mem_buf ((void *)buf
, len
);
21 case MONO_BTLS_X509_FORMAT_DER
:
22 cert
= d2i_X509_bio (bio
, NULL
);
24 case MONO_BTLS_X509_FORMAT_PEM
:
25 cert
= PEM_read_bio_X509 (bio
, NULL
, NULL
, NULL
);
33 mono_btls_x509_up_ref (X509
*x509
)
40 mono_btls_x509_free (X509
*x509
)
46 mono_btls_x509_dup (X509
*x509
)
48 return X509_dup (x509
);
52 mono_btls_x509_get_subject_name (X509
*x509
)
54 return mono_btls_x509_name_copy (X509_get_subject_name (x509
));
58 mono_btls_x509_get_issuer_name (X509
*x509
)
60 return mono_btls_x509_name_copy (X509_get_issuer_name (x509
));
64 mono_btls_x509_get_subject_name_string (X509
*name
, char *buffer
, int size
)
67 return X509_NAME_oneline (X509_get_subject_name (name
), buffer
, size
) != NULL
;
71 mono_btls_x509_get_issuer_name_string (X509
*name
, char *buffer
, int size
)
74 return X509_NAME_oneline (X509_get_issuer_name (name
), buffer
, size
) != NULL
;
78 mono_btls_x509_get_raw_data (X509
*x509
, BIO
*bio
, MonoBtlsX509Format format
)
81 case MONO_BTLS_X509_FORMAT_DER
:
82 return i2d_X509_bio (bio
, x509
);
83 case MONO_BTLS_X509_FORMAT_PEM
:
84 return PEM_write_bio_X509 (bio
, x509
);
91 mono_btls_x509_cmp (const X509
*a
, const X509
*b
)
93 return X509_cmp (a
, b
);
97 mono_btls_x509_get_hash (X509
*x509
, const void **data
)
99 X509_check_purpose (x509
, -1, 0);
100 *data
= x509
->sha1_hash
;
101 return SHA_DIGEST_LENGTH
;
105 mono_btls_x509_get_not_before (X509
*x509
)
107 return mono_btls_util_asn1_time_to_ticks (X509_get_notBefore (x509
));
111 mono_btls_x509_get_not_after (X509
*x509
)
113 return mono_btls_util_asn1_time_to_ticks (X509_get_notAfter (x509
));
117 mono_btls_x509_get_public_key (X509
*x509
, BIO
*bio
)
119 ASN1_BIT_STRING
*pkey
;
122 if (!x509
|| !x509
->cert_info
|| !x509
->cert_info
->key
)
125 pkey
= x509
->cert_info
->key
->public_key
;
126 if (!pkey
|| !pkey
->data
)
129 ret
= BIO_write (bio
, pkey
->data
, pkey
->length
);
130 if (ret
!= pkey
->length
)
137 mono_btls_x509_get_serial_number (X509
*x509
, char *buffer
, int size
, int mono_style
)
139 ASN1_INTEGER
*serial
;
140 unsigned char *temp
, *p
;
143 serial
= X509_get_serialNumber (x509
);
144 if (serial
->length
== 0 || serial
->length
+1 > size
)
148 memcpy (buffer
, serial
->data
, serial
->length
);
149 return serial
->length
;
152 temp
= OPENSSL_malloc (serial
->length
+ 1);
157 len
= i2c_ASN1_INTEGER (serial
, &p
);
164 memcpy (buffer
, temp
, len
);
172 mono_btls_x509_get_public_key_algorithm (X509
*x509
, char *buffer
, int size
)
179 pkey
= X509_get_X509_PUBKEY (x509
);
183 ret
= X509_PUBKEY_get0_param (&ppkalg
, NULL
, NULL
, NULL
, pkey
);
187 return OBJ_obj2txt (buffer
, size
, ppkalg
, 1);
191 mono_btls_x509_get_version (X509
*x509
)
193 return (int)X509_get_version (x509
) + 1;
197 mono_btls_x509_get_signature_algorithm (X509
*x509
, char *buffer
, int size
)
199 const ASN1_OBJECT
*obj
;
204 nid
= X509_get_signature_nid (x509
);
206 obj
= OBJ_nid2obj (nid
);
210 return OBJ_obj2txt (buffer
, size
, obj
, 1);
214 mono_btls_x509_get_public_key_asn1 (X509
*x509
, char *out_oid
, int oid_len
, uint8_t **buffer
, int *size
)
218 const unsigned char *pk
;
225 pkey
= X509_get_X509_PUBKEY (x509
);
226 if (!pkey
|| !pkey
->public_key
)
229 ret
= X509_PUBKEY_get0_param (&ppkalg
, &pk
, &pk_len
, NULL
, pkey
);
230 if (ret
!= 1 || !ppkalg
|| !pk
)
234 OBJ_obj2txt (out_oid
, oid_len
, ppkalg
, 1);
239 *buffer
= OPENSSL_malloc (pk_len
);
243 memcpy (*buffer
, pk
, pk_len
);
251 mono_btls_x509_get_public_key_parameters (X509
*x509
, char *out_oid
, int oid_len
, uint8_t **buffer
, int *size
)
263 pkey
= X509_get_X509_PUBKEY (x509
);
265 ret
= X509_PUBKEY_get0_param (NULL
, NULL
, NULL
, &algor
, pkey
);
266 if (ret
!= 1 || !algor
)
269 X509_ALGOR_get0 (&paobj
, &ptype
, &pval
, algor
);
271 if (ptype
!= V_ASN1_NULL
&& ptype
!= V_ASN1_SEQUENCE
)
274 if (ptype
== V_ASN1_NULL
) {
278 *buffer
= OPENSSL_malloc (2);
287 OBJ_obj2txt (out_oid
, oid_len
, paobj
, 1);
290 } else if (ptype
== V_ASN1_SEQUENCE
) {
291 ASN1_STRING
*pstr
= pval
;
293 *size
= pstr
->length
;
294 *buffer
= OPENSSL_malloc (pstr
->length
);
298 memcpy (*buffer
, pstr
->data
, pstr
->length
);
301 OBJ_obj2txt (out_oid
, oid_len
, paobj
, 1);
310 mono_btls_x509_get_pubkey (X509
*x509
)
312 return X509_get_pubkey (x509
);
316 mono_btls_x509_get_subject_key_identifier (X509
*x509
, uint8_t **buffer
, int *size
)
318 ASN1_OCTET_STRING
*skid
;
323 if (X509_get_version (x509
) != 2)
326 skid
= X509_get_ext_d2i (x509
, NID_subject_key_identifier
, NULL
, NULL
);
330 *size
= skid
->length
;
331 *buffer
= OPENSSL_malloc (*size
);
335 memcpy (*buffer
, skid
->data
, *size
);
340 mono_btls_x509_print (X509
*x509
, BIO
*bio
)
342 return X509_print_ex (bio
, x509
, XN_FLAG_COMPAT
, X509_FLAG_COMPAT
);
346 get_trust_nid (MonoBtlsX509Purpose purpose
)
349 case MONO_BTLS_X509_PURPOSE_SSL_CLIENT
:
350 return NID_client_auth
;
351 case MONO_BTLS_X509_PURPOSE_SSL_SERVER
:
352 return NID_server_auth
;
359 mono_btls_x509_add_trust_object (X509
*x509
, MonoBtlsX509Purpose purpose
)
364 nid
= get_trust_nid (purpose
);
368 trust
= ASN1_OBJECT_new ();
373 return X509_add1_trust_object (x509
, trust
);
377 mono_btls_x509_add_reject_object (X509
*x509
, MonoBtlsX509Purpose purpose
)
382 nid
= get_trust_nid (purpose
);
386 reject
= ASN1_OBJECT_new ();
391 return X509_add1_reject_object (x509
, reject
);
395 mono_btls_x509_add_explicit_trust (X509
*x509
, MonoBtlsX509TrustKind kind
)
399 if ((kind
& MONO_BTLS_X509_TRUST_KIND_REJECT_ALL
) != 0)
400 kind
|= MONO_BTLS_X509_TRUST_KIND_REJECT_CLIENT
| MONO_BTLS_X509_TRUST_KIND_REJECT_SERVER
;
402 if ((kind
& MONO_BTLS_X509_TRUST_KIND_TRUST_ALL
) != 0)
403 kind
|= MONO_BTLS_X509_TRUST_KIND_TRUST_CLIENT
| MONO_BTLS_X509_TRUST_KIND_TRUST_SERVER
;
406 if ((kind
& MONO_BTLS_X509_TRUST_KIND_REJECT_CLIENT
) != 0) {
407 ret
= mono_btls_x509_add_reject_object (x509
, MONO_BTLS_X509_PURPOSE_SSL_CLIENT
);
412 if ((kind
& MONO_BTLS_X509_TRUST_KIND_REJECT_SERVER
) != 0) {
413 ret
= mono_btls_x509_add_reject_object (x509
, MONO_BTLS_X509_PURPOSE_SSL_SERVER
);
419 // Ignore any MONO_BTLS_X509_TRUST_KIND_TRUST_* settings if we added
420 // any kind of MONO_BTLS_X509_TRUST_KIND_REJECT_* before.
424 if ((kind
& MONO_BTLS_X509_TRUST_KIND_TRUST_CLIENT
) != 0) {
425 ret
= mono_btls_x509_add_trust_object (x509
, MONO_BTLS_X509_PURPOSE_SSL_CLIENT
);
430 if ((kind
& MONO_BTLS_X509_TRUST_KIND_TRUST_SERVER
) != 0) {
431 ret
= mono_btls_x509_add_trust_object (x509
, MONO_BTLS_X509_PURPOSE_SSL_SERVER
);