1 /* mechanisms for preshared keys (public, private, and preshared secrets)
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * RCSID $Id: keys.c,v 1.24 2006/01/27 08:59:40 as Exp $
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
29 #include <sys/queue.h>
33 # define GLOB_ABORTED GLOB_ABEND /* fix for old versions */
37 #include <ipsec_policy.h>
39 #include "constants.h"
46 #include "smartcard.h"
47 #include "connections.h"
51 #include "adns.h" /* needs <resolv.h> */
52 #include "dnskey.h" /* needs keys.h and adns.h */
54 #include "whack.h" /* for RC_LOG_SERIOUS */
59 const char *shared_secrets_file
= SHARED_SECRETS_FILE
;
61 typedef struct id_list id_list_t
;
68 typedef struct secret secret_t
;
72 enum PrivateKeyKind kind
;
74 chunk_t preshared_secret
;
75 RSA_private_key_t RSA_private_key
;
77 smartcard_t
*smartcard
;
83 allocate_RSA_public_key(const cert_t cert
)
85 pubkey_t
*pk
= alloc_thing(pubkey_t
, "pubkey");
91 e
= cert
.u
.pgp
->publicExponent
;
92 n
= cert
.u
.pgp
->modulus
;
94 case CERT_X509_SIGNATURE
:
95 e
= cert
.u
.x509
->publicExponent
;
96 n
= cert
.u
.x509
->modulus
;
99 plog("RSA public key allocation error");
102 init_RSA_public_key(&pk
->u
.rsa
, e
, n
);
104 RSA_show_public_key(&pk
->u
.rsa
)
107 pk
->alg
= PUBKEY_ALG_RSA
;
109 pk
->issuer
= empty_chunk
;
110 pk
->serial
= empty_chunk
;
116 * free a public key struct
119 free_public_key(pubkey_t
*pk
)
121 free_id_content(&pk
->id
);
122 freeanychunk(pk
->issuer
);
123 freeanychunk(pk
->serial
);
125 /* algorithm-specific freeing */
129 free_RSA_public_content(&pk
->u
.rsa
);
137 secret_t
*secrets
= NULL
;
139 /* find the struct secret associated with the combination of
140 * me and the peer. We match the Id (if none, the IP address).
141 * Failure is indicated by a NULL.
143 static const secret_t
*
144 get_secret(const struct connection
*c
, enum PrivateKeyKind kind
, bool asym
)
152 unsigned int best_match
= 0;
153 secret_t
*best
= NULL
;
155 const struct id
*my_id
= &c
->spd
.this.id
156 , *his_id
= &c
->spd
.that
.id
;
159 /* is there a certificate assigned to this connection? */
160 if (kind
== PPK_RSA
&& c
->spd
.this.cert
.type
!= CERT_NONE
)
162 pubkey_t
*my_public_key
= allocate_RSA_public_key(c
->spd
.this.cert
);
164 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
166 if (s
->kind
== kind
&&
167 same_RSA_public_key(&s
->u
.RSA_private_key
.pub
, &my_public_key
->u
.rsa
))
170 break; /* we have found the private key - no sense in searching further */
173 free_public_key(my_public_key
);
177 if (his_id_was_instantiated(c
))
179 /* roadwarrior: replace him with 0.0.0.0 */
180 rw_id
.kind
= c
->spd
.that
.id
.kind
;
181 rw_id
.name
= empty_chunk
;
182 happy(anyaddr(addrtypeof(&c
->spd
.that
.host_addr
), &rw_id
.ip_addr
));
185 else if (kind
== PPK_PSK
186 && (c
->policy
& (POLICY_PSK
| POLICY_XAUTH_PSK
))
187 && ((c
->kind
== CK_TEMPLATE
&& c
->spd
.that
.id
.kind
== ID_NONE
) ||
188 (c
->kind
== CK_INSTANCE
&& id_is_ipaddr(&c
->spd
.that
.id
))))
190 /* roadwarrior: replace him with 0.0.0.0 */
191 rw_id
.kind
= ID_IPV4_ADDR
;
192 happy(anyaddr(addrtypeof(&c
->spd
.that
.host_addr
), &rw_id
.ip_addr
));
196 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
200 unsigned int match
= 0;
204 /* a default (signified by lack of ids):
205 * accept if no more specific match found
207 match
= match_default
;
211 /* check if both ends match ids */
214 for (i
= s
->ids
; i
!= NULL
; i
= i
->next
)
216 if (same_id(my_id
, &i
->id
))
219 if (same_id(his_id
, &i
->id
))
223 /* If our end matched the only id in the list,
224 * default to matching any peer.
225 * A more specific match will trump this.
227 if (match
== match_me
228 && s
->ids
->next
== NULL
)
229 match
|= match_default
;
235 /* if this is an asymmetric (eg. public key) system,
236 * allow this-side-only match to count, even if
237 * there are other ids in the list.
242 case match_default
: /* default all */
243 case match_me
| match_default
: /* default peer */
244 case match_me
| match_him
: /* explicit */
245 if (match
== best_match
)
247 /* two good matches are equally good:
255 same
= s
->u
.preshared_secret
.len
== best
->u
.preshared_secret
.len
256 && memcmp(s
->u
.preshared_secret
.ptr
, best
->u
.preshared_secret
.ptr
, s
->u
.preshared_secret
.len
) == 0;
259 /* Dirty trick: since we have code to compare
260 * RSA public keys, but not private keys, we
261 * make the assumption that equal public keys
262 * mean equal private keys. This ought to work.
264 same
= same_RSA_public_key(&s
->u
.RSA_private_key
.pub
265 , &best
->u
.RSA_private_key
.pub
);
272 loglog(RC_LOG_SERIOUS
, "multiple ipsec.secrets entries with distinct secrets match endpoints:"
273 " first secret used");
274 best
= s
; /* list is backwards: take latest in list */
277 else if (match
> best_match
)
279 /* this is the best match so far */
289 /* find the appropriate preshared key (see get_secret).
290 * Failure is indicated by a NULL pointer.
291 * Note: the result is not to be freed by the caller.
294 get_preshared_secret(const struct connection
*c
)
296 const secret_t
*s
= get_secret(c
, PPK_PSK
, FALSE
);
300 DBG_log("no Preshared Key Found");
302 DBG_dump_chunk("Preshared Key", s
->u
.preshared_secret
);
304 return s
== NULL? NULL
: &s
->u
.preshared_secret
;
307 /* check the existence of an RSA private key matching an RSA public
308 * key contained in an X.509 or OpenPGP certificate
311 has_private_key(cert_t cert
)
314 bool has_key
= FALSE
;
315 pubkey_t
*pubkey
= allocate_RSA_public_key(cert
);
317 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
319 if (s
->kind
== PPK_RSA
&&
320 same_RSA_public_key(&s
->u
.RSA_private_key
.pub
, &pubkey
->u
.rsa
))
326 free_public_key(pubkey
);
331 * get the matching RSA private key belonging to a given X.509 certificate
333 const RSA_private_key_t
*
334 get_x509_private_key(const x509cert_t
*cert
)
337 const RSA_private_key_t
*pri
= NULL
;
338 const cert_t c
= {CERT_X509_SIGNATURE
, {cert
}};
340 pubkey_t
*pubkey
= allocate_RSA_public_key(c
);
342 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
344 if (s
->kind
== PPK_RSA
&&
345 same_RSA_public_key(&s
->u
.RSA_private_key
.pub
, &pubkey
->u
.rsa
))
347 pri
= &s
->u
.RSA_private_key
;
351 free_public_key(pubkey
);
355 /* find the appropriate RSA private key (see get_secret).
356 * Failure is indicated by a NULL pointer.
358 const RSA_private_key_t
*
359 get_RSA_private_key(const struct connection
*c
)
361 const secret_t
*s
= get_secret(c
, PPK_RSA
, TRUE
);
363 return s
== NULL? NULL
: &s
->u
.RSA_private_key
;
366 /* digest a secrets file
368 * The file is a sequence of records. A record is a maximal sequence of
369 * tokens such that the first, and only the first, is in the first column
372 * Tokens are generally separated by whitespace and are key words, ids,
373 * strings, or data suitable for ttodata(3). As a nod to convention,
374 * a trailing ":" on what would otherwise be a token is taken as a
375 * separate token. If preceded by whitespace, a "#" is taken as starting
376 * a comment: it and the rest of the line are ignored.
378 * One kind of record is an include directive. It starts with "include".
379 * The filename is the only other token in the record.
380 * If the filename does not start with /, it is taken to
381 * be relative to the directory containing the current file.
383 * The other kind of record describes a key. It starts with a
384 * sequence of ids and ends with key information. Each id
385 * is an IP address, a Fully Qualified Domain Name (which will immediately
386 * be resolved), or @FQDN which will be left as a name.
388 * The key part can be in several forms.
390 * The old form of the key is still supported: a simple
391 * quoted strings (with no escapes) is taken as a preshred key.
393 * The new form starts the key part with a ":".
395 * For Preshared Key, use the "PSK" keyword, and follow it by a string
396 * or a data token suitable for ttodata(3).
398 * For RSA Private Key, use the "RSA" keyword, followed by a
399 * brace-enclosed list of key field keywords and data values.
400 * The data values are large integers to be decoded by ttodata(3).
401 * The fields are a subset of those used by BIND 8.2 and have the
405 /* parse PSK from file */
407 process_psk_secret(chunk_t
*psk
)
411 if (*tok
== '"' || *tok
== '\'')
413 clonetochunk(*psk
, tok
+1, flp
->cur
- tok
- 2, "PSK");
418 char buf
[BUF_LEN
]; /* limit on size of binary representation of key */
421 ugh
= ttodatav(tok
, flp
->cur
- tok
, 0, buf
, sizeof(buf
), &sz
422 , diag_space
, sizeof(diag_space
), TTODATAV_SPACECOUNTS
);
425 /* ttodata didn't like PSK data */
426 ugh
= builddiag("PSK data malformed (%s): %s", ugh
, tok
);
430 clonetochunk(*psk
, buf
, sz
, "PSK");
437 /* Parse fields of RSA private key.
438 * A braced list of keyword and value pairs.
439 * At the moment, each field is required, in order.
440 * The fields come from BIND 8.2's representation
443 process_rsa_secret(RSA_private_key_t
*rsak
)
445 char buf
[RSA_MAX_ENCODING_BYTES
]; /* limit on size of binary representation of key */
448 /* save bytes of Modulus and PublicExponent for keyid calculation */
449 unsigned char ebytes
[sizeof(buf
)];
450 unsigned char *eb_next
= ebytes
;
451 chunk_t pub_bytes
[2];
452 chunk_t
*pb_next
= &pub_bytes
[0];
454 for (p
= RSA_private_field
; p
< &RSA_private_field
[RSA_PRIVATE_FIELD_ELEMENTS
]; p
++)
461 return "premature end of RSA key";
463 else if (!tokeqword(p
->name
))
465 return builddiag("%s keyword not found where expected in RSA key"
469 && (!tokeq(":") || shift()))) /* ignore optional ":" */
471 return "premature end of RSA key";
473 else if (NULL
!= (ugh
= ttodatav(tok
, flp
->cur
- tok
474 , 0, buf
, sizeof(buf
), &sz
, diag_space
, sizeof(diag_space
)
475 , TTODATAV_SPACECOUNTS
)))
477 /* in RSA key, ttodata didn't like */
478 return builddiag("RSA data malformed (%s): %s", ugh
, tok
);
482 MP_INT
*n
= (MP_INT
*) ((char *)rsak
+ p
->offset
);
484 n_to_mpz(n
, buf
, sz
);
485 if (pb_next
< &pub_bytes
[elemsof(pub_bytes
)])
487 if (eb_next
- ebytes
+ sz
> sizeof(ebytes
))
488 return "public key takes too many bytes";
490 setchunk(*pb_next
, eb_next
, sz
);
491 memcpy(eb_next
, buf
, sz
);
495 #if 0 /* debugging info that compromises security */
497 size_t sz
= mpz_sizeinbase(n
, 16);
498 char buf
[RSA_MAX_OCTETS
* 2 + 2]; /* ought to be big enough */
500 passert(sz
<= sizeof(buf
));
501 mpz_get_str(buf
, 16, n
);
503 loglog(RC_LOG_SERIOUS
, "%s: %s", p
->name
, buf
);
509 /* We require an (indented) '}' and the end of the record.
510 * We break down the test so that the diagnostic will be
511 * more helpful. Some people don't seem to wish to indent
514 if (!shift() || !tokeq("}"))
516 return "malformed end of RSA private key -- indented '}' required";
520 return "malformed end of RSA private key -- unexpected token after '}'";
524 unsigned bits
= mpz_sizeinbase(&rsak
->pub
.n
, 2);
526 rsak
->pub
.k
= (bits
+ BITS_PER_BYTE
- 1) / BITS_PER_BYTE
;
527 rsak
->pub
.keyid
[0] = '\0'; /* in case of splitkeytoid failure */
528 splitkeytoid(pub_bytes
[1].ptr
, pub_bytes
[1].len
529 , pub_bytes
[0].ptr
, pub_bytes
[0].len
530 , rsak
->pub
.keyid
, sizeof(rsak
->pub
.keyid
));
531 return RSA_private_key_sanity(rsak
);
535 /* process rsa key file protected with optional passphrase which can either be
536 * read from ipsec.secrets or prompted for by using whack
539 process_rsa_keyfile(RSA_private_key_t
*rsak
, int whackfd
)
541 char filename
[BUF_LEN
];
544 memset(filename
,'\0', BUF_LEN
);
545 memset(pass
.secret
,'\0', sizeof(pass
.secret
));
549 /* we expect the filename of a PKCS#1 private key file */
551 if (*tok
== '"' || *tok
== '\'') /* quoted filename */
552 memcpy(filename
, tok
+1, flp
->cur
- tok
- 2);
554 memcpy(filename
, tok
, flp
->cur
- tok
);
558 /* we expect an appended passphrase or passphrase prompt*/
559 if (tokeqword("%prompt"))
561 if (pass
.fd
== NULL_FD
)
562 return "RSA private key file -- enter passphrase using 'ipsec secrets'";
567 char *passphrase
= tok
;
568 size_t len
= flp
->cur
- passphrase
;
570 if (*tok
== '"' || *tok
== '\'') /* quoted passphrase */
575 if (len
> PROMPT_PASS_LEN
)
576 return "RSA private key file -- passphrase exceeds 64 characters";
578 memcpy(pass
.secret
, passphrase
, len
);
581 return "RSA private key file -- unexpected token after passphrase";
583 return load_rsa_private_key(filename
, &pass
, rsak
);
587 * process xauth secret read from ipsec.secrets
590 process_xauth(secret_t
*s
)
597 return "missing xauth user name";
598 if (*tok
== '"' || *tok
== '\'') /* quoted user name */
600 user_name
.ptr
= tok
+ 1;
601 user_name
.len
= flp
->cur
- tok
- 2;
606 user_name
.len
= flp
->cur
- tok
;
608 plog(" loaded xauth credentials of user '%.*s'"
611 clonetochunk(s
->u
.xauth_secret
.user_name
612 , user_name
.ptr
, user_name
.len
, "xauth user name");
615 return "missing xauth user password";
616 return process_psk_secret(&s
->u
.xauth_secret
.user_password
);
619 /* get XAUTH secret from chained secrets lists
620 * only one entry is currently supported
623 xauth_get_secret(xauth_t
*xauth_secret
)
628 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
630 if (s
->kind
== PPK_XAUTH
)
634 plog("found multiple xauth secrets - first selected");
639 *xauth_secret
= s
->u
.xauth_secret
;
647 * find a matching secret
650 xauth_verify_secret(const xauth_t
*xauth_secret
)
655 for (s
= secrets
; s
!= NULL
; s
= s
->next
)
657 if (s
->kind
== PPK_XAUTH
)
659 if (!same_chunk(xauth_secret
->user_name
, s
->u
.xauth_secret
.user_name
))
662 if (same_chunk(xauth_secret
->user_password
, s
->u
.xauth_secret
.user_password
))
666 plog("xauth user '%.*s' %s"
667 , xauth_secret
->user_name
.len
, xauth_secret
->user_name
.ptr
668 , found?
"sent wrong password":"not found");
673 * the global xauth_module struct is defined here
675 xauth_module_t xauth_module
;
678 * assign the default xauth functions to any null function pointers
683 if (xauth_module
.get_secret
== NULL
)
686 DBG_log("xauth_module: using default get_secret() function")
688 xauth_module
.get_secret
= xauth_get_secret
;
690 if (xauth_module
.verify_secret
== NULL
)
693 DBG_log("xauth_module: using default verify_secret() function")
695 xauth_module
.verify_secret
= xauth_verify_secret
;
700 * process pin read from ipsec.secrets or prompted for it using whack
703 process_pin(secret_t
*s
, int whackfd
)
706 const char *pin_status
= "no pin";
710 /* looking for the smartcard keyword */
711 if (!shift() || strncmp(tok
, SCX_TOKEN
, strlen(SCX_TOKEN
)) != 0)
712 return "PIN keyword must be followed by %smartcard<reader>:<id>";
714 sc
= scx_add(scx_parse_number_slot_id(tok
+ strlen(SCX_TOKEN
)));
717 if (sc
->pin
.ptr
!= NULL
)
719 scx_release_context(sc
);
720 scx_free_pin(&sc
->pin
);
725 return "PIN statement must be terminated either by <pin code>, %pinpad or %prompt";
727 if (tokeqword("%prompt"))
730 /* if whackfd exists, whack will be used to prompt for a pin */
731 if (whackfd
!= NULL_FD
)
732 pin_status
= scx_get_pin(sc
, whackfd
) ?
"valid pin" : "invalid pin";
734 pin_status
= "pin entry via prompt";
736 else if (tokeqword("%pinpad"))
739 /* pin will be entered via pin pad during verification */
740 clonetochunk(sc
->pin
, "", 0, "empty pin");
743 pin_status
= "pin entry via pad";
744 if (pkcs11_keep_state
)
749 /* we read the pin directly from ipsec.secrets */
750 err_t ugh
= process_psk_secret(&sc
->pin
);
754 pin_status
= scx_verify_pin(sc
) ?
"valid PIN" : "invalid PIN";
761 snprintf(buf
, BUF_LEN
, "any slot");
763 snprintf(buf
, BUF_LEN
, "slot: %lu", sc
->slot
);
765 plog(" %s for #%d (%s, id: %s)"
766 , pin_status
, sc
->number
, scx_print_slot(sc
, ""), sc
->id
);
769 plog(" warning: SMARTCARD support is deactivated in pluto/Makefile!");
775 process_secret(secret_t
*s
, int whackfd
)
779 s
->kind
= PPK_PSK
; /* default */
780 if (*tok
== '"' || *tok
== '\'')
782 /* old PSK format: just a string */
783 ugh
= process_psk_secret(&s
->u
.preshared_secret
);
785 else if (tokeqword("psk"))
787 /* preshared key: quoted string or ttodata format */
788 ugh
= !shift()?
"unexpected end of record in PSK"
789 : process_psk_secret(&s
->u
.preshared_secret
);
791 else if (tokeqword("rsa"))
793 /* RSA key: the fun begins.
794 * A braced list of keyword and value pairs.
799 ugh
= "bad RSA key syntax";
803 ugh
= process_rsa_secret(&s
->u
.RSA_private_key
);
807 ugh
= process_rsa_keyfile(&s
->u
.RSA_private_key
, whackfd
);
810 else if (tokeqword("xauth"))
812 ugh
= process_xauth(s
);
814 else if (tokeqword("pin"))
816 ugh
= process_pin(s
, whackfd
);
820 ugh
= builddiag("unrecognized key format: %s", tok
);
825 loglog(RC_LOG_SERIOUS
, "\"%s\" line %d: %s"
826 , flp
->filename
, flp
->lino
, ugh
);
829 else if (flushline("expected record boundary in key"))
831 /* gauntlet has been run: install new secret */
832 lock_certs_and_keys("process_secret");
835 unlock_certs_and_keys("process_secrets");
839 static void process_secrets_file(const char *file_pat
, int whackfd
); /* forward declaration */
842 process_secret_records(int whackfd
)
844 /* read records from ipsec.secrets and load them into our table */
847 (void)flushline(NULL
); /* silently ditch leftovers, if any */
848 if (flp
->bdry
== B_file
)
851 flp
->bdry
= B_none
; /* eat the Record Boundary */
852 (void)shift(); /* get real first token */
854 if (tokeqword("include"))
856 /* an include directive */
857 char fn
[MAX_TOK_LEN
]; /* space for filename (I hope) */
859 char *end_prefix
= strrchr(flp
->filename
, '/');
863 loglog(RC_LOG_SERIOUS
, "\"%s\" line %d: unexpected end of include directive"
864 , flp
->filename
, flp
->lino
);
865 continue; /* abandon this record */
868 /* if path is relative and including file's pathname has
869 * a non-empty dirname, prefix this path with that dirname.
871 if (tok
[0] != '/' && end_prefix
!= NULL
)
873 size_t pl
= end_prefix
- flp
->filename
+ 1;
875 /* "clamp" length to prevent problems now;
876 * will be rediscovered and reported later.
880 memcpy(fn
, flp
->filename
, pl
);
883 if (flp
->cur
- tok
>= &fn
[sizeof(fn
)] - p
)
885 loglog(RC_LOG_SERIOUS
, "\"%s\" line %d: include pathname too long"
886 , flp
->filename
, flp
->lino
);
887 continue; /* abandon this record */
890 (void) shift(); /* move to Record Boundary, we hope */
891 if (flushline("ignoring malformed INCLUDE -- expected Record Boundary after filename"))
893 process_secrets_file(fn
, whackfd
);
894 tok
= NULL
; /* correct, but probably redundant */
899 /* expecting a list of indices and then the key info */
900 secret_t
*s
= alloc_thing(secret_t
, "secret");
903 s
->kind
= PPK_PSK
; /* default */
904 setchunk(s
->u
.preshared_secret
, NULL
, 0);
909 if (tok
[0] == '"' || tok
[0] == '\'')
912 process_secret(s
, whackfd
);
918 shift(); /* discard explicit separator */
919 process_secret(s
, whackfd
);
925 * See RFC2407 IPsec Domain of Interpretation 4.6.2
933 id
.kind
= ID_IPV4_ADDR
;
934 ugh
= anyaddr(AF_INET
, &id
.ip_addr
);
936 else if (tokeq("%any6"))
939 id
.kind
= ID_IPV6_ADDR
;
940 ugh
= anyaddr(AF_INET6
, &id
.ip_addr
);
944 ugh
= atoid(tok
, &id
, FALSE
);
949 loglog(RC_LOG_SERIOUS
950 , "ERROR \"%s\" line %d: index \"%s\" %s"
951 , flp
->filename
, flp
->lino
, tok
, ugh
);
955 id_list_t
*i
= alloc_thing(id_list_t
959 unshare_id_content(&i
->id
);
962 /* DBG_log("id type %d: %s %.*s", i->kind, ip_str(&i->ip_addr), (int)i->name.len, i->name.ptr); */
966 /* unexpected Record Boundary or EOF */
967 loglog(RC_LOG_SERIOUS
, "\"%s\" line %d: unexpected end of id list"
968 , flp
->filename
, flp
->lino
);
978 globugh(const char *epath
, int eerrno
)
980 log_errno_routine(eerrno
, "problem with secrets file \"%s\"", epath
);
981 return 1; /* stop glob */
985 process_secrets_file(const char *file_pat
, int whackfd
)
987 struct file_lex_position pos
;
991 pos
.depth
= flp
== NULL?
0 : flp
->depth
+ 1;
995 loglog(RC_LOG_SERIOUS
, "preshared secrets file \"%s\" nested too deeply", file_pat
);
1001 int r
= glob(file_pat
, GLOB_ERR
, globugh
, &globbuf
);
1008 loglog(RC_LOG_SERIOUS
, "out of space processing secrets filename \"%s\"", file_pat
);
1011 break; /* already logged */
1013 loglog(RC_LOG_SERIOUS
, "no secrets filename matched \"%s\"", file_pat
);
1016 loglog(RC_LOG_SERIOUS
, "unknown glob error %d", r
);
1024 /* for each file... */
1025 for (fnp
= globbuf
.gl_pathv
; *fnp
!= NULL
; fnp
++)
1027 if (lexopen(&pos
, *fnp
, FALSE
))
1029 plog("loading secrets from \"%s\"", *fnp
);
1030 (void) flushline("file starts with indentation (continuation notation)");
1031 process_secret_records(whackfd
);
1040 free_preshared_secrets(void)
1042 lock_certs_and_keys("free_preshared_secrets");
1044 if (secrets
!= NULL
)
1048 plog("forgetting secrets");
1050 for (s
= secrets
; s
!= NULL
; s
= ns
)
1054 ns
= s
->next
; /* grab before freeing s */
1055 for (i
= s
->ids
; i
!= NULL
; i
= ni
)
1057 ni
= i
->next
; /* grab before freeing i */
1058 free_id_content(&i
->id
);
1064 pfree(s
->u
.preshared_secret
.ptr
);
1067 free_RSA_private_content(&s
->u
.RSA_private_key
);
1070 pfree(s
->u
.xauth_secret
.user_name
.ptr
);
1071 pfree(s
->u
.xauth_secret
.user_password
.ptr
);
1074 scx_release(s
->u
.smartcard
);
1084 unlock_certs_and_keys("free_preshard_secrets");
1088 load_preshared_secrets(int whackfd
)
1090 free_preshared_secrets();
1091 (void) process_secrets_file(shared_secrets_file
, whackfd
);
1094 /* public key machinery
1095 * Note: caller must set dns_auth_level.
1099 public_key_from_rsa(const RSA_public_key_t
*k
)
1101 pubkey_t
*p
= alloc_thing(pubkey_t
, "pubkey");
1103 p
->id
= empty_id
; /* don't know, doesn't matter */
1104 p
->issuer
= empty_chunk
;
1105 p
->serial
= empty_chunk
;
1106 p
->alg
= PUBKEY_ALG_RSA
;
1108 memcpy(p
->u
.rsa
.keyid
, k
->keyid
, sizeof(p
->u
.rsa
.keyid
));
1110 mpz_init_set(&p
->u
.rsa
.e
, &k
->e
);
1111 mpz_init_set(&p
->u
.rsa
.n
, &k
->n
);
1113 /* note that we return a 1 reference count upon creation:
1114 * invariant: recount > 0.
1117 time(&p
->installed_time
);
1121 /* Free a public key record.
1122 * As a convenience, this returns a pointer to next.
1125 free_public_keyentry(pubkey_list_t
*p
)
1127 pubkey_list_t
*nxt
= p
->next
;
1130 unreference_key(&p
->key
);
1136 free_public_keys(pubkey_list_t
**keys
)
1138 while (*keys
!= NULL
)
1139 *keys
= free_public_keyentry(*keys
);
1142 /* root of chained public key list */
1144 pubkey_list_t
*pubkeys
= NULL
; /* keys from ipsec.conf */
1147 free_remembered_public_keys(void)
1149 free_public_keys(&pubkeys
);
1152 /* transfer public keys from *keys list to front of pubkeys list */
1154 transfer_to_public_keys(struct gw_info
*gateways_from_dns
1156 , pubkey_list_t
**keys
1157 #endif /* USE_KEYRR */
1161 struct gw_info
*gwp
;
1163 for (gwp
= gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
1165 pubkey_list_t
*pl
= alloc_thing(pubkey_list_t
, "from TXT");
1167 pl
->key
= gwp
->key
; /* note: this is a transfer */
1168 gwp
->key
= NULL
; /* really, it is! */
1176 pubkey_list_t
**pp
= keys
;
1184 #endif /* USE_KEYRR */
1187 /* decode of RSA pubkey chunk
1188 * - format specified in RFC 2537 RSA/MD5 Keys and SIGs in the DNS
1189 * - exponent length in bytes (1 or 3 octets)
1190 * + 1 byte if in [1, 255]
1191 * + otherwise 0x00 followed by 2 bytes of length
1196 unpack_RSA_public_key(RSA_public_key_t
*rsa
, const chunk_t
*pubkey
)
1201 if (pubkey
->len
< 3)
1202 return "RSA public key blob way to short"; /* not even room for length! */
1204 if (pubkey
->ptr
[0] != 0x00)
1206 setchunk(exp
, pubkey
->ptr
+ 1, pubkey
->ptr
[0]);
1210 setchunk(exp
, pubkey
->ptr
+ 3
1211 , (pubkey
->ptr
[1] << BITS_PER_BYTE
) + pubkey
->ptr
[2]);
1214 if (pubkey
->len
- (exp
.ptr
- pubkey
->ptr
) < exp
.len
+ RSA_MIN_OCTETS_RFC
)
1215 return "RSA public key blob too short";
1217 mod
.ptr
= exp
.ptr
+ exp
.len
;
1218 mod
.len
= &pubkey
->ptr
[pubkey
->len
] - mod
.ptr
;
1220 if (mod
.len
< RSA_MIN_OCTETS
)
1221 return RSA_MIN_OCTETS_UGH
;
1223 if (mod
.len
> RSA_MAX_OCTETS
)
1224 return RSA_MAX_OCTETS_UGH
;
1226 init_RSA_public_key(rsa
, exp
, mod
);
1227 rsa
->k
= mpz_sizeinbase(&rsa
->n
, 2); /* size in bits, for a start */
1228 rsa
->k
= (rsa
->k
+ BITS_PER_BYTE
- 1) / BITS_PER_BYTE
; /* now octets */
1230 RSA_show_public_key(rsa
)
1233 if (rsa
->k
!= mod
.len
)
1237 return "RSA modulus shorter than specified";
1244 install_public_key(pubkey_t
*pk
, pubkey_list_t
**head
)
1246 pubkey_list_t
*p
= alloc_thing(pubkey_list_t
, "pubkey entry");
1248 unshare_id_content(&pk
->id
);
1250 /* copy issuer dn */
1251 if (pk
->issuer
.ptr
!= NULL
)
1252 pk
->issuer
.ptr
= clone_bytes(pk
->issuer
.ptr
, pk
->issuer
.len
, "issuer dn");
1254 /* copy serial number */
1255 if (pk
->serial
.ptr
!= NULL
)
1256 pk
->serial
.ptr
= clone_bytes(pk
->serial
.ptr
, pk
->serial
.len
, "serialNumber");
1258 /* store the time the public key was installed */
1259 time(&pk
->installed_time
);
1261 /* install new key at front */
1262 p
->key
= reference_key(pk
);
1269 delete_public_keys(const struct id
*id
, enum pubkey_alg alg
1270 , chunk_t issuer
, chunk_t serial
)
1272 pubkey_list_t
**pp
, *p
;
1275 for (pp
= &pubkeys
; (p
= *pp
) != NULL
; )
1279 if (same_id(id
, &pk
->id
) && pk
->alg
== alg
1280 && (issuer
.ptr
== NULL
|| pk
->issuer
.ptr
== NULL
1281 || same_dn(issuer
, pk
->issuer
))
1282 && same_serial(serial
, pk
->serial
))
1283 *pp
= free_public_keyentry(p
);
1290 reference_key(pubkey_t
*pk
)
1297 unreference_key(pubkey_t
**pkp
)
1299 pubkey_t
*pk
= *pkp
;
1306 DBG(DBG_CONTROLMORE
,
1307 idtoa(&pk
->id
, b
, sizeof(b
));
1308 DBG_log("unreference key: %p %s cnt %d--", pk
, b
, pk
->refcnt
)
1311 /* cancel out the pointer */
1314 passert(pk
->refcnt
!= 0);
1316 if (pk
->refcnt
== 0)
1317 free_public_key(pk
);
1321 add_public_key(const struct id
*id
1322 , enum dns_auth_level dns_auth_level
1323 , enum pubkey_alg alg
1324 , const chunk_t
*key
1325 , pubkey_list_t
**head
)
1327 pubkey_t
*pk
= alloc_thing(pubkey_t
, "pubkey");
1329 /* first: algorithm-specific decoding of key chunk */
1332 case PUBKEY_ALG_RSA
:
1334 err_t ugh
= unpack_RSA_public_key(&pk
->u
.rsa
, key
);
1348 pk
->dns_auth_level
= dns_auth_level
;
1350 pk
->until_time
= UNDEFINED_TIME
;
1351 pk
->issuer
= empty_chunk
;
1352 pk
->serial
= empty_chunk
;
1354 install_public_key(pk
, head
);
1358 /* extract id and public key from x.509 certificate and
1359 * insert it into a pubkeyrec
1362 add_x509_public_key(x509cert_t
*cert
, time_t until
1363 , enum dns_auth_level dns_auth_level
)
1367 cert_t c
= { CERT_X509_SIGNATURE
, {cert
} };
1369 /* we support RSA only */
1370 if (cert
->subjectPublicKeyAlgorithm
!= PUBKEY_ALG_RSA
)
1373 /* ID type: ID_DER_ASN1_DN (X.509 subject field) */
1374 pk
= allocate_RSA_public_key(c
);
1375 pk
->id
.kind
= ID_DER_ASN1_DN
;
1376 pk
->id
.name
= cert
->subject
;
1377 pk
->dns_auth_level
= dns_auth_level
;
1378 pk
->until_time
= until
;
1379 pk
->issuer
= cert
->issuer
;
1380 pk
->serial
= cert
->serialNumber
;
1381 delete_public_keys(&pk
->id
, pk
->alg
, pk
->issuer
, pk
->serial
);
1382 install_public_key(pk
, &pubkeys
);
1384 gn
= cert
->subjectAltName
;
1386 while (gn
!= NULL
) /* insert all subjectAltNames */
1388 struct id id
= empty_id
;
1391 if (id
.kind
!= ID_NONE
)
1393 pk
= allocate_RSA_public_key(c
);
1395 pk
->dns_auth_level
= dns_auth_level
;
1396 pk
->until_time
= until
;
1397 pk
->issuer
= cert
->issuer
;
1398 pk
->serial
= cert
->serialNumber
;
1399 delete_public_keys(&pk
->id
, pk
->alg
, pk
->issuer
, pk
->serial
);
1400 install_public_key(pk
, &pubkeys
);
1406 /* extract id and public key from OpenPGP certificate and
1407 * insert it into a pubkeyrec
1410 add_pgp_public_key(pgpcert_t
*cert
, time_t until
1411 , enum dns_auth_level dns_auth_level
)
1419 /* we support RSA only */
1420 if (cert
->pubkeyAlg
!= PUBKEY_ALG_RSA
)
1422 plog(" RSA public keys supported only");
1426 pk
= allocate_RSA_public_key(c
);
1427 pk
->id
.kind
= ID_KEY_ID
;
1428 pk
->id
.name
.ptr
= cert
->fingerprint
;
1429 pk
->id
.name
.len
= PGP_FINGERPRINT_SIZE
;
1430 pk
->dns_auth_level
= dns_auth_level
;
1431 pk
->until_time
= until
;
1432 delete_public_keys(&pk
->id
, pk
->alg
, empty_chunk
, empty_chunk
);
1433 install_public_key(pk
, &pubkeys
);
1436 /* when a X.509 certificate gets revoked, all instances of
1437 * the corresponding public key must be removed
1440 remove_x509_public_key(const x509cert_t
*cert
)
1442 const cert_t c
= {CERT_X509_SIGNATURE
, {cert
}};
1443 pubkey_list_t
*p
, **pp
;
1444 pubkey_t
*revoked_pk
;
1446 revoked_pk
= allocate_RSA_public_key(c
);
1452 if (same_RSA_public_key(&p
->key
->u
.rsa
, &revoked_pk
->u
.rsa
))
1454 /* remove p from list and free memory */
1455 *pp
= free_public_keyentry(p
);
1456 loglog(RC_LOG_SERIOUS
,
1457 "invalid RSA public key deleted");
1465 free_public_key(revoked_pk
);
1469 * list all public keys in the chained list
1471 void list_public_keys(bool utc
)
1473 pubkey_list_t
*p
= pubkeys
;
1477 whack_log(RC_COMMENT
, " ");
1478 whack_log(RC_COMMENT
, "List of Public Keys:");
1479 whack_log(RC_COMMENT
, " ");
1484 pubkey_t
*key
= p
->key
;
1486 if (key
->alg
== PUBKEY_ALG_RSA
)
1489 char expires_buf
[TIMETOA_BUF
];
1491 idtoa(&key
->id
, buf
, BUF_LEN
);
1492 strcpy(expires_buf
, timetoa(&key
->until_time
, utc
));
1493 whack_log(RC_COMMENT
, "%s, %4d RSA Key %s, until %s %s",
1495 timetoa(&key
->installed_time
, utc
), 8*key
->u
.rsa
.k
, key
->u
.rsa
.keyid
,
1497 check_expiry(key
->until_time
, PUBKEY_WARNING_INTERVAL
, TRUE
));
1498 whack_log(RC_COMMENT
," %s '%s'",
1499 enum_show(&ident_names
, key
->id
.kind
), buf
);
1500 if (key
->issuer
.len
> 0)
1502 dntoa(buf
, BUF_LEN
, key
->issuer
);
1503 whack_log(RC_COMMENT
," issuer: '%s'", buf
);
1505 if (key
->serial
.len
> 0)
1507 datatot(key
->serial
.ptr
, key
->serial
.len
, ':'
1509 whack_log(RC_COMMENT
," serial: %s", buf
);