]> git.saurik.com Git - apple/security.git/blame - SecurityTests/clxutils/importExport/importExportOpensshTool
Security-57740.31.2.tar.gz
[apple/security.git] / SecurityTests / clxutils / importExport / importExportOpensshTool
CommitLineData
d8f41ccd
A
1#! /bin/csh -f
2#
3# Run one iteration of OpenSSH portion of import/export tests.
4# Only used as a subroutine call from importExportOpenssh.
5#
6# Arguments (all required):
7#
8# privKey
9# privKeyFormat -- ssh1|ssh1Wrap|ssh2|ssh2Wrap
10# pubKey
11# pubKeyFormat -- ssh1|ssh2
12# algorithm -- rsa|dsa
13# quiet -- YES|NO
14# noACL -- YES|NO
15# securePassphrase -- YES|NO
16# noClean -- YES|NO
17#
18# If incoming private key is wrapped, we assume it has password "password"
19# without the quotes.
20#
21# We assume the presence of keychain $KEYCHAIN_PATH, which we clean out before
22# using.
23#
24if ( $#argv != 9 ) then
25 echo See importExportOpensshTool script for usage.
26 exit(1)
27endif
28
29source setupCommon
30
31set PRIV_KEY_IN=$argv[1]
32set PRIV_KEY_FORM=$argv[2]
33set PUB_KEY_IN=$argv[3]
34set PUB_KEY_FORM=$argv[4]
35set ALG_ARG=
36set KEY_ALG=$argv[5]
37if($KEY_ALG == dsa) then
38 set ALG_ARG="a=d"
39endif
40set QUIET=$argv[6]
41set QUIET_ARG=
42if ($QUIET == YES) then
43 set QUIET_ARG=-q
44endif
45set NOACL_ARG=
46if ($argv[7] == YES) then
47 set NOACL_ARG=-n
48endif
49set SECURE_PHRASE_ARG=
50if ($argv[8] == YES) then
51 set SECURE_PHRASE_ARG=-Z
52endif
53set NOCLEAN=$argv[9]
54
55set SSHKEY=$BUILD_DIR/sshKey
56
57if ($QUIET == NO) then
58 echo -- Testing SSH privFormat $PRIV_KEY_FORM pubFormat $PUB_KEY_FORM --
59endif
60
61#
62# rsatool filename - it has particular requirements here...
63# All of are produced by exporting from a keychain.
64#
65set RSA_TOOL_FILENAME=$BUILD_DIR/rawSshKey
66set RAW_PRIV_KEY=${RSA_TOOL_FILENAME}_priv.der
67set RAW_PUB_KEY=${RSA_TOOL_FILENAME}_pub.der
68
69#
70# The file we sign with rsatool, in cwd, and a signature file.
71#
72set PLAINTEXT=somePlainText
73set SIGFILE=$BUILD_DIR/sig
74
75set PASSWORD=password
76
77# empty the keychain
78if ($QUIET == NO) then
79 echo $CLEANKC
80endif
81$CLEANKC || exit(1)
82
83#
84# Import incoming private key, export to unencrypted format.
85# Wrapped SSH1 --> SSH1
86# Wrapped SSH2 = Wrapped Openssl --> OpenSSL = PKCS1.
87# SSH2 = OpenSSL/PEM = PKCS1/PEM --> OpenSSL = PKCS1.
88#
89# Note
90#
91set RAW_PRIV_KEY_FORM=$PRIV_KEY_FORM
92set DO_CONVERT=no
93# these are for kcImport/kcExport
94set IN_FORM=foo
95set OUT_FORM=foo
96# these are for rsatool
97set RSA_PUB_FORM=foo
98set RSA_PRIV_FORM=foo
99
100switch( $PRIV_KEY_FORM )
101 case ssh1Wrap:
102 set DO_CONVERT=yes
103 set IN_FORM=openssh1
104 set OUT_FORM=openssh1
105 set RSA_PRIV_FORM=s
106 breaksw
107 case ssh2Wrap:
108 set DO_CONVERT=yes
109 set IN_FORM=openssl
110 set OUT_FORM=openssl
111 if($KEY_ALG == rsa) then
112 set RSA_PRIV_FORM=1
113 else
114 set RSA_PRIV_FORM=o
115 endif
116 breaksw
117 case ssh1:
118 set DO_CONVERT=no
119 set IN_FORM=openssh1
120 set OUT_FORM=openssh1
121 set RSA_PRIV_FORM=s
122 breaksw
123 case ssh2:
124 set DO_CONVERT=no
125 set IN_FORM=openssl
126 set OUT_FORM=openssl
127 if($KEY_ALG == rsa) then
128 set RSA_PRIV_FORM=1
129 else
130 set RSA_PRIV_FORM=o
131 endif
132 breaksw
133 default:
134 echo See importExportOpensshTool script for usage.
135 exit(1)
136endsw
137
138#
139# Note we always import private keys with the -e option so we can export in the clear.
140#
141if($DO_CONVERT == yes) then
142 if ($QUIET == NO) then
143 echo Importing wrapped private key...
144 endif
145 set cmd="$KCIMPORT $PRIV_KEY_IN -k $KEYCHAIN -z $PASSWORD -t priv -f $IN_FORM -w -e -K 1 $QUIET_ARG $NOACL_ARG $SECURE_PHRASE_ARG"
146 if ($QUIET == NO) then
147 echo $cmd
148 endif
149 $cmd || exit(1)
150else
151 if ($QUIET == NO) then
152 echo Importing cleartext private key...
153 endif
154 set cmd="$KCIMPORT $PRIV_KEY_IN -k $KEYCHAIN -t priv -f $IN_FORM -e -K 1 $QUIET_ARG $NOACL_ARG"
155 if ($QUIET == NO) then
156 echo $cmd
157 endif
158 $cmd || exit(1)
159endif
160
161#
162# Export to cleartext private key format
163#
164if ($QUIET == NO) then
165 echo Exporting to cleartext private key...
166endif
167set cmd="$KCEXPORT $KEYCHAIN -t privKeys -f $OUT_FORM -o $RAW_PRIV_KEY $QUIET_ARG"
168if ($QUIET == NO) then
169 echo $cmd
170endif
171$cmd || exit(1)
172
173#
174# Import public key, and re-export it. When we're thru with this for all combinations
175# of keys sent to us by importExportOpenssh, we know we can read every format that
176# OpenSSH can generate.
177#
178switch( $PUB_KEY_FORM )
179 case ssh1:
180 set IN_FORM=openssh1
181 set OUT_FORM=openssh1
182 set RSA_PUB_FORM=o
183 breaksw
184 case ssh2:
185 set IN_FORM=openssh2
186 set OUT_FORM=openssh2
187 set RSA_PUB_FORM=O
188 breaksw
189 default:
190 echo See importExportOpensshTool script for usage.
191 exit(1)
192endsw
193
194if ($QUIET == NO) then
195 echo Importing public...
196endif
197set cmd="$KCIMPORT $PUB_KEY_IN -k $KEYCHAIN -t pub -f $IN_FORM -K 1 $QUIET_ARG"
198if ($QUIET == NO) then
199 echo $cmd
200endif
201$cmd || exit(1)
202
203#
204# Export public key
205#
206if ($QUIET == NO) then
207 echo Exporting public key...
208endif
209set cmd="$KCEXPORT $KEYCHAIN -t pubKeys -f $OUT_FORM -o $RAW_PUB_KEY $QUIET_ARG"
210if ($QUIET == NO) then
211 echo $cmd
212endif
213$cmd || exit(1)
214
215#
216# Sign and verify.
217#
218set cmd="$RSATOOL s p=$PLAINTEXT s=$SIGFILE k=$RSA_TOOL_FILENAME v=$RSA_PRIV_FORM $ALG_ARG q"
219if ($QUIET == NO) then
220 echo $cmd
221endif
222$cmd || exit(1)
223set cmd="$RSATOOL v p=$PLAINTEXT s=$SIGFILE k=$RSA_TOOL_FILENAME b=$RSA_PUB_FORM $ALG_ARG q"
224if ($QUIET == NO) then
225 echo $cmd
226endif
227$cmd || exit(1)
228
229#
230# The incoming public key and the one we exported from the keychain should be identical.
231#
232set cmd="$CMP $PUB_KEY_IN $RAW_PUB_KEY"
233if ($QUIET == NO) then
234 echo $cmd
235endif
236$cmd
237if($status != 0) then
238 echo MISCOMPARE on public key form $PUB_KEY_FORM
239 exit(1)
240endif
241
242#
243# Verifying private keys is a little tricker. Only cleartext ssh2 keys can be
244# verified by byte-for-byte compare, and then only after exporting them again
245# in PEM armour. All the others have random data added.
246#
247set PEM_PRIV_KEY=$BUILD_DIR/pemPrivKey
248if($PRIV_KEY_FORM == ssh2) then
249 set cmd="$KCEXPORT $KEYCHAIN -t privKeys -f openssl -p -o $PEM_PRIV_KEY $QUIET_ARG"
250 if ($QUIET == NO) then
251 echo $cmd
252 endif
253 $cmd || exit(1)
254 set cmd="$CMP $PRIV_KEY_IN $PEM_PRIV_KEY"
255 if ($QUIET == NO) then
256 echo $cmd
257 endif
258 $cmd
259 if($status != 0) then
260 echo MISCOMPARE on private key form $PRIV_KEY_FORM
261 exit(1)
262 endif
263endif
264
265#
266# We've verified that we write the following formats identically to OpenSSH:
267# -- public ssh1
268# -- public ssh2
269# -- private unwrapped ssh2 (a.k.a. OpenSSL w/PEM)
270#
271# Since private wrapped SSH2 keys are in a format that we've verified
272# extensively elsewhere (OpenSSL, in the importExportOpensslWrap script),
273# all we really need to test here is ssh1 format. We'll test that
274# via parsing keys with the sshKey program and comparing the output.
275# This also verifies the comments in the generated keys, which should
276# always match the comment of the original key (ssh-keygen key, import
277# that, the resulting C item's PrintName has the comment; export that
278# KC item and the original comment gets embedded in the new key).
279#
280set DONE=no
281set PWDARG=
282switch( $PRIV_KEY_FORM )
283 case ssh1:
284 set PWDARG=-P
285 breaksw
286 case ssh1Wrap:
287 set PWDARG="-p $PASSWORD"
288 breaksw
289 case ssh2Wrap:
290 case ssh2:
291 set DONE=yes
292 breaksw
293 default:
294 echo BRRZZZAP! Missed one here! PRIV_KEY_FORM $PRIV_KEY_FORM
295 exit(1)
296endsw
297
298set PARSE_REF=$BUILD_DIR/parseRef
299set PARSE_GEN=$BUILD_DIR/parseGen
300
301if($DONE == no) then
302
303 if ($QUIET == NO) then
304 echo Parsing $PRIV_KEY_IN and $RAW_PRIV_KEY....
305 endif
306 set cmd="$SSHKEY -i $PRIV_KEY_IN -r -f ssh1 -v $PWDARG"
307 if ($QUIET == NO) then
308 echo $cmd
309 endif
310 $cmd > $PARSE_REF
311 if($status != 0) then
312 echo Error parsing $PRIV_KEY_IN.
313 exit(1)
314 endif
315 set cmd="$SSHKEY -i $RAW_PRIV_KEY -r -f ssh1 -v -P"
316 if ($QUIET == NO) then
317 echo $cmd
318 endif
319 $cmd > $PARSE_GEN
320 if($status != 0) then
321 echo Error parsing $RAW_PRIV_KEY.
322 exit(1)
323 endif
324 set cmd="$CMP $PARSE_GEN $PARSE_REF"
325 if ($QUIET == NO) then
326 echo $cmd
327 endif
328 $cmd
329 if($status != 0) then
330 echo MISCOMPARE on parsed private keys
331 exit(1)
332 endif
333endif
334
335#
336# cleanup
337#
338if($NOCLEAN == NO) then
339 set cmd="rm -f $RAW_PRIV_KEY $RAW_PUB_KEY $SIGFILE $PEM_PRIV_KEY $PARSE_REF $PARSE_GEN"
340 if ($QUIET == NO) then
341 echo $cmd
342 endif
343 $cmd || exit(1)
344endif