OpenSSL RAND_byte issue with iOS Simulator - iphone

I have downloaded the latest source from OpenSSL Page (released on April 7th, 2014), and created a libcrypto.a library using Tutorial Where i have followed the steps in which i run ./build-openssl.sh script file to generate libcrypto.a file for all environments (armv7s, armv7, i386) on Mac OS X having version 10.9.2.
I am able to encrypt / decrypt the date using EVP_aes_256_cbc encryption but my code gets failed when i try to get RAND_byte. The code gets crashed on RAND_byte call.
Below are the codes, which i am trying to get the RAND_byte seeds:
// Code 1
unsigned char seed[32];
RAND_bytes(seed, 32);
// Code 2
int count = 24;
unsigned char *buffer = (unsigned char *)calloc(count, sizeof(unsigned char));
RAND_bytes(buffer, count);
// Code 3
int count = 24;
unsigned char *buffer = (unsigned char *)malloc(sizeof(int)*count);
RAND_bytes(buffer, count);
// Code 4
int count = 24;
unsigned char *buffer = OPENSSL_malloc(count);
RAND_bytes(buffer, count);
When I run above code on iOS 6.0/6.1 Simulator, It gets crashed on RAND_byte call and I get “_interposition_vtable_unimplemented” on Thread 1 and no message get displayed on console.
When I run the same code on iOS 7.0+ Simulator, It gets crashed on RAND_byte call and I get “__pthread_kill” on Thread 1 and “Detected an attempt to call a symbol in system libraries that is not present on the iPhone: open$UNIX2003 called from function RAND_poll in image CryptographyTest.” on Console.
But WHEN I run the same code on iPad with iOS 7.0.4, All above codes runs perfectly. Where i get the return value from RAND_byte is 1.
I don’t understand the behavior that some of the functions does not work on iOS simulator but everything works with iOS devices.
Any help highly appreciated! Many thanks in advance.

... using Tutorial where i have followed the steps in which i
run ./build-openssl.sh script file to generate libcrypto.a file
for all environments (armv7s, armv7, i386)
...
I don’t understand the behavior that some of the functions does
not work on iOS simulator but everything works with iOS devices.
The script is wrong for your usage:
Its not building for the simulator. Its building for the desktop. This:
ARCHS=("armv7s" "armv7" "i386")
SDKS=("iphoneos" "iphoneos" "macosx")
Probably needs to be changed to:
ARCHS=("armv7s" "armv7" "i386")
SDKS=("iphoneos" "iphoneos" "iPhoneSimulator")
With iPhoneSimulator, the proper SDK will be used:
$ ls /Applications/Xcode.app/Contents/Developer/Platforms/
MacOSX.platform iPhoneSimulator.platform
iPhoneOS.platform
Does the project you are using claim its multi-arch and for the iPhone and iPhone Simulator? Or does it claim its multi-arch and for iPhone and OS X?

and created a libcrypto.a library for iOS use using Tutorial.
I don't know about that Tutorial, but OpenSSL has procedures to build the library for iOS. You can find it in the FIPS User Guide 2.0, Appendix E.
You can fetch a prebuilt version of OpenSSL for iOS at Github. It was built using the OpenSSL procedures. Its a fat library, and it has i386, ARMv7, ARMv7s and ARM64 architectures.
“__pthread_kill” on Thread 1 and “Detected an attempt to call a symbol in system libraries that is not present on the iPhone: open$UNIX2003 called from function RAND_poll in image CryptographyTest.” on Console.
I believe this indicates the OpenSSL library was not built properly for the simulator or devices.
int count = 24;
unsigned char *buffer = OPENSSL_malloc(count);
RAND_bytes(buffer, count);
Just bike shedding here, but you ignored the return value from RAND_bytes. According to the docs on RAND_bytes:
RAND_bytes() returns 1 on success, 0 otherwise. The error code can be
obtained by ERR_get_error(3).
Silent failures are a kiss of death to high integrity software.

Related

IOCreatePlugInInterfaceForService returns mysterious error

