@@ -115,6 +115,13 @@ type basicResponse struct {
115
115
Certificates []asn1.RawValue `asn1:"explicit,tag:0,optional"`
116
116
}
117
117
118
+ type basicResponseRawTBS struct {
119
+ TBSResponseData asn1.RawValue
120
+ SignatureAlgorithm pkix.AlgorithmIdentifier
121
+ Signature asn1.BitString
122
+ Certificates []asn1.RawValue `asn1:"explicit,tag:0,optional"`
123
+ }
124
+
118
125
type responseData struct {
119
126
Raw asn1.RawContent
120
127
Version int `asn1:"optional,default:0,explicit,tag:0"`
@@ -181,32 +188,50 @@ var signatureAlgorithmDetails = []struct {
181
188
{x509 .ECDSAWithSHA512 , oidSignatureECDSAWithSHA512 , x509 .ECDSA , crypto .SHA512 },
182
189
}
183
190
191
+ func signingParamsForAlgo (requestedSigAlgo x509.SignatureAlgorithm ) (sigAlgo pkix.AlgorithmIdentifier , err error ) {
192
+ found := false
193
+ for _ , details := range signatureAlgorithmDetails {
194
+ if details .algo == requestedSigAlgo {
195
+ found = true
196
+ sigAlgo .Algorithm = details .oid
197
+ if details .pubKeyAlgo == x509 .RSA {
198
+ sigAlgo .Parameters = asn1.RawValue {
199
+ Tag : 5 ,
200
+ }
201
+ }
202
+ }
203
+ }
204
+ if ! found {
205
+ err = fmt .Errorf ("invalid requestedSigAlgo: %s" , requestedSigAlgo )
206
+ }
207
+ return
208
+ }
209
+
184
210
// TODO(rlb): This is also from crypto/x509, so same comment as AGL's below
185
- func signingParamsForPublicKey (pub interface {}, requestedSigAlgo x509.SignatureAlgorithm ) (hashFunc crypto.Hash , sigAlgo pkix.AlgorithmIdentifier , err error ) {
211
+ // NOTE(nej) - modified this a bit to return x509.SignatureAlgorithm instead of the pkix.AlgorithmIdentifier, see
212
+ // signingParamsForAlgo above.
213
+ func signingAlgoForPublicKey (pub interface {}, requestedSigAlgo x509.SignatureAlgorithm ) (hashFunc crypto.Hash , sigAlgo x509.SignatureAlgorithm , err error ) {
186
214
var pubType x509.PublicKeyAlgorithm
187
215
188
216
switch pub := pub .(type ) {
189
217
case * rsa.PublicKey :
190
218
pubType = x509 .RSA
191
219
hashFunc = crypto .SHA256
192
- sigAlgo .Algorithm = oidSignatureSHA256WithRSA
193
- sigAlgo .Parameters = asn1.RawValue {
194
- Tag : 5 ,
195
- }
220
+ sigAlgo = x509 .SHA256WithRSA
196
221
197
222
case * ecdsa.PublicKey :
198
223
pubType = x509 .ECDSA
199
224
200
225
switch pub .Curve {
201
226
case elliptic .P224 (), elliptic .P256 ():
202
227
hashFunc = crypto .SHA256
203
- sigAlgo . Algorithm = oidSignatureECDSAWithSHA256
228
+ sigAlgo = x509 . ECDSAWithSHA256
204
229
case elliptic .P384 ():
205
230
hashFunc = crypto .SHA384
206
- sigAlgo . Algorithm = oidSignatureECDSAWithSHA384
231
+ sigAlgo = x509 . ECDSAWithSHA384
207
232
case elliptic .P521 ():
208
233
hashFunc = crypto .SHA512
209
- sigAlgo . Algorithm = oidSignatureECDSAWithSHA512
234
+ sigAlgo = x509 . ECDSAWithSHA512
210
235
default :
211
236
err = errors .New ("x509: unknown elliptic curve" )
212
237
}
@@ -230,7 +255,7 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo x509.SignatureA
230
255
err = errors .New ("x509: requested SignatureAlgorithm does not match private key type" )
231
256
return
232
257
}
233
- sigAlgo . Algorithm , hashFunc = details .oid , details .hash
258
+ sigAlgo , hashFunc = details .algo , details .hash
234
259
if hashFunc == 0 {
235
260
err = errors .New ("x509: cannot sign with hash function requested" )
236
261
return
@@ -407,6 +432,42 @@ func (resp *Response) CheckSignatureFrom(issuer *x509.Certificate) error {
407
432
return issuer .CheckSignature (resp .SignatureAlgorithm , resp .TBSResponseData , resp .Signature )
408
433
}
409
434
435
+ // Marshal marshals the OCSP response to ASN.1 DER encoded form
436
+ func (resp * Response ) Marshal () ([]byte , error ) {
437
+
438
+ signatureAlgorithm , err := signingParamsForAlgo (resp .SignatureAlgorithm )
439
+ if err != nil {
440
+ return nil , err
441
+ }
442
+ response := basicResponseRawTBS {
443
+ TBSResponseData : asn1.RawValue {FullBytes : resp .TBSResponseData },
444
+ SignatureAlgorithm : signatureAlgorithm ,
445
+ Signature : asn1.BitString {
446
+ Bytes : resp .Signature ,
447
+ BitLength : 8 * len (resp .Signature ),
448
+ },
449
+ }
450
+
451
+ if resp .Certificate != nil {
452
+ response .Certificates = []asn1.RawValue {
453
+ {FullBytes : resp .Certificate .Raw },
454
+ }
455
+ }
456
+
457
+ responseDER , err := asn1 .Marshal (response )
458
+ if err != nil {
459
+ return nil , err
460
+ }
461
+
462
+ return asn1 .Marshal (responseASN1 {
463
+ Status : asn1 .Enumerated (Success ),
464
+ Response : responseBytes {
465
+ ResponseType : idPKIXOCSPBasic ,
466
+ Response : responseDER ,
467
+ },
468
+ })
469
+ }
470
+
410
471
// ParseError results from an invalid OCSP response.
411
472
type ParseError string
412
473
@@ -744,7 +805,7 @@ func CreateResponse(issuer, responderCert *x509.Certificate, template Response,
744
805
return nil , err
745
806
}
746
807
747
- hashFunc , signatureAlgorithm , err := signingParamsForPublicKey (priv .Public (), template .SignatureAlgorithm )
808
+ hashFunc , sigAlgo , err := signingAlgoForPublicKey (priv .Public (), template .SignatureAlgorithm )
748
809
if err != nil {
749
810
return nil , err
750
811
}
@@ -756,29 +817,12 @@ func CreateResponse(issuer, responderCert *x509.Certificate, template Response,
756
817
return nil , err
757
818
}
758
819
759
- response := basicResponse {
760
- TBSResponseData : tbsResponseData ,
761
- SignatureAlgorithm : signatureAlgorithm ,
762
- Signature : asn1.BitString {
763
- Bytes : signature ,
764
- BitLength : 8 * len (signature ),
765
- },
766
- }
767
- if template .Certificate != nil {
768
- response .Certificates = []asn1.RawValue {
769
- {FullBytes : template .Certificate .Raw },
770
- }
771
- }
772
- responseDER , err := asn1 .Marshal (response )
773
- if err != nil {
774
- return nil , err
820
+ resp := & Response {
821
+ Certificate : template .Certificate ,
822
+ TBSResponseData : tbsResponseDataDER ,
823
+ Signature : signature ,
824
+ SignatureAlgorithm : sigAlgo ,
775
825
}
776
826
777
- return asn1 .Marshal (responseASN1 {
778
- Status : asn1 .Enumerated (Success ),
779
- Response : responseBytes {
780
- ResponseType : idPKIXOCSPBasic ,
781
- Response : responseDER ,
782
- },
783
- })
827
+ return resp .Marshal ()
784
828
}
0 commit comments