Coder Social home page Coder Social logo

opensc / pam_p11 Goto Github PK

View Code? Open in Web Editor NEW
22.0 7.0 20.0 169 KB

Authentication with PKCS#11 modules

License: GNU Lesser General Public License v2.1

Shell 0.44% C 88.05% Makefile 3.44% M4 8.07%
security pam certificate smartcard opensc pgp authentication

pam_p11's Introduction

Welcome to pam_p11

Pam_p11 is a plugable authentication module (pam) package for using crpytographic tokens such as smart cards and usb crypto tokens for authentication.

Pam_p11 uses libp11 to access any PKCS#11 module. It should be compatible with any implementation, but it is primarely developed using OpenSC.

Pam_p11 implements two authentication methods:

  • verify a token using a known public key found in OpenSSH's ~/.ssh/authorized_keys.
  • verify a token using a known certificate found in ~/.eid/authorized_certificates.

Pam_p11 is very simple, it has no config file, does not know about certificate chains, certificate authorities, revocation lists or OCSP. Perfect for the small installation with no frills.

Pam_p11 was written by an international team and is licensed as Open Source software under the LGPL license.

GitHub CI Status Coverity Scan CI Status CodeQL CI Status

Installing pam_p11

Installation is quite easy:

wget https://github.com/OpenSC/pam_p11/releases/download/pam_p11-0.6.0/pam_p11-0.6.0.tar.gz
tar xfvz pam_p11-0.6.0.tar.gz
cd pam_p11-0.6.0
./configure --prefix=/usr --libdir=/lib/
make
make install

Pam_p11 depends on pkg-config, openssl, libp11 and pam. If you don't have pkg-config installed, please do so and try again. If pkg-config is not found, please change your PATH environment setting. If openssl is not installed, please do so. If openssl is not found, please change your PKG_CONFIG_PATH environment setting to include the directory with "openssl.pc" or "libp11.pc" file. Some linux distributions split openssl into a runtime package and a development package, you need to install both. Same might be true for pam and libp11.

Using pam_p11

Login

To use pam_p11 with some application like sudo, edit /etc/pam.d/sudo and add something like the following at the beginning of the file:

auth  sufficient  /usr/local/lib/security/pam_p11.so  /usr/local/lib/opensc-pkcs11.so

Replace /usr/local/lib/opensc-pkcs11.so with your PKCS#11 implementation. Using an absolute path to pam_p11.so avoids the need to write to a system directory, which is especially useful for macOS with system integrity protection (SIP) enabled.

An optional second argument to pam_p11.so may be used to check for a specific format when prompting for the token's password. On macOS this defaults to the regular expression ^[[:digit:]]*$ to avoid confusion with the user's password in the login screen. pam_p11 uses POSIX-Extended Regular Expressions for matching.

While testing it is best to keep a door open. Editing the configuration files from a different machine via SSH helps reverting a bad PAM login configuration. Replace sufficient with required and remove other unwanted PAM modules from the file only when you've successfully verified the configuration.

To enable pam_p11 for all logins (graphical and terminal based), change the following configuration files as described above:

Operating System PAM configuration file
macOS /etc/pam.d/authorization
Debian /etc/pam.d/common-auth
Arch Linux /etc/pam.d/system-auth

PIN change and unblock

To allow changing and unblocking the PIN via pam_p11, add the following to your configuration:

password  optional    /usr/local/lib/security/pam_p11.so  /usr/local/lib/opensc-pkcs11.so

An optional second argument to pam_p11.so may be used to check for a specific format when prompting for the token's password. On macOS this defaults to the regular expression ^[[:digit:]]*$ to avoid confusion with the user's password in the login screen. pam_p11 uses POSIX-Extended Regular Expressions for matching.

User configuration via ~/.eid/authorized_certificates

A user may create a ~/.eid/ directory and create a file ~/.eid/authorized_certificates with authorized certificates. You can do that via

mkdir -p ~/.eid
chmod 0755 ~/.eid
pkcs11-tool --read-object --type cert --id 45 --module /usr/lib/opensc-pkcs11.so --output-file cert.cer
openssl x509 -inform DER -in cert.cer -outform PEM >> ~/.eid/authorized_certificates
chmod 0644 ~/.eid/authorized_certificates

