cert-file.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # $OpenBSD: cert-file.sh,v 1.8 2019/11/26 23:43:10 djm Exp $
  2. # Placed in the Public Domain.
  3. tid="ssh with certificates"
  4. rm -f $OBJ/user_ca_key* $OBJ/user_key*
  5. rm -f $OBJ/cert_user_key*
  6. # Create a CA key
  7. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key1 ||
  8. fatal "ssh-keygen failed"
  9. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key2 ||
  10. fatal "ssh-keygen failed"
  11. # Make some keys and certificates.
  12. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key1 ||
  13. fatal "ssh-keygen failed"
  14. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key2 ||
  15. fatal "ssh-keygen failed"
  16. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key3 ||
  17. fatal "ssh-keygen failed"
  18. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key4 ||
  19. fatal "ssh-keygen failed"
  20. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key5 ||
  21. fatal "ssh-keygen failed"
  22. # Move the certificate to a different address to better control
  23. # when it is offered.
  24. ${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
  25. -z $$ -n ${USER} $OBJ/user_key1 ||
  26. fatal "couldn't sign user_key1 with user_ca_key1"
  27. mv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1_1.pub
  28. ${SSHKEYGEN} -q -s $OBJ/user_ca_key2 -I "regress user key for $USER" \
  29. -z $$ -n ${USER} $OBJ/user_key1 ||
  30. fatal "couldn't sign user_key1 with user_ca_key2"
  31. mv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1_2.pub
  32. ${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
  33. -z $$ -n ${USER} $OBJ/user_key3 ||
  34. fatal "couldn't sign user_key3 with user_ca_key1"
  35. rm $OBJ/user_key3.pub # to test use of private key w/o public half.
  36. ${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
  37. -z $$ -n ${USER} $OBJ/user_key4 ||
  38. fatal "couldn't sign user_key4 with user_ca_key1"
  39. rm $OBJ/user_key4 $OBJ/user_key4.pub # to test no matching pub/private key case.
  40. trace 'try with identity files'
  41. opts="-F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
  42. opts2="$opts -i $OBJ/user_key1 -i $OBJ/user_key2"
  43. echo "cert-authority $(cat $OBJ/user_ca_key1.pub)" > $OBJ/authorized_keys_$USER
  44. # Make a clean config that doesn't have any pre-added identities.
  45. cat $OBJ/ssh_proxy | grep -v IdentityFile > $OBJ/no_identity_config
  46. # XXX: verify that certificate used was what we expect. Needs exposure of
  47. # keys via environment variable or similar.
  48. # Key with no .pub should work - finding the equivalent *-cert.pub.
  49. verbose "identity cert with no plain public file"
  50. ${SSH} -F $OBJ/no_identity_config -oIdentitiesOnly=yes \
  51. -i $OBJ/user_key3 somehost exit 52
  52. [ $? -ne 52 ] && fail "ssh failed"
  53. # CertificateFile matching private key with no .pub file should work.
  54. verbose "CertificateFile with no plain public file"
  55. ${SSH} -F $OBJ/no_identity_config -oIdentitiesOnly=yes \
  56. -oCertificateFile=$OBJ/user_key3-cert.pub \
  57. -i $OBJ/user_key3 somehost exit 52
  58. [ $? -ne 52 ] && fail "ssh failed"
  59. # Just keys should fail
  60. verbose "plain keys"
  61. ${SSH} $opts2 somehost exit 52
  62. r=$?
  63. if [ $r -eq 52 ]; then
  64. fail "ssh succeeded with no certs"
  65. fi
  66. # Keys with untrusted cert should fail.
  67. verbose "untrusted cert"
  68. opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_2.pub"
  69. ${SSH} $opts3 somehost exit 52
  70. r=$?
  71. if [ $r -eq 52 ]; then
  72. fail "ssh succeeded with bad cert"
  73. fi
  74. # Good cert with bad key should fail.
  75. verbose "good cert, bad key"
  76. opts3="$opts -i $OBJ/user_key2"
  77. opts3="$opts3 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
  78. ${SSH} $opts3 somehost exit 52
  79. r=$?
  80. if [ $r -eq 52 ]; then
  81. fail "ssh succeeded with no matching key"
  82. fi
  83. # Keys with one trusted cert, should succeed.
  84. verbose "single trusted"
  85. opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
  86. ${SSH} $opts3 somehost exit 52
  87. r=$?
  88. if [ $r -ne 52 ]; then
  89. fail "ssh failed with trusted cert and key"
  90. fi
  91. # Multiple certs and keys, with one trusted cert, should succeed.
  92. verbose "multiple trusted"
  93. opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_2.pub"
  94. opts3="$opts3 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
  95. ${SSH} $opts3 somehost exit 52
  96. r=$?
  97. if [ $r -ne 52 ]; then
  98. fail "ssh failed with multiple certs"
  99. fi
  100. #next, using an agent in combination with the keys
  101. SSH_AUTH_SOCK=/nonexistent ${SSHADD} -l > /dev/null 2>&1
  102. if [ $? -ne 2 ]; then
  103. fatal "ssh-add -l did not fail with exit code 2"
  104. fi
  105. trace "start agent"
  106. eval $(${SSHAGENT} ${EXTRA_AGENT_ARGS} -s) > /dev/null
  107. r=$?
  108. if [ $r -ne 0 ]; then
  109. fatal "could not start ssh-agent: exit code $r"
  110. fi
  111. # add private keys to agent
  112. ${SSHADD} -k $OBJ/user_key2 > /dev/null 2>&1
  113. if [ $? -ne 0 ]; then
  114. fatal "ssh-add did not succeed with exit code 0"
  115. fi
  116. ${SSHADD} -k $OBJ/user_key1 > /dev/null 2>&1
  117. if [ $? -ne 0 ]; then
  118. fatal "ssh-add did not succeed with exit code 0"
  119. fi
  120. # try ssh with the agent and certificates
  121. opts="-F $OBJ/ssh_proxy"
  122. # with no certificates, should fail
  123. ${SSH} $opts somehost exit 52
  124. if [ $? -eq 52 ]; then
  125. fail "ssh connect with agent in succeeded with no cert"
  126. fi
  127. #with an untrusted certificate, should fail
  128. opts="$opts -oCertificateFile=$OBJ/cert_user_key1_2.pub"
  129. ${SSH} $opts somehost exit 52
  130. if [ $? -eq 52 ]; then
  131. fail "ssh connect with agent in succeeded with bad cert"
  132. fi
  133. #with an additional trusted certificate, should succeed
  134. opts="$opts -oCertificateFile=$OBJ/cert_user_key1_1.pub"
  135. ${SSH} $opts somehost exit 52
  136. if [ $? -ne 52 ]; then
  137. fail "ssh connect with agent in failed with good cert"
  138. fi
  139. trace "kill agent"
  140. ${SSHAGENT} -k > /dev/null
  141. #cleanup
  142. rm -f $OBJ/user_ca_key* $OBJ/user_key*
  143. rm -f $OBJ/cert_user_key*