SecOTRSession.h
1 /* 2 * Copyright (c) 2011-2014 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25 #ifndef _SECOTRSESSION_H_ 26 #define _SECOTRSESSION_H_ 27 28 #include <CoreFoundation/CFBase.h> 29 #include <CoreFoundation/CFData.h> 30 31 #include <Security/SecOTR.h> 32 33 __BEGIN_DECLS 34 35 // MARK: MessageTypes 36 37 enum SecOTRSMessageKind { 38 kOTRNegotiationPacket, 39 kOTRDataPacket, 40 kOTRUnknownPacket 41 }; 42 43 // MARK: OTR Session 44 45 enum SecOTRCreateFlags { 46 kSecOTRSendTextMessages = 1 << 0, // OTR messages will be encoded as Base-64 with header/footer per the standard, not just given back in binary 47 kSecOTRUseAppleCustomMessageFormat = 1 << 1, // OTR Messages will be encoded without revealing MAC keys and as compact as we can (P-256) 48 kSecOTRIncludeHashesInMessages = 1 << 2, 49 kSecOTRSlowRoll = 1 << 3, 50 }; 51 52 /*! 53 @typedef 54 @abstract OTRSessions encapsulate a commuincaiton between to parties using the 55 otr protocol. 56 @discussion Sessions start with IDs. One end sends a start packet (created with AppendStartPacket). 57 Both sides process packets they exchange on the negotiation channel. 58 */ 59 typedef struct _SecOTRSession* SecOTRSessionRef; 60 61 SecOTRSessionRef SecOTRSessionCreateFromID(CFAllocatorRef allocator, 62 SecOTRFullIdentityRef myID, 63 SecOTRPublicIdentityRef theirID); 64 65 SecOTRSessionRef SecOTRSessionCreateFromIDAndFlags(CFAllocatorRef allocator, 66 SecOTRFullIdentityRef myID, 67 SecOTRPublicIdentityRef theirID, 68 uint32_t flags); 69 70 SecOTRSessionRef SecOTRSessionCreateFromData(CFAllocatorRef allocator, CFDataRef data); 71 72 void SecOTRSessionReset(SecOTRSessionRef session); 73 OSStatus SecOTRSAppendSerialization(SecOTRSessionRef publicID, CFMutableDataRef serializeInto); 74 75 OSStatus SecOTRSAppendStartPacket(SecOTRSessionRef session, CFMutableDataRef appendInitiatePacket); 76 77 OSStatus SecOTRSAppendRestartPacket(SecOTRSessionRef session, CFMutableDataRef appendPacket); 78 79 OSStatus SecOTRSProcessPacket(SecOTRSessionRef session, 80 CFDataRef incomingPacket, 81 CFMutableDataRef negotiationResponse); 82 83 bool SecOTRSIsForKeys(SecOTRSessionRef session, SecKeyRef myPublic, SecKeyRef theirPublic); 84 bool SecOTRSGetIsReadyForMessages(SecOTRSessionRef session); 85 bool SecOTRSGetIsIdle(SecOTRSessionRef session); 86 87 enum SecOTRSMessageKind SecOTRSGetMessageKind(SecOTRSessionRef session, CFDataRef incomingPacket); 88 89 /*! 90 @function 91 @abstract Precalculates keys for current key sets to save time when sending or receiving. 92 @param session OTRSession receiving message 93 */ 94 void SecOTRSPrecalculateKeys(SecOTRSessionRef session); 95 96 /*! 97 @function 98 @abstract Encrypts and Signs a message with OTR credentials. 99 @param session OTRSession receiving message 100 @param sourceMessage Cleartext message to protect 101 @param protectedMessage Data to append the encoded protected message to 102 @result OSStatus errSecAuthFailed -> bad signature, no data appended. 103 */ 104 105 OSStatus SecOTRSSignAndProtectMessage(SecOTRSessionRef session, 106 CFDataRef sourceMessage, 107 CFMutableDataRef protectedMessage); 108 109 /*! 110 @function 111 @abstract Verifies and exposes a message sent via OTR 112 @param session OTRSession receiving message 113 @param incomingMessage Encoded message 114 @param exposedMessageContents Data to append the exposed message to 115 @result OSStatus errSecAuthFailed -> bad signature, no data appended. 116 */ 117 118 OSStatus SecOTRSVerifyAndExposeMessage(SecOTRSessionRef session, 119 CFDataRef incomingMessage, 120 CFMutableDataRef exposedMessageContents); 121 122 123 124 const char *SecOTRPacketTypeString(CFDataRef message); 125 126 CFDataRef SecOTRSessionCreateRemote(CFDataRef publicPeerId, CFErrorRef *error); 127 bool SecOTRSessionProcessPacketRemote(CFDataRef sessionData, CFDataRef inputPacket, CFDataRef* outputSessionData, CFDataRef* outputPacket, bool *readyForMessages, CFErrorRef *error); 128 129 bool SecOTRSessionIsSessionInAwaitingState(SecOTRSessionRef session); 130 131 __END_DECLS 132 133 #endif