38#include <Security/Security.h>
39#include <Security/SecureTransport.h>
40#include <CoreFoundation/CoreFoundation.h>
43SecIdentityRef
SecIdentityCreate(CFAllocatorRef allocator, SecCertificateRef certificate, SecKeyRef privateKey);
57 case errSSLWouldBlock:
59 case errSSLXCertChainInvalid:
73#if !HAVE_SECITEMIMPORT
80 SecExternalFormat
format = kSecFormatPEMSequence;
81 SecExternalFormat
type = kSecItemTypeAggregate;
82 CFStringRef pathStr = CFStringCreateWithCString(
NULL, path, 0x08000100);
89 &
h->interrupt_callback,
NULL,
90 h->protocol_whitelist,
h->protocol_blacklist)) < 0)
109 data = CFDataCreate(kCFAllocatorDefault, buf, ret);
117 if (CFArrayGetCount(*
array) == 0) {
143 if (!(
c->ca_array = CFRetain(
array))) {
158 CFArrayRef certArray =
NULL;
159 CFArrayRef keyArray =
NULL;
160 SecIdentityRef
id =
NULL;
161 CFMutableArrayRef outArray =
NULL;
163 if ((ret =
import_pem(
h,
c->tls_shared.cert_file, &certArray)) < 0)
166 if ((ret =
import_pem(
h,
c->tls_shared.key_file, &keyArray)) < 0)
170 (SecCertificateRef)CFArrayGetValueAtIndex(certArray, 0),
171 (SecKeyRef)CFArrayGetValueAtIndex(keyArray, 0)))) {
176 if (!(outArray = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, certArray))) {
181 CFArraySetValueAtIndex(outArray, 0,
id);
183 SSLSetCertificate(
c->ssl_context, outArray);
187 CFRelease(certArray);
197static OSStatus
tls_read_cb(SSLConnectionRef connection,
void *
data,
size_t *dataLength)
201 size_t requested = *dataLength;
208 return errSSLClosedGraceful;
210 return errSSLClosedAbort;
212 return errSSLWouldBlock;
219 if (
read < requested)
220 return errSSLWouldBlock;
226static OSStatus
tls_write_cb(SSLConnectionRef connection,
const void *
data,
size_t *dataLength)
235 return errSSLWouldBlock;
237 c->lastErr = written;
241 *dataLength = written;
249 if (
c->ssl_context) {
250 SSLClose(
c->ssl_context);
251 CFRelease(
c->ssl_context);
254 CFRelease(
c->ca_array);
259#define CHECK_ERROR(func, ...) do { \
260 OSStatus status = func(__VA_ARGS__); \
261 if (status != noErr) { \
262 ret = AVERROR_UNKNOWN; \
263 av_log(h, AV_LOG_ERROR, #func ": Error %i\n", (int)status); \
277 c->ssl_context = SSLCreateContext(
NULL,
s->listen ? kSSLServerSide : kSSLClientSide, kSSLStreamType);
278 if (!
c->ssl_context) {
287 if (
s->ca_file || !
s->verify)
288 CHECK_ERROR(SSLSetSessionOption,
c->ssl_context, kSSLSessionOptionBreakOnServerAuth,
true);
292 CHECK_ERROR(SSLSetPeerDomainName,
c->ssl_context,
s->host, strlen(
s->host));
296 OSStatus status = SSLHandshake(
c->ssl_context);
297 if (status == errSSLServerAuthCompleted) {
298 SecTrustRef peerTrust;
299 SecTrustResultType trustResult;
303 if (SSLCopyPeerTrust(
c->ssl_context, &peerTrust) != noErr) {
308 if (SecTrustSetAnchorCertificates(peerTrust,
c->ca_array) != noErr) {
313 if (SecTrustEvaluate(peerTrust, &trustResult) != noErr) {
318 if (trustResult == kSecTrustResultProceed ||
319 trustResult == kSecTrustResultUnspecified) {
321 status = errSSLWouldBlock;
322 }
else if (trustResult == kSecTrustResultRecoverableTrustFailure) {
324 status = errSSLXCertChainInvalid;
327 status = errSSLBadCert;
331 CFRelease(peerTrust);
333 if (status == noErr) {
335 }
else if (status != errSSLWouldBlock) {
353 case errSSLClosedGraceful:
354 case errSSLClosedNoNotify:
356 case errSSLWouldBlock:
368 size_t available = 0, processed = 0;
370 SSLGetBufferedReadSize(
c->ssl_context, &available);
373 ret = SSLRead(
c->ssl_context, buf,
size, &processed);
385 size_t processed = 0;
386 int ret = SSLWrite(
c->ssl_context, buf,
size, &processed);
static const char *const format[]
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
#define AVIO_FLAG_READ
read-only
int64_t avio_size(AVIOContext *s)
Get the filesize.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define flags(name, subs,...)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
common internal api header.
Macro definitions for various function/variable attributes.
Memory handling functions.
miscellaneous OS support macros and functions.
const URLProtocol ff_tls_protocol
Describe the class of an AVClass context structure.
mbedtls_ssl_context ssl_context
static int array[MAX_W *MAX_W]
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
#define TLS_COMMON_OPTIONS(pstruct, options_field)
static int tls_close(URLContext *h)
static const AVClass tls_class
static int tls_read(URLContext *h, uint8_t *buf, int size)
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int tls_get_short_seek(URLContext *h)
static int tls_write(URLContext *h, const uint8_t *buf, int size)
static int tls_get_file_handle(URLContext *h)
static OSStatus tls_read_cb(SSLConnectionRef connection, void *data, size_t *dataLength)
static int load_ca(URLContext *h)
static int import_pem(URLContext *h, char *path, CFArrayRef *array)
static int tls_close(URLContext *h)
static OSStatus tls_write_cb(SSLConnectionRef connection, const void *data, size_t *dataLength)
static int map_ssl_error(OSStatus status, size_t processed)
static int tls_read(URLContext *h, uint8_t *buf, int size)
SecIdentityRef SecIdentityCreate(CFAllocatorRef allocator, SecCertificateRef certificate, SecKeyRef privateKey)
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int load_cert(URLContext *h)
static int tls_get_short_seek(URLContext *h)
static int tls_write(URLContext *h, const uint8_t *buf, int size)
static int print_tls_error(URLContext *h, int ret)
#define CHECK_ERROR(func,...)
static int tls_get_file_handle(URLContext *h)
unbuffered private I/O API
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
#define URL_PROTOCOL_FLAG_NETWORK