001 | #! /usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | # JPKIの証明書ダウンロード |
---|
005 | # |
---|
006 | #com.cocolog-nifty.quicktimer.icefloe |
---|
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
008 | use AppleScript version "2.8" |
---|
009 | use framework "Foundation" |
---|
010 | use framework "AppKit" |
---|
011 | use scripting additions |
---|
012 | |
---|
013 | property refMe : a reference to current application |
---|
014 | |
---|
015 | |
---|
016 | #ダウンロードするファイルとHASH値 |
---|
017 | set strSign01 to ("https://www.jpki.go.jp/ca/pdf/signca01.cer") as text |
---|
018 | set strSign02 to ("https://www.jpki.go.jp/ca/pdf/signca02.cer") as text |
---|
019 | set strSign03 to ("https://www.jpki.go.jp/ca/pdf/signca03.cer") as text |
---|
020 | # |
---|
021 | set strSignHash01 to ("19645405a1fe14377434bd55957628ac4038557c542403a2243f21c706fc9355") as text |
---|
022 | set strSignHash02 to ("79679c33e4cc9319440f1ad120a597ff1844e2ef217063adb176966fd5e6fbeb") as text |
---|
023 | set strSignHash03 to ("d227f6cde11d35c5252178f106f843d24651944975413b539fa2fb68dbfa365f") as text |
---|
024 | # |
---|
025 | set strAuth01 to ("https://www.jpki.go.jp/ca/pdf/authca01.cer") as text |
---|
026 | set strAuth02 to ("https://www.jpki.go.jp/ca/pdf/authca02.cer") as text |
---|
027 | set strAuth03 to ("https://www.jpki.go.jp/ca/pdf/authca03.cer") as text |
---|
028 | # |
---|
029 | set strAuthHash01 to ("4dcfc4ca0aaf9fb2a3081b3f0c991c320b3acc008667a341eeaec5af1d58ca7d") as text |
---|
030 | set strAuthHash02 to ("0d98de47fc11b7f67126874b7028b6026a803d2638d6edfe53176953961fade6") as text |
---|
031 | set strAuthHash03 to ("64114f29620c24ed2631d46476a040e7ee740d956089c00b036a6a8118c322b5") as text |
---|
032 | |
---|
033 | |
---|
034 | #保存先のURLとHASHをDICTにしておく |
---|
035 | set ocidURLDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
036 | ocidURLDict's setValue:(strSignHash01) forKey:(strSign01) |
---|
037 | ocidURLDict's setValue:(strSignHash02) forKey:(strSign02) |
---|
038 | ocidURLDict's setValue:(strSignHash03) forKey:(strSign03) |
---|
039 | # |
---|
040 | ocidURLDict's setValue:(strAuthHash01) forKey:(strAuth01) |
---|
041 | ocidURLDict's setValue:(strAuthHash02) forKey:(strAuth02) |
---|
042 | ocidURLDict's setValue:(strAuthHash03) forKey:(strAuth03) |
---|
043 | |
---|
044 | #保存先確保 |
---|
045 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
046 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
047 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
---|
048 | #保存先フォルダパス |
---|
049 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Adobe/Acrobat/JPKI証明書") isDirectory:(true) |
---|
050 | #フォルダ生成 |
---|
051 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
052 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
---|
053 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
054 | if (item 1 of listDone) is true then |
---|
055 | log "正常処理A" |
---|
056 | else if (item 2 of listDone) ≠ (missing value) then |
---|
057 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
058 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
059 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
060 | return "エラーしました" & strErrorNO & strErrorMes |
---|
061 | end if |
---|
062 | |
---|
063 | #DICTのキーのリスト |
---|
064 | set ocidAllKey to ocidURLDict's allKeys() |
---|
065 | set numCntKey to (ocidAllKey's |count|()) as integer |
---|
066 | #キーの数だけ繰り返し |
---|
067 | repeat with itemNo from 0 to (numCntKey - 1) by 1 |
---|
068 | set ocidItemKey to (ocidAllKey's objectAtIndex:(itemNo)) |
---|
069 | #URLにして |
---|
070 | set ocidURLString to (refMe's NSString's stringWithString:(ocidItemKey)) |
---|
071 | set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLString)) |
---|
072 | set strURL to ocidURL's absoluteString() as text |
---|
073 | #ファイル名 |
---|
074 | set ocidFileName to ocidURL's lastPathComponent() |
---|
075 | #HASH値を取得しておく |
---|
076 | set strHash to (ocidURLDict's valueForKey:(ocidItemKey)) as text |
---|
077 | #ダウンロード |
---|
078 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
079 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)) |
---|
080 | if (item 2 of listResponse) = (missing value) then |
---|
081 | log "正常処理B" |
---|
082 | #ダウンロードしたデータ |
---|
083 | set ocidCerData to (item 1 of listResponse) |
---|
084 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
085 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
086 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
087 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
088 | return "エラーしました" & strErrorNO & strErrorMes |
---|
089 | end if |
---|
090 | #保存先ディレクトリ(テンポラリ) |
---|
091 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
---|
092 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
---|
093 | set ocidUUIDString to ocidUUID's UUIDString |
---|
094 | set ocidTmpDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true) |
---|
095 | set listDone to (appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
---|
096 | if (item 1 of listDone) is true then |
---|
097 | log "正常処理C" |
---|
098 | else if (item 2 of listDone) ≠ (missing value) then |
---|
099 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
100 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
101 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
102 | return "エラーしました" & strErrorNO & strErrorMes |
---|
103 | end if |
---|
104 | #テンポラリーに保存するファイルパス |
---|
105 | set ocidTmpFilePathURL to (ocidTmpDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
---|
106 | set strTmpFilePath to ocidTmpFilePathURL's |path| as text |
---|
107 | #保存 |
---|
108 | set ocidOption to (refMe's NSDataWritingAtomic) |
---|
109 | set listDone to (ocidCerData's writeToURL:(ocidTmpFilePathURL) options:(ocidOption) |error| :(reference)) |
---|
110 | if (item 1 of listDone) is true then |
---|
111 | log "正常処理C" |
---|
112 | else if (item 2 of listDone) ≠ (missing value) then |
---|
113 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
114 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
115 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
116 | return "エラーしました" & strErrorNO & strErrorMes |
---|
117 | end if |
---|
118 | #HASHチェック |
---|
119 | set strCommandText to ("/bin/zsh -c '/usr/bin/shasum -a 256 \"" & strTmpFilePath & "\"' | /usr/bin/awk '{print $1}'") as text |
---|
120 | log strCommandText |
---|
121 | set strGetHash to (do shell script strCommandText) as text |
---|
122 | if strGetHash is strHash then |
---|
123 | #移動先 書類フォルダ |
---|
124 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
---|
125 | set listDone to (appFileManager's moveItemAtURL:(ocidTmpFilePathURL) toURL:(ocidSaveFilePathURL) |error| :(reference)) |
---|
126 | if (item 1 of listDone) is true then |
---|
127 | log "正常処理D" |
---|
128 | set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text |
---|
129 | # |
---|
130 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
131 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
---|
132 | set ocidUserKeyChainPathURL to (ocidLibraryDirPathURL's URLByAppendingPathComponent:("Keychains/login.keychain") isDirectory:(false)) |
---|
133 | set strUserKeyChainPath to (ocidUserKeyChainPathURL's |path|()) as text |
---|
134 | |
---|
135 | set strCommandText to ("/bin/zsh -c '/usr/bin/security import \"" & strSaveFilePath & "\" -k \"" & strUserKeyChainPath & "\" -T /usr/bin/codesign'") as text |
---|
136 | log strCommandText |
---|
137 | try |
---|
138 | set strResponse to (do shell script strCommandText) as text |
---|
139 | on error |
---|
140 | log "証明書の読み込みでエラーしました" |
---|
141 | end try |
---|
142 | else if (item 2 of listDone) ≠ (missing value) then |
---|
143 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
144 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
145 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
146 | log "エラーしました" & strErrorNO & strErrorMes |
---|
147 | end if |
---|
148 | else |
---|
149 | return "HASH値に相違があります" |
---|
150 | end if |
---|
151 | |
---|
152 | |
---|
153 | end repeat |
---|
154 | |
---|
155 | |
---|
156 | #保存先を開く |
---|
157 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
158 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
---|
159 | |
---|
160 | |
---|
161 | |
---|