1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
|
{-# LANGUAGE DoAndIfThenElse #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE CPP #-}
module Main ( main ) where
import Control.Applicative
import Control.Monad
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.Binary
import Data.Bits
import Data.Char
import Data.IORef
import Data.List
import Data.Maybe
import Data.OpenPGP
import Data.Ord
import Data.Text.Encoding
import System.Environment
import System.Exit
import System.IO (hPutStrLn,stderr)
import qualified Codec.Binary.Base64 as Base64
import qualified Crypto.Hash.RIPEMD160 as RIPEMD160
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as Char8
import qualified Data.Map as Map
import qualified Data.Text as T
import Control.Arrow (first,second)
import Data.Binary.Get (runGet)
import Data.Binary.Put (putWord32be,runPut,putByteString)
import DotLock
import LengthPrefixedBE
import KeyRing
import Base58
import qualified CryptoCoins
import Data.OpenPGP.Util (verify,fingerprint)
-- {-# ANN module ("HLint: ignore Eta reduce"::String) #-}
-- {-# ANN module ("HLint: ignore Use camelCase"::String) #-}
{-
-
-
accBindings :: forall t a a1 a2.
Bits t =>
[(t, (Packet, Packet), [a], [a1], [a2])]
-> [(t, (Packet, Packet), [a], [a1], [a2])]
bitcoinAddress :: Word8 -> Packet -> String
cannonical_eckey :: forall b b1.
(Integral b1, Integral b) =>
b -> b1 -> [Word8]
commands :: [(String, String)]
decode_sshrsa :: Char8.ByteString -> Maybe RSAPublicKey
disjoint_fp :: [Packet] -> [[Packet]]
doAutosign :: forall t. t -> KeyData -> [PacketUpdate]
fpmatch :: Maybe [Char] -> Packet -> Bool
getBindings :: [Packet]
-> ([([Packet], [SignatureOver])],
[(Word8,
(Packet, Packet),
[String],
[SignatureSubpacket],
[Packet])])
isCertificationSig :: SignatureOver -> Bool
isSubkeySignature :: SignatureOver -> Bool
kiki :: forall a.
(Eq a, Data.String.IsString a) =>
a -> [[Char]] -> IO ()
kiki_sync_help :: IO ()
listKeys :: [Packet] -> [Char]
listKeysFiltered :: [[Char]] -> [Packet] -> [Char]
:Main.main :: IO ()
main :: IO ()
partitionStaticArguments :: forall a.
Ord a =>
[(a, Int)] -> [a] -> ([[a]], [a])
readPublicKey :: Char8.ByteString -> RSAPublicKey
show_all :: KeyDB -> IO ()
show_key :: forall t.
String -> t -> Map.Map KeyRing.KeyKey KeyData -> IO ()
show_pem :: String -> String -> KeyDB -> IO ()
show_ssh :: String -> String -> KeyDB -> IO ()
show_whose_key :: Maybe RSAPublicKey -> KeyDB -> IO ()
show_wip :: String -> String -> KeyDB -> IO ()
show_wk :: FilePath
-> Maybe [Char] -> Map.Map KeyRing.KeyKey KeyData -> IO ()
smallpr :: Packet -> [Char]
sshrsa :: Integer -> Integer -> Char8.ByteString
toLast :: forall x. (x -> x) -> [x] -> [x]
verifyBindings :: [Packet]
-> [Packet] -> ([SignatureOver], [SignatureOver])
warn :: String -> IO ()
whoseKey :: RSAPublicKey -> KeyDB -> [KeyData]
-
-
-}
warn str = hPutStrLn stderr str
sshrsa :: Integer -> Integer -> Char8.ByteString
sshrsa e n = runPut $ do
putWord32be 7
putByteString "ssh-rsa"
put (LengthPrefixedBE e)
put (LengthPrefixedBE n)
decode_sshrsa :: Char8.ByteString -> Maybe RSAPublicKey
decode_sshrsa bs = do
let (pre,bs1) = Char8.splitAt 11 bs
guard $ pre == runPut (putWord32be 7 >> putByteString "ssh-rsa")
let rsakey = flip runGet bs1 $ do
LengthPrefixedBE e <- get
LengthPrefixedBE n <- get
return $ RSAKey (MPI n) (MPI e)
return rsakey
isCertificationSig (CertificationSignature {}) = True
isCertificationSig _ = True
isSubkeySignature (SubkeySignature {}) = True
isSubkeySignature _ = False
verifyBindings keys nonkeys = (top ++ filter isSubkeySignature embedded,othersigs)
where
verified = do
sig <- signatures (Message nonkeys)
let v = verify (Message keys) sig
guard (not . null $ signatures_over v)
return v
(top,othersigs) = partition isSubkeySignature verified
embedded = do
sub <- top
let sigover = signatures_over sub
unhashed = sigover >>= unhashed_subpackets
subsigs = mapMaybe backsig unhashed
-- This should consist only of 0x19 values
-- subtypes = map signature_type subsigs
-- trace ("subtypes = "++show subtypes) (return ())
-- trace ("issuers: "++show (map signature_issuer subsigs)) (return ())
sig <- signatures (Message ([topkey sub,subkey sub]++subsigs))
let v = verify (Message [subkey sub]) sig
guard (not . null $ signatures_over v)
return v
smallpr k = drop 24 $ fingerprint k
disjoint_fp ks = {- concatMap group2 $ -} transpose grouped
where
grouped = groupBy samepr . sortBy (comparing smallpr) $ ks
samepr a b = smallpr a == smallpr b
{-
-- useful for testing
group2 :: [a] -> [[a]]
group2 (x:y:ys) = [x,y]:group2 ys
group2 [x] = [[x]]
group2 [] = []
-}
getBindings ::
[Packet]
->
( [([Packet],[SignatureOver])] -- ^ other signatures with key sets
-- that were used for the verifications
, [(Word8,
(Packet, Packet), -- (topkey,subkey)
[String], -- usage flags
[SignatureSubpacket], -- hashed data
[Packet])] -- ^ binding signatures
)
getBindings pkts = (sigs,bindings)
where
(sigs,concat->bindings) = unzip $ do
let (keys,_) = partition isKey pkts
keys <- disjoint_fp keys
let (bs,sigs) = verifyBindings keys pkts
return . ((keys,sigs),) $ do
b <- bs -- trace ("sigs = "++show (map (map signature_issuer . signatures_over) sigs)) bs
i <- map signature_issuer (signatures_over b)
i <- maybeToList i
who <- maybeToList $ find_key fingerprint (Message keys) i
let (code,claimants) =
case () of
_ | who == topkey b -> (1,[])
_ | who == subkey b -> (2,[])
_ -> (0,[who])
let hashed = signatures_over b >>= hashed_subpackets
kind = guard (code==1) >> hashed >>= maybeToList . usage
return (code,(topkey b,subkey b), kind, hashed,claimants)
-- Returned data is simmilar to getBindings but the Word8 codes
-- are ORed together.
accBindings ::
Bits t =>
[(t, (Packet, Packet), [a], [a1], [a2])]
-> [(t, (Packet, Packet), [a], [a1], [a2])]
accBindings bs = as
where
gs = groupBy samePair . sortBy (comparing bindingPair) $ bs
as = map (foldl1 combine) gs
bindingPair (_,p,_,_,_) = pub2 p
where
pub2 (a,b) = (pub a, pub b)
pub a = fingerprint_material a
samePair a b = bindingPair a == bindingPair b
combine (ac,p,akind,ahashed,aclaimaints)
(bc,_,bkind,bhashed,bclaimaints)
= (ac .|. bc,p,akind++bkind,ahashed++bhashed,aclaimaints++bclaimaints)
fpmatch grip key =
(==) Nothing
(fmap (backend (fingerprint key)) grip >>= guard . not)
where
backend xs ys = and $ zipWith (==) (reverse xs) (reverse ys)
listKeys pkts = listKeysFiltered [] pkts
listKeysFiltered grips pkts = do
let (certs,bs) = getBindings pkts
as = accBindings bs
defaultkind (k:_) hs = k
defaultkind [] hs = fromMaybe "subkey"
( listToMaybe
. mapMaybe (fmap usageString . keyflags)
$ hs)
kinds = map (\(_,_,k,h,_)->defaultkind k h) as
kindwidth = maximum $ map length kinds
kindcol = min 20 kindwidth
code (c,(m,s),_,_,_) = (fingerprint_material m,-c)
ownerkey (_,(a,_),_,_,_) = a
sameMaster (ownerkey->a) (ownerkey->b) = fingerprint_material a==fingerprint_material b
matchgrip _ | null grips = True
matchgrip ((code,(top,sub), kind, hashed,claimants):_) | any (flip fpmatch top . Just) grips = True
matchgrip _ = False
gs = filter matchgrip $ groupBy sameMaster (sortBy (comparing code) as)
showsigs claimants = map (\k -> " " ++ "^ signed: " ++ fingerprint k) claimants
subs@((_,(top,_),_,_,_):_) <- gs
let subkeys = do
(code,(top,sub), kind, hashed,claimants) <- subs
let ar = case code of
0 -> " ??? "
1 -> " --> "
2 -> " <-- "
3 -> " <-> "
formkind = take kindcol $ defaultkind kind hashed ++ repeat ' '
-- torhash = fromMaybe "" $ derToBase32 <$> derRSA sub
(netid,kind') = maybe (0x0,"bitcoin")
(\n->(CryptoCoins.publicByteFromName n,n))
$ listToMaybe kind
unlines $
concat [ " "
-- , grip top
, ar
, formkind
, " "
, fingerprint sub
-- , " " ++ torhash
-- , " " ++ (concatMap (printf "%02X") $ S.unpack (ecc_curve sub))
] -- ++ ppShow hashed
: if isCryptoCoinKey sub
-- then (" " ++ "B⃦ " ++ bitcoinAddress sub) : showsigs claimants
-- then (" " ++ "BTC " ++ bitcoinAddress sub) : showsigs claimants
then (" " ++ "¢ "++kind'++":" ++ bitcoinAddress netid sub) : showsigs claimants
else showsigs claimants
torkeys = do
(code,(top,sub), kind, hashed,claimants) <- subs
guard ("tor" `elem` kind)
guard (code .&. 0x2 /= 0)
maybeToList $ derToBase32 <$> derRSA sub
uid = {- fromMaybe "" . listToMaybe $ -} do
(keys,sigs) <- certs
sig <- sigs
guard (isCertificationSig sig)
guard (topkey sig == top)
let issuers = do
sig_over <- signatures_over sig
i <- maybeToList $ signature_issuer sig_over
maybeToList $ find_key (matchpr i) (Message keys) (reverse (take 16 (reverse i)))
(primary,secondary) = partition (==top) issuers
-- trace ("PRIMARY: "++show (map fingerprint primary)) $ return ()
-- trace ("SECONDARY: "++show (map fingerprint secondary)) $ return ()
guard (not (null primary))
let UserIDPacket uid = user_id sig
parsed = parseUID uid
ar = maybe " --> " (const " <-> ") $ do
guard (uid_topdomain parsed == "onion" )
guard ( uid_realname parsed `elem` ["","Anonymous"])
guard ( uid_user parsed == "root" )
let subdom0 = L.fromChunks [encodeUtf8 (uid_subdomain parsed)]
len = L.length subdom0
subdom = Char8.unpack subdom0
match = (==subdom) . take (fromIntegral len)
guard (len >= 16)
listToMaybe $ filter match torkeys
unlines $ (" " ++ ar ++ "@" ++ " " ++ uid_full parsed) : showsigs secondary
-- (_,sigs) = unzip certs
"master-key " ++ fingerprint top ++ "\n" ++ uid ++" ...\n" ++ subkeys ++ "\n"
{-
- modify a UID to test the verify function properly
- fails
modifyUID (UserIDPacket str) = UserIDPacket str'
where
(fstname,rst) = break (==' ') str
str' = mod fstname ++ rst
mod "Bob" = "Bob Slacking"
mod x = x
modifyUID other = other
-}
readPublicKey :: Char8.ByteString -> RSAPublicKey
readPublicKey bs = fromMaybe er $ do
let (pre,bs1) = Char8.splitAt 7 bs
guard $ pre == "ssh-rsa"
let (sp,bs2) = Char8.span isSpace bs1
guard $ not (Char8.null sp)
bs3 <- listToMaybe $ Char8.words bs2
qq <- L.pack `fmap` Base64.decode (Char8.unpack bs3)
decode_sshrsa qq
where
er = error "Unsupported key format"
-- | Returns the given list with its last element modified.
toLast :: (x -> x) -> [x] -> [x]
toLast f [] = []
toLast f [x] = [f x]
toLast f (x:xs) = x : toLast f xs
-- partitionStaticArguments :: Ord a => [(a, Int)] -> [a] -> ([[a]], [a])
partitionStaticArguments specs args = psa args
where
smap = Map.fromList specs
psa [] = ([],[])
psa (a:as) =
case Map.lookup a smap of
Nothing -> second (a:) $ psa as
Just n -> first ((a:take n as):) $ psa (drop n as)
show_wk secring_file grip db = do
let sec_db = Map.filter gripmatch db
gripmatch (KeyData p _ _ _) =
Map.member secring_file (locations p)
Message sec = flattenKeys False sec_db
putStrLn $ listKeysFiltered (maybeToList grip) sec
show_all db = do
let Message packets = flattenKeys True db
putStrLn $ listKeys packets
show_whose_key input_key db =
flip (maybe $ return ()) input_key $ \input_key -> do
let ks = whoseKey input_key db
case ks of
[KeyData k _ uids _] -> do
putStrLn $ fingerprint (packet k)
mapM_ putStrLn $ Map.keys uids
(_:_) -> error "ambiguous"
[] -> return ()
show_pem keyspec wkgrip db = do
let s = parseSpec wkgrip keyspec
flip (maybe . void $ warn (keyspec ++ ": not found"))
(selectPublicKey s db)
$ \k -> do
let rsa = pkcs8 . fromJust $ rsaKeyFromPacket k
der = encodeASN1 DER (toASN1 rsa [])
qq = Base64.encode (L.unpack der)
putStrLn $
writePEM "PUBLIC KEY" qq -- ("TODO "++show keyspec)
show_ssh keyspec wkgrip db = do
let s = parseSpec wkgrip keyspec
flip (maybe . void $ warn (keyspec ++ ": not found"))
(selectPublicKey s db)
$ \k -> do
let Just (RSAKey (MPI n) (MPI e)) = rsaKeyFromPacket k
bs = sshrsa e n
blob = Base64.encode (L.unpack bs)
putStrLn $ "ssh-rsa " ++ blob
show_key keyspec wkgrip db = do
let s = parseSpec "" keyspec
let ps = do
(_,k) <- filterMatches (fst s) (Map.toList db)
mp <- flattenTop "" True k
return $ packet mp
-- putStrLn $ "show key " ++ show s
putStrLn $ listKeys ps
show_wip keyspec wkgrip db = do
let s = parseSpec wkgrip keyspec
flip (maybe $ void (warn (keyspec ++ ": not found")))
(selectSecretKey s db)
$ \k -> do
let nwb = maybe 0x80 CryptoCoins.secretByteFromName $ snd s
putStrLn $ walletImportFormat nwb k
cannonical_eckey x y = 0x4:pad32(numToBytes x) ++ pad32(numToBytes y) :: [Word8]
where
numToBytes n = reverse $ unfoldr getbyte n
where
getbyte d = do
guard (d/=0)
let (q,b) = d `divMod` 256
return (fromIntegral b,q)
pad32 xs = replicate zlen 0 ++ xs
where
zlen = 32 - length xs
bitcoinAddress network_id k = address
where
Just (MPI x) = lookup 'x' (key k)
Just (MPI y) = lookup 'y' (key k)
pub = cannonical_eckey x y
hash = S.cons network_id . RIPEMD160.hash . SHA256.hash . S.pack $ pub
address = base58_encode hash
whoseKey :: RSAPublicKey -> KeyDB -> [KeyData]
whoseKey rsakey db = filter matchkey (Map.elems db)
where
matchkey (KeyData k _ _ subs) =
any (ismatch k) $ Map.elems subs
ismatch k (SubKey mp sigs) =
Just rsakey == rsaKeyFromPacket (packet mp)
&& any (check (packet k) (packet mp)) sigs
check k sub (sig,_) = not . null $ do
s <- signatures . Message $ [k,sub,packet sig]
fw <- signatures_over $ verify (Message [k]) s
subsig <- mapMaybe backsig (unhashed_subpackets $ packet sig)
subsig_so <- signatures (Message [k,sub,subsig])
guard ( isSubkeySignature subsig_so
&& isSameKey (topkey subsig_so) k
&& isSameKey (subkey subsig_so) sub )
s2 <- signatures . Message $ [k,sub,subsig]
signatures_over $ verify (Message [sub]) s2
isSameKey a b = sort (key apub) == sort (key bpub)
where
apub = secretToPublic a
bpub = secretToPublic b
kiki_usage bSecret cmd = putStr $
case cmd of
"show" -> unlines $
["kiki show [options...]"
,""
," show displays infomration about keys stored in the data files which resides in"
," the home directory (see --homedir)."
,""
," The files pubring.gpg and subring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set."
,""
," Subkeys that are imported with kiki are given an annotation \"usage@\" which"
," indicates what the key is for. This tag can be used as a SPEC to select a"
," particular key. Master keys may be specified by using fingerprints or by"
," specifying a substring of an associated UID."
,"Options: "
] ++ commonOptions ++
[" --working"
," Show fingerprints for the working key (which will be used to"
," make signatures) and all its subkeys and UID."
,""
," --key SPEC"
," Show fingerprints for the specified key and all its subkeys"
," and UID."
,""
," --all Show fingerprints and UIDs and usage tags for all known keys."
,""
," --whose-key"
," Shows the fingerprint and UIDs of the key that owns the one that"
," is input on stdin in ssh-rsa format."
,""
," --pem SPEC"
," Outputs the PKCS #8 public key corresponding to SPEC."
,""
," --ssh SPEC"
," Outputs the ssh-rsa blob for the specified public key."
,""
," --wip SPEC"
," Outputs the secret crypto-coin key in Wallet Input Format."
,""
," --help Shows this help screen."
,""
] ++ keyspec
"sync-secret" -> unlines $
["kiki sync-secret [options...]"
,""
," sync-secret merges a set of key files into a combined database and then"
," uses the database to update all the input files, those inside and outside of"
," of the home directory (see --homedir), to have the most complete information."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set."
,""
," Subkeys that are imported with kiki are given an annotation \"usage@\" which"
," indicates what the key is for. This tag can be used as a SPEC to select a"
," particular key. Master keys may be specified by using fingerprints or by"
," specifying a substring of an associated UID."
] ++ syncflags ++ keyspec
"sync-public" -> unlines $
["kiki sync-public [options...]"
,""
," sync-public merges a set of key files into a combined database and then"
," uses the database to update all the input files, those inside and outside of"
," of the home directory (see --homedir), to have the most complete information."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set. However, the"
," difference betwen this command and sync-secret is that no secret keys are"
," modified by this command regardless of input. Export of secret keys is"
," possible using this command, but will only occur if the secret master key"
," is already in the external file. (TODO, remove this capacity entirely)"
,""
," Subkeys that are imported with kiki are given an annotation \"usage@\" which"
," indicates what the key is for. This tag can be used as a SPEC to select a"
," particular key. Master keys may be specified by using fingerprints or by"
," specifying a substring of an associated UID."
] ++ syncflags ++ keyspec
"import-secret" -> unlines $
["kiki import-secret [options...]"
,""
," import-secret uses a set of key files to update your keyring. It does not"
," alter any files outside of the home directory (see --homedir)."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set."
,""
," Subkeys that are imported with kiki are given an annotation \"usage@\" which"
," indicates what the key is for. This tag can be used as a SPEC to select a"
," particular key. Master keys may be specified by using fingerprints or by"
," specifying a substring of an associated UID."
] ++ syncflags ++ keyspec
"import-public" -> unlines $
["kiki import-public [options...]"
,""
," import-public uses a set of key files to update your keyring. It does not"
," alter any files outside of the home directory (see --homedir). Nor does it"
," alter your secring.gpg file."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set. However, the"
," difference betwen this command and import-secret is that no secret keys are"
," modified by this command regardless of input. Export of secret keys is"
," possible using this command, but will only occur if the secret master key"
," is already in the external file. (TODO, remove this capacity entirely)"
,""
," Subkeys that are imported with kiki are given an annotation \"usage@\" which"
," indicates what the key is for. This tag can be used as a SPEC to select a"
," particular key. Master keys may be specified by using fingerprints or by"
," specifying a substring of an associated UID."
] ++ syncflags ++ keyspec
"export-secret" -> unlines $
["kiki export-secret [options...]"
,""
," export-secret updates a set of key files using information from your keyring."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set."
,""
] ++ syncflags ++ keyspec
"export-public" -> unlines $
["kiki export-public [options...]"
,""
," export-public updates a set of key files using information from your keyring."
,""
," The files pubring.gpg and secring.gpg in the directory specified by the "
," --homedir option are implicitly included in the keyring set. However, the"
," difference betwen this command and export-secret is that no secret keys are"
," exported by this command regardless of input."
,""
] ++ syncflags ++ keyspec
where
commonOptions :: [String]
commonOptions =
[" --homedir DIR"
," Where to find the the files secring.gpg and pubring.gpg. The "
," default location is taken from the environment variable "
," GNUPGHOME."
,""
," --passphrase-fd N"
," Read passphrase from the given file descriptor."
,""
]
syncflags :: [String]
syncflags =
[""
,"Flags:"] ++ commonOptions ++
[" --import Add master keys to pubring.gpg. Without this option, only UID"
," and subkey data is updated. "
,""
," --import-if-authentic"
," Add signed master keys to pubring.gpg. Like --import except that"
," only keys with signatures from the working key (--show-wk) are"
," imported."
,""
," --autosign Sign all cross-certified tor-style UIDs."
," A tor-style UID is of the form:"
," Anonymous <root@HOSTNAME.onion>"
," It is considered cross certified if there exists a cross-certified"
," 'tor' subkey corresponding to the address HOSTNAME.onion."
,""
,"Merging:"
," --keyrings FILE FILE..."
," Provide keyring files other than the implicit secring.gpg and"
," pubring.gpg in the --homedir. This option is implicit unless"
," --keypairs or --wallets is used."
,""]
++ do
guard bSecret
return $ unlines
[" --wallets FILE FILE..."
," Provide wallet files with secret crypto-coin keys in Wallet"
," Import Format. The keys will be treated as subkeys of your"
," current working key (the one shown by --show-wk)."
,""
," --keypairs KEYSPEC KEYSPEC..."
," A keypair is a secret key coupled with it's corresponding public"
," key, both of which are ordinarily stored in a single file in pem"
," format. Users incognisant of the fact that the public key (which"
," is also stored separately) is in this file, often think of it as"
," their secret key file."
,""
," Each KEYSPEC specifies that a key should match the content and"
," timestamp of an external PKCS #1 private RSA key file."
," "
," KEYSPEC ::= SPEC=FILE{CMD} "
,""
," The form of SPEC is documented below. If there is only one master"
," key in your keyring and only one key is used for each purpose, then"
," it is possible for SPEC in this case to merely be a tag which offers"
," information about what this key is used for, for example, any of"
," `tor', `ssh-client', `ssh-host', or `strongswan' will do."
,""
," If neither SPEC or FILE match any keys, then the CMD will be "
," executed in order to create the FILE."
,""]
keyspec = -- unlines $
["Specifying keys on the kiki command line:"
,""
," SPEC ::= MASTER/SUBKEY"
,""
," SPEC indicates a specific key in the keyring, in it's longest incarnation,"
," it is of the form MASTER/SUBKEY where MASTER and SUBKEY are documented below."
," If kiki can infer the key unambiguously, either via the command in question or"
," the contents of the keyring, then it is permissable to ommit either MASTER or"
," SUBKEY, in which case the slash may also be ommitted unless it is used via its"
," position to indicate whether a SUBKEY or MASTER is intended."
,""
," MASTER may be any of"
," * The tail end of a fingerprint prefixed by 'fp:'"
," * A sub-string of a user id (without slashes) prefixed by 'u:'"
," * 40 characters of hexidecimal (kiki will assume this to be a fingerprint)"
," * A sub-string of a user id (without slashes, the prefix 'u:' is optional)"
,""
," SUBKEY may be any of"
," * The tail end of a fingerprint prefixed by 'fp:'"
," * An exact match of a usage tag prefixed by 't:'"
," * 40 characters of hexidecimal (kiki will assume this to be a fingerprint)"
," * An exact match of a usage tag (The prefix 't:' is optional)"
,""
," In parsing the spec, kiki will attempt to match the string to one of the"
," above formats, in the order presented."
,""
," Examples of valid SPEC strings:"
,""
," fp:4A39F/tor"
," u:joe/tor"
," u:joe/t:tor"
," u:joe/fp:4abf30"
," joe/tor"
," 5E24CD442AA6965D2012E62A905C24185D5379C2"
]
doAutosign rt kd@(KeyData k ksigs umap submap) = ops
where
ops = map (\u -> InducerSignature u []) us
us = filter torStyle $ Map.keys umap
torStyle str = and [ uid_topdomain parsed == "onion"
, uid_realname parsed `elem` ["","Anonymous"]
, uid_user parsed == "root"
, fmap (match . fst) (lookup (packet k) torbindings)
== Just True ]
where parsed = parseUID str
match = (==subdom) . take (fromIntegral len)
subdom0 = L.fromChunks [encodeUtf8 (uid_subdomain parsed)]
subdom = Char8.unpack subdom0
len = T.length (uid_subdomain parsed)
torbindings = getTorKeys (map packet $ flattenTop "" True kd)
getTorKeys pub = do
xs <- groupBindings pub
(_,(top,sub),us,_,_) <- xs
guard ("tor" `elem` us)
let torhash = fromMaybe "" $ derToBase32 <$> derRSA sub
return (top,(torhash,sub))
groupBindings pub = gs
where (_,bindings) = getBindings pub
bindings' = accBindings bindings
code (c,(m,s),_,_,_) = (fingerprint_material m,-c)
ownerkey (_,(a,_),_,_,_) = a
sameMaster (ownerkey->a) (ownerkey->b)
= fingerprint_material a==fingerprint_material b
gs = groupBy sameMaster (sortBy (comparing code) bindings')
processArgs sargspec polyVariadicArgs defaultPoly args_raw = (sargs,margs)
where
(args,trail1) = break (=="--") args_raw
trail = drop 1 trail1
commonArgSpec = [ ("--homedir",1)
, ("--passphrase-fd",1)
, ("--help",0)
]
sargspec' = commonArgSpec ++ sargspec
(sargs,margs) =
(sargs, foldl' (\m (k:xs)->Map.alter (appendArgs k xs) k m)
Map.empty
gargs)
where (sargs,vargs) = partitionStaticArguments sargspec' args
argspec = map fst sargspec' ++ polyVariadicArgs
args' = if map (take 1) (take 1 vargs) == ["-"]
then vargs
else defaultPoly:vargs
-- grouped args
gargs = (sargs ++)
. toLast (++trail)
. groupBy (\_ s-> take 1 s /= "-")
$ args'
appendArgs k xs opt =
if k `elem` argspec
then Just . maybe xs (++xs) $ opt
else error . unlines $ [ "unrecognized option "++k
, "Use --help for usage." ]
data CommonArgsParsed = CommonArgsParsed { cap_homespec :: Maybe String, cap_passfd :: Maybe InputFile }
parseCommonArgs margs = CommonArgsParsed { cap_homespec = homespec, cap_passfd = passfd }
where
passphrase_fd = concat <$> Map.lookup "--passphrase-fd" margs
homespec = join . take 1 <$> Map.lookup "--homedir" margs
passfd = fmap (FileDesc . read) passphrase_fd
parseKeySpecs = map $ \specfile -> do
let (spec,efilecmd) = break (=='=') specfile
guard $ take 1 efilecmd=="="
let filecmd = drop 1 efilecmd
let (file,bcmdb0) = break (=='{') filecmd
bcmdb = if null bcmdb0 then "{}" else bcmdb0
guard $ take 1 bcmdb=="{"
let bdmcb = (dropWhile isSpace . reverse) bcmdb
guard $ take 1 bdmcb == "}"
let cmd = (drop 1 . reverse . drop 1) bdmcb
Just (spec,file,cmd)
--kiki :: (Eq a, Data.String.IsString a) => a -> [String] -> IO ()
sync bExport bImport bSecret cmdarg args_raw = do
let (sargs,margs) = processArgs sargspec polyVariadicArgs "--keyrings" args_raw
sargspec = [ ("--import",0)
, ("--autosign",0)
, ("--import-if-authentic",0)
, ("--show-wk",0)
{-, ("--show-all",0)
, ("--show-whose-key",0)
, ("--show-key",1)
, ("--show-pem",1)
, ("--show-ssh",1)
, ("--show-wip",1) -}
]
polyVariadicArgs = ["--keyrings"
,"--hosts" ]
++ do guard bSecret
[ "--keypairs", "--wallets" ]
-- putStrLn $ "margs = " ++ show (Map.assocs margs)
unkeysRef <- newIORef Map.empty
pwRef <- newIORef Nothing
let keypairs0 = parseKeySpecs (fromMaybe [] $ Map.lookup "--keypairs" margs)
keyrings_ = fromMaybe [] $ Map.lookup "--keyrings" margs
wallets = fromMaybe [] $ Map.lookup "--wallets" margs
passphrase_fd = concat <$> Map.lookup "--passphrase-fd" margs
when (any isNothing keypairs0) $ do
warn "Syntax error in key pair specification"
exitFailure
input_key <- maybe (return Nothing)
(const $ fmap (Just . readPublicKey) Char8.getContents)
$ Map.lookup "--show-whose-key" margs
let keypairs = catMaybes keypairs0
homespec = join . take 1 <$> Map.lookup "--homedir" margs
passfd = fmap (FileDesc . read) passphrase_fd
reftyp = if bExport then MutableRef Nothing
else ConstRef
pems = flip map keypairs
$ \(usage,path,cmd) ->
let cmd' = mfilter (not . null) (Just cmd)
in if bExport
then (ArgFile path, (MutableRef cmd', PEMFile usage))
else if isNothing cmd'
then (ArgFile path, (ConstRef, PEMFile usage))
else error "Unexpected PEM file initializer."
walts = map (\fname -> (ArgFile fname, (reftyp, WalletFile)))
wallets
rings = map (\fname -> (ArgFile fname, (reftyp, KeyRingFile passfd)))
keyrings_
hosts = maybe [] (map decorate) $ Map.lookup "--hosts" margs
where decorate fname = (ArgFile fname, (reftyp, Hosts))
importStyle = maybe (\_ _ -> subkeysOnly)
(\f rt kd -> f rt kd >> importPublic)
$ mplus import_f importifauth_f
where
import_f = do Map.lookup "--import" margs
return $ \rt kd -> Just ()
importifauth_f = do Map.lookup "--import-if-authentic" margs
return guardAuthentic
kikiOp = KeyRingOperation
{ kFiles = Map.fromList $
[ ( HomeSec, (if bSecret && bImport then MutableRef Nothing else ConstRef, KeyRingFile passfd) )
, ( HomePub, (if bImport then MutableRef Nothing else ConstRef, KeyRingFile Nothing) )
]
++ rings
++ if bSecret then pems else []
++ if bSecret then walts else []
++ hosts
, kImports = Map.fromList [ ( HomePub, importStyle ) ]
, kManip = maybe noManip (const doAutosign) $ Map.lookup "--autosign" margs
, homeSpec = homespec
}
(\f -> maybe f (const $ kiki_usage bSecret cmdarg) $ Map.lookup "--help" margs) $ do
KikiResult rt report <- runKeyRing kikiOp
case rt of
KikiSuccess rt -> do -- interpret --show-* commands.
let grip = rtGrip rt
let shspec = Map.fromList [("--show-wk", const $ show_wk (rtSecring rt) grip)
{-,("--show-all",const show_all)
,("--show-whose-key", const $ show_whose_key input_key)
,("--show-key",\[x] -> show_key x $ fromMaybe "" grip)
,("--show-pem",\[x] -> show_pem x $ fromMaybe "" grip)
,("--show-ssh",\[x] -> show_ssh x $ fromMaybe "" grip)
,("--show-wip",\[x] -> show_wip x $ fromMaybe "" grip)-}
]
shargs = mapMaybe (\(x:xs) -> (,xs) <$> Map.lookup x shspec) sargs
forM_ shargs $ \(cmd,args) -> cmd args (rtKeyDB rt)
err -> putStrLn $ errorString err
forM_ report $ \(fname,act) -> do
putStrLn $ fname ++ ": " ++ reportString act
kiki "sync-secret" args_raw =
sync True True True "sync-secret" args_raw
kiki "sync-public" args_raw =
sync True True False "sync-public" args_raw
kiki "import-secret" args_raw =
sync False True True "import-secret" args_raw
kiki "import-public" args_raw =
sync False True False "import-public" args_raw
kiki "export-secret" args_raw =
sync True False True "export-secret" args_raw
kiki "export-public" args_raw =
sync True False False "export-public" args_raw
kiki "working-key" args = do
if "--help" `notElem` args
then sync False False False "working-key" ["--show-wk"]
else putStrLn $
unlines ["working-key"
,""
," Displays the master key with its subkeys that will be"
," used for making signatures"]
-- Generic help
kiki "help" [] = do
putStrLn "Valid commands are:"
let longest = maximum $ map (length . fst) commands
pad cmd = take (longest+3) $ cmd ++ repeat ' '
forM commands $ \(cmd,help) -> do
putStrLn $ " " ++ pad cmd ++ help
putStr . unlines $ [""
,"See 'kiki help <command>' for more information on a specific command."
]
return ()
kiki "help" args = forM_ args $ \arg -> case lookup arg commands of
Nothing -> putStrLn $ "No help available for commmand '" ++ arg ++ "'."
_ -> kiki arg ["--help"]
kiki "show" args = do
let (sargs,margs) = processArgs sargspec polyVariadicArgs "--show" args
sargspec = [ ("--working",0) --("--show-wk",0)
, ("--all",0) --("--show-all",0)
, ("--whose-key",0)
, ("--key",1)
, ("--pem",1)
, ("--ssh",1)
, ("--wip",1)
]
polyVariadicArgs = ["--show"]
let cap = parseCommonArgs margs
homespec = cap_homespec cap
passfd = cap_passfd cap
pems = []
rings = []
hosts = []
walts = []
kikiOp = KeyRingOperation
{ kFiles = Map.fromList $
[ ( HomeSec, (ConstRef, KeyRingFile passfd) )
, ( HomePub, (ConstRef, KeyRingFile Nothing) )
]
++ rings
++ pems
++ walts
++ hosts
, kImports = Map.empty
, kManip = noManip
, homeSpec = homespec
}
(\f -> maybe f (const $ kiki_usage False "show") $ Map.lookup "--help" margs) $ do
KikiResult rt report <- runKeyRing kikiOp
input_key <- maybe (return Nothing)
(const $ fmap (Just . readPublicKey) Char8.getContents)
$ Map.lookup "--whose-key" margs
case rt of
KikiSuccess rt -> do -- interpret --show-* commands.
let grip = rtGrip rt
let shspec = Map.fromList [("--working", const $ show_wk (rtSecring rt) grip)
,("--all",const show_all)
,("--whose-key", const $ show_whose_key input_key)
,("--key",\[x] -> show_key x $ fromMaybe "" grip)
,("--pem",\[x] -> show_pem x $ fromMaybe "" grip)
,("--ssh",\[x] -> show_ssh x $ fromMaybe "" grip)
,("--wip",\[x] -> show_wip x $ fromMaybe "" grip)
]
shargs = mapMaybe (\(x:xs) -> (,xs) <$> Map.lookup x shspec) sargs
forM_ shargs $ \(cmd,args) -> cmd args (rtKeyDB rt)
err -> putStrLn $ errorString err
forM_ report $ \(fname,act) -> do
putStrLn $ fname ++ ": " ++ reportString act
commands :: [(String,String)]
commands =
[ ( "help", "display usage information" )
--, ( "sync", "update key files of various kinds by propogating information" )
, ( "show", "display information from your keyrings")
, ( "sync-secret", "update key files of various kinds by propogating information (both secret and public)" )
, ( "sync-public", "update key files of various kinds by propogating public information" )
, ( "import-secret", "import (both public and secret) information into your keyring" )
, ( "import-public", "import (public) information into your keyring" )
, ( "export-secret", "export (both public and secret) information into your keyring" )
, ( "export-public", "import (public) information into your keyring" )
, ( "working-key", "show the current working master key and its subkeys" )
]
main = do
dotlock_init
args_raw <- getArgs
case args_raw of
[] -> kiki "working-key" []
cmd : args | cmd `elem` map fst commands
-> kiki cmd args
_ -> kiki "help" [] --args_raw
|