I am trying to use some old IOKit functionality in a new Swift 4.0 Mac app (not iOS). I have created a bridging header to use an existing Objective C third party framework, DDHidLib, and I am current working in Xcode 9.
The code that attempts to create a plug in interface for a usb gamepad falls over on IOCreatePlugInInterfaceForService, returning a non-zero error.
The truly bizarre thing is I have an older app created in a previous version of Xcode that uses the same framework and works correctly after opening in the new Xcode 9. This previous project is still Swift using a bridging header for the same Obj-C framework. I have checked the build settings and tried to make everything match, but I get the same result; the old app works but any new apps do not.
Is there a way to either: find out the exact differences in build settings/compilers to see what the elusive difference may be, OR to step into the IOCreatePlugInInterfaceForService IOKit method to see what may be causing the error to be returned in one project but not another?
EDIT: Here is the method that is failing:
- (BOOL) createDeviceInterfaceWithError: (NSError **) error_; {
io_name_t className;
IOCFPlugInInterface ** plugInInterface = NULL;
SInt32 score = 0;
NSError * error = nil;
BOOL result = NO;
mDeviceInterface = NULL;
NSXReturnError(IOObjectGetClass(mHidDevice, className));
if (error)
goto done;
NSXReturnError(IOCreatePlugInInterfaceForService(mHidDevice, kIOHIDDeviceUserClientTypeID,kIOCFPlugInInterfaceID,&plugInInterface,&score));
if (error)
goto done;
//Call a method of the intermediate plug-in to create the device interface
NSXReturnError((*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &mDeviceInterface));
if (error)
goto done;
result = YES;
done:
if (plugInInterface != NULL)
{
(*plugInInterface)->Release(plugInInterface);
}
if (error_)
*error_ = error;
return result;
}
In the old version that works, IOCreatePlugInInterfaceForService always returns a value of 0. In all the versions that don't work, the return value appears to always be -536870210. The mHidDevice in this function is the io_object_t handle for the device.
EDIT2: Here is the IORegistryExplorer page for the device
Finally managed to resolve this after weeks of head scratching. The new Xcode 9 uses app sandboxing to basically prevent access to USB, bluetooth, camera and microphone etc. by default in a new app. Once I switched this off it reverted to it's expected behaviour.
Glad it was such a simple answer in the end but disappointed Xcode does not provide more descriptive error messages or responses to let a user know they are essentially preventing themselves from accessing the parts of the system they need.
Looks like kIOReturnNoResources is returned if the loop at the end of IOCreatePlugInInterfaceForService completes with haveOne == false for whatever reason. Perhaps Start() is returning false because another process or driver already has exclusive access? I'd check what clients the device has in IORegistryExplorer.
This error also happens when an application is trying to access the camera or bluetooth on MacOS 10.14 and higher. Permission shall be granted either explicitly by user (pop-up window), or through the Security & Privacy. The application should check for permission as shown here.

Can't seem to conditionally compile for iPhone/Mac

