| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package pkcs9 |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "encoding/asn1" |
| 22 | "encoding/base64" |
| 23 | "net/http" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 26 | ) |
| 27 | |
| 28 | // Microsoft non-RFC-3161 timestamping |
| 29 | // https://msdn.microsoft.com/en-us/library/windows/desktop/bb931395(v=vs.85).aspx |
| 30 | |
| 31 | type MicrosoftTimeStampRequest struct { |
| 32 | CounterSignatureType asn1.ObjectIdentifier |
| 33 | Attributes struct{} `asn1:"optional"` |
| 34 | Content struct { |
| 35 | ContentType asn1.ObjectIdentifier |
| 36 | Content []byte `asn1:"explicit,tag:0"` |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func NewLegacyRequest(url string, encryptedDigest []byte) (*http.Request, error) { |
| 41 | var msg MicrosoftTimeStampRequest |
| 42 | msg.CounterSignatureType = OidSpcTimeStampRequest |
| 43 | msg.Content.ContentType = pkcs7.OidData |
| 44 | msg.Content.Content = encryptedDigest |
| 45 | blob, err := asn1.Marshal(msg) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | req, err := http.NewRequest("POST", url, bytes.NewReader(blob)) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | req.Header.Set("Content-Type", "application/octet-stream") |
| 54 | return req, nil |
| 55 | } |
| 56 | |
| 57 | func ParseLegacyResponse(body []byte) (*pkcs7.ContentInfoSignedData, error) { |
| 58 | rblob, err := base64.StdEncoding.DecodeString(string(bytes.TrimRight(body, "\x00"))) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | psd := new(pkcs7.ContentInfoSignedData) |
| 63 | if _, err := asn1.Unmarshal(rblob, psd); err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | return psd, nil |
| 67 | } |