Exemplo n.º 1
0
        private static void CertificateCallback(void *userData, UnityTls.unitytls_tlsctx *ctx, byte *cn, size_t cnLen,
                                                UnityTls.unitytls_x509name *caList, size_t caListLen, UnityTls.unitytls_x509list_ref *chain,
                                                UnityTls.unitytls_key_ref *key, UnityTls.unitytls_errorstate *errorState)
        {
            var handle  = (GCHandle)(IntPtr)userData;
            var context = (UnityTlsContext)handle.Target;

            context.CertificateCallback(ctx, cn, cnLen, caList, caListLen, chain, key, errorState);
        }
Exemplo n.º 2
0
        private void CertificateCallback(UnityTls.unitytls_tlsctx *ctx, byte *cn, size_t cnLen,
                                         UnityTls.unitytls_x509name *caList, size_t caListLen, UnityTls.unitytls_x509list_ref *chain,
                                         UnityTls.unitytls_key_ref *key, UnityTls.unitytls_errorstate *errorState)
        {
            try
            {
                if (remoteCertificate == null)
                {
                    throw new TlsException(AlertDescription.InternalError,
                                           "Cannot request client certificate before receiving one from the server.");
                }

                localClientCertificate = SelectClientCertificate(null);

                if (localClientCertificate == null)
                {
                    *chain = new UnityTls.unitytls_x509list_ref
                    {
                        handle = UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE
                    };
                    *key = new UnityTls.unitytls_key_ref {
                        handle = UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE
                    };
                }
                else
                {
                    // Need to create native objects for client chain/key. Need to keep them cached.
                    // Make sure we don't have old native objects still around.
                    UnityTls.NativeInterface.unitytls_x509list_free(requestedClientCertChain);
                    UnityTls.NativeInterface.unitytls_key_free(requestedClientKey);

                    ExtractNativeKeyAndChainFromManagedCertificate(localClientCertificate, errorState,
                                                                   out requestedClientCertChain, out requestedClientKey);
                    *chain = UnityTls.NativeInterface.unitytls_x509list_get_ref(requestedClientCertChain, errorState);
                    *key   = UnityTls.NativeInterface.unitytls_key_get_ref(requestedClientKey, errorState);
                }

                Unity.Debug.CheckAndThrow(*errorState, "Failed to retrieve certificates on request.",
                                          AlertDescription.HandshakeFailure);
            }
            catch (Exception ex)
            {
                // handle all exceptions and store them for later since we don't want to let them go through native code.
                UnityTls.NativeInterface.unitytls_errorstate_raise_error(errorState,
                                                                         UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
                if (lastException == null)
                {
                    lastException = ex;
                }
            }
        }