You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

  1. #!/bin/sh
  2. set -e
  3. # avoid problems with sudo path
  4. SYSREPOCFG=`which sysrepocfg`
  5. OPENSSL=`which openssl`
  6. # check that there is no SSH key with this name yet
  7. KEYSTORE_KEY=`$SYSREPOCFG -X -x "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key[name='genkey']/name"`
  8. if [ -z "$KEYSTORE_KEY" ]; then
  9. # generate a new key
  10. PRIVPEM=`$OPENSSL genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -outform PEM 2>/dev/null`
  11. # remove header/footer
  12. PRIVKEY=`grep -v -- "-----" - <<STDIN
  13. $PRIVPEM
  14. STDIN`
  15. # get public key
  16. PUBPEM=`$OPENSSL rsa -pubout 2>/dev/null <<STDIN
  17. $PRIVPEM
  18. STDIN`
  19. # remove header/footer
  20. PUBKEY=`grep -v -- "-----" - <<STDIN
  21. $PUBPEM
  22. STDIN`
  23. # generate edit config
  24. CONFIG="<keystore xmlns=\"urn:ietf:params:xml:ns:yang:ietf-keystore\">
  25. <asymmetric-keys>
  26. <asymmetric-key>
  27. <name>genkey</name>
  28. <algorithm>rsa2048</algorithm>
  29. <public-key>$PUBKEY</public-key>
  30. <private-key>$PRIVKEY</private-key>
  31. </asymmetric-key>
  32. </asymmetric-keys>
  33. </keystore>"
  34. TMPFILE=`mktemp -u`
  35. printf -- "$CONFIG" > $TMPFILE
  36. # apply it to startup and running
  37. $SYSREPOCFG --edit=$TMPFILE -d startup -f xml -m ietf-keystore -v2
  38. $SYSREPOCFG -C startup -m ietf-keystore -v2
  39. # remove the tmp file
  40. rm $TMPFILE
  41. fi