]> git.saurik.com Git - apple/security.git/blob - Security/libsecurity_ssl/lib/sslUtils.c
Security-57031.40.6.tar.gz
[apple/security.git] / Security / libsecurity_ssl / lib / sslUtils.c
1 /*
2 * Copyright (c) 1999-2001,2005-2008,2010-2012,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 * sslUtils.c - Misc. OS independant SSL utility functions
26 */
27
28 /* THIS FILE CONTAINS KERNEL CODE */
29
30 #include "sslUtils.h"
31 #include "sslTypes.h"
32 #include "sslDebug.h"
33
34 #include <AssertMacros.h>
35
36 #ifndef NDEBUG
37 void SSLDump(const unsigned char *data, unsigned long len)
38 {
39 unsigned long i;
40 for(i=0;i<len;i++)
41 {
42 if((i&0xf)==0) printf("%04lx :",i);
43 printf(" %02x", data[i]);
44 if((i&0xf)==0xf) printf("\n");
45 }
46 printf("\n");
47 }
48 #endif
49
50 unsigned int
51 SSLDecodeInt(const uint8_t *p, size_t length)
52 {
53 unsigned int val = 0;
54 check(length > 0 && length <= 4); //anything else would be an internal error.
55 while (length--)
56 val = (val << 8) | *p++;
57 return val;
58 }
59
60 uint8_t *
61 SSLEncodeInt(uint8_t *p, size_t value, size_t length)
62 {
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 */
67 value >>= 8;
68 }
69 return retVal;
70 }
71
72 size_t
73 SSLDecodeSize(const uint8_t *p, size_t length)
74 {
75 unsigned int val = 0;
76 check(length > 0 && length <= 4); //anything else would be an internal error.
77 while (length--)
78 val = (val << 8) | *p++;
79 return val;
80 }
81
82 uint8_t *
83 SSLEncodeSize(uint8_t *p, size_t value, size_t length)
84 {
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 */
89 value >>= 8;
90 }
91 return retVal;
92 }
93
94
95 uint8_t *
96 SSLEncodeUInt64(uint8_t *p, uint64_t value)
97 {
98 p = SSLEncodeInt(p, (value>>32)&0xffffffff, 4);
99 return SSLEncodeInt(p, value&0xffffffff, 4);
100 }
101
102
103 void
104 IncrementUInt64(sslUint64 *v)
105 {
106 (*v)++;
107 }
108
109 void
110 SSLDecodeUInt64(const uint8_t *p, size_t length, uint64_t *v)
111 {
112 check(length > 0 && length <= 8);
113 if(length<=4) {
114 *v=SSLDecodeInt(p, length);
115 } else {
116 *v=((uint64_t)SSLDecodeInt(p, length-4))<<32 | SSLDecodeInt(p+length-4, 4);
117 }
118 }
119
120
121 #if SSL_DEBUG
122
123 const char *protocolVersStr(SSLProtocolVersion prot)
124 {
125 switch(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";
133 }
134 return NULL; /* NOT REACHED */
135 }
136
137 #endif /* SSL_DEBUG */
138
139
140