This example uses the pkcs11-tool command from opensc to read a certificate (id 45) from the smart card. Use pkcs11-tool --list-objects --type cert --module /usr/lib/opensc-pkcs11.so to view all certificates available on the card.

It is very important that only the user of the file can write to it. You can have any number of certificates in that file. The certificates need to be in PEM format. DER format is not supported.

User configuration via ~/.ssh/authorized_keys

A user may create a ~/.ssh/ directory and create a file ~/.ssh/authorized_keys with authorized public keys. You can do that via

mkdir -p ~/.ssh
chmod 0755 ~/.ssh
ssh-keygen -D /usr/lib/opensc-pkcs11.so >> ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys

This example uses the ssh-keygen command from openssh to read the default user public key (id 45) from the smart card in reader 0. Note that this tool prints the public keys in two formats: ssh v1 and ssh v2 format. It is recommended to edit the file and delete one of those two lines. Also you might want to add a comment / identifier at the end of the line.

It is very important that only the user of the file can write to it. You can have any number of public keys in that file.

Note it is currently not possible to convert existing ssh keys into pem format and store them on a smart card. (To be precise: OpenSC has no such functionality, not sure about other implementations.)

Security Note

pam_p11 simply compares public keys and request the cryptographic token to sign some random data and verifiy the signature with the public key. No CA chain checking is done, no CRL is looked at, and they don't know what OCSP is. This works fine for small installations, but if you want any of those features, please have a look at Pam_pkcs11 for a fully fledged PAM module for smart card authentication.

pam_p11's People

Contributors

alonbl avatar archxlith avatar ciz avatar frankmorgner avatar jorgectf avatar ludovicrousseau avatar mikhailnov avatar milocasagrande avatar orbea avatar popovec avatar scrwnl avatar tolonuga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pam_p11's Issues

0.2.0 fails to build on Arch/gcc 8.2.1

Hello! I am having a hard time building latest version:

make  all-recursive
make[1]: Entering directory '/tmp/pam_p11/pam_p11-0.2.0'
Making all in po
make[2]: Entering directory '/tmp/pam_p11/pam_p11-0.2.0/po'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/tmp/pam_p11/pam_p11-0.2.0/po'
Making all in src
make[2]: Entering directory '/tmp/pam_p11/pam_p11-0.2.0/src'
/bin/sh ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..       -DLOCALEDIR=\"/usr/share/locale\" -g -O2 -Wall -Wextra -Wno-unused-parameter -Werror -MT pam_p11.lo -MD -MP -MF .deps/pam_p11.Tpo -c -o pam_p11.lo pam_p11.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -DLOCALEDIR=\"/usr/share/locale\" -g -O2 -Wall -Wextra -Wno-unused-parameter -Werror -MT pam_p11.lo -MD -MP -MF .deps/pam_p11.Tpo -c pam_p11.c  -fPIC -DPIC -o .libs/pam_p11.o
pam_p11.c: In function ‘key_verify’:
pam_p11.c:593:8: error: implicit declaration of function ‘EVP_MD_CTX_cleanup’; did you mean ‘EVP_MD_CTX_create’? [-Werror=implicit-function-declaration]
    || !EVP_MD_CTX_cleanup(md_ctx)
        ^~~~~~~~~~~~~~~~~~
        EVP_MD_CTX_create
cc1: all warnings being treated as errors
make[2]: *** [Makefile:519: pam_p11.lo] Error 1
make[2]: Leaving directory '/tmp/pam_p11/pam_p11-0.2.0/src'
make[1]: *** [Makefile:496: all-recursive] Error 1
make[1]: Leaving directory '/tmp/pam_p11/pam_p11-0.2.0'
make: *** [Makefile:407: all] Error 2

Unexpected behaviour - NitroKey Storage 2

Hello! I have some problems logging in with 0.3.0 using Nitrokey Storage 2
Trying to use public key with ssh-keygen. It produces 3 public keys in authorized_keys
If I use the first one logs give me:

pam_p11(gdm-password:auth): Error verifying key: Key type inconsistent

If I comment it out and try any of the other two it just dumps core.

