Path: blob/main/contrib/bearssl/src/codec/pemenc.c
39482 views
/*1* Copyright (c) 2018 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* Get the appropriate Base64 character for a numeric value in the28* 0..63 range. This is constant-time.29*/30static char31b64char(uint32_t x)32{33/*34* Values 0 to 25 map to 0x41..0x5A ('A' to 'Z')35* Values 26 to 51 map to 0x61..0x7A ('a' to 'z')36* Values 52 to 61 map to 0x30..0x39 ('0' to '9')37* Value 62 maps to 0x2B ('+')38* Value 63 maps to 0x2F ('/')39*/40uint32_t a, b, c;4142a = x - 26;43b = x - 52;44c = x - 62;4546/*47* Looking at bits 8..15 of values a, b and c:48*49* x a b c50* ---------------------51* 0..25 FF FF FF52* 26..51 00 FF FF53* 52..61 00 00 FF54* 62..63 00 00 0055*/56return (char)(((x + 0x41) & ((a & b & c) >> 8))57| ((x + (0x61 - 26)) & ((~a & b & c) >> 8))58| ((x - (52 - 0x30)) & ((~a & ~b & c) >> 8))59| ((0x2B + ((x & 1) << 2)) & (~(a | b | c) >> 8)));60}6162/* see bearssl_pem.h */63size_t64br_pem_encode(void *dest, const void *data, size_t len,65const char *banner, unsigned flags)66{67size_t dlen, banner_len, lines;68char *d;69unsigned char *buf;70size_t u;71int off, lim;7273banner_len = strlen(banner);74/* FIXME: try to avoid divisions here, as they may pull75an extra libc function. */76if ((flags & BR_PEM_LINE64) != 0) {77lines = (len + 47) / 48;78} else {79lines = (len + 56) / 57;80}81dlen = (banner_len << 1) + 30 + (((len + 2) / 3) << 2)82+ lines + 2;83if ((flags & BR_PEM_CRLF) != 0) {84dlen += lines + 2;85}8687if (dest == NULL) {88return dlen;89}9091d = dest;9293/*94* We always move the source data to the end of output buffer;95* the encoding process never "catches up" except at the very96* end. This also handles all conditions of partial or total97* overlap.98*/99buf = (unsigned char *)d + dlen - len;100memmove(buf, data, len);101102memcpy(d, "-----BEGIN ", 11);103d += 11;104memcpy(d, banner, banner_len);105d += banner_len;106memcpy(d, "-----", 5);107d += 5;108if ((flags & BR_PEM_CRLF) != 0) {109*d ++ = 0x0D;110}111*d ++ = 0x0A;112113off = 0;114lim = (flags & BR_PEM_LINE64) != 0 ? 16 : 19;115for (u = 0; (u + 2) < len; u += 3) {116uint32_t w;117118w = ((uint32_t)buf[u] << 16)119| ((uint32_t)buf[u + 1] << 8)120| (uint32_t)buf[u + 2];121*d ++ = b64char(w >> 18);122*d ++ = b64char((w >> 12) & 0x3F);123*d ++ = b64char((w >> 6) & 0x3F);124*d ++ = b64char(w & 0x3F);125if (++ off == lim) {126off = 0;127if ((flags & BR_PEM_CRLF) != 0) {128*d ++ = 0x0D;129}130*d ++ = 0x0A;131}132}133if (u < len) {134uint32_t w;135136w = (uint32_t)buf[u] << 16;137if (u + 1 < len) {138w |= (uint32_t)buf[u + 1] << 8;139}140*d ++ = b64char(w >> 18);141*d ++ = b64char((w >> 12) & 0x3F);142if (u + 1 < len) {143*d ++ = b64char((w >> 6) & 0x3F);144} else {145*d ++ = 0x3D;146}147*d ++ = 0x3D;148off ++;149}150if (off != 0) {151if ((flags & BR_PEM_CRLF) != 0) {152*d ++ = 0x0D;153}154*d ++ = 0x0A;155}156157memcpy(d, "-----END ", 9);158d += 9;159memcpy(d, banner, banner_len);160d += banner_len;161memcpy(d, "-----", 5);162d += 5;163if ((flags & BR_PEM_CRLF) != 0) {164*d ++ = 0x0D;165}166*d ++ = 0x0A;167168/* Final zero, not counted in returned length. */169*d ++ = 0x00;170171return dlen;172}173174175