Imported Upstream version 2.2.15
[platform/upstream/gpg2.git] / common / t-ssh-utils.c
1 /* t-ssh-utils.c - Module test for ssh-utils.c
2  * Copyright (C) 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "util.h"
28 #include "ssh-utils.h"
29
30
31 static struct
32 {
33   const char *key;
34   const char *fpr_md5;
35   const char *fpr_sha256;
36 } sample_keys[] = {
37   { "(protected-private-key "
38     "(rsa "
39     "(n #"
40     "00D88E47BCE0DA99D6180E8A9F4E6A673CC16F5BB6CF930E0E868BAABA715A8E1D3E2BEA"
41     "5477170E1F6CAFC0F8907B9892993C70AC476BBB301669F68EE0593532FB522DD60755A3"
42     "2F8B08649E856271A7F9BCB25F29554DF11707F812EA377683A99DD4698C4DBF797A0ABF"
43     "43C8EBB364B9FFC9EE78CBEA348C590507A4EA390312153DDD905EC4F1A63D5DA56C08FD"
44     "C3F6E5707BFC5DBDC09D19723B1AC6E466906F13AA2ECDBD258148F86C980D45CF233415"
45     "38C5857C2CF0B4C9AB2B4E6A4517FF084FDB009A33553A68907A29691B6FAE994E864F78"
46     "7B83F714730BEDB0AF1723D636E034D73EB7EC9BA127BB4BE80FD805813E3F45E7FAE514"
47     "AD2ECA9607#)"
48     "(e \"#\")"
49     "(protected openpgp-s2k3-sha1-aes-cbc "
50     "("
51     "(sha1 #B5847F9A2DB4E0C5# \"5242880\")"
52     "#342D81BDE21301F18FDCE169A99A47C5#)"
53     "#23512602219EC7A97DBA89347CCD59D2072D80CE3F7DD6C97A058B83DAB3C829D97DF5"
54     "DFE9181F27DBF58258C4CDBD562A5B20BB5BC35EDCA7B1E57B8CDBF92D798F46EE5567BD"
55     "8A67EF3BE09908A49D41AA166A3398B64227BC75021C69A8FE8354E2903EF52DC91B1FE3"
56     "EF9558E5C2D34CF38BFC29E99A49AE30B0C22CE81EE14FC71E986E7C7CB5FCF807433FDA"
57     "EF1D00985767265BA0BE333754E44CCF622CBB98A029D78A6A9AADBC24613127B6448350"
58     "23DA355ED31CF089DD11A7FC6003CEEB53FB327A05604D053C99996F9E01CB355983F66E"
59     "7BEB9687A9277BBF440ED5FAF1A8396C9B06C9B47BA7A994E1931B08DAD34449952CD343"
60     "9A691477682C324EA07CCCE5DF0F0E9DAEFAE3A4717AACA6DC18ED91DD5A820C924BD36B"
61     "B3BA85BD63B3180C7F94EE58956940621280B9628FA5CC560BB14331AF1A7B5B499F8F03"
62     "0ED464ABD4E26C5FD610697EDD0FD1203983E73418F3776568A613D3CEFF17199473052A"
63     "18807A6F5C52A2A643185801D087EE4DC930ABEEB67C5B8A1CB2F29D0ACBD855972BEC0B"
64     "DE6E52387CFCC54B4C2B87EE947C97173BFCAE3E2658EB819D87F542C9A9FE6C410D08F5"
65     "3CD5451FB50253F4A848DFE136B3A5861D58B76A26A7E3E4E7A8F8D4BD5B80430674A6B9"
66     "A2C8EDD53DB37865D1ACBB07E1758DFF64A944E0126F948BF088C0FC0C3607E39522EC94"
67     "91483A90D9498D7F6C3C8720124C7E3F6E271E78E1CFFB4EF64F070F7424F30372A07D02"
68     "2355D8B17BB0DEBCBE101F621E0526551A35A56830D74E0F5BD6313DF114D1E46D4844AA"
69     "E4EB6268637D04B27D200D7F40AFA9AD2CFAA5415E5FC08358FFA79A9E743CCDF6668FE5"
70     "D79FA03D61941E57244F066A31F1C9D6A34DC62BC738C52B604F00B19EB9FD0173F3B139"
71     "42932066B7DC94DC4C563392F798A1CE2D5D75B8FF93E440433263CFB7016143A9923CD9"
72     "634E964A8056946F462B06F320F44449D85B07FA26A324505C858274F89EDBD8346950DE"
73     "5F#)"
74     "(protected-at \"20110720T135431\")"
75     ")"
76     "(comment passphrase_is_abc)"
77     ")",
78     "MD5:c7:c6:a7:ec:04:6c:87:59:54:f2:88:58:09:e0:f2:b1",
79     "SHA256:ksKb4DKk2SFX56GRtpt0szBnyjiYARSb2FNlUb7snnE"
80   },
81   {
82     "(protected-private-key "
83     "(dsa "
84     "(p #00FC7DC086F4517079BCCFA7FD229477FE88B0231038DFC21B29CCBD74C6F6FE04FD"
85     "7248C0473D5028BE106D7A7C8F54B269225789E781763527D1432CD46E416C2D14DDCA70"
86     "27DA4B92D1E222B5BDF4B9C8C761CACCFBD108F7729412E8835653BE5073447287A6BDEB"
87     "4645A5411752405EE7F503E44B1DFDCA6054CD3C44630B#)"
88     "(q #00D498505BF0E7EE01239EB51F2B400B8EF6329B17#)"
89     "(g #00A127B3DD5106F0A463312E42ECB83790E6F3BEA7AC3FAF7A42FB2C00F376323676"
90     "C9E48984F0D4AC3FE5856F1C2765E9BC3C8A5C9C9CD3166C057E82569D187C48591AA66B"
91     "8966BFF2B827BE36BD0BA4B895B42136F1381D52DDA708B2A3D181F648228DFFFEB153DA"
92     "ACCAEBB51EF08A7807CD628024CEFF96FEE97DE95C8CBE#)"
93     "(y #008E2B0915A3A299D83B4333C848C5D312F25903773E8C4D50691CAF81C3B768FA41"
94     "7D19F0FD437B377CCF51D3AE598649656D4D74D210CDBC2B76209B16EAAFCB14D6F4D691"
95     "20164885852AF1CEBB4D8602AD6755DFA7163645B4DB7926CD44D2DD9F840BFEF57F3DB0"
96     "933C85EB6B0AAC20BC67E73F47B8DDBEC8EFAA64286EF1#)"
97     "(protected openpgp-s2k3-sha1-aes-cbc "
98     "("
99     "(sha1  \"ü¿jy²üa4\" \"5242880\")"
100     "#FF12BEE0B03F842349717AE1AB6D7AC2#)"
101     "#95570487C8B5C49492D4E662259F2CF9B6D7E64F728F17A1FE1B2DA616E5976FE32861E"
102     "C4B1F0DA03D9006C432CF2136871266E9444377ACEF04340B36B4550B5C1E4CC69AD4380"
103     "A709FB0DAA5104A8B#)"
104     "(protected-at \"20110720T142801\")"
105     ")"
106     "(comment sample_dsa_passphrase_is_abc)"
107     ")",
108     "MD5:2d:b1:70:1a:04:9e:41:a3:ce:27:a5:c7:22:fe:3a:a3",
109     "SHA256:z8+8HEuD/5QpegGS4tSK02dJF+a6o2V67VM2gOPz9oQ"
110   },
111   { /* OpenSSH 6.7p1 generated key:  */
112     "(protected-private-key "
113     "(ecdsa "
114     "(curve \"NIST P-256\")"
115     "(q #041F17ED5E3D637181DFA68157270F94A46C089B6F5D4518564600551C0A60A063B3"
116     "31EDE027A23CAB58A5BAD469600229DC8DED06380A92F86460ED400F963319#)"
117     "(protected openpgp-s2k3-sha1-aes-cbc "
118     "("
119     "(sha1 #43F887516D94A502# \"20971520\")"
120     "#B135DEDA02CF36F126BA661FB22A35CF#)"
121     "#37E74BEC054B17723C106BA69214CFDA245512E40F4848ECF5719E3700002C940BC7EEC"
122     "283537CA4D8779107E07F03AAA9FAF155BA9BF6286080C35EF72DDAAF303FD9069475B03"
123     "C99D9FC93C58CD83A852964D2C7BFD1D803E2ECD1331937C3#)"
124     "(protected-at \"20150922T071259\")"
125     ")"
126     "(comment \"ecdsa w/o comment\")"
127     ")", /* Passphrase="abc" */
128     "MD5:93:4f:08:02:7d:cb:16:9b:0c:39:21:4b:cf:28:5a:19",
129     "SHA256:zSj4uXfE1hlQnESD2LO723fMGXsNwzHrfqOfqep37is"
130   },
131   { /* OpenSSH 6.7p1 generated key:  */
132     "(protected-private-key "
133     "(ecdsa "
134     "(curve \"NIST P-384\") "
135     "(q #04B6E747AC2F179F96088D1DB58EB8600BB23FAEF5F58EFE712A7478FB7BF735"
136     "B015EA2DFBBA965D8C6EB135A2B9B9599D65BF0167D2DB6ABF00F641F0F5FC15A4C3"
137     "EFE432DA331B7C8A66D6C4C2B0EBB5ED11A80301C4E57C1EBD25665CEBF123#)"
138     "(protected openpgp-s2k3-sha1-aes-cbc "
139     "("
140     "(sha1 #3B13710B67D756EA# \"20971520\")"
141     "#720599AC095BF1BD73ED72F49FB77BFA#)"
142     "#F1A522F4533E3A6E40821D67CEA6C28A7FF07ACA4BEE81E0F39193B2E469E0C583D"
143     "A42E0E2D52ADB5ACFAB9C4CA7F1C3556FD7FD2770717FB3CE7C59474A3E2A7AF3D93"
144     "9EC01E067DAAA60D3D355D9BABCCD1F013E8637C555DDFA61F8FA5AFB010FF02979D"
145     "35BBBEED71BFD8BB508F7#)"
146     "(protected-at \"20150922T070806\")"
147     ")"
148     "(comment \"ecdsa w/o comment\")"
149     ")", /* Passphrase="abc" */
150     "MD5:a3:cb:44:c8:56:15:25:62:85:fd:e8:04:7a:26:dc:76",
151     "SHA256:JuQh5fjduynuuTEwI9C6yAKK1NnLX9PPd7TP0qZfbGs"
152   },
153   { /* OpenSSH 6.7p1 generated key:  */
154     "(protected-private-key "
155     "(ecdsa "
156     "(curve \"NIST P-521\")"
157     "(q #04005E460058F37DB5ADA670040203C4D7E18D9FC8A7087165904A4E25EE5EEE"
158     "3046406D922616DA7E71016A1CB9E57A45E3D3727D7C8DF0F11AE2BD75FAD3355CAA"
159     "E1019D89D33CC77424E5DA233588207444FC9F67BBE428A9528B7DC77AF8261A1D45"
160     "ACC1A657C99E361E93C1E5C0F214104C18807670F4CDC1E038B7C950FDBAAECB40#)"
161     "(protected openpgp-s2k3-sha1-aes-cbc "
162     "("
163     "(sha1 #FB2E36984DE2E17C# \"19737600\")"
164     "#85DB6445B37012F9A449E5AC0D5017E9#)"
165     "#B4C7CCDFE9B5D32B31BA7C763B80485A62EBF34FD68D8E306DA75FD2BDDBABAA098"
166     "9B51972BA3B731DA5261E0ADC3FAEF9BB4C8284C53D3E88E738AEF1490941903A5B2"
167     "9F3747E83C4D80B6A89E0B7BDEE5C6638332F4AAEA5983F760B2887A43A1C4BE0564"
168     "3F72C6943987D97FDAA7D9C235C6D31973A2400DA9BAB564A16EA#)"
169     "(protected-at \"20150922T075611\")"
170     ")"
171     "(comment \"ecdsa w/o comment\")"
172     ")", /* Passphrase="abc" */
173     "MD5:1e:a6:94:ab:bd:81:73:5f:22:bc:0e:c7:89:f6:68:df",
174     "SHA256:+pbRyYa2UBwDki1k4Wziu2CKrdJIbZM/hOWOQ/sNe/0"
175   },
176   { /* OpenSSH 6.7p1 generated key:  */
177     "(protected-private-key "
178     "(ecc "
179     "(curve Ed25519)"
180     "(flags eddsa)"
181     "(q #40A3577AA7830C50EBC15B538E9505DB2F0D2FFCD57EA477DD83dcaea530f3c277#)"
182     "(protected openpgp-s2k3-sha1-aes-cbc "
183     "("
184     "(sha1 #FA8123F1A37CBC1F# \"3812352\")"
185     "#7671C7387E2DD931CC62C35CBBE08A28#)"
186     "#75e928f4698172b61dffe9ef2ada1d3473f690f3879c5386e2717e5b2fa46884"
187     "b189ee409827aab0ff37f62996e040b5fa7e75fc4d8152c8734e2e648dff90c9"
188     "e8c3e39ea7485618d05c34b1b74ff59676e9a3d932245cc101b5904777a09f86#)"
189     "(protected-at \"20150928T050210\")"
190     ")"
191     "(comment \"eddsa w/o comment\")"
192     ")", /* Passphrase="abc" */
193     "MD5:f1:fa:c8:a6:40:bb:b9:a1:65:d7:62:65:ac:26:78:0e",
194     "SHA256:yhwBfYnTOnSXcWf1EOPo+oIIpNJ6w/bG36udZ96MmsQ"
195   },
196   {
197     NULL,
198     NULL
199   }
200 };
201
202
203
204 static char *
205 read_file (const char *fname, size_t *r_length)
206 {
207   FILE *fp;
208   char *buf;
209   size_t buflen;
210   struct stat st;
211
212   fp = fopen (fname, "rb");
213   if (!fp)
214     {
215       fprintf (stderr, "%s:%d: can't open '%s': %s\n",
216                __FILE__, __LINE__, fname, strerror (errno));
217       exit (1);
218     }
219
220   if (fstat (fileno(fp), &st))
221     {
222       fprintf (stderr, "%s:%d: can't stat '%s': %s\n",
223                __FILE__, __LINE__, fname, strerror (errno));
224       exit (1);
225     }
226
227   buflen = st.st_size;
228   buf = xmalloc (buflen+1);
229   if (fread (buf, buflen, 1, fp) != 1)
230     {
231       fprintf (stderr, "%s:%d: error reading '%s': %s\n",
232                __FILE__, __LINE__, fname, strerror (errno));
233       exit (1);
234     }
235   fclose (fp);
236
237   *r_length = buflen;
238   return buf;
239 }
240
241
242 static gcry_sexp_t
243 read_key (const char *fname)
244 {
245   gpg_error_t err;
246   char *buf;
247   size_t buflen;
248   gcry_sexp_t key;
249
250   buf = read_file (fname, &buflen);
251
252   err = gcry_sexp_sscan (&key, NULL, buf, buflen);
253   if (err)
254     {
255       fprintf (stderr, "%s:%d: gcry_sexp_sscan failed: %s\n",
256                __FILE__, __LINE__, gpg_strerror (err));
257       exit (1);                                 \
258     }
259
260   xfree (buf);
261   return key;
262 }
263
264
265 int
266 main (int argc, char **argv)
267 {
268   gpg_error_t err;
269   gcry_sexp_t key;
270   char *string;
271   int idx;
272
273   /* --dump-keys dumps the keys as KEYGRIP.key.IDX.  Useful to compute
274        fingerprints to enhance the test vectors.  */
275   if (argc == 2 && strcmp (argv[1], "--dump-keys") == 0)
276     for (idx=0; sample_keys[idx].key; idx++)
277       {
278         FILE *s;
279         char *name;
280         char grip[20];
281         char *hexgrip;
282
283         err = keygrip_from_canon_sexp (sample_keys[idx].key,
284                                        strlen (sample_keys[idx].key),
285                                        grip);
286         if (err)
287           {
288             fprintf (stderr, "%s:%d: error computing keygrip: %s\n",
289                      __FILE__, __LINE__, gpg_strerror (err));
290             exit (1);
291           }
292         hexgrip = bin2hex (grip, 20, NULL);
293
294         name = xtryasprintf ("%s.key.%d", hexgrip, idx);
295         s = fopen (name, "w");
296         if (s == NULL)
297           {
298             fprintf (stderr, "%s:%d: error opening file: %s\n",
299                      __FILE__, __LINE__, gpg_strerror (gpg_error_from_syserror ()));
300             exit (1);
301           }
302         xfree (name);
303         fprintf (s, "%s", sample_keys[idx].key);
304         fclose (s);
305       }
306   else if (argc == 2)
307     {
308       key = read_key (argv[1]);
309
310       err = ssh_get_fingerprint_string (key, GCRY_MD_MD5, &string);
311       if (err)
312         {
313           fprintf (stderr, "%s:%d: error getting fingerprint: %s\n",
314                    __FILE__, __LINE__, gpg_strerror (err));
315           exit (1);
316         }
317       puts (string);
318       xfree (string);
319
320       err = ssh_get_fingerprint_string (key, GCRY_MD_SHA256, &string);
321       if (err)
322         {
323           fprintf (stderr, "%s:%d: error getting fingerprint: %s\n",
324                    __FILE__, __LINE__, gpg_strerror (err));
325           exit (1);
326         }
327       puts (string);
328       xfree (string);
329
330       gcry_sexp_release (key);
331     }
332   else
333     {
334       for (idx=0; sample_keys[idx].key; idx++)
335         {
336           err = gcry_sexp_sscan (&key, NULL, sample_keys[idx].key,
337                                  strlen (sample_keys[idx].key));
338           if (err)
339             {
340               fprintf (stderr, "%s:%d: gcry_sexp_sscan failed for "
341                        "sample key %d: %s\n",
342                        __FILE__, __LINE__, idx, gpg_strerror (err));
343               exit (1);
344             }
345
346           err = ssh_get_fingerprint_string (key, GCRY_MD_MD5, &string);
347           if (err)
348             {
349               fprintf (stderr, "%s:%d: error getting fingerprint for "
350                        "sample key %d: %s\n",
351                        __FILE__, __LINE__, idx, gpg_strerror (err));
352               exit (1);
353             }
354
355           if (strcmp (string, sample_keys[idx].fpr_md5))
356             {
357               fprintf (stderr, "%s:%d: fingerprint mismatch for "
358                        "sample key %d\n",
359                        __FILE__, __LINE__, idx);
360               fprintf (stderr, "want: %s\n got: %s\n",
361                        sample_keys[idx].fpr_md5, string);
362               exit (1);
363             }
364           xfree (string);
365
366           err = ssh_get_fingerprint_string (key, GCRY_MD_SHA256, &string);
367           if (err)
368             {
369               fprintf (stderr, "%s:%d: error getting fingerprint for "
370                        "sample key %d: %s\n",
371                        __FILE__, __LINE__, idx, gpg_strerror (err));
372               exit (1);
373             }
374
375           if (strcmp (string, sample_keys[idx].fpr_sha256))
376             {
377               fprintf (stderr, "%s:%d: fingerprint mismatch for "
378                        "sample key %d\n",
379                        __FILE__, __LINE__, idx);
380               fprintf (stderr, "want: %s\n got: %s\n",
381                        sample_keys[idx].fpr_sha256, string);
382               exit (1);
383             }
384           xfree (string);
385
386           gcry_sexp_release (key);
387         }
388     }
389
390   return 0;
391 }