removed atosa.c and satoa.c from libfreeswan
[strongswan.git] / src / libfreeswan / keyblobtoid.c
1 /*
2 * generate printable key IDs
3 * Copyright (C) 2002 Henry Spencer.
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
14 */
15 #include "internal.h"
16 #include "freeswan.h"
17
18 /*
19 - keyblobtoid - generate a printable key ID from an RFC 2537/3110 key blob
20 * Current algorithm is just to use first nine base64 digits.
21 */
22 size_t
23 keyblobtoid(src, srclen, dst, dstlen)
24 const unsigned char *src;
25 size_t srclen;
26 char *dst; /* need not be valid if dstlen is 0 */
27 size_t dstlen;
28 {
29 char buf[KEYID_BUF];
30 size_t ret;
31 # define NDIG 9
32
33 if (srclen < (NDIG*6 + 7)/8) {
34 strcpy(buf, "?len= ?");
35 buf[5] = '0' + srclen;
36 ret = 0;
37 } else {
38 (void) datatot(src, srclen, 64, buf, NDIG+1);
39 ret = NDIG+1;
40 }
41
42 if (dstlen > 0) {
43 if (strlen(buf)+1 > dstlen)
44 *(buf + dstlen - 1) = '\0';
45 strcpy(dst, buf);
46 }
47 return ret;
48 }
49
50 /*
51 - splitkeytoid - generate a printable key ID from exponent/modulus pair
52 * Just constructs the beginnings of a key blob and calls keyblobtoid().
53 */
54 size_t
55 splitkeytoid(e, elen, m, mlen, dst, dstlen)
56 const unsigned char *e;
57 size_t elen;
58 const unsigned char *m;
59 size_t mlen;
60 char *dst; /* need not be valid if dstlen is 0 */
61 size_t dstlen;
62 {
63 unsigned char buf[KEYID_BUF]; /* ample room */
64 unsigned char *bufend = buf + sizeof(buf);
65 unsigned char *p;
66 size_t n;
67
68 p = buf;
69 if (elen <= 255)
70 *p++ = elen;
71 else if ((elen &~ 0xffff) == 0) {
72 *p++ = 0;
73 *p++ = (elen>>8) & 0xff;
74 *p++ = elen & 0xff;
75 } else
76 return 0; /* unrepresentable exponent length */
77
78 n = bufend - p;
79 if (elen < n)
80 n = elen;
81 memcpy(p, e, n);
82 p += n;
83
84 n = bufend - p;
85 if (n > 0) {
86 if (mlen < n)
87 n = mlen;
88 memcpy(p, m, n);
89 p += n;
90 }
91
92 return keyblobtoid(buf, p - buf, dst, dstlen);
93 }
94
95
96
97 #ifdef KEYBLOBTOID_MAIN
98
99 #include <stdio.h>
100
101 void regress();
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 typedef unsigned char uc;
109 uc hexblob[] = "\x01\x03\x85\xf2\xd6\x76\x9b\x03\x59\xb6\x21\x52";
110 uc hexe[] = "\x03";
111 uc hexm[] = "\x85\xf2\xd6\x76\x9b\x03\x59\xb6\x21\x52\xef\x85";
112 char b64nine[] = "AQOF8tZ2m";
113 char b64six[] = "AQOF8t";
114 char buf[100];
115 size_t n;
116 char *b = b64nine;
117 size_t bl = strlen(b) + 1;
118 int st = 0;
119
120 n = keyblobtoid(hexblob, strlen(hexblob), buf, sizeof(buf));
121 if (n != bl) {
122 fprintf(stderr, "%s: keyblobtoid returned %d not %d\n",
123 argv[0], n, bl);
124 st = 1;
125 }
126 if (strcmp(buf, b) != 0) {
127 fprintf(stderr, "%s: keyblobtoid generated `%s' not `%s'\n",
128 argv[0], buf, b);
129 st = 1;
130 }
131 n = splitkeytoid(hexe, strlen(hexe), hexm, strlen(hexm), buf,
132 sizeof(buf));
133 if (n != bl) {
134 fprintf(stderr, "%s: splitkeytoid returned %d not %d\n",
135 argv[0], n, bl);
136 st = 1;
137 }
138 if (strcmp(buf, b) != 0) {
139 fprintf(stderr, "%s: splitkeytoid generated `%s' not `%s'\n",
140 argv[0], buf, b);
141 st = 1;
142 }
143 exit(st);
144 }
145
146 #endif /* KEYBLOBTOID_MAIN */