source: openpam/trunk/lib/liboath/oath_base64.c @ 779

Last change on this file since 779 was 779, checked in by Dag-Erling Smørgrav, 7 years ago

When I changed the argument type from uint8_t * to char *, I forgot that
they were being used as array indices. Cast them back to uint8_t.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/*-
2 * Copyright (c) 2013-2014 Universitetet i Oslo
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Id: oath_base64.c 779 2014-03-09 14:11:44Z des $
30 */
31
32#ifdef HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include <sys/types.h>
37
38#include <errno.h>
39#include <stdint.h>
40
41#include <security/oath.h>
42
43#include "oath_impl.h"
44
45static const char b64enc[] =
46    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47    "abcdefghijklmnopqrstuvwxyz"
48    "0123456789+/";
49
50static const char b64dec[256] = {
51        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
52        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
53        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
54        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
55        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
56        0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 
57        0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 
58        0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
59        0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 
60        0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 
61        0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 
62        0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 
63        0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 
64        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 
65        0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 
66        0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 
67        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
68        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
69        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
70        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
71        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
72        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
73        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
74        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
75        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
76        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
77        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
78        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
79        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
80        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
81        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
82        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
83};
84
85/*
86 * Encode data in RFC 4648 base 64 representation.  The target buffer must
87 * have room for base64_enclen(len) characters and a terminating NUL.
88 */
89int
90base64_enc(const char *cin, size_t ilen, char *out, size_t *olen)
91{
92        const uint8_t *in = (uint8_t *)cin;
93        uint32_t bits;
94
95        if (*olen <= base64_enclen(ilen)) {
96                *olen = base64_enclen(ilen) + 1;
97                errno = ENOSPC;
98                return (-1);
99        }
100        *olen = 0;
101        while (ilen >= 3) {
102                bits = 0;
103                bits |= (uint32_t)in[0] << 16;
104                bits |= (uint32_t)in[1] << 8;
105                bits |= (uint32_t)in[2];
106                ilen -= 3;
107                in += 3;
108                out[0] = b64enc[bits >> 18 & 0x3f];
109                out[1] = b64enc[bits >> 12 & 0x3f];
110                out[2] = b64enc[bits >> 6 & 0x3f];
111                out[3] = b64enc[bits & 0x3f];
112                *olen += 4;
113                out += 4;
114        }
115        if (ilen > 0) {
116                bits = 0;
117                switch (ilen) {
118                case 2:
119                        bits |= (uint32_t)in[1] << 8;
120                case 1:
121                        bits |= (uint32_t)in[0] << 16;
122                COVERAGE_NO_DEFAULT_CASE
123                }
124                out[0] = b64enc[bits >> 18 & 0x3f];
125                out[1] = b64enc[bits >> 12 & 0x3f];
126                out[2] = ilen > 1 ? b64enc[bits >> 6 & 0x3f] : '=';
127                out[3] = '=';
128                *olen += 4;
129                out += 4;
130        }
131        out[0] = '\0';
132        ++*olen;
133        return (0);
134}
135
136/*
137 * Decode data in RFC 4648 base 64 representation, stopping at the
138 * terminating NUL, the first invalid (non-base64, non-whitespace)
139 * character or after len characters, whichever comes first.
140 *
141 * Padding is handled sloppily: any padding character following the data
142 * is silently consumed.  This not only simplifies the code but ensures
143 * compatibility with implementations which do not emit or understand
144 * padding.
145 *
146 * The olen argument is used by the caller to pass the size of the buffer
147 * and by base64_dec() to return the amount of data successfully decoded.
148 * If the buffer is too small, base64_dec() discards the excess data, but
149 * returns the total amount.
150 */
151int
152base64_dec(const char *cin, size_t ilen, char *out, size_t *olen)
153{
154        const uint8_t *in = (uint8_t *)cin;
155        size_t len;
156        int bits, shift, padding;
157
158        for (bits = shift = padding = len = 0; ilen && *in; --ilen, ++in) {
159                if (*in == ' ' || *in == '\t' || *in == '\r' || *in == '\n' ||
160                    (padding && *in == '=')) {
161                        /* consume */
162                        continue;
163                } else if (!padding && b64dec[*in] >= 0) {
164                        /* shift into accumulator */
165                        shift += 6;
166                        bits = bits << 6 | b64dec[*in];
167                } else if (!padding && shift > 0 && shift != 6 && *in == '=') {
168                        /* final byte */
169                        shift = 0;
170                        padding = 1;
171                } else {
172                        /* error */
173                        *olen = 0;
174                        errno = EINVAL;
175                        return (-1);
176                }
177                if (shift >= 8) {
178                        /* output accumulated byte */
179                        shift -= 8;
180                        if (len++ < *olen)
181                                *out++ = (bits >> shift) & 0xff;
182                }
183        }
184        /* report decoded length */
185        if (len > *olen) {
186                /* overflow */
187                *olen = len;
188                errno = ENOSPC;
189                return (-1);
190        }
191        *olen = len;
192        return (0);
193}
Note: See TracBrowser for help on using the repository browser.