]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_ssl/lib/sslUtils.c
2 * Copyright (c) 1999-2001,2005-2008,2010-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * sslUtils.c - Misc. OS independant SSL utility functions
28 /* THIS FILE CONTAINS KERNEL CODE */
34 #include <AssertMacros.h>
37 void SSLDump(const unsigned char *data
, unsigned long len
)
42 if((i
&0xf)==0) printf("%04lx :",i
);
43 printf(" %02x", data
[i
]);
44 if((i
&0xf)==0xf) printf("\n");
51 SSLDecodeInt(const uint8_t *p
, size_t length
)
54 check(length
> 0 && length
<= 4); //anything else would be an internal error.
56 val
= (val
<< 8) | *p
++;
61 SSLEncodeInt(uint8_t *p
, size_t value
, size_t length
)
63 unsigned char *retVal
= p
+ length
; /* Return pointer to char after int */
64 check(length
> 0 && length
<= 4); //anything else would be an internal error.
65 while (length
--) /* Assemble backwards */
66 { p
[length
] = (uint8_t)value
; /* Implicit masking to low byte */
73 SSLDecodeSize(const uint8_t *p
, size_t length
)
76 check(length
> 0 && length
<= 4); //anything else would be an internal error.
78 val
= (val
<< 8) | *p
++;
83 SSLEncodeSize(uint8_t *p
, size_t value
, size_t length
)
85 unsigned char *retVal
= p
+ length
; /* Return pointer to char after int */
86 check(length
> 0 && length
<= 4); //anything else would be an internal error.
87 while (length
--) /* Assemble backwards */
88 { p
[length
] = (uint8_t)value
; /* Implicit masking to low byte */
96 SSLEncodeUInt64(uint8_t *p
, uint64_t value
)
98 p
= SSLEncodeInt(p
, (value
>>32)&0xffffffff, 4);
99 return SSLEncodeInt(p
, value
&0xffffffff, 4);
104 IncrementUInt64(sslUint64
*v
)
110 SSLDecodeUInt64(const uint8_t *p
, size_t length
, uint64_t *v
)
112 check(length
> 0 && length
<= 8);
114 *v
=SSLDecodeInt(p
, length
);
116 *v
=((uint64_t)SSLDecodeInt(p
, length
-4))<<32 | SSLDecodeInt(p
+length
-4, 4);
123 const char *protocolVersStr(SSLProtocolVersion prot
)
126 case SSL_Version_Undetermined
: return "SSL_Version_Undetermined";
127 case SSL_Version_2_0
: return "SSL_Version_2_0";
128 case SSL_Version_3_0
: return "SSL_Version_3_0";
129 case TLS_Version_1_0
: return "TLS_Version_1_0";
130 case TLS_Version_1_1
: return "TLS_Version_1_1";
131 case TLS_Version_1_2
: return "TLS_Version_1_2";
132 default: sslErrorLog("protocolVersStr: bad prot\n"); return "BAD PROTOCOL";
134 return NULL
; /* NOT REACHED */
137 #endif /* SSL_DEBUG */