Using '/usr/lib/opensc-pkcs11.so' for 'unb0rn'
Login with User PIN (OpenPGP card):
*** stack smashing detected ***: terminated
zsh: abort (core dumped) ./test-login

           PID: 4677 (test-login)
           UID: 1000 (unb0rn)
           GID: 1000 (unb0rn)
        Signal: 6 (ABRT)
     Timestamp: Thu 2019-04-25 19:17:30 +04 (9s ago)
  Command Line: ./test-login
    Executable: /tmp/pam_p11-0.3.0/src/test-login
 Control Group: /user.slice/user-1000.slice/[email protected]/gnome-terminal-server.service
          Unit: [email protected]
     User Unit: gnome-terminal-server.service
         Slice: user-1000.slice
     Owner UID: 1000 (unb0rn)
       Boot ID: 7020b2cb4ac84eab970cf08aeaecc5f6
    Machine ID: 2eacdec89cd848c490e2abbe30d129ae
      Hostname: SpectreArch
       Storage: /var/lib/systemd/coredump/core.test-login.1000.7020b2cb4ac84eab970cf08aeaecc5f6.4677.1556205450000000.lz4
       Message: Process 4677 (test-login) of user 1000 dumped core.
                
                Stack trace of thread 4677:
                #0  0x00007f479a71982f raise (libc.so.6)
                #1  0x00007f479a704672 abort (libc.so.6)
                #2  0x00007f479a75be78 __libc_message (libc.so.6)
                #3  0x00007f479a7ed855 __GI___fortify_fail_abort (libc.so.6)
                #4  0x00007f479a7ed806 __stack_chk_fail (libc.so.6)
                #5  0x0000560b789ba7f9 n/a (/tmp/pam_p11-0.3.0/src/test-login)

