예제 #1
0
        private static bool TryExecuteTransform(
            ReadOnlySpan <byte> source,
            Span <byte> destination,
            out int bytesWritten,
            SecKeyTransform transform)
        {
            SafeCFDataHandle  outputHandle;
            SafeCFErrorHandle errorHandle;

            int ret = transform(source, out outputHandle, out errorHandle);

            using (errorHandle)
                using (outputHandle)
                {
                    switch (ret)
                    {
                    case kSuccess:
                        return(CoreFoundation.TryCFWriteData(outputHandle, destination, out bytesWritten));

                    case kErrorSeeError:
                        throw CreateExceptionForCFError(errorHandle);

                    default:
                        Debug.Fail($"transform returned {ret}");
                        throw new CryptographicException();
                    }
                }
        }
예제 #2
0
 internal static bool TryCreateSignature(
     SafeSecKeyRefHandle privateKey,
     ReadOnlySpan <byte> dataHash,
     Span <byte> destination,
     PAL_HashAlgorithm hashAlgorithm,
     PAL_SignatureAlgorithm signatureAlgorithm,
     out int bytesWritten)
 {
     using (SafeCFDataHandle signature = NativeCreateSignature(privateKey, dataHash, hashAlgorithm, signatureAlgorithm))
     {
         return(CoreFoundation.TryCFWriteData(signature, destination, out bytesWritten));
     }
 }
예제 #3
0
        private static bool ProcessPrimitiveResponse(
            int returnValue,
            SafeCFDataHandle cfData,
            SafeCFErrorHandle cfError,
            Span <byte> destination,
            out int bytesWritten)
        {
            if (returnValue == kErrorSeeError)
            {
                throw CreateExceptionForCFError(cfError);
            }

            if (returnValue == kSuccess && !cfData.IsInvalid)
            {
                return(CoreFoundation.TryCFWriteData(cfData, destination, out bytesWritten));
            }

            Debug.Fail($"Unknown return value ({returnValue}) or no data object returned");
            throw new CryptographicException();
        }
예제 #4
0
        internal static byte[]? EcdhKeyAgree(
            SafeSecKeyRefHandle privateKey,
            SafeSecKeyRefHandle publicKey,
            Span <byte> opportunisticDestination,
            out int bytesWritten)
        {
            const int Success        = 1;
            const int kErrorSeeError = -2;

            SafeCFDataHandle  data;
            SafeCFErrorHandle error;

            int status = AppleCryptoNative_EcdhKeyAgree(privateKey, publicKey, out data, out error);

            using (data)
                using (error)
                {
                    if (status == kErrorSeeError)
                    {
                        throw CreateExceptionForCFError(error);
                    }

                    if (status == Success && !data.IsInvalid)
                    {
                        if (CoreFoundation.TryCFWriteData(data, opportunisticDestination, out bytesWritten))
                        {
                            return(null);
                        }

                        bytesWritten = 0;
                        return(CoreFoundation.CFGetData(data));
                    }

                    Debug.Fail($"Unexpected status ({status})");
                    throw new CryptographicException();
                }
        }