I've got this source (xcode 3.2.5):
NSLog(#"IPHONE = %d, SIMULATOR = %d, MAC = %d", TARGET_OS_IPHONE, TARGET_IPHONE_SIMULATOR, TARGET_OS_MAC);
And I get this result:
2012-03-30 13:50:06.777 MyApp[36810:207] IPHONE = 1, SIMULATOR = 1, MAC = 1
No wonder my #if statements are confused!
What gives? Any ideas?
FWIW: On (tethered) iPod Touch I get this: IPHONE = 1, SIMULATOR = 0, MAC = 1
Added: Well, I figured out that my original problem with #if was due to misspelling TARGET_OS_IPHONE as TARGET_OS_PHONE in several places. (Good ol' copy/paste!)
Would still like to find a definitive description of how Apple's defines are supposed to be set.
Use TARGET_OS_IPHONE (only!) for differentiating between iOS and OSX.
#if TARGET_OS_IPHONE
// iOS
#else
// Mac OS X
#endif
TARGET_OS_MAC will be 1 on both OSX and iOS; it predates iOS, and iOS is considered a variant of OSX as far as TargetConditionals.h is concerned.
I might be wrong here, but my assumption was that TARGET_OS_MAC specifies that you are building for Mac OS X (as opposed to, say, win32). iOS is actually a version of Mac OS X (although it is not branded / marketed as such).
To see how they are all defined, choose a build target and command-click their definition in xcode.
In the header file, the macros are defined as such:
TARGET_OS_MAC - Generate code will run under Mac OS
TARGET_OS_WIN32 - Generate code will run under 32-bit Windows
TARGET_OS_UNIX - Generate code will run under some non Mac OS X unix
TARGET_OS_EMBEDDED - Generate code will run under an embedded OS variant
of TARGET_OS_MAC
TARGET_OS_IPHONE - Generate code will run under iPhone OS which
is a variant of TARGET_OS_MAC.
So, it can be expected that TARGET_OS_MAC is defined for iOS, as well as TARGET_OS_IPHONE for example.

Need an API that detects when an iPhone is plugged in

I'm making an application for Mac, and I need an API that detects when an iPhone is plugged in. Thanks.
EDIT : To clarify, specifically, I need an API that detects when an iPhone is plugged into a USB port on a Mac.
I don't have a complete answer, but a program that achieves what you want is USB Prober, supplied with Xcode and located at /Developer/Applications/Utilities/USB Prober.app. That program is open source, with the browser viewable repository being here and the whole project being included in this download. I actually found an older version to be more helpful, as available here, especially BusProbeClass.
They all rest on IOKit for which Apple's documentation seems to be very lacking in both quantity and quality.
That's heavy reading, but if you check out + USBProbe then you'll see it gets a list of current USB devices, gets IOUSBDeviceInterfaces for each in the variable deviceIntf and then pushes them to somewhere useful for the rest of the program. If you check out + outputDevice: locationID:deviceNumber: lower down in the same source file, you'll see that GetDescriptor can seemingly be used on an IOUSBDeviceDescriptor to get properties including the vendor and product ID, the combination of which is guaranteed to be unique by the USB Implementer's Forum.
Using the vendor and product ID, you can search for any specific USB device. From my Mac's System Information I can tell you that an iPhone 4 has a product ID of 0x1297 and Apple's vendor ID is 0x05ac.
Extra: from dissecting that code, if you remove a whole bunch of checks that things are succeeding and compress it all down to demonstrative stuff then the following is at least a test for whether an iPhone 4 is plugged in right now (you'll need to link to the Foundation and IOKit frameworks):
#include <stdio.h>
#import <Foundation/Foundation.h>
#import <IOKit/usb/IOUSBLib.h>
#import <IOKit/IOCFPlugIn.h>
#import <mach/mach_port.h>
int main (int argc, const char * argv[])
{
// get the port through which to talk to the kernel
mach_port_t masterDevicePort;
IOMasterPort(MACH_PORT_NULL, &masterDevicePort);
// create a dictionary that describes a search
// for services provided by USB
CFDictionaryRef matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName);
// get an iterator for all devices that match
// the dictionary
io_iterator_t deviceIterator;
IOServiceGetMatchingServices(
masterDevicePort,
matchingDictionary,
&deviceIterator);
// iterate through the iterator...
io_service_t ioDevice;
while((ioDevice = IOIteratorNext(deviceIterator)))
{
IOUSBDeviceInterface **deviceInterface = NULL;
IOCFPlugInInterface **ioPlugin = NULL;
SInt32 score;
// get a pointer to the device, stored to ioPlugin
IOCreatePlugInInterfaceForService(
ioDevice,
kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&ioPlugin,
&score);
// ask the device for its interface
(*ioPlugin)->QueryInterface(
ioPlugin,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(void *)&deviceInterface);
// make and issue a request to get the device descriptor
IOUSBDeviceDescriptor deviceDescriptor;
IOUSBDevRequest request;
request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
request.bRequest = kUSBRqGetDescriptor;
request.wValue = kUSBDeviceDesc << 8;
request.wIndex = 0;
request.wLength = sizeof(deviceDescriptor);
request.pData = &deviceDescriptor;
(*deviceInterface)->DeviceRequest(deviceInterface, &request);
// now we have the device descriptor, do a little cleaning up -
// release the interface and the device
(*deviceInterface)->Release(deviceInterface);
IOObjectRelease(ioDevice);
// ensure that the values returned are in the appropriate
// byte order for this platform
CFSwapInt16LittleToHost(deviceDescriptor.idVendor);
CFSwapInt16LittleToHost(deviceDescriptor.idProduct);
// check whether we have an iPhone 4 attached
if(deviceDescriptor.idVendor == 0x05ac && deviceDescriptor.idProduct == 0x1297)
printf("iPhone 4 is connected!");
}
// clean up by releasing the device iterator
// and returning the communications port
IOObjectRelease(deviceIterator);
mach_port_deallocate(mach_task_self(), masterDevicePort);
return 0;
}
I haven't yet figured out how to observe for changes in plugged-in devices.

How do I extract pixel data from DICOM files on iOS?

Does anyone know how I can extract the pixel data from a DICOM file and pass it to an image viewer on iOS?
Sorry if this is a simple question, but it seems to be a major component to a huge can of worms I have opened.
I'm using GDCM on iOS. I haven't pushed it very hard yet but it's working well so far. I basically followed the directions for hacking XCode projects to run in iOS in this excellent article on ITK.
Here's how I got it to compile for iOS:
Downloaded source from sourceforge, installed cmake via ports. You'll need a recent version of cmake (I'm using 2.8.2)
If the source is in a folder called gdcm-2.0.17/, then create another directory at that level (say gdcmbin), cd to that directory, and enter ccmake -GXCode ../gdcm-2.0.17/ in the terminal window. This creates the XCode project. When I did this I didn't create any of the example programs or create shared libraries (which won't work in iOS). Just run the defaults.
Follow the directions in the ITK paper on changing the build options (step #7 on page 4).
Then link GDCM into your project using the excellent instructions at Clint Harris' blog
When you're setting up the header search path in your project to GDCM - you have to enter two paths: blah/gdcm-2.0.17/Source/** and blah/gdcmbin/**. The trailing '/Source' on the first path is necessary - otherwise you get headers that aren't appropriate for your architecture.
One glitch (annoying but haven't spent the time to figure it out yet): you get a bunch of linking errors when you switch from simulator to device (or vice versa). This is because the gdcm project doesn't put the outputs into different directories for different targets. So - run a clean and rebuild in the gdcm project when you're switching. I'll probably get annoyed by this soon enough to change it :-).
Here's a rough snippet of how you'd call the library and put the results in an Objective-C dictionary:
NSMutableDictionary * imageDictionary = [[NSMutableDictionary alloc] initWithCapacity:40];
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader.GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file.GetDataSet();
const Tag studyInstance(0x0020,0x000d); // Study Instance UID
const DataElement &dicomVal = ds.GetDataElement(studyInstance);
std::string stringVal( dicomVal.GetByteValue()->GetPointer(), dicomVal.GetByteValue()->GetLength() );
NSString *val = [NSString stringWithCString:stringVal.c_str() encoding:[NSString defaultCStringEncoding]];
[imageDictionary setObject:val forKey:#"studyInstanceUID"];
(Note: this is in an *.mm file that mixes C++ and ObjectiveC)
Imebra has an Objective-C wrapper that can be used also with Swift.
#import "imebraobjc/imebra.h"
// Get the DICOM dataset from file
NSError* error = nil;
ImebraDataSet* pDataSet = [ImebraCodecFactory loadFromFile:#"test.dcm" error:&error]
// CHeck the patient name (automatically convert from DICOM charsets to UTF-8)
NSString* checkPatientName = [pDataSet getString:[[ImebraTagId alloc] initWithGroup:0x10 tag:0x10] elementNumber:0 error:&error]; // Automatically converted to UTF-8 if necessary
// Get the frame 0
ImebraImage* pCheckImage = [pDataSet getImageApplyModalityTransform:0 error:&error];
// Get the image data
ImebraReadingDataHandlerNumeric* readingDataHandler = [pCheckImage getReadingDataHandler:&error];
// Scan the pixels. Access the data handler memory for faster data access
for(unsigned int pixel(0); pixel != readingDataHandler.size; ++pixel)
{
unsigned int pixelValue = [readingDataHandler getUnsignedLong:pixel error:&error];
}
ImebraDrawBitmap* pDrawBitmap = [[ImebraDrawBitmap alloc] init];
// Obtain a NSImage (UIImage on iOS)
NSImage* pNsImage = [pDrawBitmap getImebraImage:pCheckImage error:&pError];
If you want to find DICOM software, look at idoimaging.com, a clearinghouse for medical imaging software. You can choose your platform, input format, output format, language, etc. iOS isn't listed as a format, but much of the software listed there is available with source, useful in library form, and available for MacOS X. For example, I selected:
input format: DICOM
platform: Macintosh
language: C
and found several packages. Given the similarities between MacOS and iOS and the fact that some of these are cross-platform with source included, it shouldn't be too difficult to get one of them working on iOS.

Compile error in CorePlot-CocaTouch project

I'm having trouble compiling the current release.
I was able to download a copy of the source distribution today using:
hg clone https://core-plot.googlecode.com/hg/ core-plot
I opened the "core-plot/framework".
I then double clicked on CorePlot-CocoaTouch.xcodeproj to launch Xcode.
When I build the project I get the following error:
-(void)bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
[NSException raise:CPException format:BindingsNotSupportedString];
Format not a string literal and no formal arguments
#else
[super bind:binding toObject:observable withKeyPath:keyPath options:options];
#endif
}
I am running on a new MacBook with OS 10.6, and IPhone Simulator 4.0.
Any help will be greatly appreciated.
Charles
A more appropriate place to ask this question would be the Core Plot mailing list, because I'm one of the few developers for the project that regularly visits here.
That said, the issue here is that we're using a string constant for a format string, which Xcode now seems to be warning about (rightly so, as this can lead to problems). To work around this for now, you can replace the line in CPLayer.m
static NSString * const BindingsNotSupportedString = #"Bindings are not supported on the iPhone in Core Plot";
with
#define BindingsNotSupportedString #"Bindings are not supported on the iPhone in Core Plot"
Search in your project for BindingsNotSupportedString, seems like it's not in the current file and therefore needs to be included. Or just try to change this to an acceptable format.