GNU gdb (GDB) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /tmp/pam_p11-0.3.0/src/test-login...done.
[New LWP 4677]
[New LWP 4678]
[New LWP 4679]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Core was generated by `./test-login'.
Program terminated with signal SIGABRT, Aborted.
#0  0x00007f479a71982f in raise () from /usr/lib/libc.so.6
[Current thread is 1 (Thread 0x7f479a6b7740 (LWP 4677))]
(gdb) bt
#0  0x00007f479a71982f in raise () at /usr/lib/libc.so.6
#1  0x00007f479a704672 in abort () at /usr/lib/libc.so.6
#2  0x00007f479a75be78 in __libc_message () at /usr/lib/libc.so.6
#3  0x00007f479a7ed855 in __fortify_fail_abort () at /usr/lib/libc.so.6
#4  0x00007f479a7ed806 in  () at /usr/lib/libc.so.6
#5  0x0000560b789ba7f9 in pam_sm_authenticate (pamh=<optimized out>, flags=0, argc=<optimized out>, argv=<optimized out>) at pam_p11.c:665
#6  0xe36cdf2ed3ccbfc6 in  ()
#7  0xf8dbcaa9fff8b57d in  ()
#8  0xd73bbb504781d487 in  ()
#9  0xd5bea48f19ec4726 in  ()
#10 0xae74d20de51d30e7 in  ()
#11 0xb7b4c35c05ccc703 in  ()
#12 0xf4584cf71db4d292 in  ()
#13 0x14cab181194e530c in  ()
#14 0x8993926d05168fa2 in  ()
#15 0x93e7e75284ce0ad7 in  ()
#16 0x17656a4207b04621 in  ()
#17 0x97b3beec643994e5 in  ()
#18 0x2175943e473411e9 in  ()
#19 0x1a268958e2d8d86b in  ()
#20 0x34f7de02c986676b in  ()
#21 0xe824a82180ed2c8c in  ()
#22 0x57647edbe3dbeb01 in  ()
#23 0x851c13c566960de5 in  ()
#24 0x262a97728f993886 in  ()
#25 0x9853aa2950bee935 in  ()
#26 0x9dbd47f0c866e94b in  ()
#27 0x953263341d0f42c9 in  ()
#28 0x27198c399d766565 in  ()
#29 0x0000000000002000 in  ()
#30 0x00007ffe00000003 in  ()
#31 0x0000000000000002 in  ()
#32 0x00007f479afd5d86 in _dl_map_object_from_fd () at /lib64/ld-linux-x86-64.so.2

The keys on the token were created with gpg . All three of them are rsa-4096. Just in case, I have no problem logging in with this token using openssh/pkcs11

libpam_p11 wrong for small key

Hi

I have problem with libpam_p11. For 1024 RSA key i found syslog message:

pam_p11_openssh(login:auth): fatal: pkcs11_sign failed

It seems the problem is in too long random string for RSA sign.

in src/pam_p11.c is defined random string of 128 bytes and this string is then
submitted to PKCS11_sign() from lib_p11.

PKCS11_sign() encloses padding data to this random string and then run checks, if this can be signed by RSA key. But this must fail, because RSA key of 1024 bites can sign only string below 128 bytes.

I suggest change:

src/pam_p11.c:

--- src/pam_p11.c.orig 2015-09-09 10:33:23.691725177 +0200
+++ src/pam_p11.c 2015-09-09 10:33:32.623722890 +0200
@@ -44,7 +44,7 @@
#define LOGNAME "pam_p11" /* name for log-file entries */

#define RANDOM_SOURCE "/dev/urandom"
-#define RANDOM_SIZE 128
+#define RANDOM_SIZE 20
#define MAX_SIGSIZE 256

extern int match_user(X509 * x509, const char *login);

same value (20) can be found in similar example code from lib_p11:

libp11-0.2.8/examples/auth.c:

define RANDOM_SOURCE "/dev/urandom"

define RANDOM_SIZE 20

define MAX_SIGSIZE 256

Thanks

Build against openssl-3 fails:

This was reported downstream at https://bugs.gentoo.org/805929

libtool: compile:  i686-pc-linux-gnu-gcc -DHAVE_CONFIG_H -I. -I.. -DLOCALEDIR=\"/usr/share/locale\" -march=native -O2 -pipe -Wall -Wextra -Wno-unused-parameter -Werror -c match_openssh.c  -fPIC -DPIC -o .libs/match_openssh.o   [181/1495]
match_openssh.c: In function ‘ssh1_line_to_key’:
match_openssh.c:71:9: error: ‘RSA_new’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
   71 |         rsa = RSA_new();
      |         ^~~
In file included from /usr/include/openssl/x509.h:36,
                 from match_openssh.c:11:
/usr/include/openssl/rsa.h:201:28: note: declared here
  201 | OSSL_DEPRECATEDIN_3_0 RSA *RSA_new(void);
      |                            ^~~~~~~
match_openssh.c:142:9: error: ‘RSA_set0_key’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  142 |         if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL))
      |         ^~
In file included from /usr/include/openssl/x509.h:36,
                 from match_openssh.c:11:
/usr/include/openssl/rsa.h:207:27: note: declared here
  207 | OSSL_DEPRECATEDIN_3_0 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
      |                           ^~~~~~~~~~~~
match_openssh.c:145:9: error: ‘EVP_PKEY_assign’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  145 |         EVP_PKEY_assign_RSA(key, rsa);
      |         ^~~~~~~~~~~~~~~~~~~
In file included from match_openssh.c:9:
/usr/include/openssl/evp.h:1328:5: note: declared here
 1328 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
      |     ^~~~~~~~~~~~~~~
match_openssh.c: In function ‘ssh2_line_to_key’:
match_openssh.c:232:9: error: ‘RSA_new’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  232 |         rsa = RSA_new();
      |         ^~~
In file included from /usr/include/openssl/x509.h:36,
                 from match_openssh.c:11:
/usr/include/openssl/rsa.h:201:28: note: declared here
  201 | OSSL_DEPRECATEDIN_3_0 RSA *RSA_new(void);
      |                            ^~~~~~~
match_openssh.c:235:9: error: ‘RSA_set0_key’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  235 |         if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL)) {
      |         ^~
In file included from /usr/include/openssl/x509.h:36,
                 from match_openssh.c:11:
/usr/include/openssl/rsa.h:207:27: note: declared here
  207 | OSSL_DEPRECATEDIN_3_0 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
      |                           ^~~~~~~~~~~~
match_openssh.c:237:17: error: ‘RSA_free’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  237 |                 RSA_free(rsa);
      |                 ^~~~~~~~
In file included from /usr/include/openssl/x509.h:36,
                 from match_openssh.c:11:
/usr/include/openssl/rsa.h:293:28: note: declared here
  293 | OSSL_DEPRECATEDIN_3_0 void RSA_free(RSA *r);
      |                            ^~~~~~~~
match_openssh.c:241:9: error: ‘EVP_PKEY_assign’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  241 |         EVP_PKEY_assign_RSA(key, rsa);
      |         ^~~~~~~~~~~~~~~~~~~
In file included from match_openssh.c:9:
/usr/include/openssl/evp.h:1328:5: note: declared here
 1328 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
      |     ^~~~~~~~~~~~~~~
match_openssh.c: In function ‘ssh_nistp_line_to_key’:
match_openssh.c:338:9: error: ‘EC_KEY_new_by_curve_name’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  338 |         ec_key = EC_KEY_new_by_curve_name(nid);
      |         ^~~~~~
In file included from /usr/include/openssl/x509.h:33,
                 from match_openssh.c:11:
/usr/include/openssl/ec.h:996:31: note: declared here
  996 | OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new_by_curve_name(int nid);
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~
match_openssh.c:346:9: error: ‘EC_KEY_set_public_key_affine_coordinates’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  346 |         if (!EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) {
      |         ^~
In file included from /usr/include/openssl/x509.h:33,
                 from match_openssh.c:11:
/usr/include/openssl/ec.h:1120:27: note: declared here                                                                                                                                                                             [110/1495]
 1120 | OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
match_openssh.c:347:17: error: ‘EC_KEY_free’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  347 |                 EC_KEY_free(ec_key);
      |                 ^~~~~~~~~~~
In file included from /usr/include/openssl/x509.h:33,
                 from match_openssh.c:11:
/usr/include/openssl/ec.h:1001:28: note: declared here
 1001 | OSSL_DEPRECATEDIN_3_0 void EC_KEY_free(EC_KEY *key);
      |                            ^~~~~~~~~~~
match_openssh.c:354:9: error: ‘EVP_PKEY_assign’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  354 |         EVP_PKEY_assign_EC_KEY(key, ec_key);
      |         ^~~~~~~~~~~~~~~~~~~~~~
In file included from match_openssh.c:9:
/usr/include/openssl/evp.h:1328:5: note: declared here
 1328 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
      |     ^~~~~~~~~~~~~~~
match_openssh.c: In function ‘match_user_openssh’:
match_openssh.c:403:17: error: ‘EVP_PKEY_cmp’ is deprecated: Since OpenSSL 3.0 [-Werror=deprecated-declarations]
  403 |                 if (1 == EVP_PKEY_cmp(authkey, key)) {
      |                 ^~
In file included from match_openssh.c:9:
/usr/include/openssl/evp.h:1418:5: note: declared here
 1418 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b);
      |     ^~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:523: match_openssh.lo] Error 1
make[2]: Leaving directory '/var/tmp/portage/sys-auth/pam_p11-0.3.1/work/pam_p11-0.3.1/src'
make[1]: *** [Makefile:496: all-recursive] Error 1
make[1]: Leaving directory '/var/tmp/portage/sys-auth/pam_p11-0.3.1/work/pam_p11-0.3.1'
make: *** [Makefile:407: all] Error 2

Cannot build : dereferencing pointer to incomplete type 'EVP_PKEY {aka struct evp_pkey_st}'

> make
make all-recursive
make[1]: Entering directory '/home/valkheim/projects/pam_p11'
Making all in src
make[2]: Entering directory '/home/valkheim/projects/pam_p11/src'
/bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT pam_p11.lo -MD -MP -MF .deps/pam_p11.Tpo -c -o pam_p11.lo pam_p11.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT pam_p11.lo -MD -MP -MF .deps/pam_p11.Tpo -c pam_p11.c -fPIC -DPIC -o .libs/pam_p11.o
pam_p11.c: In function 'pam_sm_authenticate':
pam_p11.c:254:2: warning: 'PKCS11_sign' is deprecated: This function will be removed in libp11 0.5.0 [-Wdeprecated-declarations]
rv = PKCS11_sign(NID_sha1, rand_bytes, RANDOM_SIZE, signature, &siglen,
^~
In file included from pam_p11.c:30:0:
/usr/include/libp11.h:449:32: note: declared here
P11_DEPRECATED_FUNC extern int PKCS11_sign(int type,
^~~~~~~~~~~
pam_p11.c:272:29: error: dereferencing pointer to incomplete type 'EVP_PKEY {aka struct evp_pkey_st}'
signature, siglen, pubkey->pkey.rsa);
^~
make[2]: *** [Makefile:445: pam_p11.lo] Error 1
make[2]: Leaving directory '/home/valkheim/projects/pam_p11/src'
make[1]: *** [Makefile:474: all-recursive] Error 1
make[1]: Leaving directory '/home/valkheim/projects/pam_p11'
make: *** [Makefile:385: all] Error 2

pam_p11 crashes with segmentation fault

Greetings,

I'm trying to get my Nitrokey Pro 2 and pam_p11 to work on NixOS 19.03. I got it to detect and query the hardware, however when authenticating, after I confirmed my User PIN, pam_p11 crashes with a segmentation fault.

I'm on current master: d150b60, but it also crashes on the latest release (0.3.0). In fact I hoped above commit could fix this.

Here's a run of test-login:

(gdb) run
Starting program: /home/nixos/.config/nixpkgs/custom/pkgs/pam_p11/source/src/test-login /run/current-system/sw/lib/opensc-pkcs11.so
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libthread_db.so.1".
Using '/run/current-system/sw/lib/opensc-pkcs11.so' for 'nixos'
Login with User PIN (OpenPGP card): 

Program received signal SIGSEGV, Segmentation fault.
RSA_size (r=0x1) at rsa_crpt.c:72
72	rsa_crpt.c: No such file or directory.

(gdb) bt
#0  RSA_size (r=0x1) at rsa_crpt.c:72
#1  0x00007ffff7d7d074 in pkcs11_get_key_size (key=key@entry=0x46cf50) at p11_rsa.c:330
#2  0x00007ffff7d7d0cf in pkcs11_private_encrypt (flen=flen@entry=35, 
    from=from@entry=0x46d080 "0!0\t\006\005+\016\003\002\032\005", to=to@entry=0x46cab0 "", 
    key=key@entry=0x46cf50, padding=padding@entry=1) at p11_rsa.c:89
#3  0x00007ffff7d80598 in PKCS11_private_encrypt (flen=flen@entry=35, 
    from=from@entry=0x46d080 "0!0\t\006\005+\016\003\002\032\005", to=to@entry=0x46cab0 "", 
    key=key@entry=0x46cf50, padding=padding@entry=1) at p11_front.c:327
#4  0x00007ffff7d7cf53 in pkcs11_rsa_priv_enc_method (flen=35, 
    from=0x46d080 "0!0\t\006\005+\016\003\002\032\005", to=0x46cab0 "", rsa=0x46ca00, padding=1)
    at p11_rsa.c:374
#5  0x00007ffff7e8990f in RSA_private_encrypt (flen=flen@entry=35, 
    from=from@entry=0x46d080 "0!0\t\006\005+\016\003\002\032\005", to=to@entry=0x46cab0 "", 
    rsa=rsa@entry=0x46ca00, padding=padding@entry=1) at rsa_crpt.c:98
#6  0x00007ffff7e83586 in RSA_sign (type=64, 
    m=m@entry=0x7fffffffbc60 "&\200\376\272{\035\375\316{ɖ\364jV\352\320M\024\251\337\377\177", 
    m_len=m_len@entry=20, sigret=sigret@entry=0x46cab0 "", siglen=siglen@entry=0x7fffffffbba4, 
    rsa=rsa@entry=0x46ca00) at rsa_sign.c:135
#7  0x00007ffff7e895cf in pkey_rsa_sign (ctx=0x468c40, sig=0x46cab0 "", siglen=0x7fffffffbc28, 
    tbs=0x7fffffffbc60 "&\200\376\272{\035\375\316{ɖ\364jV\352\320M\024\251\337\377\177", 
    tbslen=20) at rsa_pmeth.c:269
#8  0x00007ffff7eb5258 in EVP_PKEY_sign (ctx=ctx@entry=0x468c40, sig=sig@entry=0x46cab0 "", 
    siglen=siglen@entry=0x7fffffffbc28, 
    tbs=tbs@entry=0x7fffffffbc60 "&\200\376\272{\035\375\316{ɖ\364jV\352\320M\024\251\337\377\177", tbslen=20) at pmeth_fn.c:116
#9  0x00007ffff7eaf630 in EVP_SignFinal (ctx=ctx@entry=0x469cb0, sigret=sigret@entry=0x46cab0 "", 
    siglen=siglen@entry=0x7fffffffbcfc, pkey=pkey@entry=0x46c290) at p_sign.c:104
#10 0x00000000004043ac in key_verify (pamh=pamh@entry=0x40a6a0, flags=flags@entry=0, 
    authkey=<optimized out>) at pam_p11.c:667
#11 0x0000000000404db1 in pam_sm_authenticate (pamh=pamh@entry=0x40a6a0, flags=flags@entry=0, 
    argc=argc@entry=1, argv=argv@entry=0x7fffffffbe28) at pam_p11.c:714
#12 0x000000000040391a in pam_sm_test (pamh=0x40a6a0, flags=flags@entry=0, argc=argc@entry=1, 
    argv=argv@entry=0x7fffffffbe28) at login.c:31
#13 0x00000000004038cf in main (argc=<optimized out>, argv=<optimized out>) at test.c:88

Could this have anything to do with this comment mentioning that keys need to eligible for signing?
I generated a GPG key using the instructions here and added a (self-signed) certificate like this.

Any help with debugging this is appreciated.

Fails to build on debian unstable

make[3]: Entering directory '/build/pam-p11-0.2.0/src'
/bin/bash ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..   -
Wdate-time -D_FORTIFY_SOURCE=2    -DLOCALEDIR=\"/usr/share/locale\" -g -O2 -fdeb
ug-prefix-map=/build/pam-p11-0.2.0=. -fstack-protector-strong -Wformat -Werror=f
ormat-security -Wall -Wextra -Wno-unused-parameter -Werror -c -o pam_p11.lo pam_
p11.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -Wdate-time -D_FORTIFY_SOURCE=2
-DLOCALEDIR=\"/usr/share/locale\" -g -O2 -fdebug-prefix-map=/build/pam-p11-0.2.0
=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wextra -Wno-
unused-parameter -Werror -c pam_p11.c  -fPIC -DPIC -o .libs/pam_p11.o
pam_p11.c: In function 'key_verify':
pam_p11.c:593:8: error: implicit declaration of function 'EVP_MD_CTX_cleanup'; d
id you mean 'EVP_MD_CTX_create'? [-Werror=implicit-function-declaration]
    || !EVP_MD_CTX_cleanup(md_ctx)
        ^~~~~~~~~~~~~~~~~~
        EVP_MD_CTX_create
cc1: all warnings being treated as errors
make[3]: *** [Makefile:523: pam_p11.lo] Error 1
make[3]: Leaving directory '/build/pam-p11-0.2.0/src'
make[3]: *** Waiting for unfinished jobs....
make[3]: Entering directory '/build/pam-p11-0.2.0/src'
/bin/bash ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..   -
Wdate-time -D_FORTIFY_SOURCE=2    -DLOCALEDIR=\"/usr/share/locale\" -g -O2 -fdeb
ug-prefix-map=/build/pam-p11-0.2.0=. -fstack-protector-strong -Wformat -Werror=f
ormat-security -Wall -Wextra -Wno-unused-parameter -Werror -c -o match_openssh.l
o match_openssh.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -Wdate-time -D_FORTIFY_SOURCE=2
-DLOCALEDIR=\"/usr/share/locale\" -g -O2 -fdebug-prefix-map=/build/pam-p11-0.2.0
=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wextra -Wno-
unused-parameter -Werror -c match_openssh.c  -fPIC -DPIC -o .libs/match_openssh.
o
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -Wdate-time -D_FORTIFY_SOURCE=2
-DLOCALEDIR=\"/usr/share/locale\" -g -O2 -fdebug-prefix-map=/build/pam-p11-0.2.0
=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wextra -Wno-
unused-parameter -Werror -c match_openssh.c -o match_openssh.o >/dev/null 2>&1
make[3]: Leaving directory '/build/pam-p11-0.2.0/src'
make[2]: *** [Makefile:496: all-recursive] Error 1
make[1]: *** [Makefile:407: all] Error 2
dh_auto_build: make -j2 -Oline returned exit code 2
make: *** [debian/rules:6: build] Error 25

Authenticate with Smartcard-HSM 4K (Nitrokey HSM v2) doesn't work

Hello, I have successfully installed pam_p11 on my Ubuntu 16.04 machine.

However, when I tried to use this pam module with my Nitrokey HSM v2, I was not able to successfully authenticate.

I've added the proper configuration in /etc/pam.d/sudo and ~/.ssh/authorized_keys. I can see that the HSM is being queried (red led on). Also the logs (opensc debug-Level set to 9) show, that the smartcard is recognized and also my SSH key on the HSM is found and identified as pin protected.

But after that, I'm not asked for the USER-PIN, but instead pam_p11 (pam_p11_openssh.so) quits and I'm asked for my superuser password.

Any ideas what could be wrong?

authenticating with rutoken ecp – unexpected behaviour

Using this configuration,

auth required pam_env.so
auth sufficient pam_p11_opensc.so /usr/lib/opensc-pkcs11.so
auth required pam_unix.so try_first_pass likeauth nullok
auth optional pam_permit.so

I've noticed that slimlock (X screen locker from https://sourceforge.net/projects/slim.berlios/) behave differently if token was not inserted when slimlock started – I can authenticate with token pin only on second try. Looking inside slimlock code I found that pam_start is called once but pam_authenticate – inside a loop until proper requisites are given.

Tried to investigate further I took this (http://atlee.ca/software/pam/pam.py.html) python module and altered to get same loop logic. Here's test results:

test 1. token inserted before running script:
password asked, I can enter either user's password or token pin, auth is successful on first iteration

test 2. token inserted after script launched:
password asked, entering pin for first time – auth failed, second – passed. In syslog I see:

unix_chkpwd[21804]: password check failed for user (beelze)
python2: pam_unix(login:auth): authentication failure; logname=beelze uid=1000 euid=1000 tty= ruser= rhost= user=beelze

seems that first iteration passed without pam_p11_opensc.so interaction…

test 3. token inserted before launching script but reinserted before entering password:
both first and second tries are failed, third was successful. syslog:

python2: pam_p11_opensc(login:auth): PKCS11_login failed
unix_chkpwd[24143]: password check failed for user (beelze)
python2: pam_unix(login:auth): authentication failure; logname=beelze uid=1000 euid=1000 tty= ruser= rhost= user=beelze

why PKCS11_login was failed? I don't understand – at this moment token was inserted again…

I'm not sure there is a bug in pam_p11 – maybe pam logic is broken ot even (though unlikely) rutoken problem.

Update README.md

wget link refers to an outdated version

wget https://github.com/OpenSC/pam_p11/releases/download/pam_p11-0.3.1/pam_p11-0.3.1.tar.gz

base64.c fails to build with gcc 12 on certain arches: maybe-uninitialized warning

When building pam-p11 0.3.1 with gcc-12 on ubuntu kinetic in s390x, ppc64el or riscv64, and with -Wall -Werror, the build fails in this step:

gcc -DHAVE_CONFIG_H -I. -I..   -Wdate-time -D_FORTIFY_SOURCE=2    -DLOCALEDIR=\"/usr/share/locale\" -DLIBDIR=\"/lib/riscv64-linux-gnu\" -DTEST -g -O2 -ffile-prefix-map=/<<PKGBUILDDIR>>=. -fstack-protector-strong -Wformat -Werror=format-security -Wno-error=deprecated-declarations -Wall -Wextra -Wno-unused-parameter -Werror -c -o test_passwd-pam_p11.o `test -f 'pam_p11.c' || echo './'`pam_p11.c
base64.c: In function ‘sc_base64_decode’:
base64.c:89:36: error: ‘i’ may be used uninitialized [-Werror=maybe-uninitialized]
   89 |                         *out++ = i >> s;
      |                                  ~~^~~~
base64.c:79:22: note: ‘i’ was declared here
   79 |         unsigned int i;
      |                      ^
base64.c:94:20: error: ‘skip’ may be used uninitialized [-Werror=maybe-uninitialized]
   94 |                 in += skip;
      |                    ^~
base64.c:78:25: note: ‘skip’ was declared here
   78 |         int len = 0, r, skip;
      |                         ^~~~

On PPC64el, gcc is being invoked with -O3, whereas in the other arches it's -O2. Experimenting with this flag showed that on ppc64el the build passes if -O1 is used, but fails with -O2 or -O3 (other values not tested). On s390x it fails with -O2, and works with -O1. On amd64 a -O combination that makes it fail was not found.

With gcc-11, these builds passed. I didn't see an obvious -Wmaybe-uninitialized behavior change in gcc-12's release notes at https://gcc.gnu.org/gcc-12/changes.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.