]> git.ipfire.org Git - people/ms/strongswan.git/blob - linux/lib/libfreeswan/keyblobtoid.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / linux / lib / 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 * RCSID $Id: keyblobtoid.c,v 1.1 2004/03/15 20:35:26 as Exp $
16 */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21 - keyblobtoid - generate a printable key ID from an RFC 2537/3110 key blob
22 * Current algorithm is just to use first nine base64 digits.
23 */
24 size_t
25 keyblobtoid(src, srclen, dst, dstlen)
26 const unsigned char *src;
27 size_t srclen;
28 char *dst; /* need not be valid if dstlen is 0 */
29 size_t dstlen;
30 {
31 char buf[KEYID_BUF];
32 size_t ret;
33 # define NDIG 9
34
35 if (srclen < (NDIG*6 + 7)/8) {
36 strcpy(buf, "?len= ?");
37 buf[5] = '0' + srclen;
38 ret = 0;
39 } else {
40 (void) datatot(src, srclen, 64, buf, NDIG+1);
41 ret = NDIG+1;
42 }
43
44 if (dstlen > 0) {
45 if (strlen(buf)+1 > dstlen)
46 *(buf + dstlen - 1) = '\0';
47 strcpy(dst, buf);
48 }
49 return ret;
50 }
51
52 /*
53 - splitkeytoid - generate a printable key ID from exponent/modulus pair
54 * Just constructs the beginnings of a key blob and calls keyblobtoid().
55 */
56 size_t
57 splitkeytoid(e, elen, m, mlen, dst, dstlen)
58 const unsigned char *e;
59 size_t elen;
60 const unsigned char *m;
61 size_t mlen;
62 char *dst; /* need not be valid if dstlen is 0 */
63 size_t dstlen;
64 {
65 unsigned char buf[KEYID_BUF]; /* ample room */
66 unsigned char *bufend = buf + sizeof(buf);
67 unsigned char *p;
68 size_t n;
69
70 p = buf;
71 if (elen <= 255)
72 *p++ = elen;
73 else if ((elen &~ 0xffff) == 0) {
74 *p++ = 0;
75 *p++ = (elen>>8) & 0xff;
76 *p++ = elen & 0xff;
77 } else
78 return 0; /* unrepresentable exponent length */
79
80 n = bufend - p;
81 if (elen < n)
82 n = elen;
83 memcpy(p, e, n);
84 p += n;
85
86 n = bufend - p;
87 if (n > 0) {
88 if (mlen < n)
89 n = mlen;
90 memcpy(p, m, n);
91 p += n;
92 }
93
94 return keyblobtoid(buf, p - buf, dst, dstlen);
95 }
96
97
98
99 #ifdef KEYBLOBTOID_MAIN
100
101 #include <stdio.h>
102
103 void regress();
104
105 int
106 main(argc, argv)
107 int argc;
108 char *argv[];
109 {
110 typedef unsigned char uc;
111 uc hexblob[] = "\x01\x03\x85\xf2\xd6\x76\x9b\x03\x59\xb6\x21\x52";
112 uc hexe[] = "\x03";
113 uc hexm[] = "\x85\xf2\xd6\x76\x9b\x03\x59\xb6\x21\x52\xef\x85";
114 char b64nine[] = "AQOF8tZ2m";
115 char b64six[] = "AQOF8t";
116 char buf[100];
117 size_t n;
118 char *b = b64nine;
119 size_t bl = strlen(b) + 1;
120 int st = 0;
121
122 n = keyblobtoid(hexblob, strlen(hexblob), buf, sizeof(buf));
123 if (n != bl) {
124 fprintf(stderr, "%s: keyblobtoid returned %d not %d\n",
125 argv[0], n, bl);
126 st = 1;
127 }
128 if (strcmp(buf, b) != 0) {
129 fprintf(stderr, "%s: keyblobtoid generated `%s' not `%s'\n",
130 argv[0], buf, b);
131 st = 1;
132 }
133 n = splitkeytoid(hexe, strlen(hexe), hexm, strlen(hexm), buf,
134 sizeof(buf));
135 if (n != bl) {
136 fprintf(stderr, "%s: splitkeytoid returned %d not %d\n",
137 argv[0], n, bl);
138 st = 1;
139 }
140 if (strcmp(buf, b) != 0) {
141 fprintf(stderr, "%s: splitkeytoid generated `%s' not `%s'\n",
142 argv[0], buf, b);
143 st = 1;
144 }
145 exit(st);
146 }
147
148 #endif /* KEYBLOBTOID_MAIN */