├── .github ├── build.sh ├── ci-test.sh ├── setup-libressl.sh └── workflows │ ├── ci.yml │ ├── codeql.yml │ └── coverity.yml ├── .gitignore ├── COPYING ├── Makefile.am ├── NEWS ├── README.md ├── bootstrap ├── configure.ac ├── m4 └── .keep ├── po ├── Makevars ├── POTFILES.in ├── de.po ├── it.po ├── ja.po ├── pam_p11.pot └── ru.po └── src ├── Makefile.am ├── base64.c ├── login.c ├── match_opensc.c ├── match_openssh.c ├── pam_p11.c ├── pam_p11.exports ├── passwd.c └── test.c /.github/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # CI script to build for "ubuntu", "coverity", "macos" 4 | 5 | set -ex -o xtrace 6 | 7 | DEPS="gettext automake" 8 | 9 | case "$1" in 10 | ubuntu|coverity) 11 | DEPS="$DEPS autopoint libp11-dev libssl-dev libpam0g-dev" 12 | ;; 13 | macos) 14 | DEPS="$DEPS libp11 openssl" 15 | ;; 16 | esac 17 | 18 | case "$1" in 19 | ubuntu|coverity|mingw-32|mingw-64) 20 | sudo apt-get install -y $DEPS 21 | ;; 22 | macos) 23 | brew install $DEPS 24 | export LDFLAGS="-L$(brew --prefix gettext)/lib -lintl" 25 | export OPENSSL_CFLAGS="-I$(brew --prefix openssl)/include" 26 | export OPENSSL_LIBS="-L$(brew --prefix openssl)/lib -lcrypto" 27 | ;; 28 | esac 29 | 30 | autoreconf -vis 31 | ./configure 32 | 33 | case "$1" in 34 | ubuntu|macos) 35 | make distcheck 36 | ;; 37 | esac 38 | -------------------------------------------------------------------------------- /.github/ci-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | SOPIN="12345678" 4 | PIN="123456" 5 | 6 | make 7 | 8 | P11LIB=/usr/lib/softhsm/libsofthsm2.so 9 | 10 | echo "directories.tokendir = .tokens/" > .softhsm2.conf 11 | export SOFTHSM2_CONF=".softhsm2.conf" 12 | 13 | echo "" 14 | echo "Testing login using .ssh/authorized_keys" 15 | echo "----------------------------------------" 16 | mkdir -p $HOME/.ssh 17 | for KEY in RSA:2048 EC:secp256r1 EC:secp384r1 EC:secp521r1; do 18 | rm -rf .tokens 19 | mkdir .tokens 20 | 21 | softhsm2-util --init-token --slot 0 --label "SC test" --so-pin="$SOPIN" --pin="$PIN" 22 | 23 | # ubuntu 20.04, ssh-keygen is unable to extract EC key if --id is not specified 24 | # 25 | # ssh-keygen -D /usr/lib/softhsm/libsofthsm2.so 26 | # xmalloc: zero size 27 | 28 | pkcs11-tool --module="$P11LIB" --keypairgen --key-type=$KEY --label "${KEY}" --login --pin=$PIN --id 01 29 | 30 | ssh-keygen -D $P11LIB >> $HOME/.ssh/authorized_keys 31 | echo $PIN | sudo -E src/test-login $P11LIB $(whoami) 32 | done 33 | rm $HOME/.ssh/authorized_keys 34 | 35 | echo "" 36 | echo "Testing login Using .eid/authorized_certificates" 37 | echo "------------------------------------------------" 38 | mkdir -p $HOME/.eid 39 | for KEY in 2048 prime256v1 secp384r1 secp521r1; do 40 | 41 | if [ $[$KEY + 0 ] != 0 ]; then 42 | openssl genrsa -out user.key $KEY 43 | openssl rsa -in user.key -pubout -outform DER -out user.pub.der 2>/dev/null 44 | openssl rsa -in user.key -outform DER -out user.key.der 2>/dev/null 45 | else 46 | openssl ecparam -out user.key -name $KEY -genkey 47 | openssl ec -in user.key -pubout -outform DER -out user.pub.der 2>/dev/null 48 | openssl ec -in user.key -outform DER -out user.key.der 2>/dev/null 49 | fi 50 | openssl req -new -nodes -key user.key -outform pem -out user.csr -sha256 -subj "/C=EX/CN=example.com" >/dev/null 51 | openssl x509 -signkey user.key -in user.csr -req -days 365 -out user.crt 2>/dev/null 52 | cp user.crt $HOME/.eid/authorized_certificates 53 | 54 | echo "Using softhsm (key + public key)" 55 | echo "................................" 56 | rm -rf .tokens 57 | mkdir .tokens 58 | softhsm2-util --init-token --slot 0 --label "SC test" --so-pin="$SOPIN" --pin="$PIN" 59 | pkcs11-tool --module="$P11LIB" --write-object user.key.der --type privkey --label "${KEY}" --login --pin=$PIN --id 01 2>/dev/null 60 | pkcs11-tool --module="$P11LIB" --write-object user.pub.der --type pubkey --label "${KEY}" --login --pin=$PIN --id 01 >/dev/null 2>/dev/null 61 | echo $PIN |src/test-login $P11LIB $(whoami) 62 | 63 | echo "Using softhsm (key + certificate)" 64 | echo "................................." 65 | rm -rf .tokens 66 | mkdir .tokens 67 | softhsm2-util --init-token --slot 0 --label "SC test" --so-pin="$SOPIN" --pin="$PIN" 68 | pkcs11-tool --module="$P11LIB" --write-object user.key.der --type privkey --label "${KEY}" --login --pin=$PIN --id 01 2>/dev/null 69 | pkcs11-tool --module="$P11LIB" --write-object user.crt --type cert --label "${KEY}" --login --pin=$PIN --id 01 >/dev/null 2>/dev/null 70 | 71 | echo $PIN |src/test-login $P11LIB $(whoami) 72 | 73 | done 74 | rm $HOME/.eid/authorized_certificates 75 | 76 | rm user.key user.pub.der user.key.der user.csr user.crt 77 | rmdir $HOME/.eid/ 78 | 79 | rm -rf .tokens 80 | rm .softhsm2.conf 81 | -------------------------------------------------------------------------------- /.github/setup-libressl.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -ex -o xtrace 4 | 5 | V=libressl-3.6.1 6 | 7 | # keep libp11-dev installed! 8 | sudo dpkg --purge --force-depends libssl-dev 9 | 10 | if [ ! -d "$V" ]; then 11 | # letsencrypt CA does not seem to be included in CI runner 12 | wget --no-check-certificate https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/$V.tar.gz 13 | tar xzf $V.tar.gz 14 | fi 15 | pushd $V 16 | ./configure --prefix=/usr/local 17 | make -j $(nproc) 18 | sudo make install 19 | popd 20 | 21 | # update dynamic linker to find the libraries in non-standard path 22 | echo "/usr/local/lib64" | sudo tee /etc/ld.so.conf.d/openssl.conf 23 | sudo ldconfig 24 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - '**.c' 7 | - '**.h' 8 | - '**.in' 9 | - '**.po' 10 | - .github/workflows/ci.yml 11 | - .github/ci-test.sh 12 | push: 13 | 14 | 15 | jobs: 16 | macos: 17 | runs-on: macos-11 18 | steps: 19 | - uses: actions/checkout@v3 20 | - run: .github/build.sh macos 21 | 22 | ubuntu: 23 | runs-on: ubuntu-20.04 24 | steps: 25 | - uses: actions/checkout@v3 26 | - run: .github/build.sh ubuntu 27 | - uses: actions/upload-artifact@v3 28 | with: 29 | name: pam_p11 30 | path: 31 | pam_p11*.tar.gz 32 | - run: sudo apt install softhsm2 opensc 33 | - run: .github/ci-test.sh 34 | 35 | ubuntu-22: 36 | runs-on: ubuntu-22.04 37 | steps: 38 | - uses: actions/checkout@v3 39 | - run: .github/build.sh ubuntu 40 | - run: sudo apt install softhsm2 opensc 41 | - name: upgrading broken libp11 (using libp11 from lunar) 42 | run: | 43 | echo "deb http://us.archive.ubuntu.com/ubuntu/ lunar main"|sudo tee -a /etc/apt/sources.list 44 | sudo apt update 45 | sudo apt install libp11-3 libp11-dev 46 | - run: .github/ci-test.sh 47 | 48 | ubuntu-22-libressl: 49 | runs-on: ubuntu-22.04 50 | steps: 51 | - uses: actions/checkout@v3 52 | - run: .github/build.sh ubuntu 53 | - run: sudo apt install softhsm2 opensc 54 | - name: upgrading broken libp11 (using libp11 from lunar) 55 | run: | 56 | echo "deb http://us.archive.ubuntu.com/ubuntu/ lunar main"|sudo tee -a /etc/apt/sources.list 57 | sudo apt update 58 | sudo apt install libp11-3 libp11-dev 59 | - run: .github/setup-libressl.sh 60 | - run: .github/ci-test.sh 61 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ 'master' ] 6 | pull_request: 7 | # The branches below must be a subset of the branches above 8 | branches: [ 'master' ] 9 | schedule: 10 | - cron: '53 15 * * 1' 11 | 12 | jobs: 13 | analyze: 14 | name: Analyze 15 | runs-on: ubuntu-latest 16 | permissions: 17 | actions: read 18 | contents: read 19 | security-events: write 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | language: [ 'cpp' ] 25 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 26 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 27 | 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v3 31 | 32 | # Initializes the CodeQL tools for scanning. 33 | - name: Initialize CodeQL 34 | uses: github/codeql-action/init@v2 35 | with: 36 | languages: ${{ matrix.language }} 37 | # If you wish to specify custom queries, you can do so here or in a config file. 38 | # By default, queries listed here will override any specified in a config file. 39 | # Prefix the list here with "+" to use these queries and those in the config file. 40 | 41 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 42 | queries: +security-and-quality 43 | 44 | 45 | - run: .github/build.sh ubuntu 46 | 47 | - name: Perform CodeQL Analysis 48 | uses: github/codeql-action/analyze@v2 49 | with: 50 | category: "/language:${{matrix.language}}" 51 | -------------------------------------------------------------------------------- /.github/workflows/coverity.yml: -------------------------------------------------------------------------------- 1 | name: Coverity Scan 2 | 3 | # We only want to test master or explicitly via coverity branch 4 | on: 5 | push: 6 | branches: [master, coverity] 7 | 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-20.04 12 | steps: 13 | - uses: actions/checkout@v2 14 | - run: .github/build.sh coverity 15 | - uses: vapier/coverity-scan-action@v0 16 | with: 17 | project: OpenSC%2Fpam_p11 18 | token: ${{ secrets.COVERITY_SCAN_TOKEN }} 19 | email: 'frankmorgner@gmail.com' 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | Makefile.in 3 | core 4 | archive 5 | acinclude.m4 6 | aclocal.m4 7 | autom4te.cache 8 | compile 9 | confdefs.h 10 | config.* 11 | configure 12 | conftest 13 | conftest.c 14 | depcomp 15 | install-sh 16 | libtool 17 | libtool.m4 18 | ltmain.sh 19 | missing 20 | mkinstalldirs 21 | so_locations 22 | stamp-h* 23 | 24 | .deps 25 | .libs 26 | .#*# 27 | .*.bak 28 | .*.orig 29 | .*.rej 30 | .*~ 31 | #*# 32 | *.bak 33 | *.d 34 | *.def 35 | *.dll 36 | *.exe 37 | *.la 38 | *.lib 39 | *.lo 40 | *.o 41 | *.orig 42 | *.pdb 43 | *.rej 44 | *.u 45 | *.rc 46 | *.pc 47 | *~ 48 | *.gz 49 | *.bz2 50 | *.out 51 | 52 | m4/ltoptions.m4 53 | m4/ltsugar.m4 54 | m4/ltversion.m4 55 | m4/lt~obsolete.m4 56 | 57 | src/test-login 58 | src/test-passwd 59 | 60 | ABOUT-NLS 61 | m4/codeset.m4 62 | m4/extern-inline.m4 63 | m4/fcntl-o.m4 64 | m4/gettext.m4 65 | m4/glibc2.m4 66 | m4/glibc21.m4 67 | m4/iconv.m4 68 | m4/intdiv0.m4 69 | m4/intl.m4 70 | m4/intldir.m4 71 | m4/intlmacosx.m4 72 | m4/intmax.m4 73 | m4/inttypes-pri.m4 74 | m4/inttypes_h.m4 75 | m4/lcmessage.m4 76 | m4/lib-ld.m4 77 | m4/lib-link.m4 78 | m4/lib-prefix.m4 79 | m4/lock.m4 80 | m4/longlong.m4 81 | m4/nls.m4 82 | m4/po.m4 83 | m4/printf-posix.m4 84 | m4/progtest.m4 85 | m4/size_max.m4 86 | m4/stdint_h.m4 87 | m4/threadlib.m4 88 | m4/uintmax_t.m4 89 | m4/visibility.m4 90 | m4/wchar_t.m4 91 | m4/wint_t.m4 92 | m4/xsize.m4 93 | po/Makefile.in.in 94 | po/Makevars.template 95 | po/Rules-quot 96 | po/boldquot.sed 97 | po/en@boldquot.header 98 | po/en@quot.header 99 | po/insert-header.sin 100 | po/quot.sed 101 | po/remove-potcdate.sin 102 | po/POTFILES 103 | po/stamp-po 104 | po/remove-potcdate.sed 105 | *.gmo 106 | *.mo 107 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | 504 | 505 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | AUTOMAKE_OPTIONS = foreign 1.10 3 | ACLOCAL_AMFLAGS = -I m4 4 | 5 | MAINTAINERCLEANFILES = \ 6 | config.log config.status \ 7 | $(srcdir)/Makefile.in \ 8 | $(srcdir)/config.h.in $(srcdir)/config.h.in~ $(srcdir)/configure \ 9 | $(srcdir)/install-sh $(srcdir)/ltmain.sh $(srcdir)/missing \ 10 | $(srcdir)/depcomp $(srcdir)/aclocal.m4 \ 11 | $(srcdir)/config.guess $(srcdir)/config.sub \ 12 | $(srcdir)/m4/ltsugar.m4 $(srcdir)/m4/libtool.m4 \ 13 | $(srcdir)/m4/ltversion.m4 $(srcdir)/m4/lt~obsolete.m4 \ 14 | $(srcdir)/m4/ltoptions.m4 \ 15 | $(srcdir)/packaged 16 | EXTRA_DIST = ./config.rpath .gitignore 17 | 18 | SUBDIRS = po src 19 | 20 | dist_noinst_SCRIPTS = bootstrap 21 | dist_doc_DATA = NEWS README.md 22 | 23 | # Allow detection of packaged tarball 24 | dist-hook: 25 | $(MKDIR_P) "$(distdir)/m4" 26 | echo > "$(distdir)/packaged" 27 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | NEWS for Pam_p11 -- History of user visible changes 2 | 3 | New in 0.5.0; 2023-08-03; Frank Morgner 4 | * Add support for tokens that only contain a certificate (and no public key) 5 | * Fixed never-ending loop if the PIN is locked 6 | 7 | New in 0.4.0; 2023-06-08; Frank Morgner 8 | * Add Russian translation 9 | * Add support for building with LibreSSL 10 | * Add support for building with OpenSSL 3.0 and later 11 | 12 | New in 0.3.1; 2019-09-11; Frank Morgner 13 | * CVE-2019-16058: Fixed buffer overflow when creating signatures longer than 256 bytes 14 | 15 | New in 0.3.0; 2019-04-24; Frank Morgner 16 | * Add Italian translation 17 | * Add support for matching the PIN-input with a regular expression 18 | * Add support for macOS 19 | * Add support for building with OpenSSL 1.1.1 20 | * Add support for nistp256/384/521 keys in authorized_keys file 21 | 22 | New in 0.2.0; 2018-05-16; Frank Morgner 23 | * Add user documentation in Readme.md 24 | * Add support for PIN pad readers 25 | * Add support for changing/unblocking PIN (use with passwd) 26 | * Add support for localized user feedback 27 | * Add support for cards without certificates (e.g. OpenPGP card) 28 | * Add support for PKCS#11 modules with multiple slots 29 | * Add support for building with OpenSSL 1.1 30 | * Merged opensc and openssh module into pam_p11.so 31 | * Fixed memory leaks, coverity issues, compiler warnings 32 | * Created `test-passwd` and `test-login` for testing standard use cases 33 | 34 | New in 0.1.6; 2017-03-06; Alon Bar-Lev 35 | * Build system rewritten (NOTICE: configure options was modified). 36 | 37 | New in 0.1.5; 2008-08-27; Andreas Jellinghaus 38 | * fix script for wiki to html export. 39 | 40 | New in 0.1.4; 2008-07-31; Andreas Jellinghaus 41 | * new version with a number of build fixes 42 | 43 | New in 0.1.3; 2007-07-11; Andreas Jellinghaus 44 | * update wiki export script (add images, fix links). 45 | * make some functions static. 46 | * rename variables to avoid names of glibc functions (e.g. random). 47 | * do not save the password (i.e. pin - shouldn't go anywhere except 48 | to the card). 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to pam_p11 2 | 3 | Pam_p11 is a plugable authentication module (pam) package for using crpytographic tokens such as smart cards and usb crypto tokens for authentication. 4 | 5 | Pam_p11 uses [libp11](https://github.com/OpenSC/libp11/) to access any PKCS#11 module. It should be compatible with any implementation, but it is primarely developed using [OpenSC](https://github.com/OpenSC/OpenSC/). 6 | 7 | Pam_p11 implements two authentication methods: 8 | 9 | - verify a token using a known public key found in OpenSSH's `~/.ssh/authorized_keys`. 10 | - verify a token using a known certificate found in `~/.eid/authorized_certificates`. 11 | 12 | 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. 13 | 14 | Pam_p11 was written by an international team and is licensed as Open Source software under the LGPL license. 15 | 16 | [![GitHub CI Status](https://img.shields.io/github/actions/workflow/status/OpenSC/pam_p11/ci.yml?branch=master&label=Linux%2FmacOS&logo=github)](https://github.com/OpenSC/pam_p11/actions/workflows/ci.yml?branch=master) [![Coverity Scan CI Status](https://img.shields.io/coverity/scan/15452.svg?label=Coverity%20Scan)](https://scan.coverity.com/projects/15452) [![CodeQL CI Status](https://img.shields.io/github/actions/workflow/status/OpenSC/pam_p11/codeql.yml?branch=master&label=CodeQL&logo=github)](https://github.com/OpenSC/pam_p11/actions/workflows/codeql.yml?branch=master) 17 | 18 | ## Installing pam_p11 19 | 20 | Installation is quite easy: 21 | 22 | ``` 23 | wget https://github.com/OpenSC/pam_p11/releases/download/pam_p11-0.6.0/pam_p11-0.6.0.tar.gz 24 | tar xfvz pam_p11-0.6.0.tar.gz 25 | cd pam_p11-0.6.0 26 | ./configure --prefix=/usr --libdir=/lib/ 27 | make 28 | make install 29 | ``` 30 | 31 | 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. 32 | 33 | ## Using pam_p11 34 | 35 | ### Login 36 | 37 | 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: 38 | 39 | ``` 40 | auth sufficient /usr/local/lib/security/pam_p11.so /usr/local/lib/opensc-pkcs11.so 41 | ``` 42 | 43 | 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. 44 | 45 | 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](https://man.openbsd.org/re_format.7) for matching. 46 | 47 | 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. 48 | 49 | To enable pam_p11 for all logins (graphical and terminal based), change the following configuration files as described above: 50 | 51 | | Operating System | PAM configuration file | 52 | | ---------------- | -------------------------- | 53 | | macOS | `/etc/pam.d/authorization` | 54 | | Debian | `/etc/pam.d/common-auth` | 55 | | Arch Linux | `/etc/pam.d/system-auth` | 56 | 57 | ### PIN change and unblock 58 | 59 | To allow changing and unblocking the PIN via pam_p11, add the following to your configuration: 60 | 61 | ``` 62 | password optional /usr/local/lib/security/pam_p11.so /usr/local/lib/opensc-pkcs11.so 63 | ``` 64 | 65 | 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](https://man.openbsd.org/re_format.7) for matching. 66 | 67 | ### User configuration via `~/.eid/authorized_certificates` 68 | 69 | A user may create a `~/.eid/` directory and create a file `~/.eid/authorized_certificates` with authorized certificates. You can do that via 70 | 71 | ``` 72 | mkdir -p ~/.eid 73 | chmod 0755 ~/.eid 74 | pkcs11-tool --read-object --type cert --id 45 --module /usr/lib/opensc-pkcs11.so --output-file cert.cer 75 | openssl x509 -inform DER -in cert.cer -outform PEM >> ~/.eid/authorized_certificates 76 | chmod 0644 ~/.eid/authorized_certificates 77 | ``` 78 | 79 | 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. 80 | 81 | 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. 82 | 83 | ### User configuration via `~/.ssh/authorized_keys` 84 | 85 | A user may create a `~/.ssh/` directory and create a file `~/.ssh/authorized_keys` with authorized public keys. You can do that via 86 | 87 | ``` 88 | mkdir -p ~/.ssh 89 | chmod 0755 ~/.ssh 90 | ssh-keygen -D /usr/lib/opensc-pkcs11.so >> ~/.ssh/authorized_keys 91 | chmod 0644 ~/.ssh/authorized_keys 92 | ``` 93 | 94 | 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. 95 | 96 | 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. 97 | 98 | 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.) 99 | 100 | ## Security Note 101 | 102 | 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](https://github.com/OpenSC/pam_pkcs11) for a fully fledged PAM module for smart card authentication. 103 | -------------------------------------------------------------------------------- /bootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | set -x 5 | if test -f Makefile; then 6 | make distclean 7 | fi 8 | rm -rf *~ *.cache *.m4 config.guess config.log \ 9 | config.status config.sub depcomp ltmain.sh 10 | autoreconf --verbose --install --force 11 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ([2.69]) 2 | 3 | define([PACKAGE_VERSION_MAJOR], [0]) 4 | define([PACKAGE_VERSION_MINOR], [6]) 5 | define([PACKAGE_VERSION_FIX], [0]) 6 | define([PACKAGE_SUFFIX], []) 7 | define([PRODUCT_BUGREPORT], [https://github.com/OpenSC/pam_p11/issues]) 8 | 9 | AC_INIT([pam_p11],[PACKAGE_VERSION_MAJOR.PACKAGE_VERSION_MINOR.PACKAGE_VERSION_FIX[]PACKAGE_SUFFIX],[PRODUCT_BUGREPORT]) 10 | AC_CONFIG_AUX_DIR([.]) 11 | AM_CONFIG_HEADER([config.h]) 12 | AC_CONFIG_MACRO_DIR([m4]) 13 | AM_INIT_AUTOMAKE 14 | 15 | AC_CONFIG_SRCDIR([src/pam_p11.c]) 16 | 17 | AC_CANONICAL_HOST 18 | AC_PROG_CC 19 | PKG_PROG_PKG_CONFIG 20 | AC_C_BIGENDIAN 21 | 22 | AC_ARG_ENABLE( 23 | [strict], 24 | [AS_HELP_STRING([--disable-strict],[disable strict compile mode @<:@enabled@:>@])], 25 | , 26 | [enable_strict="yes"] 27 | ) 28 | 29 | AC_ARG_ENABLE( 30 | [pedantic], 31 | [AS_HELP_STRING([--enable-pedantic],[enable pedantic compile mode @<:@disabled@:>@])], 32 | , 33 | [enable_pedantic="no"] 34 | ) 35 | 36 | AC_ARG_WITH( 37 | [pamdir], 38 | [AS_HELP_STRING([--with-pamdir=PATH],[Specify the directory where PAM modules are stored])], 39 | [pamdir="${withval}"], 40 | [ 41 | if test "${prefix}" = "/usr"; then 42 | pamdir="/lib${libdir##*/lib}/security" 43 | else 44 | pamdir="\$(libdir)/security" 45 | fi 46 | ] 47 | ) 48 | 49 | AM_GNU_GETTEXT([external]) 50 | AM_GNU_GETTEXT_VERSION(0.18.3) 51 | 52 | dnl Add the languages which your application supports here. 53 | ALL_LINGUAS="de it ru ja" 54 | 55 | dnl Checks for programs. 56 | AC_PROG_CPP 57 | AC_PROG_INSTALL 58 | AC_PROG_LN_S 59 | AC_PROG_MKDIR_P 60 | AC_PROG_SED 61 | AC_PROG_MAKE_SET 62 | 63 | dnl Add libtool support. 64 | ifdef( 65 | [LT_INIT], 66 | [LT_INIT], 67 | [AC_PROG_LIBTOOL] 68 | ) 69 | 70 | PKG_CHECK_MODULES([LIBP11], [libp11 >= 0.2.4],, [AC_MSG_ERROR([libp11 is required])]) 71 | PKG_CHECK_MODULES( 72 | [OPENSSL], 73 | [libcrypto >= 1.1.1], 74 | , 75 | [PKG_CHECK_MODULES( 76 | [OPENSSL], 77 | [openssl >= 1.1.1], 78 | , 79 | [AC_CHECK_LIB( 80 | [crypto], 81 | [RSA_version], 82 | [OPENSSL_LIBS="-lcrypto"], 83 | [AC_MSG_ERROR([Cannot find OpenSSL])] 84 | )] 85 | )] 86 | ) 87 | 88 | #saved_LIBS="$LIBS" 89 | #LIBS="$OPENSSL_LIBS $LIBS" 90 | #AC_CHECK_FUNCS(EVP_MD_CTX_new EVP_MD_CTX_free EVP_MD_CTX_reset) 91 | #LIBS="$saved_LIBS" 92 | 93 | if test -z "${PAM_LIBS}"; then 94 | AC_ARG_VAR([PAM_CFLAGS], [C compiler flags for pam]) 95 | AC_ARG_VAR([PAM_LIBS], [linker flags for pam]) 96 | AC_CHECK_LIB( 97 | [pam], 98 | [pam_authenticate], 99 | [PAM_LIBS="-lpam"], 100 | [AC_MSG_ERROR([Cannot find pam])] 101 | ) 102 | fi 103 | 104 | saved_CFLAGS="${CFLAGS}" 105 | CFLAGS="${CFLAGS} ${OPENSSL_CFLAGS} ${LIBP11_CFLAGS} ${PAM_CFLAGS}" 106 | LIBS="$LIBS ${OPENSSL_LIBS} ${LIBP11_LIBS} ${PAM_LIBS}" 107 | 108 | AC_HEADER_SYS_WAIT 109 | AC_CHECK_HEADERS([ \ 110 | string.h syslog.h fcntl.h unistd.h security/pam_ext.h security/pam_misc.h \ 111 | ]) 112 | AC_TYPE_SIZE_T 113 | AC_FUNC_MALLOC 114 | AC_FUNC_REALLOC 115 | AC_FUNC_STAT 116 | AC_FUNC_VPRINTF 117 | AC_CHECK_FUNCS([memset strdup strerror PKCS11_enumerate_public_keys PKCS11_is_logged_in pam_vprompt openpam_ttyconv]) 118 | 119 | AC_CHECK_LIB([pam_misc], [misc_conv]) 120 | AM_CONDITIONAL([HAVE_PAM_MISC], [test "$ac_cv_lib_pam_misc_misc_conv" = yes]) 121 | 122 | CFLAGS="${saved_CFLAGS}" 123 | LIBS="$saved_LIBS" 124 | 125 | AC_SUBST([pamdir]) 126 | 127 | if test "${enable_pedantic}" = "yes"; then 128 | enable_strict="yes"; 129 | CFLAGS="${CFLAGS} -pedantic" 130 | fi 131 | if test "${enable_strict}" = "yes"; then 132 | CFLAGS="${CFLAGS} -Wall -Wextra -Wno-unused-parameter -Werror" 133 | fi 134 | 135 | AC_CONFIG_FILES([ 136 | Makefile 137 | src/Makefile 138 | po/Makefile.in 139 | ]) 140 | AC_OUTPUT 141 | 142 | cat <, 2018. 5 | # 6 | msgid "" 7 | msgstr "" 8 | "Project-Id-Version: pam_p11 0.1.7_git\n" 9 | "Report-Msgid-Bugs-To: https://github.com/OpenSC/pam_p11/issues\n" 10 | "POT-Creation-Date: 2024-03-20 22:58+0100\n" 11 | "PO-Revision-Date: 2018-04-05 11:14+0200\n" 12 | "Last-Translator: Frank Morgner \n" 13 | "Language-Team: German\n" 14 | "Language: de\n" 15 | "MIME-Version: 1.0\n" 16 | "Content-Type: text/plain; charset=UTF-8\n" 17 | "Content-Transfer-Encoding: 8bit\n" 18 | "Plural-Forms: nplurals=2; plural=(n != 1);\n" 19 | 20 | #: src/pam_p11.c:194 21 | msgid "Error loading PKCS#11 module" 22 | msgstr "Fehler beim Laden des PKCS#11-Moduls" 23 | 24 | #: src/pam_p11.c:202 src/pam_p11.c:254 25 | msgid "Error initializing PKCS#11 module" 26 | msgstr "Fehler beim Initialisieren des PKCS#11-Moduls" 27 | 28 | #: src/pam_p11.c:322 29 | msgid " (last try)" 30 | msgstr " (letzter Versuch)" 31 | 32 | #: src/pam_p11.c:329 33 | #, c-format 34 | msgid "Login on PIN pad with %s%s" 35 | msgstr "Login auf dem PIN-Pad mit %s%s" 36 | 37 | #: src/pam_p11.c:335 38 | #, c-format 39 | msgid "Login with %s%s: " 40 | msgstr "Login mit %s%s: " 41 | 42 | #: src/pam_p11.c:359 43 | msgid "Invalid PIN" 44 | msgstr "" 45 | 46 | #: src/pam_p11.c:367 47 | msgid "PIN not verified; PIN locked" 48 | msgstr "PIN nicht verifiziert; PIN gesperrt" 49 | 50 | #: src/pam_p11.c:369 51 | msgid "PIN not verified; one try remaining" 52 | msgstr "PIN nicht verifiziert; ein Versuch verbleibend" 53 | 54 | #: src/pam_p11.c:371 55 | msgid "PIN not verified" 56 | msgstr "PIN nicht verifiziert" 57 | 58 | #: src/pam_p11.c:413 59 | #, c-format 60 | msgid "Change PIN with PUK on PIN pad for %s" 61 | msgstr "Ändere PIN mit PUK auf dem PIN-Pad für %s" 62 | 63 | #: src/pam_p11.c:417 64 | #, c-format 65 | msgid "Change PIN on PIN pad for %s" 66 | msgstr "Ändere PIN auf dem PIN-Pad für %s" 67 | 68 | #: src/pam_p11.c:424 69 | #, c-format 70 | msgid "PUK for %s: " 71 | msgstr "PUK für %s: " 72 | 73 | #: src/pam_p11.c:435 74 | msgid "Current PIN: " 75 | msgstr "Aktuelle PIN: " 76 | 77 | #: src/pam_p11.c:453 78 | msgid "Enter new PIN: " 79 | msgstr "Neue PIN eingeben: " 80 | 81 | #: src/pam_p11.c:456 82 | msgid "Retype new PIN: " 83 | msgstr "Neue PIN wiederholen: " 84 | 85 | #: src/pam_p11.c:460 86 | msgid "PINs don't match" 87 | msgstr "PINs verschieden" 88 | 89 | #: src/pam_p11.c:467 90 | #, fuzzy 91 | msgid "PIN not changed; PIN locked" 92 | msgstr "PIN nicht verifiziert; PIN gesperrt" 93 | 94 | #: src/pam_p11.c:469 95 | #, fuzzy 96 | msgid "PIN not changed; one try remaining" 97 | msgstr "PIN nicht verifiziert; ein Versuch verbleibend" 98 | 99 | #: src/pam_p11.c:471 100 | #, fuzzy 101 | msgid "PIN not changed" 102 | msgstr "PIN nicht verifiziert" 103 | 104 | #: src/pam_p11.c:596 105 | msgid "No token found" 106 | msgstr "Kein Token gefunden" 107 | 108 | #: src/pam_p11.c:599 109 | msgid "Could not find authorized keys on any of the tokens." 110 | msgstr "Auf keinem der Token konnten autorisierte Schlüssel gefunden werden." 111 | 112 | #: src/pam_p11.c:660 113 | msgid "Error verifying key" 114 | msgstr "Fehler beim Verifizieren des Schlüssels" 115 | -------------------------------------------------------------------------------- /po/it.po: -------------------------------------------------------------------------------- 1 | # Italian translation for pam-p11 2 | # Copyright (c) 2019 OpenSC Project 3 | # This file is distributed under the same license as the pam-p11 package. 4 | # Milo Casagrande , 2019. 5 | # 6 | msgid "" 7 | msgstr "" 8 | "Project-Id-Version: pam-p11\n" 9 | "Report-Msgid-Bugs-To: https://github.com/OpenSC/pam_p11/issues\n" 10 | "POT-Creation-Date: 2024-03-20 22:58+0100\n" 11 | "PO-Revision-Date: 2019-02-28 14:03+0000\n" 12 | "Last-Translator: Milo Casagrande \n" 13 | "Language-Team: Italian \n" 14 | "Language: it\n" 15 | "MIME-Version: 1.0\n" 16 | "Content-Type: text/plain; charset=UTF-8\n" 17 | "Content-Transfer-Encoding: 8bit\n" 18 | 19 | #: src/pam_p11.c:194 20 | msgid "Error loading PKCS#11 module" 21 | msgstr "Errore nel caricare il modulo PKCS#11" 22 | 23 | #: src/pam_p11.c:202 src/pam_p11.c:254 24 | msgid "Error initializing PKCS#11 module" 25 | msgstr "Errore nell'inizializzare il modulo PKCS#11" 26 | 27 | #: src/pam_p11.c:322 28 | msgid " (last try)" 29 | msgstr " (ultimo tentativo)" 30 | 31 | #: src/pam_p11.c:329 32 | #, c-format 33 | msgid "Login on PIN pad with %s%s" 34 | msgstr "Accesso su dispositivo inserimento PIN con %s%s" 35 | 36 | #: src/pam_p11.c:335 37 | #, c-format 38 | msgid "Login with %s%s: " 39 | msgstr "Accesso con %s%s: " 40 | 41 | #: src/pam_p11.c:359 42 | msgid "Invalid PIN" 43 | msgstr "" 44 | 45 | #: src/pam_p11.c:367 46 | msgid "PIN not verified; PIN locked" 47 | msgstr "PIN non verificato; PIN bloccato" 48 | 49 | #: src/pam_p11.c:369 50 | msgid "PIN not verified; one try remaining" 51 | msgstr "PIN non verificato; un tentativo rimasto" 52 | 53 | #: src/pam_p11.c:371 54 | msgid "PIN not verified" 55 | msgstr "PIN non verificato" 56 | 57 | #: src/pam_p11.c:413 58 | #, c-format 59 | msgid "Change PIN with PUK on PIN pad for %s" 60 | msgstr "Modifica del PIN con PUK su dispositivo inserimento PIN per %s" 61 | 62 | #: src/pam_p11.c:417 63 | #, c-format 64 | msgid "Change PIN on PIN pad for %s" 65 | msgstr "Modifica del PIN su dispositivo inserimento PIN per %s" 66 | 67 | #: src/pam_p11.c:424 68 | #, c-format 69 | msgid "PUK for %s: " 70 | msgstr "PUK per %s: " 71 | 72 | #: src/pam_p11.c:435 73 | msgid "Current PIN: " 74 | msgstr "PIN attuale: " 75 | 76 | #: src/pam_p11.c:453 77 | msgid "Enter new PIN: " 78 | msgstr "Inserire nuovo PIN: " 79 | 80 | #: src/pam_p11.c:456 81 | msgid "Retype new PIN: " 82 | msgstr "Ripetere nuovo PIN: " 83 | 84 | #: src/pam_p11.c:460 85 | msgid "PINs don't match" 86 | msgstr "I PIN non sono uguali" 87 | 88 | #: src/pam_p11.c:467 89 | msgid "PIN not changed; PIN locked" 90 | msgstr "PIN non modificato; PIN bloccato" 91 | 92 | #: src/pam_p11.c:469 93 | msgid "PIN not changed; one try remaining" 94 | msgstr "PIN non modificato; un tentativo rimasto" 95 | 96 | #: src/pam_p11.c:471 97 | msgid "PIN not changed" 98 | msgstr "PIN non modificato" 99 | 100 | #: src/pam_p11.c:596 101 | msgid "No token found" 102 | msgstr "Nessun token trovato" 103 | 104 | #: src/pam_p11.c:599 105 | msgid "Could not find authorized keys on any of the tokens." 106 | msgstr "Impossibile trovare chiavi autorizzate su nessuno dei token." 107 | 108 | #: src/pam_p11.c:660 109 | msgid "Error verifying key" 110 | msgstr "Errore nel verificare la chiave" 111 | -------------------------------------------------------------------------------- /po/ja.po: -------------------------------------------------------------------------------- 1 | # Japanese translations for pam_p11 package. 2 | # Copyright (C) 2024 OpenSC Project 3 | # This file is distributed under the same license as the pam_p11 package. 4 | # Nejikugi <63852665+scrwnl@users.noreply.github.com>, 2024. 5 | # 6 | msgid "" 7 | msgstr "" 8 | "Project-Id-Version: pam_p11 0.5.0\n" 9 | "Report-Msgid-Bugs-To: https://github.com/OpenSC/pam_p11/issues\n" 10 | "POT-Creation-Date: 2024-03-20 22:58+0100\n" 11 | "PO-Revision-Date: 2024-03-19 00:24+0900\n" 12 | "Last-Translator: Nejikugi <63852665+scrwnl@users.noreply.github.com>\n" 13 | "Language: ja\n" 14 | "MIME-Version: 1.0\n" 15 | "Content-Type: text/plain; charset=UTF-8\n" 16 | "Content-Transfer-Encoding: 8bit\n" 17 | "Plural-Forms: nplurals=1; plural=0;\n" 18 | 19 | #: src/pam_p11.c:194 20 | msgid "Error loading PKCS#11 module" 21 | msgstr "PKCS#11 モジュールの読み込みエラー" 22 | 23 | #: src/pam_p11.c:202 src/pam_p11.c:254 24 | msgid "Error initializing PKCS#11 module" 25 | msgstr "PKCS#11 モジュールの初期化エラー" 26 | 27 | #: src/pam_p11.c:322 28 | msgid " (last try)" 29 | msgstr "(最後の試行)" 30 | 31 | #: src/pam_p11.c:329 32 | #, c-format 33 | msgid "Login on PIN pad with %s%s" 34 | msgstr "%sのPINパッドでログイン%s" 35 | 36 | #: src/pam_p11.c:335 37 | #, c-format 38 | msgid "Login with %s%s: " 39 | msgstr "%sでログイン%s:" 40 | 41 | #: src/pam_p11.c:359 42 | msgid "Invalid PIN" 43 | msgstr "不正な暗証番号です。" 44 | 45 | #: src/pam_p11.c:367 46 | msgid "PIN not verified; PIN locked" 47 | msgstr "暗証番号はロックされているため、検証されていません。" 48 | 49 | #: src/pam_p11.c:369 50 | msgid "PIN not verified; one try remaining" 51 | msgstr "暗証番号は検証されていません。残り試行回数は1回です。" 52 | 53 | #: src/pam_p11.c:371 54 | msgid "PIN not verified" 55 | msgstr "暗証番号は検証されていません。" 56 | 57 | #: src/pam_p11.c:413 58 | #, c-format 59 | msgid "Change PIN with PUK on PIN pad for %s" 60 | msgstr "" 61 | "%sのPINパッドにPUK(暗証番号ロック解除コード)を入力して暗証番号を変更してくだ" 62 | "さい。" 63 | 64 | #: src/pam_p11.c:417 65 | #, c-format 66 | msgid "Change PIN on PIN pad for %s" 67 | msgstr "%sのPINパッドで暗証番号を変更してください。" 68 | 69 | #: src/pam_p11.c:424 70 | #, c-format 71 | msgid "PUK for %s: " 72 | msgstr "%s のPUK(暗証番号ロック解除コード)を入力: " 73 | 74 | #: src/pam_p11.c:435 75 | msgid "Current PIN: " 76 | msgstr "現在の暗証番号を入力: " 77 | 78 | #: src/pam_p11.c:453 79 | msgid "Enter new PIN: " 80 | msgstr "新しい暗証番号を入力: " 81 | 82 | #: src/pam_p11.c:456 83 | msgid "Retype new PIN: " 84 | msgstr "新しい暗証番号を再入力: " 85 | 86 | #: src/pam_p11.c:460 87 | msgid "PINs don't match" 88 | msgstr "暗証番号が一致しません。" 89 | 90 | #: src/pam_p11.c:467 91 | msgid "PIN not changed; PIN locked" 92 | msgstr "暗証番号はロックされているため、変更されていません。" 93 | 94 | #: src/pam_p11.c:469 95 | msgid "PIN not changed; one try remaining" 96 | msgstr "暗証番号は変更されていません。残り試行回数は1回です。" 97 | 98 | #: src/pam_p11.c:471 99 | msgid "PIN not changed" 100 | msgstr "暗証番号は変更されていません。" 101 | 102 | #: src/pam_p11.c:596 103 | msgid "No token found" 104 | msgstr "トークンが見つかりません。" 105 | 106 | #: src/pam_p11.c:599 107 | msgid "Could not find authorized keys on any of the tokens." 108 | msgstr "いずれのトークンでも認可された鍵が見つかりませんでした。" 109 | 110 | #: src/pam_p11.c:660 111 | msgid "Error verifying key" 112 | msgstr "鍵検証エラー" 113 | -------------------------------------------------------------------------------- /po/pam_p11.pot: -------------------------------------------------------------------------------- 1 | # SOME DESCRIPTIVE TITLE. 2 | # Copyright (C) YEAR OpenSC Project 3 | # This file is distributed under the same license as the pam_p11 package. 4 | # FIRST AUTHOR , YEAR. 5 | # 6 | #, fuzzy 7 | msgid "" 8 | msgstr "" 9 | "Project-Id-Version: pam_p11 0.6.0\n" 10 | "Report-Msgid-Bugs-To: https://github.com/OpenSC/pam_p11/issues\n" 11 | "POT-Creation-Date: 2024-03-20 22:58+0100\n" 12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 13 | "Last-Translator: FULL NAME \n" 14 | "Language-Team: LANGUAGE \n" 15 | "Language: \n" 16 | "MIME-Version: 1.0\n" 17 | "Content-Type: text/plain; charset=CHARSET\n" 18 | "Content-Transfer-Encoding: 8bit\n" 19 | 20 | #: src/pam_p11.c:194 21 | msgid "Error loading PKCS#11 module" 22 | msgstr "" 23 | 24 | #: src/pam_p11.c:202 src/pam_p11.c:254 25 | msgid "Error initializing PKCS#11 module" 26 | msgstr "" 27 | 28 | #: src/pam_p11.c:322 29 | msgid " (last try)" 30 | msgstr "" 31 | 32 | #: src/pam_p11.c:329 33 | #, c-format 34 | msgid "Login on PIN pad with %s%s" 35 | msgstr "" 36 | 37 | #: src/pam_p11.c:335 38 | #, c-format 39 | msgid "Login with %s%s: " 40 | msgstr "" 41 | 42 | #: src/pam_p11.c:359 43 | msgid "Invalid PIN" 44 | msgstr "" 45 | 46 | #: src/pam_p11.c:367 47 | msgid "PIN not verified; PIN locked" 48 | msgstr "" 49 | 50 | #: src/pam_p11.c:369 51 | msgid "PIN not verified; one try remaining" 52 | msgstr "" 53 | 54 | #: src/pam_p11.c:371 55 | msgid "PIN not verified" 56 | msgstr "" 57 | 58 | #: src/pam_p11.c:413 59 | #, c-format 60 | msgid "Change PIN with PUK on PIN pad for %s" 61 | msgstr "" 62 | 63 | #: src/pam_p11.c:417 64 | #, c-format 65 | msgid "Change PIN on PIN pad for %s" 66 | msgstr "" 67 | 68 | #: src/pam_p11.c:424 69 | #, c-format 70 | msgid "PUK for %s: " 71 | msgstr "" 72 | 73 | #: src/pam_p11.c:435 74 | msgid "Current PIN: " 75 | msgstr "" 76 | 77 | #: src/pam_p11.c:453 78 | msgid "Enter new PIN: " 79 | msgstr "" 80 | 81 | #: src/pam_p11.c:456 82 | msgid "Retype new PIN: " 83 | msgstr "" 84 | 85 | #: src/pam_p11.c:460 86 | msgid "PINs don't match" 87 | msgstr "" 88 | 89 | #: src/pam_p11.c:467 90 | msgid "PIN not changed; PIN locked" 91 | msgstr "" 92 | 93 | #: src/pam_p11.c:469 94 | msgid "PIN not changed; one try remaining" 95 | msgstr "" 96 | 97 | #: src/pam_p11.c:471 98 | msgid "PIN not changed" 99 | msgstr "" 100 | 101 | #: src/pam_p11.c:596 102 | msgid "No token found" 103 | msgstr "" 104 | 105 | #: src/pam_p11.c:599 106 | msgid "Could not find authorized keys on any of the tokens." 107 | msgstr "" 108 | 109 | #: src/pam_p11.c:660 110 | msgid "Error verifying key" 111 | msgstr "" 112 | -------------------------------------------------------------------------------- /po/ru.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: pam_p11 0.5.0\n" 4 | "Report-Msgid-Bugs-To: https://github.com/OpenSC/pam_p11/issues\n" 5 | "POT-Creation-Date: 2024-03-20 22:58+0100\n" 6 | "Last-Translator: Mikhail Novosyolov 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library 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 GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include 22 | 23 | extern int sc_base64_decode(const char *in, unsigned char *out, size_t outlen); 24 | 25 | static const unsigned char bin_table[128] = { 26 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 27 | 0xFF, 0xE0, 0xD0, 0xFF, 0xFF, 0xD0, 0xFF, 0xFF, 28 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 29 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 30 | 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 31 | 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F, 32 | 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 33 | 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xC0, 0xFF, 0xFF, 34 | 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 35 | 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 36 | 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 37 | 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 38 | 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 39 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 40 | 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 41 | 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 42 | }; 43 | 44 | static int from_base64(const char *in, unsigned int *out, int *skip) 45 | { 46 | unsigned int res = 0, c, s = 18; 47 | const char *in0 = in; 48 | 49 | for (c = 0; c < 4; c++, in++) { 50 | unsigned char b; 51 | int k = *in; 52 | 53 | if (k < 0) 54 | return -1; 55 | if (k == 0 && c == 0) 56 | return 0; 57 | b = bin_table[k]; 58 | if (b == 0xC0) /* '=' */ 59 | break; 60 | switch (b) { 61 | case 0xD0: /* '\n' or '\r' */ 62 | c--; 63 | continue; 64 | } 65 | if (b > 0x3f) 66 | return -1; 67 | 68 | res |= b << s; 69 | s -= 6; 70 | } 71 | *skip = in - in0; 72 | *out = res; 73 | return c * 6 / 8; 74 | } 75 | 76 | int sc_base64_decode(const char *in, unsigned char *out, size_t outlen) 77 | { 78 | int len = 0, r = 0, skip = 0; 79 | unsigned int i = 0; 80 | 81 | while ((r = from_base64(in, &i, &skip)) > 0) { 82 | int finished = 0, s = 16; 83 | 84 | if (r < 3) 85 | finished = 1; 86 | while (r--) { 87 | if (outlen <= 0) 88 | return -1; 89 | *out++ = i >> s; 90 | s -= 8; 91 | outlen--; 92 | len++; 93 | } 94 | in += skip; 95 | if (finished || *in == 0) 96 | return len; 97 | } 98 | if (r == 0) 99 | return len; 100 | return -1; 101 | } 102 | -------------------------------------------------------------------------------- /src/login.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Frank Morgner 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifdef HAVE_CONFIG_H 20 | #include "config.h" 21 | #endif 22 | 23 | #include 24 | 25 | extern int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv); 26 | 27 | int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv) 28 | { 29 | int r; 30 | 31 | r = pam_sm_authenticate(pamh, flags, argc, argv); 32 | if (PAM_SUCCESS != r) 33 | goto err; 34 | 35 | r = pam_sm_acct_mgmt(pamh, flags, argc, argv); 36 | if (PAM_SUCCESS != r) 37 | goto err; 38 | 39 | err: 40 | return r; 41 | } 42 | -------------------------------------------------------------------------------- /src/match_opensc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | extern int match_user_opensc(EVP_PKEY *authkey, const char *login) 15 | { 16 | char filename[PATH_MAX]; 17 | struct passwd *pw; 18 | int found; 19 | BIO *in; 20 | X509 *cert = NULL; 21 | 22 | if (NULL == authkey || NULL == login) 23 | return -1; 24 | 25 | pw = getpwnam(login); 26 | if (!pw || !pw->pw_dir) 27 | return -1; 28 | 29 | snprintf(filename, PATH_MAX, "%s/.eid/authorized_certificates", 30 | pw->pw_dir); 31 | 32 | in = BIO_new(BIO_s_file()); 33 | if (!in) 34 | return -1; 35 | 36 | if (BIO_read_filename(in, filename) != 1) { 37 | syslog(LOG_ERR, "BIO_read_filename from %s failed\n", filename); 38 | return -1; 39 | } 40 | 41 | found = 0; 42 | do { 43 | EVP_PKEY *key; 44 | if (NULL == PEM_read_bio_X509(in, &cert, 0, NULL)) { 45 | break; 46 | } 47 | key = X509_get_pubkey(cert); 48 | if (key == NULL) 49 | continue; 50 | 51 | #if OPENSSL_VERSION_NUMBER < 0x30000000L 52 | if (1 == EVP_PKEY_cmp(authkey, key)) { 53 | found = 1; 54 | } 55 | #else 56 | if (1 == EVP_PKEY_eq(authkey, key)) { 57 | found = 1; 58 | } 59 | #endif 60 | EVP_PKEY_free(key); 61 | } while (found == 0); 62 | 63 | if (cert) { 64 | X509_free(cert); 65 | } 66 | 67 | BIO_free(in); 68 | 69 | return found; 70 | } 71 | -------------------------------------------------------------------------------- /src/match_openssh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L 11 | #include 12 | #include 13 | #endif 14 | #include 15 | #include 16 | #include 17 | 18 | /* how to read the authorized_keys file and the key? 19 | * see openssh source code auth2-pubkey.c user_key_allowed2 20 | * and misc.c read_keyfile_line and key.c 21 | */ 22 | 23 | #define OPENSSH_LINE_MAX 16384 /* from openssh SSH_MAX_PUBKEY_BYTES */ 24 | 25 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \ 26 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3000000L) 27 | void RSA_get0_key(const RSA *r, 28 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) 29 | { 30 | if (n != NULL) 31 | *n = r->n; 32 | if (e != NULL) 33 | *e = r->e; 34 | if (d != NULL) 35 | *d = r->d; 36 | } 37 | 38 | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) 39 | { 40 | /* If the fields n and e in r are NULL, the corresponding input 41 | * parameters MUST be non-NULL for n and e. d may be 42 | * left NULL (in case only the public key is used). 43 | */ 44 | if ((r->n == NULL && n == NULL) 45 | || (r->e == NULL && e == NULL)) 46 | return 0; 47 | 48 | if (n != NULL) { 49 | BN_free(r->n); 50 | r->n = n; 51 | } 52 | if (e != NULL) { 53 | BN_free(r->e); 54 | r->e = e; 55 | } 56 | if (d != NULL) { 57 | BN_free(r->d); 58 | r->d = d; 59 | } 60 | 61 | return 1; 62 | } 63 | 64 | #endif 65 | 66 | static EVP_PKEY *init_evp_pkey_rsa(BIGNUM *rsa_n, BIGNUM *rsa_e) 67 | { 68 | EVP_PKEY *key = NULL; 69 | 70 | if (!rsa_e || !rsa_n) 71 | return NULL; 72 | 73 | #if OPENSSL_VERSION_NUMBER < 0x30000000L 74 | key = EVP_PKEY_new(); 75 | if (!key) 76 | return NULL; 77 | 78 | RSA *rsa = RSA_new(); 79 | if (!rsa) { 80 | EVP_PKEY_free(key); 81 | return NULL; 82 | } 83 | 84 | /* set e and n */ 85 | if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL)) { 86 | RSA_free(rsa); 87 | EVP_PKEY_free(key); 88 | return NULL; 89 | } 90 | 91 | EVP_PKEY_assign_RSA(key, rsa); 92 | #else 93 | OSSL_PARAM_BLD *bld = NULL; 94 | OSSL_PARAM *params = NULL; 95 | EVP_PKEY_CTX *pctx = NULL; 96 | 97 | if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL 98 | || (bld = OSSL_PARAM_BLD_new()) == NULL 99 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, rsa_n) 100 | || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, rsa_e) 101 | || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL 102 | || EVP_PKEY_fromdata_init(pctx) <= 0 103 | || EVP_PKEY_fromdata(pctx, &key, EVP_PKEY_PUBLIC_KEY, params) <= 0) { 104 | EVP_PKEY_CTX_free(pctx); 105 | OSSL_PARAM_free(params); 106 | OSSL_PARAM_BLD_free(bld); 107 | return NULL; 108 | } 109 | #endif 110 | 111 | return key; 112 | } 113 | 114 | static EVP_PKEY *init_evp_pkey_ec(int nid_curve, const unsigned char *buf, size_t len) 115 | { 116 | EVP_PKEY *key = NULL; 117 | 118 | #if defined(LIBRESSL_VERSION_NUMBER) 119 | BIGNUM *x = NULL; 120 | BIGNUM *y = NULL; 121 | EC_KEY *ec_key = NULL; 122 | 123 | if ((key = EVP_PKEY_new()) == NULL 124 | || (x = BN_bin2bn(buf + 1, len >> 1, NULL)) == NULL 125 | || (y = BN_bin2bn(buf + 1 + (len >> 1), len >> 1, NULL)) == NULL 126 | || ((ec_key = EC_KEY_new_by_curve_name(nid_curve)) == NULL 127 | || (1 != EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) 128 | || (1 != EVP_PKEY_assign_EC_KEY(key, ec_key)))) { 129 | EVP_PKEY_free(key); 130 | BN_free(x); 131 | BN_free(y); 132 | EC_KEY_free(ec_key); 133 | EVP_PKEY_free(key); 134 | return NULL; 135 | } 136 | #else 137 | 138 | #if OPENSSL_VERSION_NUMBER < 0x30000000L 139 | BN_CTX *ctx = NULL; 140 | EC_KEY *ec_key = NULL; 141 | 142 | if ((key = EVP_PKEY_new()) == NULL 143 | || (ctx = BN_CTX_new()) == NULL 144 | || (ec_key = EC_KEY_new_by_curve_name(nid_curve)) == NULL 145 | || (1 != EC_KEY_oct2key(ec_key, buf, len, ctx)) 146 | || (1 != EVP_PKEY_assign_EC_KEY(key, ec_key))) { 147 | EC_KEY_free(ec_key); 148 | BN_CTX_free(ctx); 149 | EVP_PKEY_free(key); 150 | return NULL; 151 | } 152 | #else 153 | OSSL_PARAM_BLD *bld = NULL; 154 | OSSL_PARAM *params = NULL; 155 | EVP_PKEY_CTX *pctx = NULL; 156 | char *group_name; 157 | switch (nid_curve) { 158 | case NID_X9_62_prime256v1: 159 | group_name = SN_X9_62_prime256v1; 160 | break; 161 | case NID_secp384r1: 162 | group_name = SN_secp384r1; 163 | break; 164 | case NID_secp521r1: 165 | group_name = SN_secp521r1; 166 | break; 167 | default: 168 | return NULL; 169 | } 170 | 171 | if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL 172 | || (bld = OSSL_PARAM_BLD_new()) == NULL 173 | || !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0) 174 | || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, buf, len) 175 | || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL 176 | || EVP_PKEY_fromdata_init(pctx) <= 0 177 | || EVP_PKEY_fromdata(pctx, &key, EVP_PKEY_PUBLIC_KEY, params) <= 0) { 178 | EVP_PKEY_CTX_free(pctx); 179 | OSSL_PARAM_free(params); 180 | OSSL_PARAM_BLD_free(bld); 181 | return NULL; 182 | } 183 | #endif 184 | #endif 185 | return key; 186 | } 187 | 188 | static EVP_PKEY *ssh1_line_to_key(char *line) 189 | { 190 | EVP_PKEY *key = NULL; 191 | char *b, *e, *m, *c; 192 | BIGNUM *rsa_e = NULL, *rsa_n = NULL; 193 | 194 | /* first digitstring: the bits */ 195 | b = line; 196 | 197 | /* second digitstring: the exponent */ 198 | /* skip all digits */ 199 | for (e = b; *e >= '0' && *e <= '0'; e++) ; 200 | 201 | /* must be a whitespace */ 202 | if (*e != ' ' && *e != '\t') 203 | goto err; 204 | 205 | /* cut the string in two part */ 206 | *e = 0; 207 | e++; 208 | 209 | /* skip more whitespace */ 210 | while (*e == ' ' || *e == '\t') 211 | e++; 212 | 213 | /* third digitstring: the modulus */ 214 | /* skip all digits */ 215 | for (m = e; *m >= '0' && *m <= '0'; m++) ; 216 | 217 | /* must be a whitespace */ 218 | if (*m != ' ' && *m != '\t') 219 | goto err; 220 | 221 | /* cut the string in two part */ 222 | *m = 0; 223 | m++; 224 | 225 | /* skip more whitespace */ 226 | while (*m == ' ' || *m == '\t') 227 | m++; 228 | 229 | /* look for a comment after the modulus */ 230 | for (c = m; *c >= '0' && *c <= '0'; c++) ; 231 | 232 | /* could be a whitespace or end of line */ 233 | if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r' && *c != 0) 234 | goto err; 235 | 236 | if (*c == ' ' || *c == '\t') { 237 | *c = 0; 238 | c++; 239 | 240 | /* skip more whitespace */ 241 | while (*c == ' ' || *c == '\t') 242 | c++; 243 | 244 | if (*c && *c != '\r' && *c != '\n') { 245 | /* we have a comment */ 246 | } else { 247 | c = NULL; 248 | } 249 | 250 | } else { 251 | *c = 0; 252 | c = NULL; 253 | } 254 | 255 | /* ok, now we have b e m pointing to pure digit 256 | * null terminated strings and maybe c pointing to a comment */ 257 | 258 | BN_dec2bn(&rsa_e, e); 259 | BN_dec2bn(&rsa_n, m); 260 | 261 | key = init_evp_pkey_rsa(rsa_n, rsa_e); 262 | 263 | err: 264 | if (!key) { 265 | if (rsa_n) 266 | BN_free(rsa_n); 267 | if (rsa_e) 268 | BN_free(rsa_e); 269 | } 270 | 271 | return key; 272 | } 273 | 274 | extern int sc_base64_decode(const char *in, unsigned char *out, size_t outlen); 275 | 276 | static EVP_PKEY *ssh2_line_to_key(char *line) 277 | { 278 | EVP_PKEY *key = NULL; 279 | BIGNUM *rsa_e = NULL, *rsa_n = NULL; 280 | unsigned char decoded[OPENSSH_LINE_MAX]; 281 | int len; 282 | 283 | char *b, *c; 284 | int i; 285 | 286 | /* find the mime-blob */ 287 | b = line; 288 | 289 | if (!b) 290 | goto err; 291 | 292 | /* find the first whitespace */ 293 | while (*b && *b != ' ') 294 | b++; 295 | 296 | /* skip that whitespace */ 297 | b++; 298 | 299 | /* find the end of the blob / comment */ 300 | for (c = b; *c && *c != ' ' && 'c' != '\t' && *c != '\r' 301 | && *c != '\n'; c++) ; 302 | 303 | *c = 0; 304 | 305 | /* decode binary data */ 306 | if (sc_base64_decode(b, decoded, OPENSSH_LINE_MAX) < 0) 307 | goto err; 308 | 309 | i = 0; 310 | 311 | /* get integer from blob */ 312 | len = 313 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 314 | (decoded[i + 2] << 8) + (decoded[i + 3]); 315 | i += 4; 316 | 317 | /* now: key_from_blob */ 318 | if (strncmp((char *)&decoded[i], "ssh-rsa", 7) != 0) 319 | goto err; 320 | 321 | i += len; 322 | 323 | /* to prevent access beyond 'decoded' array, index 'i' must be always checked */ 324 | if ( i + 4 > OPENSSH_LINE_MAX ) 325 | goto err; 326 | /* get integer from blob */ 327 | len = 328 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 329 | (decoded[i + 2] << 8) + (decoded[i + 3]); 330 | i += 4; 331 | 332 | if ( i + len > OPENSSH_LINE_MAX ) 333 | goto err; 334 | /* get bignum */ 335 | rsa_e = BN_bin2bn(decoded + i, len, NULL); 336 | i += len; 337 | 338 | if ( i + 4 > OPENSSH_LINE_MAX ) 339 | goto err; 340 | /* get integer from blob */ 341 | len = 342 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 343 | (decoded[i + 2] << 8) + (decoded[i + 3]); 344 | i += 4; 345 | 346 | if ( i + len > OPENSSH_LINE_MAX ) 347 | goto err; 348 | /* get bignum */ 349 | rsa_n = BN_bin2bn(decoded + i, len, NULL); 350 | 351 | key = init_evp_pkey_rsa(rsa_n, rsa_e); 352 | 353 | err: 354 | if (!key) { 355 | if (rsa_n) 356 | BN_free(rsa_n); 357 | if (rsa_e) 358 | BN_free(rsa_e); 359 | } 360 | 361 | return key; 362 | } 363 | 364 | static EVP_PKEY *ssh_nistp_line_to_key(char *line) 365 | { 366 | unsigned char decoded[OPENSSH_LINE_MAX]; 367 | int len; 368 | int flen; 369 | 370 | char *b, *c; 371 | int i; 372 | int nid; 373 | 374 | /* check allowed key size */ 375 | if (strncmp(line + 16, "256", 3) == 0) 376 | flen = 32, nid = NID_X9_62_prime256v1; 377 | else if (strncmp(line + 16, "384", 3) == 0) 378 | flen = 48, nid = NID_secp384r1; 379 | else if (strncmp(line + 16, "521", 3) == 0) 380 | flen = 66, nid = NID_secp521r1; 381 | else 382 | return NULL; 383 | 384 | /* find the mime-blob */ 385 | b = line; 386 | 387 | if (!b) 388 | return NULL; 389 | 390 | /* find the first whitespace */ 391 | while (*b && *b != ' ') 392 | b++; 393 | 394 | /* skip that whitespace */ 395 | b++; 396 | 397 | /* find the end of the blob / comment */ 398 | for (c = b; *c && *c != ' ' && 'c' != '\t' && *c != '\r' 399 | && *c != '\n'; c++) ; 400 | 401 | *c = 0; 402 | 403 | /* decode binary data */ 404 | if (sc_base64_decode(b, decoded, OPENSSH_LINE_MAX) < 0) 405 | return NULL; 406 | 407 | i = 0; 408 | /* get integer from blob */ 409 | len = 410 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 411 | (decoded[i + 2] << 8) + (decoded[i + 3]); 412 | i += 4; 413 | 414 | /* always check 'len' to get safe 'i' as index into 'decoded' array */ 415 | if (len != 19) 416 | return NULL; 417 | /* check key type (must be same in decoded data and at line start) */ 418 | if (strncmp((char *)&decoded[i], line, 19) != 0) 419 | return NULL; 420 | i += len; 421 | 422 | /* get integer from blob */ 423 | len = 424 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 425 | (decoded[i + 2] << 8) + (decoded[i + 3]); 426 | i += 4; 427 | 428 | /* check curve name - must match key type */ 429 | if(len != 8) 430 | return NULL; 431 | if (strncmp((char *)&decoded[i], line + 11, 8) != 0) 432 | return NULL; 433 | i += len; 434 | 435 | /* get integer from blob */ 436 | len = 437 | (decoded[i] << 24) + (decoded[i + 1] << 16) + 438 | (decoded[i + 2] << 8) + (decoded[i + 3]); 439 | i += 4; 440 | 441 | /* read public key (uncompressed point) */ 442 | /* test if data length is corresponding to key size */ 443 | if (len != 1 + flen * 2) 444 | return NULL; 445 | 446 | /* check uncompressed indicator */ 447 | if (decoded[i] != 4 ) 448 | return NULL; 449 | 450 | return init_evp_pkey_ec(nid, decoded + i, len); 451 | } 452 | 453 | extern int match_user_openssh(EVP_PKEY *authkey, const char *login) 454 | { 455 | char filename[PATH_MAX]; 456 | char line[OPENSSH_LINE_MAX]; 457 | struct passwd *pw; 458 | int found; 459 | FILE *file; 460 | 461 | pw = getpwnam(login); 462 | if (!pw || !pw->pw_dir) 463 | return -1; 464 | 465 | snprintf(filename, PATH_MAX, "%s/.ssh/authorized_keys", pw->pw_dir); 466 | 467 | file = fopen(filename, "r"); 468 | if (!file) 469 | return -1; 470 | 471 | found = 0; 472 | do { 473 | EVP_PKEY *key = NULL; 474 | char *cp; 475 | if (!fgets(line, sizeof line, file)) 476 | break; 477 | 478 | /* Skip leading whitespace, empty and comment lines. */ 479 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) { 480 | } 481 | if (!*cp || *cp == '\n' || *cp == '#') { 482 | continue; 483 | } 484 | 485 | if (*cp >= '0' && *cp <= '9') { 486 | /* ssh v1 key format */ 487 | key = ssh1_line_to_key(cp); 488 | } else if (strncmp("ssh-rsa", cp, 7) == 0) { 489 | /* ssh v2 rsa key format */ 490 | key = ssh2_line_to_key(cp); 491 | } else if (strncmp("ecdsa-sha2-nistp", cp, 16) == 0) { 492 | /* ssh nistp256/384/521 key */ 493 | key = ssh_nistp_line_to_key(cp); 494 | } 495 | if (key == NULL) 496 | continue; 497 | 498 | #if OPENSSL_VERSION_NUMBER < 0x30000000L 499 | if (1 == EVP_PKEY_cmp(authkey, key)) { 500 | found = 1; 501 | } 502 | #else 503 | if (1 == EVP_PKEY_eq(authkey, key)) { 504 | found = 1; 505 | } 506 | #endif 507 | EVP_PKEY_free(key); 508 | } while (found == 0); 509 | 510 | fclose(file); 511 | 512 | return found; 513 | } 514 | -------------------------------------------------------------------------------- /src/pam_p11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libp11 PAM Login Module 3 | * Copyright (C) 2003 Mario Strasser , 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | */ 15 | #ifdef HAVE_CONFIG_H 16 | #include "config.h" 17 | #endif 18 | 19 | #ifndef PACKAGE 20 | #define PACKAGE "pam_p11" 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #ifdef ENABLE_NLS 37 | #include 38 | #include 39 | #define _(string) gettext(string) 40 | #ifndef LOCALEDIR 41 | #define LOCALEDIR "/usr/share/locale" 42 | #endif 43 | #else 44 | #define _(string) string 45 | #endif 46 | 47 | /* We have to make this definitions before we include the pam header files! */ 48 | #define PAM_SM_AUTH 49 | #define PAM_SM_ACCOUNT 50 | #define PAM_SM_SESSION 51 | #define PAM_SM_PASSWORD 52 | #include 53 | #include 54 | #ifdef HAVE_SECURITY_PAM_EXT_H 55 | #include 56 | #else 57 | #define pam_syslog(handle, level, msg...) syslog(level, ## msg) 58 | #endif 59 | 60 | #ifndef HAVE_PAM_VPROMPT 61 | static int pam_vprompt(pam_handle_t *pamh, int style, char **response, 62 | const char *fmt, va_list args) 63 | { 64 | int r = PAM_CRED_INSUFFICIENT; 65 | const struct pam_conv *conv; 66 | struct pam_message msg; 67 | struct pam_response *resp = NULL; 68 | struct pam_message *(msgp[1]); 69 | 70 | char text[128]; 71 | vsnprintf(text, sizeof text, fmt, args); 72 | 73 | msgp[0] = &msg; 74 | msg.msg_style = style; 75 | msg.msg = text; 76 | 77 | if (PAM_SUCCESS != pam_get_item(pamh, PAM_CONV, (const void **) &conv) 78 | || NULL == conv || NULL == conv->conv 79 | || conv->conv(1, (const struct pam_message **) msgp, &resp, conv->appdata_ptr) 80 | || NULL == resp) { 81 | goto err; 82 | } 83 | if (NULL != response) { 84 | if (resp[0].resp) { 85 | *response = strdup(resp[0].resp); 86 | if (NULL == *response) { 87 | pam_syslog(pamh, LOG_CRIT, "strdup() failed: %s", 88 | strerror(errno)); 89 | goto err; 90 | } 91 | } else { 92 | *response = NULL; 93 | } 94 | } 95 | 96 | r = PAM_SUCCESS; 97 | err: 98 | if (resp) { 99 | OPENSSL_cleanse(&resp[0].resp, sizeof resp[0].resp); 100 | free(&resp[0]); 101 | } 102 | return r; 103 | } 104 | #endif 105 | 106 | #ifndef PAM_EXTERN 107 | #define PAM_EXTERN extern 108 | #endif 109 | 110 | int prompt(int flags, pam_handle_t *pamh, int style, char **response, 111 | const char *fmt, ...) 112 | { 113 | int r; 114 | 115 | if (PAM_SILENT == (flags & PAM_SILENT) 116 | && style != PAM_TEXT_INFO 117 | && style != PAM_PROMPT_ECHO_OFF) { 118 | /* PAM_SILENT does not override the prompting of the user for passwords 119 | * etc., it only stops informative messages from being generated. We 120 | * use PAM_TEXT_INFO and PAM_PROMPT_ECHO_OFF exclusively for the 121 | * password prompt. */ 122 | r = PAM_SUCCESS; 123 | } else { 124 | va_list args; 125 | va_start (args, fmt); 126 | if (!response) { 127 | char *p = NULL; 128 | r = pam_vprompt(pamh, style, &p, fmt, args); 129 | free(p); 130 | } else { 131 | r = pam_vprompt(pamh, style, response, fmt, args); 132 | } 133 | va_end(args); 134 | } 135 | 136 | return r; 137 | } 138 | 139 | struct module_data { 140 | PKCS11_CTX *ctx; 141 | PKCS11_SLOT *slots; 142 | unsigned int nslots; 143 | int module_loaded; 144 | }; 145 | 146 | #ifdef TEST 147 | static struct module_data *global_module_data = NULL; 148 | #endif 149 | 150 | void module_data_cleanup(pam_handle_t *pamh, void *data, int error_status) 151 | { 152 | struct module_data *module_data = data; 153 | if (module_data) { 154 | if (1 == module_data->module_loaded) { 155 | PKCS11_release_all_slots(module_data->ctx, module_data->slots, module_data->nslots); 156 | PKCS11_CTX_unload(module_data->ctx); 157 | } 158 | PKCS11_CTX_free(module_data->ctx); 159 | EVP_cleanup(); 160 | ERR_free_strings(); 161 | free(module_data); 162 | } 163 | } 164 | 165 | static int module_initialize(pam_handle_t * pamh, 166 | int flags, int argc, const char **argv, 167 | struct module_data **module_data) 168 | { 169 | int r; 170 | struct module_data *data = calloc(1, sizeof *data); 171 | if (NULL == data) { 172 | pam_syslog(pamh, LOG_CRIT, "calloc() failed: %s", 173 | strerror(errno)); 174 | r = PAM_BUF_ERR; 175 | goto err; 176 | } 177 | 178 | #ifdef ENABLE_NLS 179 | setlocale(LC_ALL, ""); 180 | bindtextdomain(PACKAGE, LOCALEDIR); 181 | textdomain(PACKAGE); 182 | #endif 183 | 184 | /* Initialize OpenSSL */ 185 | OpenSSL_add_all_algorithms(); 186 | ERR_load_crypto_strings(); 187 | 188 | /* Load and initialize PKCS#11 module */ 189 | data->ctx = PKCS11_CTX_new(); 190 | if (0 == argc || NULL == data->ctx 191 | || 0 != PKCS11_CTX_load(data->ctx, argv[0])) { 192 | pam_syslog(pamh, LOG_ALERT, "Loading PKCS#11 engine failed: %s\n", 193 | ERR_reason_error_string(ERR_get_error())); 194 | prompt(flags, pamh, PAM_ERROR_MSG , NULL, _("Error loading PKCS#11 module")); 195 | r = PAM_NO_MODULE_DATA; 196 | goto err; 197 | } 198 | data->module_loaded = 1; 199 | if (0 != PKCS11_enumerate_slots(data->ctx, &data->slots, &data->nslots)) { 200 | pam_syslog(pamh, LOG_ALERT, "Initializing PKCS#11 engine failed: %s\n", 201 | ERR_reason_error_string(ERR_get_error())); 202 | prompt(flags, pamh, PAM_ERROR_MSG , NULL, _("Error initializing PKCS#11 module")); 203 | r = PAM_AUTHINFO_UNAVAIL; 204 | goto err; 205 | } 206 | 207 | #ifdef TEST 208 | /* pam_set_data() is reserved for actual modules. For testing this would 209 | * return PAM_SYSTEM_ERR, so we're saving the module data in a static 210 | * variable. */ 211 | r = PAM_SUCCESS; 212 | global_module_data = data; 213 | #else 214 | r = pam_set_data(pamh, PACKAGE, data, module_data_cleanup); 215 | if (PAM_SUCCESS != r) { 216 | goto err; 217 | } 218 | #endif 219 | 220 | *module_data = data; 221 | data = NULL; 222 | 223 | err: 224 | module_data_cleanup(pamh, data, r); 225 | 226 | return r; 227 | } 228 | 229 | static int module_refresh(pam_handle_t *pamh, 230 | int flags, int argc, const char **argv, 231 | const char **user, PKCS11_CTX **ctx, 232 | PKCS11_SLOT **slots, unsigned int *nslots, 233 | const char **pin_regex) 234 | { 235 | int r; 236 | struct module_data *module_data; 237 | 238 | if (PAM_SUCCESS != pam_get_data(pamh, PACKAGE, (void *)&module_data) 239 | || NULL == module_data) { 240 | r = module_initialize(pamh, flags, argc, argv, &module_data); 241 | if (PAM_SUCCESS != r) { 242 | goto err; 243 | } 244 | } else { 245 | /* refresh all known slots */ 246 | PKCS11_release_all_slots(module_data->ctx, 247 | module_data->slots, module_data->nslots); 248 | module_data->slots = NULL; 249 | module_data->nslots = 0; 250 | if (0 != PKCS11_enumerate_slots(module_data->ctx, 251 | &module_data->slots, &module_data->nslots)) { 252 | pam_syslog(pamh, LOG_ALERT, "Initializing PKCS#11 engine failed: %s\n", 253 | ERR_reason_error_string(ERR_get_error())); 254 | prompt(flags, pamh, PAM_ERROR_MSG , NULL, _("Error initializing PKCS#11 module")); 255 | r = PAM_AUTHINFO_UNAVAIL; 256 | goto err; 257 | } 258 | } 259 | 260 | if (1 < argc) { 261 | *pin_regex = argv[1]; 262 | } else { 263 | #ifdef __APPLE__ 264 | /* If multiple PAMs are allowed for macOS' login, then the captured 265 | * password is used for all possible modules. To not block the token's 266 | * PIN if the user enters his standard password, we're refusing to use 267 | * anything that doesn't look like a PIN. */ 268 | *pin_regex = "^[[:digit:]]*$"; 269 | #else 270 | *pin_regex = NULL; 271 | #endif 272 | } 273 | 274 | r = pam_get_user(pamh, user, NULL); 275 | if (PAM_SUCCESS != r) { 276 | pam_syslog(pamh, LOG_ERR, "pam_get_user() failed %s", 277 | pam_strerror(pamh, r)); 278 | r = PAM_USER_UNKNOWN; 279 | goto err; 280 | } 281 | 282 | *ctx = module_data->ctx; 283 | *nslots = module_data->nslots; 284 | *slots = module_data->slots; 285 | 286 | err: 287 | return r; 288 | } 289 | 290 | extern int match_user_opensc(EVP_PKEY *authkey, const char *login); 291 | extern int match_user_openssh(EVP_PKEY *authkey, const char *login); 292 | 293 | static int key_login(pam_handle_t *pamh, int flags, PKCS11_SLOT *slot, const char *pin_regex) 294 | { 295 | char *password = NULL; 296 | int ok; 297 | 298 | if (0 == slot->token->loginRequired 299 | #ifdef HAVE_PKCS11_IS_LOGGED_IN 300 | || (0 == PKCS11_is_logged_in(slot, 0, &ok) 301 | && ok == 1) 302 | #endif 303 | ) { 304 | ok = 1; 305 | goto err; 306 | } 307 | ok = 0; 308 | 309 | /* try to get stored item */ 310 | if (PAM_SUCCESS == pam_get_item(pamh, PAM_AUTHTOK, (void *)&password) 311 | && NULL != password) { 312 | password = strdup(password); 313 | if (NULL == password) { 314 | pam_syslog(pamh, LOG_CRIT, "strdup() failed: %s", 315 | strerror(errno)); 316 | goto err; 317 | } 318 | } else { 319 | const char *pin_info; 320 | 321 | if (slot->token->userPinFinalTry) { 322 | pin_info = _(" (last try)"); 323 | } else { 324 | pin_info = ""; 325 | } 326 | 327 | if (0 != slot->token->secureLogin) { 328 | prompt(flags, pamh, PAM_TEXT_INFO, NULL, 329 | _("Login on PIN pad with %s%s"), 330 | slot->token->label, pin_info); 331 | } else { 332 | /* ask the user for the password if variable text is set */ 333 | if (PAM_SUCCESS != prompt(flags, pamh, 334 | PAM_PROMPT_ECHO_OFF, &password, 335 | _("Login with %s%s: "), 336 | slot->token->label, pin_info)) { 337 | goto err; 338 | } 339 | } 340 | } 341 | 342 | if (NULL != password && NULL != pin_regex && 0 < strlen(pin_regex)) { 343 | regex_t regex; 344 | int regex_compiled = 0; 345 | int result = 0; 346 | result = regcomp(®ex, pin_regex, REG_EXTENDED); 347 | if (0 == result) { 348 | regex_compiled = 1; 349 | result = regexec(®ex, password, 0, NULL, 0); 350 | } 351 | if (result) { 352 | char regex_error[256]; 353 | regerror(result, ®ex, regex_error, sizeof regex_error); 354 | pam_syslog(pamh, LOG_CRIT, "PIN regex didn't match: %s", 355 | regex_error); 356 | if (1 == regex_compiled) { 357 | regfree(®ex); 358 | } 359 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("Invalid PIN")); 360 | goto err; 361 | } 362 | regfree(®ex); 363 | } 364 | 365 | if (0 != PKCS11_login(slot, 0, password)) { 366 | if (slot->token->userPinLocked) { 367 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not verified; PIN locked")); 368 | } else if (slot->token->userPinFinalTry) { 369 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not verified; one try remaining")); 370 | } else { 371 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not verified")); 372 | } 373 | goto err; 374 | } 375 | 376 | pam_set_item(pamh, PAM_AUTHTOK, password); 377 | 378 | ok = 1; 379 | 380 | err: 381 | if (NULL != password) { 382 | OPENSSL_cleanse(password, strlen(password)); 383 | free(password); 384 | } 385 | 386 | return ok; 387 | } 388 | 389 | static int key_change_login(pam_handle_t *pamh, int flags, PKCS11_SLOT *slot, const char *pin_regex) 390 | { 391 | char *old = NULL, *new = NULL, *retyped = NULL; 392 | int ok; 393 | 394 | if (0 == slot->token->loginRequired) { 395 | /* we can't change a PIN */ 396 | ok = 0; 397 | goto err; 398 | } 399 | ok = 0; 400 | 401 | /* We need a R/W public session to change the PIN via PUK or 402 | * a R/W user session to change the PIN via PIN */ 403 | if (0 != PKCS11_open_session(slot, 1) 404 | || (0 == slot->token->userPinLocked 405 | && 1 != key_login(pamh, flags, slot, pin_regex))) { 406 | goto err; 407 | } 408 | 409 | /* prompt for new PIN (and PUK if needed) */ 410 | if (0 != slot->token->secureLogin) { 411 | if (0 != slot->token->userPinLocked) { 412 | prompt(flags, pamh, PAM_TEXT_INFO, NULL, 413 | _("Change PIN with PUK on PIN pad for %s"), 414 | slot->token->label); 415 | } else { 416 | prompt(flags, pamh, PAM_TEXT_INFO, NULL, 417 | _("Change PIN on PIN pad for %s"), 418 | slot->token->label); 419 | } 420 | } else { 421 | if (0 != slot->token->userPinLocked) { 422 | if (PAM_SUCCESS == prompt(flags, pamh, 423 | PAM_PROMPT_ECHO_OFF, &old, 424 | _("PUK for %s: "), 425 | slot->token->label)) { 426 | goto err; 427 | } 428 | } else { 429 | #ifdef TEST 430 | /* In the tests we're calling pam_sm_chauthtok() directly, so 431 | * pam_get_item(PAM_AUTHTOK) will return PAM_BAD_ITEM. As 432 | * workaround, we simply enter the current PIN twice. */ 433 | if (PAM_SUCCESS != prompt(flags, pamh, 434 | PAM_PROMPT_ECHO_OFF, &old, 435 | _("Current PIN: "))) { 436 | goto err; 437 | } 438 | #else 439 | if (PAM_SUCCESS != pam_get_item(pamh, PAM_AUTHTOK, (void *)&old) 440 | || NULL == old) { 441 | goto err; 442 | } 443 | old = strdup(old); 444 | if (NULL == old) { 445 | pam_syslog(pamh, LOG_CRIT, "strdup() failed: %s", 446 | strerror(errno)); 447 | goto err; 448 | } 449 | #endif 450 | } 451 | if (PAM_SUCCESS != prompt(flags, pamh, 452 | PAM_PROMPT_ECHO_OFF, &new, 453 | _("Enter new PIN: ")) 454 | || PAM_SUCCESS != prompt(flags, pamh, 455 | PAM_PROMPT_ECHO_OFF, &retyped, 456 | _("Retype new PIN: "))) { 457 | goto err; 458 | } 459 | if (0 != strcmp(new, retyped)) { 460 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PINs don't match")); 461 | goto err; 462 | } 463 | } 464 | 465 | if (0 != PKCS11_change_pin(slot, old, new)) { 466 | if (slot->token->userPinLocked) { 467 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not changed; PIN locked")); 468 | } else if (slot->token->userPinFinalTry) { 469 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not changed; one try remaining")); 470 | } else { 471 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("PIN not changed")); 472 | } 473 | goto err; 474 | } 475 | 476 | pam_set_item(pamh, PAM_AUTHTOK, new); 477 | 478 | ok = 1; 479 | 480 | err: 481 | if (NULL != retyped) { 482 | OPENSSL_cleanse(retyped, strlen(retyped)); 483 | free(retyped); 484 | } 485 | if (NULL != new) { 486 | OPENSSL_cleanse(new, strlen(new)); 487 | free(new); 488 | } 489 | if (NULL != old) { 490 | OPENSSL_cleanse(old, strlen(old)); 491 | free(old); 492 | } 493 | 494 | return ok; 495 | } 496 | 497 | static int key_find(pam_handle_t *pamh, int flags, const char *user, 498 | PKCS11_CTX *ctx, PKCS11_SLOT *slots, unsigned int nslots, 499 | PKCS11_SLOT **authslot, PKCS11_KEY **authkey, 500 | EVP_PKEY **authpubkey, PKCS11_CERT **authcert) 501 | { 502 | int token_found = 0; 503 | 504 | if (NULL == authslot || NULL == authkey) { 505 | return 0; 506 | } 507 | 508 | *authkey = NULL; 509 | *authslot = NULL; 510 | *authcert = NULL; 511 | 512 | /* search all valuable slots for a key that is authorized by the user */ 513 | while (0 < nslots) { 514 | PKCS11_SLOT *slot = NULL; 515 | PKCS11_CERT *certs = NULL; 516 | #ifdef HAVE_PKCS11_ENUMERATE_PUBLIC_KEYS 517 | PKCS11_KEY *keys = NULL; 518 | #endif 519 | unsigned int count = 0; 520 | 521 | slot = PKCS11_find_token(ctx, slots, nslots); 522 | if (NULL == slot || NULL == slot->token) { 523 | break; 524 | } 525 | token_found = 1; 526 | /* Update "slots" pointer: PKCS11 slots are implemented as array, 527 | * so starting to look at slot + 1 and decrementing nslots accordingly 528 | * will search the rest of slots. */ 529 | nslots -= (slot + 1 - slots); 530 | slots = slot + 1; 531 | 532 | if (slot->token->initialized == 0) 533 | continue; 534 | 535 | if (slot->token->loginRequired && slot->token->userPinLocked) { 536 | pam_syslog(pamh, LOG_DEBUG, "%s: PIN locked", 537 | slot->token->label); 538 | continue; 539 | } 540 | pam_syslog(pamh, LOG_DEBUG, "Searching %s for keys", 541 | slot->token->label); 542 | 543 | #ifdef HAVE_PKCS11_ENUMERATE_PUBLIC_KEYS 544 | /* First, search all available public keys to allow using tokens 545 | * without certificates (e.g. OpenPGP card) */ 546 | if (0 == PKCS11_enumerate_public_keys(slot->token, &keys, &count)) { 547 | while (0 < count && NULL != keys) { 548 | EVP_PKEY *pubkey = PKCS11_get_public_key(keys); 549 | int r = match_user_opensc(pubkey, user); 550 | if (1 != r) { 551 | r = match_user_openssh(pubkey, user); 552 | } 553 | if (1 == r) { 554 | *authpubkey = pubkey; 555 | *authkey = keys; 556 | *authslot = slot; 557 | pam_syslog(pamh, LOG_DEBUG, "Found %s", 558 | keys->label); 559 | return 1; 560 | } 561 | 562 | /* Try the next possible public key */ 563 | keys++; 564 | count--; 565 | } 566 | } 567 | #endif 568 | 569 | /* Next, search all certificates */ 570 | if (0 == PKCS11_enumerate_certs(slot->token, &certs, &count)) { 571 | while (0 < count && NULL != certs) { 572 | EVP_PKEY *pubkey = X509_get_pubkey(certs->x509); 573 | int r = match_user_opensc(pubkey, user); 574 | if (1 != r) { 575 | r = match_user_openssh(pubkey, user); 576 | } 577 | if (1 == r) { 578 | *authpubkey = pubkey; 579 | *authcert = certs; 580 | *authslot = slot; 581 | pam_syslog(pamh, LOG_DEBUG, "Found %s", 582 | certs->label); 583 | return 1; 584 | } 585 | 586 | /* Try the next possible certificate */ 587 | certs++; 588 | count--; 589 | } 590 | } 591 | pam_syslog(pamh, LOG_DEBUG, "No authorized key found on token %s", 592 | slot->token->label); 593 | } 594 | 595 | if (0 == token_found) { 596 | prompt(flags, pamh, PAM_ERROR_MSG , NULL, _("No token found")); 597 | } else { 598 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, 599 | _("Could not find authorized keys on any of the tokens.")); 600 | } 601 | 602 | return 0; 603 | } 604 | 605 | static int randomize(pam_handle_t *pamh, unsigned char *r, unsigned int r_len) 606 | { 607 | int ok = 0; 608 | int fd = open("/dev/urandom", O_RDONLY); 609 | if (0 <= fd && read(fd, r, r_len) == (ssize_t)r_len) { 610 | ok = 1; 611 | } else { 612 | pam_syslog(pamh, LOG_CRIT, "Error reading from /dev/urandom: %s", 613 | strerror(errno)); 614 | } 615 | if (0 <= fd) { 616 | close(fd); 617 | } 618 | return ok; 619 | } 620 | 621 | static int key_verify(pam_handle_t *pamh, int flags, PKCS11_KEY *authkey, EVP_PKEY *pubkey) 622 | { 623 | int ok = 0; 624 | unsigned char challenge[30]; 625 | unsigned char *signature = NULL; 626 | unsigned int siglen; 627 | const EVP_MD *md = EVP_sha1(); 628 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); 629 | EVP_PKEY *privkey = PKCS11_get_private_key(authkey); 630 | 631 | if (NULL == privkey) 632 | goto err; 633 | siglen = EVP_PKEY_size(privkey); 634 | if (siglen <= 0) 635 | goto err; 636 | signature = malloc(siglen); 637 | if (NULL == signature) 638 | goto err; 639 | 640 | /* Verify a SHA-1 hash of random data, signed by the key. 641 | * 642 | * Note that this will not work keys that aren't eligible for signing. 643 | * Unfortunately, libp11 currently has no way of checking 644 | * C_GetAttributeValue(CKA_SIGN), see 645 | * https://github.com/OpenSC/libp11/issues/219. Since we don't want to 646 | * implement try and error, we live with this limitation */ 647 | if (1 != randomize(pamh, challenge, sizeof challenge)) { 648 | goto err; 649 | } 650 | if (NULL == pubkey || NULL == privkey || NULL == md_ctx || NULL == md 651 | || !EVP_SignInit(md_ctx, md) 652 | || !EVP_SignUpdate(md_ctx, challenge, sizeof challenge) 653 | || !EVP_SignFinal(md_ctx, signature, &siglen, privkey) 654 | || !EVP_MD_CTX_reset(md_ctx) 655 | || !EVP_VerifyInit(md_ctx, md) 656 | || !EVP_VerifyUpdate(md_ctx, challenge, sizeof challenge) 657 | || 1 != EVP_VerifyFinal(md_ctx, signature, siglen, pubkey)) { 658 | pam_syslog(pamh, LOG_DEBUG, "Error verifying key: %s\n", 659 | ERR_reason_error_string(ERR_get_error())); 660 | prompt(flags, pamh, PAM_ERROR_MSG, NULL, _("Error verifying key")); 661 | goto err; 662 | } 663 | ok = 1; 664 | 665 | err: 666 | free(signature); 667 | if (NULL != pubkey) 668 | EVP_PKEY_free(pubkey); 669 | if (NULL != privkey) 670 | EVP_PKEY_free(privkey); 671 | if (NULL != md_ctx) { 672 | EVP_MD_CTX_free(md_ctx); 673 | } 674 | return ok; 675 | } 676 | 677 | PAM_EXTERN int pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc, 678 | const char **argv) 679 | { 680 | int r; 681 | PKCS11_CTX *ctx; 682 | unsigned int nslots; 683 | PKCS11_KEY *authkey; 684 | PKCS11_CERT *authcert; 685 | EVP_PKEY *authpubkey = NULL; 686 | PKCS11_SLOT *slots, *authslot; 687 | const char *user; 688 | const char *pin_regex; 689 | 690 | r = module_refresh(pamh, flags, argc, argv, 691 | &user, &ctx, &slots, &nslots, &pin_regex); 692 | if (PAM_SUCCESS != r) { 693 | goto err; 694 | } 695 | 696 | if (1 != key_find(pamh, flags, user, ctx, slots, nslots, 697 | &authslot, &authkey, &authpubkey, &authcert)) { 698 | r = PAM_AUTHINFO_UNAVAIL; 699 | goto err; 700 | } 701 | 702 | if (1 != key_login(pamh, flags, authslot, pin_regex)) { 703 | r = PAM_CRED_INSUFFICIENT; 704 | goto err; 705 | } 706 | 707 | if (authkey == NULL && authcert) { 708 | if (NULL == (authkey = PKCS11_find_key(authcert))) { 709 | r = PAM_AUTHINFO_UNAVAIL; 710 | goto err; 711 | } 712 | } 713 | if (1 != key_verify(pamh, flags, authkey, authpubkey)) { 714 | if (authslot->token->userPinLocked) { 715 | r = PAM_MAXTRIES; 716 | } else { 717 | r = PAM_AUTH_ERR; 718 | } 719 | goto err; 720 | } 721 | 722 | r = PAM_SUCCESS; 723 | 724 | err: 725 | #ifdef TEST 726 | module_data_cleanup(pamh, global_module_data, r); 727 | #endif 728 | return r; 729 | } 730 | 731 | PAM_EXTERN int pam_sm_setcred(pam_handle_t * pamh, int flags, int argc, 732 | const char **argv) 733 | { 734 | /* Actually, we should return the same value as pam_sm_authenticate(). */ 735 | return PAM_SUCCESS; 736 | } 737 | 738 | PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags, int argc, 739 | const char **argv) 740 | { 741 | /* if the user has been authenticated (precondition of this call), then 742 | * everything is OK. Yes, we explicitly don't want to check CRLs, OCSP or 743 | * exparation of certificates (use pam_pkcs11 for this). */ 744 | return PAM_SUCCESS; 745 | } 746 | 747 | PAM_EXTERN int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc, 748 | const char **argv) 749 | { 750 | pam_syslog(pamh, LOG_DEBUG, 751 | "Function pam_sm_open_session() is not implemented in this module"); 752 | return PAM_SERVICE_ERR; 753 | } 754 | 755 | PAM_EXTERN int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc, 756 | const char **argv) 757 | { 758 | pam_syslog(pamh, LOG_DEBUG, 759 | "Function pam_sm_close_session() is not implemented in this module"); 760 | return PAM_SERVICE_ERR; 761 | } 762 | 763 | PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags, int argc, 764 | const char **argv) 765 | { 766 | int r; 767 | PKCS11_CTX *ctx; 768 | unsigned int nslots; 769 | PKCS11_KEY *authkey; 770 | PKCS11_CERT *authcert; 771 | EVP_PKEY *authpubkey = NULL; 772 | PKCS11_SLOT *slots, *authslot; 773 | const char *user, *pin_regex; 774 | 775 | r = module_refresh(pamh, flags, argc, argv, 776 | &user, &ctx, &slots, &nslots, &pin_regex); 777 | if (PAM_SUCCESS != r) { 778 | goto err; 779 | } 780 | 781 | if (flags & PAM_CHANGE_EXPIRED_AUTHTOK) { 782 | /* Yes, we explicitly don't want to check CRLs, OCSP or exparation of 783 | * certificates (use pam_pkcs11 for this). */ 784 | r = PAM_SUCCESS; 785 | goto err; 786 | } 787 | 788 | if (1 != key_find(pamh, flags, user, ctx, slots, nslots, 789 | &authslot, &authkey, &authpubkey, &authcert)) { 790 | r = PAM_AUTHINFO_UNAVAIL; 791 | goto err; 792 | } 793 | 794 | if (flags & PAM_PRELIM_CHECK) { 795 | r = PAM_TRY_AGAIN; 796 | goto err; 797 | } 798 | 799 | if (flags & PAM_UPDATE_AUTHTOK) { 800 | if (1 != key_change_login(pamh, flags, authslot, pin_regex)) { 801 | if (authslot->token->userPinLocked) { 802 | r = PAM_MAXTRIES; 803 | } else { 804 | r = PAM_AUTH_ERR; 805 | } 806 | goto err; 807 | } 808 | } 809 | 810 | r = PAM_SUCCESS; 811 | 812 | err: 813 | EVP_PKEY_free(authpubkey); 814 | #ifdef TEST 815 | module_data_cleanup(pamh, global_module_data, r); 816 | #endif 817 | return r; 818 | } 819 | 820 | #ifdef PAM_STATIC 821 | /* static module data */ 822 | struct pam_module _pam_group_modstruct = { 823 | PACKAGE, 824 | pam_sm_authenticate, 825 | pam_sm_setcred, 826 | pam_sm_acct_mgmt, 827 | pam_sm_open_session, 828 | pam_sm_close_session, 829 | pam_sm_chauthtok 830 | }; 831 | #endif 832 | -------------------------------------------------------------------------------- /src/pam_p11.exports: -------------------------------------------------------------------------------- 1 | pam_sm_authenticate 2 | pam_sm_setcred 3 | pam_sm_acct_mgmt 4 | pam_sm_open_session 5 | pam_sm_close_session 6 | pam_sm_chauthtok 7 | -------------------------------------------------------------------------------- /src/passwd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Frank Morgner 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifdef HAVE_CONFIG_H 20 | #include "config.h" 21 | #endif 22 | 23 | #include 24 | 25 | extern int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv); 26 | 27 | int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv) 28 | { 29 | return pam_sm_chauthtok(pamh, PAM_UPDATE_AUTHTOK, argc, argv); 30 | } 31 | -------------------------------------------------------------------------------- /src/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Frank Morgner 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifdef HAVE_CONFIG_H 20 | #include "config.h" 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | #ifdef HAVE_SECURITY_PAM_MISC_H 31 | #include 32 | #endif 33 | 34 | #ifndef LIBDIR 35 | #define LIBDIR "/usr/lib" 36 | #endif 37 | 38 | extern int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv); 39 | 40 | int main(int argc, const char **argv) 41 | { 42 | char user[32]; 43 | char module[4096]; 44 | const char *pam_argv[] = { 45 | module, 46 | }; 47 | pam_handle_t *pamh = NULL; 48 | struct pam_conv conv = { 49 | #ifdef HAVE_OPENPAM_TTYCONV 50 | openpam_ttyconv, 51 | #else 52 | misc_conv, 53 | #endif 54 | NULL, 55 | }; 56 | int r; 57 | int status = EXIT_FAILURE; 58 | 59 | /* initialize default values */ 60 | strcpy(module, LIBDIR "/opensc-pkcs11.so"); 61 | if (argc < 3) { 62 | if (0 != getlogin_r(user, sizeof user)) { 63 | perror("getlogin_r"); 64 | goto err; 65 | } 66 | } 67 | 68 | switch (argc) { 69 | case 3: 70 | strncpy(user, argv[2], (sizeof user) - 1); 71 | /* fall through */ 72 | case 2: 73 | strncpy(module, argv[1], (sizeof module) - 1); 74 | /* fall through */ 75 | case 1: 76 | break; 77 | 78 | default: 79 | printf("Usage: %s [pkcs11_module.so [username]]\n", argv[0]); 80 | /* fall through */ 81 | case 0: 82 | goto err; 83 | } 84 | module[(sizeof module) - 1] = '\0'; 85 | user[(sizeof user) - 1] = '\0'; 86 | printf("Using '%s' for '%s'\n", module, user); 87 | 88 | r = pam_start("test", user, &conv, &pamh); 89 | if (PAM_SUCCESS != r) 90 | goto pam_err; 91 | 92 | r = pam_sm_test(pamh, 0, sizeof pam_argv/sizeof *pam_argv, pam_argv); 93 | if (PAM_SUCCESS != r) 94 | goto pam_err; 95 | 96 | status = EXIT_SUCCESS; 97 | 98 | pam_err: 99 | if (PAM_SUCCESS != r) { 100 | printf("Error: %s\n", pam_strerror(pamh, r)); 101 | } else { 102 | printf("OK\n"); 103 | } 104 | pam_end(pamh, r); 105 | 106 | err: 107 | exit(status); 108 | } 109 | --------------------------------------------------------------------------------